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

Discussion in 'Python' started by johan2sson@gmail.com, Aug 6, 2006.

  1. Guest

    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
     
    , Aug 6, 2006
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 7, 2006
    #2
    1. Advertising

  3. Guest

    Martin v. Löwis wrote:
    > 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_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
     
    , Aug 8, 2006
    #3
  4. Guest

    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, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    Subtyping issue

    valentin tihomirov, Jun 30, 2005, in forum: VHDL
    Replies:
    1
    Views:
    556
    Jonathan Bromley
    Jun 30, 2005
  2. Ragnar
    Replies:
    2
    Views:
    353
    Dan Cernat
    Nov 7, 2003
  3. shawn
    Replies:
    2
    Views:
    279
    shawn
    Oct 17, 2005
  4. bdb112
    Replies:
    2
    Views:
    318
    Chris Torek
    Jul 2, 2011
  5. Michael Le Barbier Grünewald

    Subtyping iterators

    Michael Le Barbier Grünewald, May 9, 2012, in forum: C++
    Replies:
    5
    Views:
    423
Loading...

Share This Page