Skip to content

Commit 605ef84

Browse files
committed
reformatting and clean up
1 parent 88e8b11 commit 605ef84

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# u-msgpack-python v1.0
22

3-
u-msgpack-python is a lightweight [msgpack](https://github.com/msgpack/msgpack) serializer and deserializer module that is compatible with Python 2 and Python 3. u-msgpack-python is fully compliant with the latest msgpack specification, as of 09/29/2013. In particular, it supports the new binary, UTF-8 string, and application ext types.
3+
u-msgpack-python is a lightweight [msgpack](https://github.com/msgpack/msgpack) serializer and deserializer module that is compatible with Python 2 and Python 3. u-msgpack-python is fully compliant with the latest [msgpack specification](https://github.com/msgpack/msgpack/blob/master/spec.md), as of 09/29/2013. In particular, it supports the new binary, UTF-8 string, and application ext types.
44

55
u-msgpack-python is written in pure Python and is currently distributed as a single file: [umsgpack.py](umsgpack.py)
66

@@ -42,6 +42,7 @@ An example of encoding and decoding an application ext type:
4242
>>> umsgpack.packb({u"special stuff": foo, u"awesome": True})
4343
b'\x82\xadspecial stuff\xc7\x03\x05\x01\x02\x03\xa7awesome\xc3'
4444
>>> bar = umsgpack.unpackb(_)
45+
4546
>>> print(bar["special stuff"])
4647
Ext Object
4748
Type: 05
@@ -95,17 +96,17 @@ If a non-byte-string argument is passed to `umsgpack.unpackb()`, it will raise a
9596
>>>
9697
```
9798
98-
* `InsufficientdataException`: Insufficient data to unpack encoded object.
99+
* `InsufficientDataException`: Insufficient data to unpack encoded object.
99100
100101
```
101102
>>> # Attempt to unpack a cut-off encoded 32-bit unsigned int
102-
... umsgpack.unpackb("\xce\xff\xff\xff")
103+
... umsgpack.unpackb(b"\xce\xff\xff\xff")
103104
...
104105
umsgpack.InsufficientDataException
105106
>>>
106107
107108
>>> # Attempt to unpack an array of length 2 missing the second item
108-
... umsgpack.unpackb("\x92\xc2")
109+
... umsgpack.unpackb(b"\x92\xc2")
109110
...
110111
umsgpack.InsufficientDataException
111112
>>>
@@ -117,7 +118,7 @@ If a non-byte-string argument is passed to `umsgpack.unpackb()`, it will raise a
117118
118119
```
119120
>>> # Attempt to unpack the string "\x80\x81"
120-
... umsgpack.unpackb("\xa2\x80\x81")
121+
... umsgpack.unpackb(b"\xa2\x80\x81")
121122
...
122123
umsgpack.InvalidStringException: unpacked string is not utf-8
123124
>>>
@@ -127,7 +128,7 @@ If a non-byte-string argument is passed to `umsgpack.unpackb()`, it will raise a
127128
128129
```
129130
>>> # Attempt to unpack reserved code 0xc1
130-
... umsgpack.unpackb("\xc1")
131+
... umsgpack.unpackb(b"\xc1")
131132
...
132133
umsgpack.ReservedCodeException: reserved code encountered: 0xc1
133134
>>>
@@ -139,7 +140,7 @@ If a non-byte-string argument is passed to `umsgpack.unpackb()`, it will raise a
139140
140141
```
141142
>>> # Attempt to unpack { {} : False }
142-
... umsgpack.unpackb("\x82\x80\xc2")
143+
... umsgpack.unpackb(b"\x82\x80\xc2")
143144
...
144145
umsgpack.KeyNotPrimitiveException: encountered non-primitive key type: <type 'dict'>
145146
>>>
@@ -151,23 +152,23 @@ If a non-byte-string argument is passed to `umsgpack.unpackb()`, it will raise a
151152
152153
```
153154
>>> # Attempt to unpack { 1: True, 1: False }
154-
... umsgpack.unpackb("\x82\x01\xc3\x01\xc2")
155+
... umsgpack.unpackb(b"\x82\x01\xc3\x01\xc2")
155156
...
156157
umsgpack.KeyDuplicateException: encountered duplicate key: 1, <type 'int'>
157158
>>>
158159
```
159160
160-
## Notes
161+
## Behavior Notes
161162
162163
* Python 2
163-
* `unicode` type objects are packed into, and unpacked from, the msgpack string format
164-
* `str` type objects are packed into, and unpacked from, the msgpack bytes format
164+
* `unicode` type objects are packed into, and unpacked from, the msgpack `string` format
165+
* `str` type objects are packed into, and unpacked from, the msgpack `binary` format
165166
* Python 3
166-
* `str` type objects are packed into, and unpacked from, the msgpack string format
167-
* `bytes` type objects are packed into, and unpacked from, the msgpack bytes format
167+
* `str` type objects are packed into, and unpacked from, the msgpack `string` format
168+
* `bytes` type objects are packed into, and unpacked from, the msgpack `binary` format
168169
* The msgpack string format is strictly decoded with UTF-8. An exception is thrown if the string bytes cannot be decoded into a valid UTF-8 string.
169-
* msgpack arrays are unpacked into Python lists, not tuples
170-
* Python floats are packed into the msgpack float32 or float64 format depending on the system's sys.float\_info
170+
* The msgpack array format is unpacked into a Python list, not tuple
171+
* Python float types are packed into the msgpack float32 or float64 format depending on the system's `sys.float_info`
171172
172173
## Testing
173174

test_umsgpack.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# or
44
# $ py.test2 # Python 2
55
#
6-
# (Default Python version may depend on your system.)
6+
# (Actual Python version invoked may depend on your system.)
77

88
import umsgpack
99
import struct
@@ -213,3 +213,4 @@ def test_unpack_exceptions():
213213
for (name, data, exception) in unpack_exception_test_vectors:
214214
print("\tTesting %s" % name)
215215
with pytest.raises(exception): umsgpack.unpackb(data)
216+

umsgpack.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# u-msgpack-python v1.0 - vsergeev at gmail
22
#
33
# u-msgpack-python is a Python 2 and Python 3 compatible serializer and
4-
# deserializer for msgpack written in pure Python. It fully supports the latest
5-
# msgpack spec, as of 09/29/2013.
4+
# deserializer for msgpack written in pure Python. It supports the latest
5+
# msgpack spec, as of 09/29/2013, namely the utf-8 string, binary, and ext
6+
# types.
67
#
78

89
import struct
@@ -16,12 +17,15 @@ class Ext:
1617
def __init__(self, ext_type, data):
1718
self.type = ext_type
1819
self.data = data
20+
1921
def __eq__(self, other):
2022
return (isinstance(other, self.__class__) and
2123
self.type == other.type and
2224
self.data == other.data)
25+
2326
def __ne__(self, other):
2427
return not self.__eq__(other)
28+
2529
def __str__(self):
2630
s = "Ext Object\n"
2731
s += " Type: %02x\n" % self.type
@@ -59,6 +63,11 @@ class KeyDuplicateException(UnpackException): pass
5963

6064
################################################################################
6165

66+
# You may notice struct.pack("B", x) instead of the simpler chr(x) in the code
67+
# below. This is to allow for seamless Python 2 and 3 compatibility, as chr(x)
68+
# has a str return type instead of bytes in Python 3, and struct.pack(...) has
69+
# the right return type in both versions.
70+
6271
def pack_integer(x):
6372
if x < 0:
6473
if x >= -32:
@@ -174,8 +183,6 @@ def pack_map(x):
174183

175184
return s
176185

177-
########################################
178-
179186
# Pack for Python 2, with 'unicode' type, 'str' type, and 'long' type
180187
def packb2(x):
181188
if x is None:
@@ -346,12 +353,15 @@ def unpack_map(code, read_fn):
346353

347354
d = {}
348355
for i in range(length):
356+
# Unpack key
349357
k = _unpackb(read_fn)
350358

351359
if not isinstance(k, collections.Hashable):
352360
raise KeyNotPrimitiveException("encountered non-primitive key type: %s" % str(type(k)))
353361
elif k in d:
354362
raise KeyDuplicateException("encountered duplicate key: %s, %s" % (str(k), str(type(k))))
363+
364+
# Unpack value
355365
v = _unpackb(read_fn)
356366

357367
d[k] = v
@@ -373,8 +383,6 @@ def _unpackb(read_fn):
373383
code = read_fn(1)
374384
return unpack_dispatch_table[code](code, read_fn)
375385

376-
# Wrapper unpack function that builds a read stream function from s
377-
378386
# For Python 2, expects a str object
379387
def unpackb2(s):
380388
if not isinstance(s, str):
@@ -397,7 +405,6 @@ def __init():
397405
global unpackb
398406
global unpack_dispatch_table
399407

400-
401408
# Auto-detect system float precision
402409
if sys.float_info.mant_dig == 53:
403410
float_size = 64

0 commit comments

Comments
 (0)