Registry / serialization / phpserialize

phpserialize

JSON →
library1.3pypypi✓ verified 85d ago

phpserialize is a Python library that ports the `serialize` and `unserialize` functions of PHP, allowing Python applications to encode and decode data in PHP's native serialization format. It implements the standard Python serialization interface, providing `dumps` and `loads` functions. The current version is 1.3, with the last update in 2012, making it in a maintenance phase rather than actively developed, though it officially supports Python 3.

pip install phpserialize
INSTALL
IMPORT
SIG · PHPSERIALIZE
P
phpserialize
serializationpythonv1.3
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dumps
from phpserialize import dumps
loads
from phpserialize import loads
phpobject
from phpserialize import phpobject
Used as an `object_hook` for simple PHP object deserialization.

Demonstrates basic serialization and deserialization of strings and PHP arrays. It highlights the necessity of providing byte strings for input to `loads` in Python 3 and handling byte strings for output, or using `decode_strings=True` for automatic string decoding. Also includes an example of deserializing a simple PHP object using `phpobject` as an `object_hook`.

from phpserialize import dumps, loads # Basic serialization of a string python_string = 'Hello World' php_serialized_string = dumps(python_string) print(f"Serialized string: {php_serialized_string}") assert loads(php_serialized_string) == b'Hello World' # Deserialization of a simple PHP array string (as bytes for Python 3) php_array_data = b'a:1:{s:3:"key";s:5:"value";}' python_dict = loads(php_array_data) print(f"Deserialized array: {python_dict}") # Note: Keys and string values are bytes in Python 3, require decoding assert python_dict[b'key'] == b'value' # Deserialization with string decoding python_dict_decoded = loads(php_array_data, decode_strings=True) print(f"Deserialized array (decoded): {python_dict_decoded}") assert python_dict_decoded['key'] == 'value' # Object serialization example (requires special handling) # Using a simple PHP object string from a stdClass php_object_data = b'O:8:"stdClass":1:{s:3:"baz";s:3:"qux";}' from phpserialize import phpobject python_obj = loads(php_object_data, object_hook=phpobject, decode_strings=True) print(f"Deserialized object: {python_obj}") assert python_obj['baz'] == 'qux'
Debug
Known issues
gotchaIn Python 3, `loads` (unserialize) expects byte strings as input. All string-like values (keys, string contents) in the deserialized output will also be byte strings by default. You must explicitly decode them or use the `decode_strings=True` parameter during `loads`.
fix
Ensure input to `loads` is `bytes`. For output, either manually decode `bytes` results (e.g., `value.decode('utf-8')`) or pass `decode_strings=True` to `loads` to get Python unicode strings directly.
affects: 1.3 (Python 3 support) onwards
gotchaPHP's serialization format has specific ways of handling objects and arrays. When deserializing PHP objects into Python, a custom `object_hook` is often required to map PHP class names to Python classes or dictionaries. Similarly, Python lists and tuples are not distinct PHP types and are typically serialized as PHP associative arrays.
fix
For PHP objects, use the `object_hook` parameter with `loads`. For simple cases, `phpserialize.phpobject` can convert PHP objects to Python dictionaries. For complex type mapping, implement a custom `object_hook` function. Be aware that Python lists/tuples will be represented as PHP arrays, which are dictionaries in Python.
affects: All versions
deprecatedThe original `phpserialize` library has not seen active development since version 1.3 in 2012. While it states Python 3 support, newer Python versions might encounter subtle compatibility issues not addressed by new releases. A community fork, `phpserialize3`, exists specifically for Python 3, which might offer more up-to-date compatibility, although it also appears to be infrequently updated.
fix
Test thoroughly on your target Python version. Consider inspecting the `phpserialize3` fork if facing issues, or evaluate alternative, more actively maintained serialization libraries if strict PHP serialization compatibility is not paramount.
affects: Post-1.3, specifically with newer Python 3.x versions
Errors
Common errors & fixes
TypeError: a bytes-like object is required, not 'str'
In Python 3, the `phpserialize.loads` function expects a `bytes` object as input, but a Python `str` object was provided.
fix
Encode your string data into bytes before passing it to `loads`, typically using `your_string.encode('utf-8')`.
AttributeError: 'bytes' object has no attribute 'decode'
After `phpserialize.loads` processes a PHP serialized string in Python 3, all string-like values (keys and string contents) in the deserialized output are returned as `bytes` objects by default; this error occurs when you try to apply a string-specific method like `decode()` or other string operations on an item that is already a `bytes` object, or without explicit decoding.
fix
To get Python unicode strings, either manually decode the `bytes` results (e.g., `value.decode('utf-8')`) or pass `decode_strings=True` to the `loads` function: `phpserialize.loads(data, decode_strings=True)`.
ValueError: Extra data after serialized value
This error occurs when the input string passed to `phpserialize.loads` contains valid PHP serialized data followed by additional, unrecognized characters or data, indicating a malformed or corrupted serialized string.
fix
Ensure that the input string passed to `loads` contains only the valid PHP serialized data and no extraneous characters. Validate or sanitize the source of your serialized string.
TypeError: can't serialize <type> as key
PHP's serialization format has strict rules for dictionary/array keys, typically limiting them to strings or integers; this error occurs when `phpserialize.dumps` attempts to serialize a Python dictionary key of an unsupported type (e.g., a complex object or a boolean).
fix
Ensure that all keys in dictionaries you are serializing are either strings or integers to be compatible with PHP's serialization format.
Upgrade
Version history
1.3latest on PyPI · released Jan 22, 2012
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
phpserialize — pip install phpserialize · libregistry