Skip to content
Closed
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
31 changes: 31 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Copyright (c) 2016 by Armin Ronacher.

Some rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 changes: 30 additions & 1 deletion phpserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class WP_User extends WP_UserBase {
xrange = range

__author__ = 'Armin Ronacher <armin.ronacher@active-4.com>'
__version__ = '1.3'
__version__ = '1.4'
__all__ = ('phpobject', 'convert_member_dict', 'dict_to_list', 'dict_to_tuple',
'load', 'loads', 'dump', 'dumps', 'serialize', 'unserialize')

Expand Down Expand Up @@ -580,6 +580,35 @@ def dict_to_list(d):
except KeyError:
raise ValueError('dict is not a sequence')

def full_dict_to_list(d, array_hook=dict):
"""Converts a full dict into a more Pythonic structure. In particular ensure that all list-like structures are handled as Python lists
Examples:

d1 = {'a': 'b', 'c': {0: '1', 1: '2', 2: {'e': 7}, 3: {2: 8}}}
full_dict_to_list(d1) returns
{'a': 'b', 'c': ['1', '2', {'e': 7}, {2: 8}]}

d2 = OrderedDict({'a': 'b', 'c': OrderedDict({0: '1', 1: '2', 2: OrderedDict({'e': 7}), 3: OrderedDict({2: 8})})})
full_dict_to_list(d2, OrderedDict) returns
OrderedDict([('a', 'b'), ('c', ['1', '2', OrderedDict([('e', 7)]), OrderedDict([(2, 8)])])])
"""
if type(d) is array_hook:
keys = list(d.keys())

if keys == list(range(0, len(keys))): #Any array with keys 0..N is considered to be a list
return [full_dict_to_list(val) for val in d.values()]
elif len(keys) == 1:
key = keys[0]
return {key : full_dict_to_list(d[key], array_hook)}
else:
tmp_dict = array_hook()
for key in keys:
tmp_dict.update( {key : full_dict_to_list(d[key], array_hook)} )
return tmp_dict
else:
return d



def dict_to_tuple(d):
"""Converts an ordered dict into a tuple."""
Expand Down
13 changes: 13 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ def test_basic_object_hook(self):
self.assertEqual(user.username, 'admin')
self.assertEqual(user.__name__, 'WP_User')

def test_full_dict_to_list(self):
#with normal dict
d1 = {'a': 'b', 'c': {0: '1', 1: '2', 2: {'e': 7}, 3: {2: 8}}}
d1_cleaned = {'a': 'b', 'c': ['1', '2', {'e': 7}, {2: 8}]}
self.assertEqual(phpserialize.full_dict_to_list(d1), d1_cleaned)

#with OrderedDict as array_hook
from collections import OrderedDict
d2 = OrderedDict({'a': 'b', 'c': OrderedDict({0: '1', 1: '2', 2: OrderedDict({'e': 7}), 3: OrderedDict({2: 8})})})
d2_cleaned = OrderedDict([('a', 'b'), ('c', ['1', '2', OrderedDict([('e', 7)]), OrderedDict([(2, 8)])])])
self.assertEqual(phpserialize.full_dict_to_list(d2, OrderedDict), d2_cleaned)


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)
Expand Down