[ python-Bugs-1121475 ] Decorated functions are unpickleable

SourceForge.net noreply at sourceforge.net
Sat Feb 12 21:21:40 CET 2005


Bugs item #1121475, was opened at 2005-02-12 11:48
Message generated for change (Settings changed) made by sswamida
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1121475&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
>Priority: 8
Submitted By: S Joshua Swamidass (sswamida)
Assigned to: Nobody/Anonymous (nobody)
Summary: Decorated functions are unpickleable

Initial Comment:
The decorator feature renders functions impossible to 
pickle if you end up wrapping the function in either a 
trivial lambda or a callable class. This is a 
significant/artificial limitation of the decorator which 
should not exist and prevents decorators from being 
used for many of my purposes. 

==========================
Examples:
==========================

>>> def dec(f):
...     return lambda a: f(a)
...
>>> @dec
... def func(a):
...     return a
...
>>> import pickle
>>> pickle.dumps(func)
Traceback (most recent call last):
...
pickle.PicklingError: Can't pickle <function <lambda> at 
0x40160ae4>: it's not found as __main__.<lambda>



>>> class C:
...         def __init__(self, f):
...                 self.f=f
...         def __call__(self, a):
...                 return f(a)
...
>>> def dec(f):
...         return C(f)
>>> @dec
>>> def func(a):
...        return a
>>> import pickle
>>> pickle.dumps(func)
Traceback (most recent call last):
   .....
pickle.PicklingError: Can't pickle <function func at 
0x40160a74>: it's not the same object as __main__.func


==============================
I've found a syntacically ugly ways of working around 
the class wrapper bug that don't use decorators. 
Perhaps this could be used to create a decorator patch:
==================================

>>> class C:
...         def __init__(self, f):
...                 self.f=f
...         def __call__(self, a):
...                 return f(a)
...
>>> def _func(a):
...        return a
>>> func=C(_func)
>>>
>>> import pickle
>>> pickle.dumps(func)
(No error)


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1121475&group_id=5470


More information about the Python-bugs-list mailing list