Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
25 changes: 11 additions & 14 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ class Packer (offset = 0, id_cache = None, hash_seed = 0,
in order to deserialize them, and since the import process may execute
arbitrary code, this key can be used to prevent against malicious input.

```python
copy ()
```

Returns a new packer that is a copy of the caller. Can be used to serialize complex
objects without the need of modifying the current object.

```python
resize (extra)
```
Expand Down Expand Up @@ -123,12 +116,16 @@ class Packer (offset = 0, id_cache = None, hash_seed = 0,

Same as calling *struct.pack_into* with the packer's stream and offset as output.

```python
pack_struct_at (format, position, *args)
```
Same as calling *struct.pack_into* with the packer's stream and *position* as output.

```python
bwrite (obj)
```

Writes _obj_ into the stream. The argument may be another packer, in which case its
byte stream will be written.
Writes _obj_ into the stream.

```python
as_bytearray ()
Expand All @@ -137,10 +134,10 @@ class Packer (offset = 0, id_cache = None, hash_seed = 0,
Returns a copy of the packer's byte stream.

```python
pack (obj, tag = True)
pack (obj)
```

Packs an object into the packer's stream. If _tag_ is true, also emits the typecode.
Packs an object into the packer's stream.

```python
class Proxy (mapping, offset = 0, size = None, rw = False,
Expand Down Expand Up @@ -199,7 +196,7 @@ class Proxy (mapping, offset = 0, size = None, rw = False,
unpack ()
```

Unpacks an object at the proxy_handler's current position and returns it.
Unpacks an object at the Proxy's current position and returns it.

```python
unpack_from (offset)
Expand All @@ -225,7 +222,7 @@ class ProxyList
type.
* The size of a ProxyList cannot be modified, even if it's mutable. This means that the
following interfaces are not available: _append_, _clear_, _extend_, _insert_, _pop_,
_remove_, _reverse_, _sort_.
_remove_, _reverse_. The method _sort_ is also not available.
* A ProxyList implements 2 methods not present in regular lists, specified below:

```python
Expand Down Expand Up @@ -379,5 +376,5 @@ The typecode can be one of the following module constants:
def unproxy (obj)
```

Converts a proxy object (**proxy_list**, **proxy_str**, **proxy_set**, **proxy_dict**)
Converts a proxy object (**ProxyList**, **ProxyStr**, **ProxySet**, **ProxyDict**)
into its 'regular' counterpart, recursively.
46 changes: 14 additions & 32 deletions tests/test_ser.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,6 @@ def test_equal_dict_many ():

tst_equality (d)

def tst_unpack_as (obj, code):
bx = zser.pack (obj)
assert obj == zser.unproxy (zser.unpack_as (bx, code, 1, rw = True))
p = zser.Packer ()
p.pack (obj, tag = False)
bx = p.as_bytearray ()
assert obj == zser.unproxy (zser.unpack_as (bx, code, rw = True))

def test_unpack_as ():
for obj, code in ((-1, zser.TYPE_INT8), (1 << 13, zser.TYPE_INT16),
(44.55, zser.TYPE_FLOAT64), (1 << 100, zser.TYPE_BIGINT),
("???_*!az", zser.TYPE_STR), (b"13345", zser.TYPE_BYTES),
(bytearray (b"abc000"), zser.TYPE_BYTEARRAY),
([44, -5.2, "abc"], zser.TYPE_LIST),
((b"!!!!", 777), zser.TYPE_TUPLE),
(None, zser.TYPE_NONE), (True, zser.TYPE_TRUE),
(False, zser.TYPE_FALSE),
(set ([3, 1, 2]), zser.TYPE_SET),
({"a": 1, (3.14, 100): ""}, zser.TYPE_DICT)):
tst_unpack_as (obj, code)

# Proxy list API

def tst_plist_atomic (mul):
Expand Down Expand Up @@ -436,10 +415,10 @@ def f (self):

@zser.register_pack (Registered)
def pack_obj (pk, obj):
tmp = pk.copy ()
size = tmp.pack (obj.a1)
pk.pack_struct ("N", size)
pk.bwrite (tmp)
off = pk.getoff ()
pk.bump (pk.struct_size ("N"))
size = pk.pack (obj.a1)
pk.pack_struct_at ("N", off, size)
pk.pack (obj.a2)

@zser.register_unpack (Registered)
Expand All @@ -461,8 +440,11 @@ def test_register ():
def tst_too_short (obj):
bx = zser.pack (obj)
for i in range (1, len (bx) - 1):
with pytest.raises (IndexError):
str (zser.unpack_from (bx[:-i]))
try:
new_obj = zser.unpack_from (bx[:-i])
assert new_obj != obj
except IndexError:
pass

def test_poisoned ():
for x in (1, 3.14, -123, "abcdef", ["qx", 1], (101, 69, 15 << 3),
Expand All @@ -478,11 +460,11 @@ def test_signature ():
with pytest.raises (ValueError):
zser.unpack_from (bx, import_key = ikey + "*")

def test_short_mview ():
buf = zser.pack (1 << 30)
for i in range (1, 7):
with pytest.raises (IndexError):
zser.unpack_from (buf, 0, size = len (buf) - i)
# Test that functions can be (un)packed

def test_function_packing ():
fn = zser.unpack_from (zser.pack (os.getpid))
assert fn () == os.getpid ()

# Test large offsets

Expand Down
Loading