<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++ Objectwrapper für Python]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe ein kleines Problem. Ich habe in C++ einen kleinen Objektwrapper geschrieben der mir eine C++ Klasse in Python zugänglich macht. Es ist auch gar nicht viel Code notwendig, jedoch kann ich nur ein Objekt erzeugen und habe leider keinen zugriff auf die Member dieser Klasse.</p>
<p>Weiß einer mit welchen Befehlen dies funktioniert?</p>
<pre><code class="language-cpp">#include &quot;Python/Python.h&quot;
#include &quot;Python/structmember.h&quot;

int NumMemberMult(void);
class Numbers
{
public:
	Numbers(int first, int second)
	{
		m_first = first;
		m_second = second;
	}
    int NumMemberMult(void)
	{
		printf(&quot;TEST&quot;);
		return m_first*m_second;
	}
private:
    int m_first;
    int m_second;
};

PyObject *wrap_Numbers_MemberMult(PyObject *, PyObject* args)
{
    PyObject *pynum = 0;
    int ok = PyArg_ParseTuple( args, &quot;O&quot;, &amp;pynum);
    if(!ok) return NULL;
    void * temp = PyCObject_AsVoidPtr(pynum);
    Numbers * thisnum = static_cast&lt;Numbers *&gt;(temp);
    int result = thisnum-&gt;NumMemberMult();	
	return Py_BuildValue(&quot;i&quot;,result);

}

static void PyDelNumbers(void *ptr)
{
    Numbers * oldnum = static_cast&lt;Numbers *&gt;(ptr);
    delete oldnum;
    return;
}

PyObject *wrap_new_Numbers(PyObject *, PyObject* args)
{
    int arg1;
    int arg2;
    int ok = PyArg_ParseTuple(args,&quot;ii&quot;,&amp;arg1,&amp;arg2);
    if(!ok) return 0;

    Numbers *newnum = new Numbers(arg1,arg2);
    return PyCObject_FromVoidPtr( newnum, PyDelNumbers);
}

static PyMethodDef numbersMethods[] = 
{
    { &quot;Numbers&quot;, (PyCFunction)wrap_new_Numbers, METH_VARARGS,      &quot;PyNumbers(int, float)-&gt;new Numbers object&quot;},
    { &quot;mult&quot;, (PyCFunction)wrap_Numbers_MemberMult,       METH_VARARGS,       &quot;Numbers_MembMult( numbers) -&gt; float&quot;},

    {NULL,NULL}
};

