PyObject_New

Gerhard Häring gh at ghaering.de
Fri Oct 7 02:28:45 EDT 2005


Jeremy Moles wrote:
> [...] What I'm trying
> now is the following:
> 
> 	PyObject* obj = _PyObject_New(&PyType_MyType);
> 	obj = PyObject_Init(obj, &PyType_MyType);
> 	
> 	...
> 
> 	return obj;
> 
> When "obj" gets back to the interpreter, Python sees it (or rather, it's
> __repr__) in accordance with what it "should" be. However, any attempt
> to USE the object results in a segfault. I feel my problem is simply
> that I'm not allocating "obj" correctly in the C++ function.
> 
> If anyone has any advice, I would really appreciate it.

When I looked for info on this, I didn't find any good documentation, 
either.

I'm currently using code like the following in pysqlite 
(http://initd.org/tracker/pysqlite/browser/pysqlite/trunk/src/connection.c?rev=154):

126	    if (factory == NULL) {
127 	        factory = (PyObject*)&CursorType;
128 	    }
129 	
130 	    cursor = PyObject_CallFunction(factory, "O", self);

So, basically I'm just calling the type like any other callable object. 
I don't remember if there was a reason why I didn't use 
PyObject_CallObject instead, at the time ...

In any case, this approach worked fine for me.

-- Gerhard




More information about the Python-list mailing list