Skip to content
Open
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
8 changes: 5 additions & 3 deletions phpserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,19 @@ def _unserialize():
raise ValueError('unexpected opcode - %s' % repr(type_))

fp_position = fp.tell()
chunk = _read_until(':');
chunk = _read_until(b':');
fp.seek(fp_position) # Reset pointer
if '|' in chunk:
if b'|' in chunk:
# We may be dealing with a serialized session, in which case keys
# followed by a pipe are preceding the serialized data.
unserialized_data = {}
while 1:
try:
key = _read_until('|');
key = _read_until(b'|');
except ValueError:
break # end of stream
if decode_strings:
key = key.decode(charset, errors)
if return_unicode:
key = unicode(key, charset)
unserialized_data[key] = _unserialize()
Expand Down
6 changes: 3 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_dumps_tuple(self):

def test_dumps_dict(self):
self.assertEqual(phpserialize.dumps({'a': 1, 'b': 2, 'c': 3}),
b'a:3:{s:1:"a";i:1;s:1:"c";i:3;s:1:"b";i:2;}')
b'a:3:{s:1:"a";i:1;s:1:"b";i:2;s:1:"c";i:3;}')

def test_loads_dict(self):
self.assertEqual(phpserialize.loads(b'a:3:{s:1:"a";i:1;s:1:"c";i:3;s:1:"b";i:2;}',
Expand Down Expand Up @@ -88,7 +88,7 @@ def dump_object_hook(obj):
x = phpserialize.dumps(user, object_hook=dump_object_hook)
y = phpserialize.loads(x, object_hook=load_object_hook,
decode_strings=True)
self.assert_(b'WP_User' in x)
self.assertTrue(b'WP_User' in x)
self.assertEqual(type(y), type(user))
self.assertEqual(y.username, user.username)

Expand All @@ -101,7 +101,7 @@ def test_basic_object_hook(self):

def test_session(self):
data = b'foo|a:1:{s:1:"a";s:1:"b";}bar|a:1:{s:1:"c";s:1:"d";}'
session = phpserialize.loads(data)
session = phpserialize.loads(data, decode_strings=True)
self.assertEqual(session, {'foo': {'a': 'b'}, 'bar': {'c': 'd'}})

def test_loads_unicode_strings(self):
Expand Down