[Python-Dev] How to suppress instance __dict__?

David Abrahams dave@boost-consulting.com
Wed, 02 Apr 2003 07:57:34 -0500

Hi, Joel --

I don't think this is more than marginally appropriate for python-dev,
and probably we shouldn't bother Guido about it until I've failed to
help you first.  Everybody else can ignore the rest of this message
unless they have a sick fascination with Boost.Python...

"Joel de Guzman" <joel@boost-consulting.com> writes:

> Ok, I'm lost. Please be easy with me, I'm still learning the C API
> interfacing with Python :) Here's what I have so far. Emulating the 
> desired behavior in Python, I can do:
>     class EnumMeta(type):
>         def __new__(cls, name, bases, dict):
>             C = type.__new__(cls, name, bases, dict)
>             del C.__getstate__
>             return C
>     class Enum(int):
>         __metaclass__ = EnumMeta
>         __slots__ = ()
>     x = Enum(1964)
>     print x
>     import pickle
>     print "SAVING"
>     out_x = pickle.dumps(x)
>     print "LOADING"
>     xl = pickle.loads(out_x)
>     print xl
> I'm trying to rewrite this in C/C++ with the intent to patch 
> Boost.Python to allow pickling on enums. I took on this task to
> learn more about the low level details of Python C interfacing.    
> So far, I have implemented EnumMeta in C that does not override
> anything yet and installed that as the metaclass of Enum.
> I was wondering... Is there some C code somewhere that I can see
> that implements some sort of meta-stuff? 

We have some in Boost.Python already, and I'm about to check in some
more to implement static data members.

> I read PEP253 and 253 and "Unifying Types and Classes in Python
> 2.2". The examples there (specifically the class autoprop) is
> written in Python. I tried searching for examples in C from the
> current CVS snapsot of 2.3 but I failed in doing so. I'm sure it's
> there, but I don't know where to find.

Actually there are very few metaclasses in Python proper. AFAIK,
PyType_Type is the only metaclass in the core.

> To be specific, I'm lost in trying to implement tp_new of
> PyTypeObject.  How do I call the default tp_new for metaclasses?

  PyTypeObject.tp_new( /*args here*/ ) should work.


Dave Abrahams
Boost Consulting