summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavis Kirkendall <davis.e.kirkendall@gmail.com>2018-09-12 22:51:29 +0200
committerAsif Saif Uddin (Auvi) <auvipy@gmail.com>2018-09-13 02:51:29 +0600
commitdc5b07c2320f932b2dd8342d0f4d578c095600cf (patch)
treea912f6cfdb91d38685c1769439ef952cce401526
parent3345fd6b962716905ed0b41290ce671b5d211fd9 (diff)
downloadpy-amqp-dc5b07c2320f932b2dd8342d0f4d578c095600cf.tar.gz
Add field type "x" (byte array) (#204)
-rw-r--r--amqp/serialization.py14
-rw-r--r--t/unit/test_serialization.py8
2 files changed, 18 insertions, 4 deletions
diff --git a/amqp/serialization.py b/amqp/serialization.py
index c22f90b..95c18ea 100644
--- a/amqp/serialization.py
+++ b/amqp/serialization.py
@@ -50,6 +50,12 @@ def _read_item(buf, offset=0, unpack_from=unpack_from, ftype_t=ftype_t):
offset += 1
val = pstr_t(buf[offset:offset + slen])
offset += slen
+ # 'x': Bytes Array
+ elif ftype == 'x':
+ blen, = unpack_from('>I', buf, offset)
+ offset += 4
+ val = buf[offset:offset + blen]
+ offset += blen
# 'b': short-short int
elif ftype == 'b':
val, = unpack_from('>B', buf, offset)
@@ -202,6 +208,11 @@ def loads(format, buf, offset=0,
offset += 4
val = buf[offset:offset + slen].decode('utf-8', 'surrogatepass')
offset += slen
+ elif p == 'x':
+ blen, = unpack_from('>I', buf, offset)
+ offset += 4
+ val = buf[offset:offset + blen]
+ offset += blen
elif p == 'F':
bitcount = bits = 0
tlen, = unpack_from('>I', buf, offset)
@@ -252,6 +263,7 @@ def dumps(format, values):
long long = L
shortstr = s
longstr = S
+ byte array = x
table = F
array = A
"""
@@ -293,7 +305,7 @@ def dumps(format, values):
val = val.encode('utf-8', 'surrogatepass')
write(pack('B', len(val)))
write(val)
- elif p == 'S':
+ elif p == 'S' or p == 'x':
val = val or ''
bitcount = _flushbits(bits, write)
if isinstance(val, string):
diff --git a/t/unit/test_serialization.py b/t/unit/test_serialization.py
index ad5738b..811b1a6 100644
--- a/t/unit/test_serialization.py
+++ b/t/unit/test_serialization.py
@@ -25,6 +25,7 @@ class test_serialization:
@pytest.mark.parametrize('descr,frame,expected,cast', [
('S', b's8thequick', 'thequick', None),
+ ('x', b'x\x00\x00\x00\x09thequick\xffIGNORED', b'thequick\xff', None),
('b', b'b' + pack('>B', True), True, None),
('B', b'B' + pack('>b', 123), 123, None),
('U', b'U' + pack('>h', -321), -321, None),
@@ -43,17 +44,18 @@ class test_serialization:
assert _read_item(b'V')[0] is None
def test_roundtrip(self):
- format = b'bobBlLbsbST'
+ format = b'bobBlLbsbSTx'
x = dumps(format, [
True, 32, False, 3415, 4513134, 13241923419,
True, b'thequickbrownfox', False, 'jumpsoverthelazydog',
datetime(2015, 3, 13, 10, 23),
+ b'thequick\xff'
])
y = loads(format, x)
assert [
True, 32, False, 3415, 4513134, 13241923419,
True, 'thequickbrownfox', False, 'jumpsoverthelazydog',
- datetime(2015, 3, 13, 10, 23),
+ datetime(2015, 3, 13, 10, 23), b'thequick\xff'
] == y[0]
def test_int_boundaries(self):
@@ -68,7 +70,7 @@ class test_serialization:
def test_loads_unknown_type(self):
with pytest.raises(FrameSyntaxError):
- loads('x', 'asdsad')
+ loads('y', 'asdsad')
def test_float(self):
assert (int(loads(b'fb', dumps(b'fb', [32.31, False]))[0][0] * 100) ==