473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

embedding a new method

J
Hi everyone,
I am fairly new to python (3rd day), but i am fairly keen on
replacing javascript. I want to externalize some of the mehtod
in my App, an allow user to imp

char* lBuffer = "def handler(color):\n"
" print 12"

PyObject* lCode = Py_CompileString(lBuffer, "<Object>",
Py_file_input);
if (lCode)
{
mFunction = PyFunction_New(lCode, ScnGlobal::sDictionary);
Py_XDECREF(lCode);
}
else
{
PyErr_Print();
}

So far, there is no error, but I am wondering if that function object
knows about the signature of the function at this point.
Next I am defining an class like
PyType_Ready(&PyType);
Py_INCREF(&PyType);
PyModule_AddObject(ScnGlobal::sModule, "Place",
(PyObject*)&PyType);

where PyType is pretty much taken out of the "embedding tutorial",
and ScnGlobal::sModule is the result of the call to
PyImport_ImportModule ("__main__"). I am installing tp_init and tp_new
in PyType.

Next, I instantiate an object of PyType like

PyObject* new_args = PyTuple_New(0);
mPyObject = (PyStruct*) PyObject_Call((PyObject*)&PyType, new_args,
0);

This also works and tp_init and tp_new get called. Now, I want to add
the new mehtod to this instance, and not the class, so I call
PyObject* lParam = PyMethod_New(mFunction, (PyObject*)mPyObject,
(PyObject*)&PyType);
if (lParam)
{
Py_XDECREF(lParam);
}
else
{
PyErr_Print();
}

This call also returns an object. The problem starts when I try to
call the new method like so

PyObject* lObject = PyObject_CallMethod((PyObject*)mPyObject,
"handler", "", NULL);

if (lObject)
{
Py_DECREF(lObject);
}
else
{
PyErr_Print();
}

This when I get an error saying "AtrributeError:handler". I would
really appreciate some help on this. Here is also what the PyType
structure looks like
PyTypeObject CmdPlace::PyType =
{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"Place", /*tp_name*/
sizeof(CmdPlace::PyStruct), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*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*/
"CmdPlace", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
CmdPlace::sPyMethods, /* tp_methods */
0, /* tp_members */
CmdPlace::sPyGetSeters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)CmdPlace::sPyInit, /* tp_init */
0, /* tp_alloc */
CmdPlace::sPyNew, /* tp_new */
};
I hopesome has an answer. I have been stuck with this for a while.
Cheers
Jochen

Jul 21 '05 #1
1 1139
J
If your are reading this, I am sorry but I forgot to finish the first
paragraph.... :(. I am
trying to externalize some methods of a C++ object using python. User
will be
able to implement the function via the GUI and I then would like to add
it to an instance... So the class consits of both python and c++
methods...

Cheers

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
0
by: Pecks | last post by:
Hi, I'm having problems embedding Python in C++. (Background - I'm hacking a COTS app written in C++, and I'd like to write in some intelligence in Python) I understand that I can call a...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
4
by: FC | last post by:
Hello folks, I was wondering if there any other method of achieving the following: I have a XSL transformation outputting a SVG document. For reasons too long to explain here, I must embed a CSS...
1
by: Johannes Zellner | last post by:
Hello, when embedding python: how can I create a type which simulates item getters and setters? Something like this: void setter(PyObject* self, int i, PyObject new_value) { // do something...
0
by: hq4000 | last post by:
Given AStyleSheet.xsl : <AStyleSheet> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.mytest.mytest2.mytest3.com" version="1.0"> <xsl:output method="xml"...
0
by: Fozzie | last post by:
Hi, I have a problem which is quite circular, and hopefully either someone has encountered something similar or has a reason why this will not work. We have a COM library providing...
0
by: Soren | last post by:
Hi, I've been trying to embed matplotlib in wxpython. I want to be able to put a figure (axes) in a wx.Panel and place it somewhere in my GUI. The GUI should have other panels with buttons etc....
6
by: -Lost | last post by:
I would like to include a specific font. Rather than create images with the font where and when I need it, I was hoping I could embed it. I do realize it is optional and is up to the user to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.