Targeting Python 3.7+, how can one return a new class instance to Python code from a C extension?
Dictionaries, lists, tuples are easy (example of the latter): return Py_BuildValue("(OO)", Py_False, Py_None);
This truly ancient thread (https://grokbase.com/t/python/python-list/99ack91fye/creating-python-class-instances-from-c-api) highlights PyInstance_New()
. Which appears long since removed from the API.
Ideally there would be something like Py_BuildClass("s{{s:s},{s,i}}","Dog","name","fido","age",11);
Any thoughts welcomed.
P.S. Ideally not constructable from Python code. But that'd be icing on the cake.
P.P.S. To clarify. Assume the extension (call it Kennel) manages a Dog class:
struct Dog
{
char* name;
int age;
};
and my Python extension has a function which returns a Dog instance to a Python consumer. It would be good to be able to do this:
dog = kennel.getDog()
print(f"{dog.name} is {dog.age}")