[Python-Dev] object capability; func_closure; __subclasses__

Phillip J. Eby pje at telecommunity.com
Thu Jun 28 19:26:28 CEST 2007

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>
   Traceback (most recent call last):
     File "ctypes_dicto.py", line 14, in <module>
       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.

More information about the Python-Dev mailing list