int main()
{
	PyObject *modules, *dictionaries, *m;

	Py_Initialize();
	modules = PyImport_AddModule(&quot;__main__&quot;);
	dictionaries = PyModule_GetDict(modules);

	m = Py_InitModule(&quot;c4d&quot;, numbersMethods);

	PyRun_SimpleString(&quot;from c4d import *\n&quot;
						&quot;t = Numbers(3,4);\n&quot;
						&quot;print('check')&quot;);

/*PyRun_SimpleString(&quot;from c4d import *\n&quot;
				   &quot;print('hello')\n&quot;
				   &quot;&quot;);*/

	Py_Finalize();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/214500/c-objectwrapper-für-python</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 08:06:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/214500.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 30 May 2008 07:17:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Fri, 30 May 2008 07:17:24 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe ein kleines Problem. Ich habe in C++ einen kleinen Objektwrapper geschrieben der mir eine C++ Klasse in Python zugänglich macht. Es ist auch gar nicht viel Code notwendig, jedoch kann ich nur ein Objekt erzeugen und habe leider keinen zugriff auf die Member dieser Klasse.</p>
<p>Weiß einer mit welchen Befehlen dies funktioniert?</p>
<pre><code class="language-cpp">#include &quot;Python/Python.h&quot;
#include &quot;Python/structmember.h&quot;

int NumMemberMult(void);
class Numbers
{
public:
	Numbers(int first, int second)
	{
		m_first = first;
		m_second = second;
	}
    int NumMemberMult(void)
	{
		printf(&quot;TEST&quot;);
		return m_first*m_second;
	}
private:
    int m_first;
    int m_second;
};

PyObject *wrap_Numbers_MemberMult(PyObject *, PyObject* args)
{
    PyObject *pynum = 0;
    int ok = PyArg_ParseTuple( args, &quot;O&quot;, &amp;pynum);
    if(!ok) return NULL;
    void * temp = PyCObject_AsVoidPtr(pynum);
    Numbers * thisnum = static_cast&lt;Numbers *&gt;(temp);
    int result = thisnum-&gt;NumMemberMult();	
	return Py_BuildValue(&quot;i&quot;,result);

}

static void PyDelNumbers(void *ptr)
{
    Numbers * oldnum = static_cast&lt;Numbers *&gt;(ptr);
    delete oldnum;
    return;
}

PyObject *wrap_new_Numbers(PyObject *, PyObject* args)
{
    int arg1;
    int arg2;
    int ok = PyArg_ParseTuple(args,&quot;ii&quot;,&amp;arg1,&amp;arg2);
    if(!ok) return 0;

    Numbers *newnum = new Numbers(arg1,arg2);
    return PyCObject_FromVoidPtr( newnum, PyDelNumbers);
}

static PyMethodDef numbersMethods[] = 
{
    { &quot;Numbers&quot;, (PyCFunction)wrap_new_Numbers, METH_VARARGS,      &quot;PyNumbers(int, float)-&gt;new Numbers object&quot;},
    { &quot;mult&quot;, (PyCFunction)wrap_Numbers_MemberMult,       METH_VARARGS,       &quot;Numbers_MembMult( numbers) -&gt; float&quot;},

    {NULL,NULL}
};

int main()
{
	PyObject *modules, *dictionaries, *m;

	Py_Initialize();
	modules = PyImport_AddModule(&quot;__main__&quot;);
	dictionaries = PyModule_GetDict(modules);

	m = Py_InitModule(&quot;c4d&quot;, numbersMethods);

	PyRun_SimpleString(&quot;from c4d import *\n&quot;
						&quot;t = Numbers(3,4);\n&quot;
						&quot;print('check')&quot;);

/*PyRun_SimpleString(&quot;from c4d import *\n&quot;
				   &quot;print('hello')\n&quot;
				   &quot;&quot;);*/

	Py_Finalize();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1519445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1519445</guid><dc:creator><![CDATA[DonnerCobra]]></dc:creator><pubDate>Fri, 30 May 2008 07:17:24 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Fri, 30 May 2008 09:27:18 GMT]]></title><description><![CDATA[<p>du weißt aber schon, dass es <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/index.html" rel="nofollow">Boost.Python</a> gibt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1519555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1519555</guid><dc:creator><![CDATA[Xantus]]></dc:creator><pubDate>Fri, 30 May 2008 09:27:18 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Fri, 30 May 2008 11:13:15 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Ja, ich weiß, aber Boost ist nicht ThreadSafe und ich muss von mehreren Threads darauf zugreifen können, daher bringt mir das leider nichts. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":-("
      alt="😞"
    /></p>
<p>Bye</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1519625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1519625</guid><dc:creator><![CDATA[DonnerCobra]]></dc:creator><pubDate>Fri, 30 May 2008 11:13:15 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Fri, 30 May 2008 11:31:18 GMT]]></title><description><![CDATA[<p>Mach doch die synchronisierung drum?<br />
(Verpackt in ein Wrapper)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1519650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1519650</guid><dc:creator><![CDATA[simon.gysi]]></dc:creator><pubDate>Fri, 30 May 2008 11:31:18 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Fri, 30 May 2008 16:25:42 GMT]]></title><description><![CDATA[<p>DonnerCobra schrieb:</p>
<blockquote>
<p>Hi!</p>
<p>Ja, ich weiß, aber Boost ist nicht ThreadSafe und ich muss von mehreren Threads darauf zugreifen können, daher bringt mir das leider nichts. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":-("
      alt="😞"
    /></p>
<p>Bye</p>
</blockquote>
<p>Boost ist nicht thread-safe? Eher Python …<br />
Und für das GIL kannst du dir mit sowas behelfen:</p>
<pre><code class="language-cpp">class global_interpreter_lock : boost::noncopyable
{
public:
    global_interpreter_lock () : _state (PyGILState_Ensure ()) {}
    ~global_interpreter_lock () { PyGILState_Release (_state); }
private:
    PyGILState_STATE _state;
};
</code></pre>
<p>Bei mir hat's das in Kombination mit dem Boost.Function-Wrapper für Boost.Python prima getan.</p>
<p>Aber beschreib mal deine genaue Situation…</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1519861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1519861</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Fri, 30 May 2008 16:25:42 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Sat, 31 May 2008 10:53:56 GMT]]></title><description><![CDATA[<p>Okay,</p>
<p>jedoch kann ich trotzdem nicht mit Boost arbeiten. Da stellt sich die Frage, ob es eventuell eine Möglichkeit gibt, das über die normalen Python Libraries zu machen.</p>
<p>Bye <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Auch wenn das jetzt etwas viel Code ist, aber folgendes sollte passieren.</p>
<p>Ich möchte (siehe ganz unten) einen Vector erstellen mittels</p>
<p>v = Vector(30,40,50)</p>
<p>Jetzt sollte letztlich das hier funktionieren</p>
<p>v.getX() und es soll wieder 30 herauskommen. Jedoch klappt das alles nicht so wie ich möchte.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":-("
      alt="😞"
    /></p>
<pre><code class="language-cpp">// python.cpp : Definiert den Einstiegspunkt f¸r die Konsolenanwendung.
//
#include &quot;Python/Python.h&quot;
#include &quot;Python/structmember.h&quot;

	//Vector.	getX	getY	getZ
	class Vector2 {

		private:
			int x1,y1,z1;

		public:
			Vector2(){
				x1 = 0;
				y1 = 0;
				z1 = 0;
			}

			Vector2(int a){

			}

			Vector2(int x,int y, int z){
				x1 = x;
				y1 = y;
				z1 = z;
			}

			int getX(){
				return x1;
			}

			int getY(){
				return y1;
			}

			int getZ(){
				return z1;
			}

	};

staticforward PyTypeObject VectorType;

typedef struct {
    PyObject_HEAD
    PyObject* number;
} Vector;

static void Vector_dealloc(Vector* self)
{
    self-&gt;ob_type-&gt;tp_free((PyObject*)self);
}

static PyObject * Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    Vector *self;

    self = (Vector *)type-&gt;tp_alloc(type, 0);
    if (self != NULL) {

		int x,y,z;

		if (!PyArg_ParseTuple(args, &quot;iii&quot;, &amp;x,&amp;y,&amp;z)) return NULL;

		self = PyObject_New(Vector2, &amp;VectorType);
		/*Vector2* k = new Vector2(x,y,z);
		self-&gt;number = &amp;k;*/
    }

    return (PyObject *)self;
}

static PyObject* noddy_new_noddy(PyObject* self, PyObject* args)
{
    noddy_NoddyObject* noddy;

    if (!PyArg_ParseTuple(args,&quot;:new_noddy&quot;)) 
        return NULL;

    noddy = PyObject_New(noddy_NoddyObject, &amp;noddy_NoddyType);

    return (PyObject*)noddy;
}

static int Vector_init(Vector *self, PyObject *args, PyObject *kwds)
{
    return 0;
}

static PyMemberDef Vector_members[] = {
    {NULL}  /* Sentinel */
};

//SubMethoden
static PyObject * Vector_x(Vector* self)
{ 
    //return PyLong_FromLong(self-&gt;number-&gt;getX());
}

static PyObject * Vector_y(Vector* self)
{ 
    //return PyLong_FromLong(self-&gt;number-&gt;getY());
}

static PyObject * Vector_z(Vector* self)
{ 
    //return PyLong_FromLong(self-&gt;number-&gt;getZ());
}
static PyMethodDef Vector_methods[] = {
    {&quot;getX&quot;, (PyCFunction)Vector_x, METH_NOARGS, &quot;Return the name, combining the first and last name&quot;},
	{&quot;getY&quot;, (PyCFunction)Vector_y, METH_NOARGS, &quot;Return the name, combining the first and last name&quot;},
	{&quot;getZ&quot;, (PyCFunction)Vector_z, METH_NOARGS, &quot;Return the name, combining the first and last name&quot;},
    {NULL}
};

static PyTypeObject VectorType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    &quot;c4d.Vector&quot;,             /*tp_name*/
    sizeof(Vector),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)Vector_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    &quot;Vector objects&quot;,           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    Vector_methods,             /* tp_methods */
    Vector_members,             /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)Vector_init,      /* tp_init */
    0,                         /* tp_alloc */
    Vector_new,                 /* tp_new */
};

