473,378 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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....2952c69182d366

/* 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

Aug 5 '06 #1
3 1729
jo********@gmail.com schrieb:
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
Aug 7 '06 #2
Martin v. Löwis wrote:
jo********@gmail.com schrieb:
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", (), {}).
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_modu le),
"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

Aug 7 '06 #3
jo********@gmail.com wrote:
I am trying to create a subclass of a python class, defined in python,
in C++, but I am having some problems.
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.

Aug 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
2
by: Ragnar | last post by:
Hello If there a derived class privately inheriting from a base class, there is no subtyping relationship between them. So a funtion that expects a base class pointer should not accept a derived...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.