Skip to content

Commit cff2881

Browse files
committed
Raise exception from Rust.
1 parent 369ee2f commit cff2881

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

amqp/serialization.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from __future__ import absolute_import, unicode_literals
88

99
import calendar
10-
import sys
1110
import os
11+
import sys
1212
from datetime import datetime
1313
from decimal import Decimal
1414
from io import BytesIO
@@ -22,17 +22,11 @@
2222

2323
ftype_t = chr if sys.version_info[0] == 3 else None
2424

25-
ILLEGAL_TABLE_TYPE = """\
26-
Table type {0!r} not handled by amqp.
27-
"""
25+
ILLEGAL_TABLE_TYPE = "Table type {0!r} not handled by amqp."
2826

29-
ILLEGAL_TABLE_TYPE_WITH_KEY = """\
30-
Table type {0!r} for key {1!r} not handled by amqp. [value: {2!r}]
31-
"""
27+
ILLEGAL_TABLE_TYPE_WITH_KEY = "Table type {0!r} for key {1!r} not handled by amqp. [value: {2!r}]"
3228

33-
ILLEGAL_TABLE_TYPE_WITH_VALUE = """\
34-
Table type {0!r} not handled by amqp. [value: {1!r}]
35-
"""
29+
ILLEGAL_TABLE_TYPE_WITH_VALUE = "Table type {0!r} not handled by amqp. [value: {1!r}]"
3630

3731

3832
def _read_item(buf, offset=0, unpack_from=unpack_from, ftype_t=ftype_t):

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use pyo3::types::*;
99
use pyo3::wrap_pyfunction;
1010
use pyo3::{IntoPy, Py};
1111
use std::io::{self, Cursor, Read, Seek, SeekFrom};
12+
use pyo3::import_exception;
1213

1314
// Since this module is being currently imported we assume the GIL is acquired.
1415
// If it isn't, this module is the least of our problems
@@ -31,6 +32,8 @@ lazy_static! {
3132
};
3233
}
3334

35+
import_exception!(amqp, FrameSyntaxError);
36+
3437
struct AMQPDeserializer<'deserializer_l> {
3538
py: &'deserializer_l Python<'deserializer_l>,
3639
format: &'deserializer_l str,
@@ -156,7 +159,7 @@ impl<'deserializer_l> AMQPDeserializer<'deserializer_l> {
156159
}
157160

158161
#[inline]
159-
fn read_item(&mut self) -> io::Result<PyObject> {
162+
fn read_item(&mut self) -> PyResult<PyObject> {
160163
let ftype = self.cursor.read_u8()? as char;
161164
match ftype {
162165
'S' => {
@@ -187,7 +190,7 @@ impl<'deserializer_l> AMQPDeserializer<'deserializer_l> {
187190
't' => Ok((self.cursor.read_u8()? == 1).to_object(*self.py)),
188191
'T' => Ok(self.read_timestamp()?.into()),
189192
'V' => Ok(self.py.None()),
190-
_ => Ok(self.py.None()), // TODO: Return error
193+
_ => Err(FrameSyntaxError::py_err(format!("Unknown value in table: '{}'", ftype)).into()),
191194
}
192195
}
193196

@@ -214,7 +217,7 @@ impl<'deserializer_l> AMQPDeserializer<'deserializer_l> {
214217
Ok(array)
215218
}
216219

217-
fn deserialize(&mut self) -> io::Result<(&'deserializer_l PyList, u64)> {
220+
fn deserialize(&mut self) -> PyResult<(&'deserializer_l PyList, u64)> {
218221
let values = PyList::empty(*self.py);
219222

220223
for p in self.format.chars() {
@@ -266,7 +269,7 @@ impl<'deserializer_l> AMQPDeserializer<'deserializer_l> {
266269
values.append(self.read_array()?)?;
267270
}
268271
_ => {
269-
// TODO: Handle errors correctly
272+
return Err(FrameSyntaxError::py_err(format!("Table type '{}' not handled by amqp.", p)).into());
270273
}
271274
}
272275
}

t/unit/test_serialization.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from amqp.basic_message import Message
1010
from amqp.exceptions import FrameSyntaxError
1111
from amqp.platform import pack
12-
from amqp.serialization import GenericContent, _read_item, dumps, loads
12+
from amqp.serialization import (ILLEGAL_TABLE_TYPE, GenericContent, _read_item,
13+
dumps, loads)
1314

1415

1516
class _ANY(object):
@@ -69,7 +70,10 @@ def test_int_boundaries(self):
6970
}]
7071

7172
def test_loads_unknown_type(self):
72-
with pytest.raises(FrameSyntaxError):
73+
with pytest.raises(
74+
FrameSyntaxError,
75+
match=ILLEGAL_TABLE_TYPE.format('y')
76+
):
7377
loads('y', 'asdsad')
7478

7579
def test_float(self):

0 commit comments

Comments
 (0)