Initializing new types
Martin v. Loewis
martin at v.loewis.de
Mon Jan 7 01:07:51 EST 2002
cpitaar at yahoo.com.ar (Carlos P.) writes:
> The most important question is: am I using PyType_Ready() as I
> should?
Yes, that looks alright.
> But I´m not sure if the way to explicitly initialize the type is
> the one I´m following.
What makes you think you might not?
> Besides this, the code in xxsubtype.c confuses
> me a little more because of the double initialization with different
> base types:
>
> """
> DL_EXPORT(void)
> initxxsubtype(void)
> {
> PyObject *m, *d;
>
> spamdict_type.tp_base = &PyDict_Type;
> if (PyType_Ready(&spamdict_type) < 0)
> return;
>
> spamlist_type.tp_base = &PyList_Type;
> if (PyType_Ready(&spamlist_type) < 0)
> return;
That, in itself, is not double-initialization. Notice that these are
two different types: spam*dict* and spam*list*, each of which is
initialized but once in this fragment. There is another initialization
of these types later on in initxxsubtype, which is superfluous.
> And a last thing: PEP 253 says something about PyType_InitDict(). But
> I couldn´t find this function in the sources. Perhaps the PEP is a
> little outdated, isn´t it?
That is very possible. Please submit a bug report or patch to
sf.net/projects/python.
> Well, as I´ve said before, my sample code is listed below. And after
> it, an alternative one that shows what I think is not the right way
> (or at least the best way) to initialize a type.
Even if you fill out all fields of a type, you should still call
PyType_Ready; future Python versions may add additional fields with
default contents. So you should put contents into the fields only
if this is specific to your type; no need to put in generic routines.
HTH,
Martin
More information about the Python-list
mailing list