[issue3657] pickle can pickle the wrong function

Antoine Pitrou report at bugs.python.org
Fri Jul 16 20:39:26 CEST 2010


Antoine Pitrou <pitrou at free.fr> added the comment:

It should be noted that, contrary to Amaury's suggestion, pickling random.seed fails under 3.x:

>>> pickle.dumps(random.seed)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/antoine/py3k/__svn__/Lib/pickle.py", line 1314, in dumps
    Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
_pickle.PicklingError: Can't pickle <class 'method'>: attribute lookup builtins.method failed

Furthermore, the original problem can also be reproduced under 3.x, using Amaury's trick:

>>> pickle.dumps(random.random)
b'\x80\x03crandom\nrandom\nq\x00.'
>>> list(sys.modules.values())[0].random = random.random
>>> pickle.dumps(random.random)
b'\x80\x03cheapq\nrandom\nq\x00.'

I think a possible heuristic in whichmodule() would be, if __module__ is not found or None, to look for a __module__ attribute on __self__:

>>> random.random.__module__
>>> random.random.__self__.__module__
'random'

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3657>
_______________________________________


More information about the Python-bugs-list mailing list