[Python-Dev] Extending types in C - help needed

Jack Jansen Jack.Jansen@oratrix.nl
Thu, 7 Feb 2002 23:43:46 +0100


On Wednesday, February 6, 2002, at 09:53  PM, Thomas Heller wrote:
>> A better solution is to store additional information in the __dict__.
>
> You loose nice features: access these (new) slots from Python
> by providing tp_members entries for them (for example).

Martin pointed at a way to solve this. And I think that with my 
proposed API (... where is it..., ah yes, found it)
void PyType_SetAnnotation(PyTypeObject *tp, char *name, void 
*unique, void *);
void *PyType_GetAnnotation(PyTypeObject *tp, char *name, void *unique);

it would be almost as easy to use as a tp_ slot. The only thing 
needed to make it 100% safe is a registry for name/descr pairs. 
(Actually the API is changed a little since I understand how the 
second arg works)

For the benefit of whoever missed the previous thread: name is 
used as the key into the dictionary, and unique is a pointer 
stored with the entry, which assures that this entry hasn't been 
used for something else accidentally.

So in stead of a new slot tp_foo what you would need to do is 
come up with a name ("tp_foo" comes to mind) and a global 
variable whose address can be used for unique.
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- 
Emma Goldman -