Re: [Python-Dev] CPython and C++ object GC

2009-03-30 Thread Nick Coghlan
Csaba Balazs wrote:
 Hello Everybody,
 
 I would like to use a C++ gui library with the following (simplified) 
 interface
 in Python.

This is a question for python-list/comp.lang.python (i.e. development
*using* Python, including the C API), not python-dev (which is for
development of the language itself).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
---
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] CPython and C++ object GC

2009-03-30 Thread Eric Smith

Nick Coghlan wrote:

Csaba Balazs wrote:

Hello Everybody,

I would like to use a C++ gui library with the following (simplified) interface
in Python.


This is a question for python-list/comp.lang.python (i.e. development
*using* Python, including the C API), not python-dev (which is for
development of the language itself).


There's also the capi-sig mailing list, 
http://mail.python.org/mailman/listinfo/capi-sig.


___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] CPython and C++ object GC

2009-03-30 Thread Csaba Balazs
Hello Everybody,

I would like to use a C++ gui library with the following (simplified) interface
in Python.

#include stdio.h

class Gui;

class GuiObject {
public:
GuiObject(Gui *Gui) {printf(creating GuiObject(gui: %X)\n, Gui);}
~GuiObject() {printf(deleting GuiObject\n);}
void Move(int x, int y) {printf(GuiObject move(%d, %d)\n, x, y);};
};

class Gui {
public:
Gui()  {printf(creating Gui\n);}
~Gui() {printf(deleting Gui\n);}
GuiObject* AddImage() {
GuiObject* ob = new GuiObject(this);
return ob;
}
void Print() {printf(Gui: %X\n, this);}
};

int main() {
Gui *gui = new Gui();
gui-Print();
GuiObject *obj = gui-AddImage();
obj-Move(50, 50);
/*GuiObject *obj2 = new GuiObject(gui); // not allowed
delete obj2;*/
delete obj;
delete gui;
return 0;
}


I created the Python Gui and GuiObject classes (PyTypeObject), and added it to
main module (PyModule_AddObject).
It works, but there is a problem at the Gui::AddImage(), with constructs a new
GuiObject, which is available in Python layer but finally it is not collected
and freed by GC:

...
obj = _PyObject_New(GuiObjectType);
PyObject_Init(obj, GuiObjectType);
...

I cannot invoke the GuiObject object constructor directly from Python,
because of the implementation of the C++ gui library (in this case it would be
collected).
I use the embedded CPython as an interpreter, so I cannot add additional
external .py file for it.

So the following Python code would be the target:

gui = GUI();

background = gui.AddImage();
#background = GuiObject(gui); -- Collected but not allowed
background.ImageFile(bg.jpg);
background.Move(0, 0);
...

How could I implement the AddImage function in order to be freed the constructed
object at the end?

Thanks in advance!

All-the-best,

Csaba

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com