{ "info": { "author": "Armin Ronacher", "author_email": "armin.ronacher@active-4.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: BSD License", "Programming Language :: PHP", "Programming Language :: Python", "Programming Language :: Python :: 3" ], "description": "phpserialize\r\n~~~~~~~~~~~~\r\n\r\na port of the ``serialize`` and ``unserialize`` functions of\r\nphp to python. This module implements the python serialization\r\ninterface (eg: provides `dumps`, `loads` and similar functions).\r\n\r\nUsage\r\n=====\r\n\r\n>>> from phpserialize import *\r\n>>> obj = dumps(\"Hello World\")\r\n>>> loads(obj)\r\n'Hello World'\r\n\r\nDue to the fact that PHP doesn't know the concept of lists, lists\r\nare serialized like hash-maps in PHP. As a matter of fact the\r\nreverse value of a serialized list is a dict:\r\n\r\n>>> loads(dumps(range(2)))\r\n{0: 0, 1: 1}\r\n\r\nIf you want to have a list again, you can use the `dict_to_list`\r\nhelper function:\r\n\r\n>>> dict_to_list(loads(dumps(range(2))))\r\n[0, 1]\r\n\r\nIt's also possible to convert into a tuple by using the `dict_to_tuple`\r\nfunction:\r\n\r\n>>> dict_to_tuple(loads(dumps((1, 2, 3))))\r\n(1, 2, 3)\r\n\r\nAnother problem are unicode strings. By default unicode strings are\r\nencoded to 'utf-8' but not decoded on `unserialize`. The reason for\r\nthis is that phpserialize can't guess if you have binary or text data\r\nin the strings:\r\n\r\n>>> loads(dumps(u'Hello W\\xf6rld'))\r\n'Hello W\\xc3\\xb6rld'\r\n\r\nIf you know that you have only text data of a known charset in the result\r\nyou can decode strings by setting `decode_strings` to True when calling\r\nloads:\r\n\r\n>>> loads(dumps(u'Hello W\\xf6rld'), decode_strings=True)\r\nu'Hello W\\xf6rld'\r\n\r\nDictionary keys are limited to strings and integers. `None` is converted\r\ninto an empty string and floats and booleans into integers for PHP\r\ncompatibility:\r\n\r\n>>> loads(dumps({None: 14, 42.23: 'foo', True: [1, 2, 3]}))\r\n{'': 14, 1: {0: 1, 1: 2, 2: 3}, 42: 'foo'}\r\n\r\nIt also provides functions to read from file-like objects:\r\n\r\n>>> from StringIO import StringIO\r\n>>> stream = StringIO('a:2:{i:0;i:1;i:1;i:2;}')\r\n>>> dict_to_list(load(stream))\r\n[1, 2]\r\n\r\nAnd to write to those:\r\n\r\n>>> stream = StringIO()\r\n>>> dump([1, 2], stream)\r\n>>> stream.getvalue()\r\n'a:2:{i:0;i:1;i:1;i:2;}'\r\n\r\nLike `pickle` chaining of objects is supported:\r\n\r\n>>> stream = StringIO()\r\n>>> dump([1, 2], stream)\r\n>>> dump(\"foo\", stream)\r\n>>> stream.seek(0)\r\n>>> load(stream)\r\n{0: 1, 1: 2}\r\n>>> load(stream)\r\n'foo'\r\n\r\nThis feature however is not supported in PHP. PHP will only unserialize\r\nthe first object.\r\n\r\nArray Serialization\r\n===================\r\n\r\nStarting with 1.2 you can provide an array hook to the unserialization\r\nfunctions that are invoked with a list of pairs to return a real array\r\nobject. By default `dict` is used as array object which however means\r\nthat the information about the order is lost for associative arrays.\r\n\r\nFor example you can pass the ordered dictionary to the unserilization\r\nfunctions:\r\n\r\n>>> from collections import OrderedDict\r\n>>> loads('a:2:{s:3:\"foo\";i:1;s:3:\"bar\";i:2;}',\r\n... array_hook=OrderedDict)\r\ncollections.OrderedDict([('foo', 1), ('bar', 2)])\r\n\r\nObject Serialization\r\n====================\r\n\r\nPHP supports serialization of objects. Starting with 1.2 of phpserialize\r\nit is possible to both serialize and unserialize objects. Because class\r\nnames in PHP and Python usually do not map, there is a separate\r\n`object_hook` parameter that is responsible for creating these classes.\r\n\r\nFor a simple test example the `phpserialize.phpobject` class can be used:\r\n\r\n>>> data = 'O:7:\"WP_User\":1:{s:8:\"username\";s:5:\"admin\";}'\r\n>>> user = loads(data, object_hook=phpobject)\r\n>>> user.username\r\n'admin'\r\n>>> user.__name__\r\n'WP_User'\r\n\r\nAn object hook is a function that takes the name of the class and a dict\r\nwith the instance data as arguments. The instance data keys are in PHP\r\nformat which usually is not what you want. To convert it into Python\r\nidentifiers you can use the `convert_member_dict` function. For more\r\ninformation about that, have a look at the next section. Here an\r\nexample for a simple object hook:\r\n\r\n>>> class User(object):\r\n... def __init__(self, username):\r\n... self.username = username\r\n...\r\n>>> def object_hook(name, d):\r\n... cls = {'WP_User': User}[name]\r\n... return cls(**d)\r\n...\r\n>>> user = loads(data, object_hook=object_hook)\r\n>>> user.username\r\n'admin'\r\n\r\nTo serialize objects you can use the `object_hook` of the dump functions\r\nand return instances of `phpobject`:\r\n\r\n>>> def object_hook(obj):\r\n... if isinstance(obj, User):\r\n... return phpobject('WP_User', {'username': obj.username})\r\n... raise LookupError('unknown object')\r\n...\r\n>>> dumps(user, object_hook=object_hook)\r\n'O:7:\"WP_User\":1:{s:8:\"username\";s:5:\"admin\";}'\r\n\r\nPHP's Object System\r\n===================\r\n\r\nThe PHP object system is derived from compiled languages such as Java\r\nand C#. Attributes can be protected from external access by setting\r\nthem to `protected` or `private`. This does not only serve the purpose\r\nto encapsulate internals but also to avoid name clashes.\r\n\r\nIn PHP each class in the inheritance chain can have a private variable\r\nwith the same name, without causing clashes. (This is similar to the\r\nPython `__var` name mangling system).\r\n\r\nThis PHP class::\r\n\r\n class WP_UserBase {\r\n protected $username;\r\n\r\n public function __construct($username) {\r\n $this->username = $username;\r\n }\r\n }\r\n\r\n class WP_User extends WP_UserBase {\r\n private $password;\r\n public $flag;\r\n\r\n public function __construct($username, $password) {\r\n parent::__construct($username);\r\n $this->password = $password;\r\n $this->flag = 0;\r\n }\r\n }\r\n\r\nIs serialized with a member data dict that looks like this:\r\n\r\n>>> data = {\r\n... ' * username': 'the username',\r\n... ' WP_User password': 'the password',\r\n... 'flag': 'the flag'\r\n... }\r\n\r\nBecause this access system does not exist in Python, the\r\n`convert_member_dict` can convert this dict:\r\n\r\n>>> d = convert_member_dict(data)\r\n>>> d['username']\r\n'the username'\r\n>>> d['password']\r\n'the password'\r\n\r\nThe `phpobject` class does this conversion on the fly. What is\r\nserialized is the special `__php_vars__` dict of the class:\r\n\r\n>>> user = phpobject('WP_User', data)\r\n>>> user.username\r\n'the username'\r\n>>> user.username = 'admin'\r\n>>> user.__php_vars__[' * username']\r\n'admin'\r\n\r\nAs you can see, reassigning attributes on a php object will try\r\nto change a private or protected attribute with the same name.\r\nSetting an unknown one will create a new public attribute:\r\n\r\n>>> user.is_admin = True\r\n>>> user.__php_vars__['is_admin']\r\nTrue\r\n\r\nTo convert the phpobject into a dict, you can use the `_asdict`\r\nmethod:\r\n\r\n>>> d = user._asdict()\r\n>>> d['username']\r\n'admin'\r\n\r\nPython 3 Notes\r\n==============\r\n\r\nBecause the unicode support in Python 3 no longer transparently\r\nhandles bytes and unicode objects we had to change the way the\r\ndecoding works. On Python 3 you most likely want to always\r\ndecode strings. Because this would totally fail on binary data\r\nphpserialize uses the \"surrogateescape\" method to not fail on\r\ninvalid data. See the documentation in Python 3 for more\r\ninformation.\r\n\r\nChangelog\r\n=========\r\n\r\n1.3\r\n - added support for Python 3\r\n\r\n1.2\r\n - added support for object serialization\r\n - added support for array hooks\r\n\r\n1.1\r\n - added `dict_to_list` and `dict_to_tuple`\r\n - added support for unicode\r\n - allowed chaining of objects like pickle does", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/mitsuhiko/phpserialize", "keywords": "", "license": "UNKNOWN", "maintainer": "", "maintainer_email": "", "name": "phpserialize", "package_url": "https://pypi.org/project/phpserialize/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/phpserialize/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/mitsuhiko/phpserialize" }, "release_url": "https://pypi.org/project/phpserialize/1.3/", "requires_dist": null, "requires_python": null, "summary": "a port of the serialize and unserialize functions of php to python.", "version": "1.3" }, "last_serial": 796218, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "cc4af12b6b0626464ab8292130d32e63", "sha256": "d7a6f7e983d9877645c05dd502e9bde118644b1cee642b5802fac6e8245d3aeb" }, "downloads": -1, "filename": "phpserialize-1.0.tar.gz", "has_sig": false, "md5_digest": "cc4af12b6b0626464ab8292130d32e63", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 1405, "upload_time": "2006-05-01T15:46:16", "url": "https://files.pythonhosted.org/packages/a3/c7/2cb58f4c0b111f50d426b1a2fec5b667e124678b6cab796cdd56ca04424f/phpserialize-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "5f643cfbcf9596b6b89927862ac3dcf8", "sha256": "3504076ee600af6cad71bfbed8d95decb16266561d647e17073468e2673c1ef2" }, "downloads": -1, "filename": "phpserialize-1.1-py2.3.egg", "has_sig": false, "md5_digest": "5f643cfbcf9596b6b89927862ac3dcf8", "packagetype": "bdist_egg", "python_version": "2.3", "requires_python": null, "size": 19282, "upload_time": "2008-06-14T18:56:32", "url": "https://files.pythonhosted.org/packages/33/f1/de2064bdbc3517179873ff548cfa084189cbc9eea978057acf84b37f5a9c/phpserialize-1.1-py2.3.egg" }, { "comment_text": "", "digests": { "md5": "34567bbc6f65bb39b2084aacaeae6360", "sha256": "e9336902a9b2f8dd7e5faf294ca4ac18eb7118764faf6ef8dee9f41bea909465" }, "downloads": -1, "filename": "phpserialize-1.1-py2.4.egg", "has_sig": false, "md5_digest": "34567bbc6f65bb39b2084aacaeae6360", "packagetype": "bdist_egg", "python_version": "2.4", "requires_python": null, "size": 7532, "upload_time": "2008-06-14T18:56:27", "url": "https://files.pythonhosted.org/packages/93/e5/ffc739852df1bb2b1a4c4f8cc172de54e265c0de067e3fae1c1e8316c5cc/phpserialize-1.1-py2.4.egg" }, { "comment_text": "", "digests": { "md5": "ae4b296da9dc98474b4aef40334a6c44", "sha256": "ee28880e8a51ad61ed96880374b47eeaca66c9215cf197f044bb5e312812611b" }, "downloads": -1, "filename": "phpserialize-1.1-py2.5.egg", "has_sig": false, "md5_digest": "ae4b296da9dc98474b4aef40334a6c44", "packagetype": "bdist_egg", "python_version": "2.5", "requires_python": null, "size": 7434, "upload_time": "2008-06-14T18:56:19", "url": "https://files.pythonhosted.org/packages/fa/5b/e98aa30bf2311694108589e51b03df05195a5f5b1933a3223a986a19dbfc/phpserialize-1.1-py2.5.egg" }, { "comment_text": "", "digests": { "md5": "b67e3a5803df06845332735810e685ab", "sha256": "386609fea2be4f183834a1e59d9e1790646167bf1a2d9b0de53f6154be99cf20" }, "downloads": -1, "filename": "phpserialize-1.1.tar.gz", "has_sig": false, "md5_digest": "b67e3a5803df06845332735810e685ab", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3421, "upload_time": "2008-06-14T18:56:19", "url": "https://files.pythonhosted.org/packages/da/68/3f6739a913a7cc89c07aff48eeeb4b88fc59bf85d1c60d259e9506b2b614/phpserialize-1.1.tar.gz" }, { "comment_text": "", "digests": { "md5": "692c2473418e07f98fedbd2e7efef95f", "sha256": "9f56167b8548f76dbbaa15892f093cf202360fd53869cc4a01e4b5d7c7181ae5" }, "downloads": -1, "filename": "phpserialize-1.1.zip", "has_sig": false, "md5_digest": "692c2473418e07f98fedbd2e7efef95f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4854, "upload_time": "2009-07-07T21:45:08", "url": "https://files.pythonhosted.org/packages/11/d9/d8305e96ea50b2039802b1eec5ce29c08c2d50cd26e22edf643517042c1c/phpserialize-1.1.zip" } ], "1.2": [ { "comment_text": "", "digests": { "md5": "be9c4def03fadcc81feab8473e68df40", "sha256": "69a6a2e85dae2c557bd32b1af802e560362751868da87ba1237f3df3ba174d24" }, "downloads": -1, "filename": "phpserialize-1.2.zip", "has_sig": false, "md5_digest": "be9c4def03fadcc81feab8473e68df40", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13067, "upload_time": "2009-10-10T17:00:05", "url": "https://files.pythonhosted.org/packages/92/87/55e1f04ebd81368b03e1227e9a9fb2d0c3602c2e4e3d19c3ab799357255d/phpserialize-1.2.zip" } ], "1.3": [ { "comment_text": "", "digests": { "md5": "cbf88a62e04135e3be3c7fe412525b8b", "sha256": "bf672d312d203d09a84c26366fab8f438a3ffb355c407e69974b7ef2d39a0fa7" }, "downloads": -1, "filename": "phpserialize-1.3.tar.gz", "has_sig": false, "md5_digest": "cbf88a62e04135e3be3c7fe412525b8b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7466, "upload_time": "2012-01-22T12:48:05", "url": "https://files.pythonhosted.org/packages/ec/6d/437efc62d7327bcbcfa18f6bb27a0de3c8621e9af045dfc322d12eb310c9/phpserialize-1.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "cbf88a62e04135e3be3c7fe412525b8b", "sha256": "bf672d312d203d09a84c26366fab8f438a3ffb355c407e69974b7ef2d39a0fa7" }, "downloads": -1, "filename": "phpserialize-1.3.tar.gz", "has_sig": false, "md5_digest": "cbf88a62e04135e3be3c7fe412525b8b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7466, "upload_time": "2012-01-22T12:48:05", "url": "https://files.pythonhosted.org/packages/ec/6d/437efc62d7327bcbcfa18f6bb27a0de3c8621e9af045dfc322d12eb310c9/phpserialize-1.3.tar.gz" } ] }