[Python-Dev] object capability; func_closure; subclasses (original) (raw)
Phillip J. Eby pje at telecommunity.com
Thu Jun 28 19:26:28 CEST 2007
- Previous message: [Python-Dev] object capability; func_closure; __subclasses__
- Next message: [Python-Dev] object capability; func_closure; __subclasses__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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, py_object
getdict = pythonapi._PyObject_GetDictPtr
getdict.restype = POINTER(py_object)
getdict.argtypes = [py_object]
def dictionary_of(ob):
dptr = getdict(ob)
if dptr and dptr.contents:
return dptr.contents.value
'dictionary_of' 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 dictionary_of(type)['subclasses'] print "after", type.subclasses
This will print something like:
before <method '__subclasses__' of 'type' objects> after Traceback (most recent call last): File "ctypes_dicto.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 dictionary_of(FunctionType)['func_closure'] del dictionary_of(FunctionType)['func_code']
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.
- Previous message: [Python-Dev] object capability; func_closure; __subclasses__
- Next message: [Python-Dev] object capability; func_closure; __subclasses__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]