[Python-Dev] Unbinding of methods (original) (raw)
Brett Cannon brett at python.org
Thu Jul 19 19:00:47 CEST 2012
- Previous message: [Python-Dev] Unbinding of methods
- Next message: [Python-Dev] Unbinding of methods
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Jul 19, 2012 at 12:53 PM, M Stefan <mstefanro at gmail.com> wrote:
Hey,
As part of pickle4, I found it interesting to add the possibility of pickling bound functions (instance methods). This is done by pickling f.self and f.func separately, and then adding a BIND opcode to tie them together. While this appears to work fine for python methods (non-builtin), some issues arise with builtins. These are partly caused because not all builtin function types support func, partly because not all of them fill module when they should and partly because there are many (7) types a function can actually have: ClassMethodDescriptorType = type(??) BuiltinFunctionType = type(len) FunctionType = type(f) MethodType = type(A().f()) MethodDescriptorType = type(list.append) WrapperDescriptorType = type(list.add) MethodWrapperType = type([].add) AllFunctionTypes = (ClassMethodDescriptorType, BuiltinFunctionType, FunctionType, MethodType, MethodDescriptorType, WrapperDescriptorType, MethodWrapperType) repr(AllFunctionTypes) = ( <class 'classmethoddescriptor'>, <class 'builtinfunctionormethod'>, <class 'function'>, <class 'method'>, <class 'methoddescriptor'>, <class 'wrapperdescriptor'>, <class 'method-wrapper'>) I have created a patch at [1], which adds func to some other function types, as well as: 1) adds AllFunctionTypes etc. to Lib/types.py 2) inspect.isanyfunction(), inspect.isanyboundfunction(), inspect.isanyunboundfunction() 3) functools.unbind Note that I am not knowledgeable of cpython internals and therefore the patch needs to be carefully reviewed. Possible issues: Should classmethods be considered bound or unbound? If cm is a classmethod, then should cm.func.self = cm.self or cm.func.self = None? Currently does the latter: >>> cm.self, hasattr(cm,'self'), hasattr(cm.func, 'self') (<class '_main_.A'>, True, False) This requires treating classmethods separately when pickling, so I'm not sure if this is ideal. Let me know if I should have opened an issue instead. I look forward to hearing your opinions/suggestions on this matter.
Yes, open an issue for your patch and reply here with the issue #. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120719/3cfb03a6/attachment.html>
- Previous message: [Python-Dev] Unbinding of methods
- Next message: [Python-Dev] Unbinding of methods
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]