static PyMethodDef module_methods[] = {
    {NULL}  /* Sentinel */

};

int main()
{

	Py_Initialize();
	PyObject* m;
    if (PyType_Ready(&amp;VectorType) &lt; 0)
			return false;

	m = Py_InitModule3(&quot;c4d&quot;, module_methods, &quot;Example module that creates an extension type.&quot;);

	if (m == NULL)
      return false;

	Py_INCREF(&amp;VectorType);
    PyModule_AddObject(m, &quot;VectorList&quot;, (PyObject *)&amp;VectorListType);

	PyRun_SimpleString(&quot;from c4d import *\n&quot;
						&quot;v1 = Vector(80,70,90)\n&quot;
						&quot;print(str(v1.getX()))&quot;);
	Py_Finalize();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1520102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1520102</guid><dc:creator><![CDATA[DonnerCobra]]></dc:creator><pubDate>Sat, 31 May 2008 10:53:56 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Objectwrapper für Python on Mon, 27 Oct 2008 10:01:12 GMT]]></title><description><![CDATA[<p>warum verwendest du eine static PyTypeObject VectorType?</p>
<p>bye</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605421</guid><dc:creator><![CDATA[cpp1]]></dc:creator><pubDate>Mon, 27 Oct 2008 10:01:12 GMT</pubDate></item></channel></rss>