prep my types for 2.2?

Martin von Loewis loewis at informatik.hu-berlin.de
Sat Nov 24 06:20:15 EST 2001


Pete Shinners <pete at shinners.org> writes:

> i've got several extension types that i'd like to get prepared for 
> python-2.2. currently i have a function that constructs the object and a 
> "type" variable available..
> 
> Myobject()
> type(Myobject()) is MyobjectType
> 
> from what i can see in python-2.2, these become the same thing. 

They need not, but they can - if you want them to.

> first, i can't figure out exactly what function slots and TP flags i 
> need to use to enable this behavior? did i overlook this in the docs 
> somewhere? (couldn't find)

You need to implement a tp_new slot. If somebody writes

your_type(arguments)

Python will look at the type of your_type (which will be the type
type), and then look at its tp_call slot
(i.e. typeobject.c:type_call). That, in turn, will use tp_new.

> the other question, what is the best way to make this work for both 
> python-2.2 and earlier releases? 

You can leave everything as-is if you want. It will continue to work
with 2.2 as it did with earlier releases.

> it looks like if i hook something nice for 2.2 up in the Type
> structure, that won't work for python-2.1. so it 2.1 i'll need to
> fallback to the classes function/type like i already have? can i
> make it so python-2.1 can construct objects from the Type?

No, you can't. In 2.1, the name of your type can be either bound to
the type obejct, or to the creator function.

In 2.2, you could bind both the name of the type object and the name
of the creator function to the type object, so that you would have

Myobject is Myobjecttype

You could then deprecate one of them, and drop it when everybody has
migrated to 2.2.

Regards,
Martin



More information about the Python-list mailing list