How are you supposed to define subclasses in C?

Stefan Behnel stefan_ml at behnel.de
Thu Apr 21 13:56:19 EDT 2016


Random832 schrieb am 21.04.2016 um 18:35:
> I was trying to write a proof of concept on including descriptors (e.g.
> a "sys.recursionlimit" instead of set/get methods) in the sys module,
> and couldn't figure out how to "properly" define a type using
> PyType_FromSpecWithBases. Everything I tried just segfaulted. I ended up
> just calling PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, ...)
> but I assume there's a better way to do it. I couldn't find any examples
> or tutorial.

I suppose you might find Cython useful:

http://cython.org/

http://docs.cython.org/

In a nutshell, it lets you write beautiful Python code instead of ugly C
code full of leaks and crashes, and then takes care of making it faster
than the usual C-API calls you'd write by hand. It has some extended syntax
for extension types and other C-ish things to make their usage explicit.

Stefan





More information about the Python-list mailing list