[Python-bugs-list] [ python-Feature Requests-558238 ] Pickling bound methods
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 30 Sep 2002 06:36:21 -0700
Feature Requests item #558238, was opened at 2002-05-20 11:17
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=558238&group_id=5470
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Konrad Hinsen (hinsen)
Assigned to: Martin v. Löwis (loewis)
Summary: Pickling bound methods
Initial Comment:
Last week I noticed that the pickle and cPickle modules
cannot handle bound methods. I found a solution that is
simple and (I think) general, so perhaps it should
become part of the standard library.
Here is my code:
import copy_reg
def pickle_bound_method(method):
return getattr, (method.im_self, method.__name__)
class _Foo:
def bar(self):
pass
_foo = _Foo()
copy_reg.constructor(getattr)
copy_reg.pickle(type(_foo.bar), pickle_bound_method)
----------------------------------------------------------------------
>Comment By: Konrad Hinsen (hinsen)
Date: 2002-09-30 13:36
Message:
Logged In: YES
user_id=11850
True. Would it be a security problem to use getattr inside a
specialized constructor? For example,
def get_method(object, name):
klass = object.__class__
fn = getattr(klass, name)
return new.instancemethod(fn, object, klass)
This would be difficult to abuse as a general getattr
replacement, because new.instancemethod would fail in most
cases other than the intended one.
----------------------------------------------------------------------
Comment By: Martin v. Löwis (loewis)
Date: 2002-09-30 10:03
Message:
Logged In: YES
user_id=21627
Reconsidering: It fails to work in the presence of
inheritance, right?
>>> class B:
... def foo(self):pass
...
>>> class D(B):pass
...
>>> d = D()
>>> m = d.foo
>>> m.im_self
<__main__.D instance at 0x1ccb28>
>>> m.__name__
'foo'
>>> D.__dict__['foo']
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: foo
>>>
----------------------------------------------------------------------
Comment By: Martin v. Löwis (loewis)
Date: 2002-09-24 18:12
Message:
Logged In: YES
user_id=21627
That sounds good to me; I'm going to install it.
----------------------------------------------------------------------
Comment By: Konrad Hinsen (hinsen)
Date: 2002-09-24 17:54
Message:
Logged In: YES
user_id=11850
Of course:
import copy_reg
def get_method(object, name):
klass = object.__class__
fn = klass.__dict__[name]
return new.instancemethod(fn, object, klass)
def pickle_bound_method(method):
return get_method, (method.im_self, method.__name__)
class _Foo:
def bar(self):
pass
_foo = _Foo()
copy_reg.constructor(get_method)
copy_reg.pickle(type(_foo.bar), pickle_bound_method)
----------------------------------------------------------------------
Comment By: Martin v. Löwis (loewis)
Date: 2002-09-22 13:08
Message:
Logged In: YES
user_id=21627
Can you perhaps rewrite this to use new.instancemethod?
----------------------------------------------------------------------
Comment By: Martin v. Löwis (loewis)
Date: 2002-06-09 17:14
Message:
Logged In: YES
user_id=21627
Making getattr a safe_constructor has security implictions
which make this approach dangerous. It seems that unpickling
might invoke arbitrary __getattr__ implementations. Adding a
protocol to declare classes as "safe for getattr" might help.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=558238&group_id=5470