Re: [capi-sig] Creating type object dynamically in run-time

Mateusz Loskot, 10.05.2012 18:17:
You want to take the result here, if only to decref it.
What you'd do next is to initialise your types in the module (or let CPython do it for you through the inittab) and then let the Python classes inherit from them, stick C methods into them, etc.
Looks ok to me.
Here's the equivalent code in Cython:
class A(object): pass
(except that it executes faster because it doesn't run through the Python parser and interpreter at runtime, but I don't think that matters much at module init time).
Stefan

On 10 May 2012 17:39, Stefan Behnel <python_capi@behnel.de> wrote:
Right, I missed it out from the example.
What you'd do next is to initialise your types in the module (or let CPython do it for you through the inittab)
I'm not sure I completely understand it. I assume that executing these two lines, in context of the module:
const char* c = "class A(object):\n\tpass"; PyRun_StringFlags(c, Py_file_input, d, d, NULL);
creates complete and initialised A class within this module. Doesn't it?
and then let the Python classes inherit from them, stick C methods into them, etc.
Right.
Thanks for the patience :)
Great!
It looks really helpful and simple, I'll remember, and chances are I will use it in next project.
Best regards,
Mateusz Loskot, http://mateusz.loskot.net

On 10 May 2012 17:39, Stefan Behnel <python_capi@behnel.de> wrote:
Right, I missed it out from the example.
What you'd do next is to initialise your types in the module (or let CPython do it for you through the inittab)
I'm not sure I completely understand it. I assume that executing these two lines, in context of the module:
const char* c = "class A(object):\n\tpass"; PyRun_StringFlags(c, Py_file_input, d, d, NULL);
creates complete and initialised A class within this module. Doesn't it?
and then let the Python classes inherit from them, stick C methods into them, etc.
Right.
Thanks for the patience :)
Great!
It looks really helpful and simple, I'll remember, and chances are I will use it in next project.
Best regards,
Mateusz Loskot, http://mateusz.loskot.net
participants (2)
-
Mateusz Loskot
-
Stefan Behnel