Consider the following script:
from marshal import dumps, loads
def square_fn(x):
return x * x
def test_serialize_marshal():
code_string = dumps(square_fn.__code__)
code_string1 = dumps(loads(code_string))
assert code_string == code_string1
Run in Python 3.6 with pytest
.
This test is failing on my computer. Why is this so?
I am trying to serialize a Python function in a way that I can test, from its serialized representation, that the function is the same... This test suggests that this is not the case for marshal dumps/loads.
EDIT:
Interestingly, this test passes:
def test_serialize_marshal2():
import types
code = dumps(square_fn.__code__)
ff = types.FunctionType(loads(code), globals())
assert square_fn.__code__ == ff.__code__
which is what this answer tests. However, I do not want the equality of the Python object, but of the string.