Author sbt
Recipients sbt
Date 2012-03-16.19:03:37
SpamBayes Score 2.77556e-16
Marked as misclassified No
Message-id <>
When pickling a function object, if it cannot be saved as a global the C implementation falls back to using copyreg/__reduce__/__reduce_ex__.

The comment for the changeset which added this fallback claims that it is for compatibility with the Python implementation.  See

However, the current python implementations do not have any such fallback.

This affects both 2.x and 3.x.

For example

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle, cPickle, copy_reg
>>> def f():
...   pass
>>> _f = f
>>> del f
>>> copy_reg.pickle(type(_f), lambda obj: (str, ("FALLBACK",)))
>>> cPickle.dumps(_f)
>>> pickle.dumps(_f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "c:\Python27\lib\", line 224, in dump
  File "c:\Python27\lib\", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\Python27\lib\", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function f at 0x0299A470>: it's not found as __main__.f

I don't know what should be done.  I would be tempted to always fall back to copyreg/__reduce__/__reduce_ex__ when save_global fails (not just for function objects) but that might make error messages less helpful.
Date User Action Args
2012-03-16 19:03:39sbtsetrecipients: + sbt
2012-03-16 19:03:39sbtsetmessageid: <>
2012-03-16 19:03:38sbtlinkissue14336 messages
2012-03-16 19:03:37sbtcreate