return own type from Python extention?
I used the examples from the "Extending and Embedding the Python Interpreter" tutorial and this works. I can use my types with python. But I do not know how to creat my own Python variable in an python extending c-code. What will I have to creat this and return it to my python programm? -- http://mail.python.org/mailman/listinfo/python-list
Re: return own type from Python extention?
Thx, but my Problem is to get my own type before.
If I have a C-Type, I know how tu return it from the Python extention,
but how will it work with my own type?
I expect something like the following:
static PyObject* wrap_MyFunction (PyObject* self, PyObject* args)
{
:
MyPyType *myType = MyTypeNew ();
return (PyObject*)myType;
}
How will the Konstructor-Funktion look for MyPyType to call this in C-Code.
...If I explain my problem to confuesed here more informations:
I have one function for MyPyType to construct it by calling from python:
static PyObject* PyMyType_new(PyTypeObject *type, PyObject *args,
PyObject *kwds)
{
PyMyType *self;
self = (PyMyType*)type->tp_alloc(type, 0);
if (self != NULL) {
self->test = 0;
}
return (PyObject *)self;
}
..but how do I have to call this from C-Code or how will another
Funktion for this look like?
Jeremy Moles wrote:
> You can use Py_BuildValue for most what you're probably going to need.
>
> http://docs.python.org/api/arg-parsing.html
>
> On Thu, 2005-09-29 at 15:39 +0200, elho wrote:
>
>>I used the examples from the "Extending and Embedding the Python
>>Interpreter" tutorial and this works. I can use my types with python.
>>But I do not know how to creat my own Python variable in an python
>>extending c-code. What will I have to creat this and return it to my
>>python programm?
>
>
--
http://mail.python.org/mailman/listinfo/python-list
return (PyObject*)myPyType; ...segmentation fault!
I called a own python type 'PyType' with a c function and returned it
into my python programm - there it fault.
It is said that the object has a NULL-Pointer when I try to debug it?
Here are the importent snips from my code:
// == test.py =
.
:
myNewPyType = PyMyExtention.GetValue ("xxx")
# printings for testing
print "...back to python... test.py"
print "pp\t ...PyMyType.PyMyObject:", type(tySdlXml)
//===/
// == PyMyExtention.c =
.
:
static PyObject* wrap_GetValue (PyObject* self, PyObject* args)
{
char* pchXXX;
if (!PyArg_ParseTuple(args, "s", &pchXXX))
{
return 0;
}
long llong = CFunktion::CallMe(pchXXX);
// returning Python-Objekt
PyObject *pyType = PyMyObject_NewC (llong);
cout << "cc ..." << ((PyMyType*)pyType)->lAttribute << endl;
cout << "\t ...proof object-valid pointer?" << (void*)pyType << endl;
return (PyObject*)pyType;
}
.
:
//===/
// == PyMyExtention.c =
.
:
typedef struct {
PyObject_HEAD
long lAttribute;
} PyMyObject;
static PyObject* PyMyObject_NewC (long lAttribute)
{
PySDLXMLNode *self;
PySDLXMLNode *type;
self = new PySDLXMLNode;
self->lAttribute = lAttribute;
return (PyObject*)self;
}
static PyMethodDef PyMyObject_methods[] = {
{"PyMyObject_NewC", (PyCFunction)PyMyObject_NewC, METH_NOARGS,
"Create PyMyObject_NewC from C-Code"},
{NULL} /* Sentinel */
};
:
static PyTypeObject PySDLXMLNodeType = {
PyObject_HEAD_INIT(NULL)
:
};
//===/
// ::: output
cc ...135603272
t ...proof object-valid pointer?: 0x8165940
...back to python... test.py
Segmentation fault
//===/
...you see: It returns to python but over there the object is something
bad. So what is wrong?
--
http://mail.python.org/mailman/listinfo/python-list
Re: return (PyObject*)myPyType; ...segmentation fault!
> > It is said that the object has a NULL-Pointer when I try to debug it?
> what object?
the python one 'myNewPyType'
Sorry, I forgot to change:
PySDLXMLNodeType = PyMyType
..above the corrections
// == PyMyExtention.c =
.
:
typedef struct {
PyObject_HEAD
long lAttribute;
} PyMyObject;
static PyObject* PyMyObject_NewC (long lAttribute)
{
PyMyObject *self;
PyMyObject *type;
self = new PyMyObject
self->lAttribute = lAttribute;
return (PyObject*)self;
}
static PyMethodDef PyMyObject_methods[] = {
{"PyMyObject_NewC", (PyCFunction)PyMyObject_NewC, METH_NOARGS,
"Create PyMyObject_NewC from C-Code"},
{NULL} /* Sentinel */
};
:
static PyTypeObject PyMyType = {
PyObject_HEAD_INIT(NULL)
:
};
//===/
--
http://mail.python.org/mailman/listinfo/python-list
Extended Language c++ in pyhton
I found serveral tool for using C++ as extended languate in python - but what's the best / easiest to use? With C I used wrappy - not sure if it's the wright name, it's included since Python 1.6 and it ist pretty ease to use. You know an example with this util for C++ or isn't it possible for C++. Would be nice to get your opinion. thx -- http://mail.python.org/mailman/listinfo/python-list
