[Python-Dev] object capability; func_closure; __subclasses__

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, 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 <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.
>
>


-- 
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