Andrew, thanks for creating a separate issue (the refleak was very rare and I thought I'd put it in the same place, but now I realize it was a bad idea).
Richard, actually, the isinstance(self, type) check I mentioned earlier would have to be before the hastattr(f, '__func__') check, because Python classmethods provide a __func__ too:
def unbind(f):
self = getattr(f, '__self__', None)
if self is not None and not isinstance(self, types.ModuleType) \
and not isinstance(self, type):
if hasattr(f, '__func__'):
return f.__func__
return getattr(type(f.__self__), f.__name__)
raise TypeError('not a bound method')
Anyway, I'm not convinced this is worth adding anymore. As Antoine Pitrou suggested on the ml, it would probably be a better idea if I implemented __reduce__ for builtin methods as well as Python methods rather than having a separate opcode for pickling methods.