[Python-Dev] 2.4.4: backport classobject.c HAVE_WEAKREFS?

Larry Hastings larry at hastings.org
Fri Oct 20 08:45:31 CEST 2006


Fredrik Lundh wrote:
> a dynamic registration approach would be even better, with a single entry point
> used to register all methods and hooks your C extension has implemented, and
> code on the other side that builds a properly initialized type descriptor from that
> set, using fallback functions and error stubs where needed.

I knocked out a prototype of this last week, emailed Mr. Lundh about it, 
then forgot about it.  Would anyone be interested in taking a peek at it?

I only changed one file to use this new-style initialization, 
sha256module.c.  The resulting init_sha256() looks like this:

PyMODINIT_FUNC
init_sha256(void)
{
    PyObject *m;

    SHA224type = PyType_New("_sha256.sha224", sizeof(SHAobject), NULL);
    if (SHA224type == NULL)
        return;

    PyType_SetPointer(SHA224type, pte_dealloc, &SHA_dealloc);
    PyType_SetPointer(SHA224type, pte_methods, &SHA_methods);
    PyType_SetPointer(SHA224type, pte_members, &SHA_members);
    PyType_SetPointer(SHA224type, pte_getset, &SHA_getseters);

    if (PyType_Ready(SHA224type) < 0)
        return;

    SHA256type = PyType_New("_sha256.sha256", sizeof(SHAobject), NULL);
    if (SHA256type == NULL)
        return;

    PyType_SetPointer(SHA256type, pte_dealloc, &SHA_dealloc);
    PyType_SetPointer(SHA256type, pte_methods, &SHA_methods);
    PyType_SetPointer(SHA256type, pte_members, &SHA_members);
    PyType_SetPointer(SHA256type, pte_getset, &SHA_getseters);

    if (PyType_Ready(SHA256type) < 0)
        return;

    m = Py_InitModule("_sha256", SHA_functions);
    if (m == NULL)
        return;
}

In a way this wasn't really a good showpiece for my code.  The 
"methods", "members", and "getseters" structs still need to be passed 
in.  However, I did change all four "as_" structures so you can set 
those directly.  For instance, the "concat" as_sequence method for a 
PyString object would be set using
        PyType_SetPointer(PyString_Type, pte_sequence_concat, 
string_concat);
(I actually converted the PyString object to my new code, but had 
chicken-and-egg initialization problems as a result and backed out of 
it.  The code is still in the branch, just commented out.)

Patch available for interested parties,


/larry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20061019/531206b1/attachment.htm 


More information about the Python-Dev mailing list