[Python-Dev] object capability; func_closure; subclasses (original) (raw)

tav tav at espians.com
Thu Jun 28 19:35:23 CEST 2007


I love you PJE! Thank you! =)

On 6/28/07, Phillip J. Eby <pje at telecommunity.com> wrote:

At 05:23 PM 6/28/2007 +0100, tav wrote: >Any pointers on removing members via ctypes front? > >Whilst I can understand even the most obscure aspects of your python >code fine, I'm not familiar with C/ctypes...

What you want is to get access to the type's real dictionary, not the proxy. Then you can just delete 'subclasses' from the dictionary using Python code. Here's some code that does the trick: from ctypes import pythonapi, POINTER, pyobject getdict = pythonapi.PyObjectGetDictPtr getdict.restype = POINTER(pyobject) getdict.argtypes = [pyobject] def dictionaryof(ob): dptr = getdict(ob) if dptr and dptr.contents: return dptr.contents.value 'dictionaryof' returns either a dictionary object, or None if the object has no dictionary. You can then simply delete any unwanted contents. However, you should never use this to assign special methods, as Python will not change the type slots correctly. Heck, you should probably never use this, period. :) Usage example: print "before", type.subclasses del dictionaryof(type)['subclasses'] print "after", type.subclasses This will print something like: before <method '_subclasses_' of 'type' objects> after Traceback (most recent call last): File "ctypesdicto.py", line 14, in print "after", type.subclasses AttributeError: type object 'type' has no attribute 'subclasses' et voila. You should also be able to delete unwanted function type attributes like this:: from types import FunctionType del dictionaryof(FunctionType)['funcclosure'] del dictionaryof(FunctionType)['funccode'] Of course, don't blame me if any of this code fries your computer and gives you a disease, doesn't work with new versions of Python, etc. etc. It works for me on Windows and Linux with Python 2.3, 2.4 and 2.5. It may also work with 3.0, but remember that func* attributes have different names there.

-- love, tav founder and ceo, esp metanational llp

plex:espians/tav | tav at espians.com | +44 (0) 7809 569 369



More information about the Python-Dev mailing list