Subtyping a non-builtin type in C/C++

J

johan2sson

Hi

I am trying to create a subclass of a python class, defined in python,
in C++, but I am having some problems. It all boils down to a problem
of finding the base class' type object and according to the PEP (253) I
would also need to figure out the size of the base class instance
structure, but I'm guessing that the latter can't be done in a way that
would allow me to define a structure for my own type.

The following code is what I've tried, which is an adaptation of
http://groups.google.com/group/comp.lang.python/msg/b32952c69182d366

/* import the module that holds the base class */
PyObject *code_module = PyImport_ImportModule("code");
if (!code_module) return;

/* get a pointer to the base class */
PyObject *ic_class = PyMapping_GetItemString(
PyModule_GetDict(code_module),
"InteractiveConsole");
if (!ic_class) return;

/* assign it as base class */
// The next line is the original code, but as far as I can understand
// the docs for Py_BuildValue the code used is equivalent. I find it
// to be clearer as well.
// EmConType.tp_bases = Py_BuildValue("(O)", ic_class);

Py_INCREF(ic_class);
EmConType.tp_bases = ic_class;

if (PyType_Ready(&EmConType) < 0)

This breaks an assertion in the function classic_mro called by
PyType_Ready however, specifically that on line 1000 of typeobject.c:
assert(PyClass_Check(cls)). To get there you need to fail PyType_Check
as well and indeed, at least in the debug build, cls.ob_type.tp_name is
"dict". ic_class appears to be a "classobj" before the call to
PyType_Ready. I would of course much rather have seen that it was a
"type", but I'm not sufficiently well versed in the python guts to know
if this is ok or not.

If anyone could please lend me a clue, I'd be terribly happy about it.

Thanks,
Johan
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

I am trying to create a subclass of a python class, defined in python,
in C++, but I am having some problems.

Is the base class a classic class or a new-style class? Depending on
the answer, the code you should write varies significantly.

To create a new type, it might be easiest to do the same as the
interpreter. Take, for example, a look at the code that gets executed
for new.classobj("Foo", (), {}).

Regards,
Martin
 
J

johan2sson

Martin said:
Is the base class a classic class or a new-style class? Depending on
the answer, the code you should write varies significantly.

To create a new type, it might be easiest to do the same as the
interpreter. Take, for example, a look at the code that gets executed
for new.classobj("Foo", (), {}).

I deleted my post once I realized that I had been setting tp_bases when
I should have been setting tp_base. As it turns out, that's not the end
of my problems.

I have looked in classobject.c, which I hope is the code you are
referring to, but that doesn't really solve my problem since it assumes
you have the correct base pointers and if I did I would be having a
different problem! Anyway, back to the current one:

The class I am trying to subclass is code.InteractiveConsole. With
tp_base set to the result of
PyMapping_GetItemString(PyModule_GetDict(code_module),
"InteractiveConsole"), PyType_Ready fails with an AttributeError with a
"value" of "mro" and looking at the structure in a debugger just before
the call to PyType_Ready makes me think it's corrupted somehow
(nonprintable name and clearly uninitialized fields).

I can however create an instance by calling PyObject_CallFunction on
that object. If I do, it's type (ob_type->tp_name) will be "instance".
Of course, I don't know the python type system that well, but I must
confess I was expecting it to be "code.InteractiveConsole". What gives?

If I then lookup the __class__ attribute of that instance I am back
full circle at the object with the unprintable name and garbage fields.
The head does seem to be valid and if it means anything to someone it's
ob_type->tp_name is "classobj". For several hours I actually wondered
why it wasn't "Type" but I guess it's just a ... subtype of Type.

I guess I should just go to bed.

Johan
 
J

johan2sson

Note to future news group archeologists:

I have since learned that that's because it "can't be done", in the
sense that there's no defined way that is supposed to work. Of course
it could probably be done by reverse engineering what is done by the
interpreter and then applying the same steps manually, but it would be
a real hack.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top