This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author georgexsh
Recipients georgexsh
Date 2019-06-16.01:40:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1560649257.15.0.526142047149.issue37297@roundup.psfhosted.org>
In-reply-to
Content
if we create a bound method object `f` with function object `A.f` and instance object `B()`,
when pickling this bound method object:

	import pickle

	class A():
	    def f(self):
	        pass

	class B():
	    def f(self):
	        pass

	o = B()
	f = A.f.__get__(o)
	pf = pickle.loads(pickle.dumps(f))
	print(f)
	print(pf)

we get:

	<bound method A.f of <__main__.B object at 0x10b82ac18>>
	<bound method B.f of <__main__.B object at 0x10b82ac88>>

the underlaying function are lost, changed from `A.f` to `B.f`.

as pickle calls `__reduce__` method of `method object`, IMO [its implementation][1] simply ignored the real function, whcih is not right.

I have tried a [wordaround][2]:

	import types
	import copyreg

	def my_reduce(obj):
	    return (obj.__func__.__get__, (obj.__self__,))

	copyreg.pickle(types.MethodType, my_reduce)


[1]: https://github.com/python/cpython/blob/v3.7.3/Objects/classobject.c#L75-L89
[2]: https://stackoverflow.com/a/56614748/4201810
History
Date User Action Args
2019-06-16 01:40:57georgexshsetrecipients: + georgexsh
2019-06-16 01:40:57georgexshsetmessageid: <1560649257.15.0.526142047149.issue37297@roundup.psfhosted.org>
2019-06-16 01:40:57georgexshlinkissue37297 messages
2019-06-16 01:40:56georgexshcreate