473,385 Members | 1,409 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,385 software developers and data experts.

PyCFunction_New() ?

Is it possible to create a Python-callable object, dynamically, in C/C++? I
have a GUI app in C++, with the app logic in Python, called from the
GUI via the Python C API. I need be able to call a callback function
in C/C++ from the Python, in response to an event (socket, whatever...)
that the Python is aware of. Callbacks in Python, called from C/C++
are very straightforward, but I haven't been able to find any examples
of the converse.

So I want to be able to do something like:

PyCFunction *myCallback(PyObject *ob, PyObject *args) { ... }

{
PyObject *somePythonObject;
...
PyObject *myCallbackObject = PyFunction_New(myCallback, ...);
PyObject_CallMethod(somePythonObject, "setCallback", myCallbackObject);
...
}

Even better would be:

PyCFunction *MyClass::pyCallback(PyObject *ob, PyObject *args) { ... }

MyClass::MyClass()
{
...
PyObject_CallMethod(somePythonObject, "setCallback", this->pyCallback);
...
}

Any help greatly appreciated!
Jul 18 '05 #1
3 4763
Scott Deerwester <sc***@deerwester.org> wrote:
Is it possible to create a Python-callable object, dynamically, in C/C++? I
Sure! But I'm not clear on why you want to create it dynamically. The
C++ code is there all the time, isn't it? So why not the wrapping of it
into Python-callable terms...?
Even better would be:

PyCFunction *MyClass::pyCallback(PyObject *ob, PyObject *args) { ... }

MyClass::MyClass()
{
...
PyObject_CallMethod(somePythonObject, "setCallback", this->pyCallback);
...
}
Here pyCallback _returns_ a pointer to a PyCFunction, yet you want to
SET it as the callback...? I'm confused! Also, PyObject_CallMethod
needs a format string as its 3rd arg, before the args 'proper' -- do you
intend to omit it? Why? Again, I'm confused.

Any help greatly appreciated!


You can call PyCFunction_New, passing it a first argument that's a
PyMethodDef struct pointer, and a 2nd argument that's a PyObject*
(whatever you want the C function to receive as the first argument,
self). PyMethodDef is, of course:

struct PyMethodDef {
char *ml_name;
PyCFunction ml_meth;
int ml_flags;
char *ml_doc;
};
typedef struct PyMethodDef PyMethodDef;

What problems is this giving you...?
Alex
Jul 18 '05 #2
Alex Martelli wrote:
Scott Deerwester <sc***@deerwester.org> wrote:
Is it possible to create a Python-callable object, dynamically, in C/C++?
I
Sure! But I'm not clear on why you want to create it dynamically. The
C++ code is there all the time, isn't it? So why not the wrapping of it
into Python-callable terms...?


Because I'd like to have multiple instances of the class that has (or is
somehow associated with) the C/C++ callback, and to be able to hand a
corresponding Python object a Python-callable callback that ends up
invoking the C++ callback for a particular C++ class instance...

That's a lot of words, but the intention is:

CObj1 = new SomeClass();
/* CObj1 constructor instantiates a Python SomePyClass object PObj1 */
/* CObj1 calls PObj1.setCallback(CObj1->someMethod) */
CObj2 = new SomeClass();
/* CObj2 constructor instantiates a Python SomePyClass object PObj2 */
/* CObj2 calls PObj2.setCallback(CObj1->someMethod) */

....

# PObj1 decides to call its callback, which calls CObj1->someMethod()

# PObj2 decides to call its callback, which calls CObj2->someMethod()

So the C++ function is (of course) not dynamic, but the Python object
that wraps it is.
You can call PyCFunction_New, passing it a first argument that's a
PyMethodDef struct pointer, and a 2nd argument that's a PyObject*
(whatever you want the C function to receive as the first argument,
self). PyMethodDef is, of course:

struct PyMethodDef {
char *ml_name;
PyCFunction ml_meth;
int ml_flags;
char *ml_doc;
};
typedef struct PyMethodDef PyMethodDef;

What problems is this giving you...?


I was getting confused between a PyCFunction (which isn't a PyObject,
is it?) and PyCFunction_New... which isn't in the API documentation.

I'll have at it with what you've given me. Thanks!
Jul 18 '05 #3
Scott Deerwester <sc***@p3international.org> wrote:
Alex Martelli wrote:
Scott Deerwester <sc***@deerwester.org> wrote:
Is it possible to create a Python-callable object, dynamically, in C/C++?
I


Sure! But I'm not clear on why you want to create it dynamically. The
C++ code is there all the time, isn't it? So why not the wrapping of it
into Python-callable terms...?


Because I'd like to have multiple instances of the class that has (or is
somehow associated with) the C/C++ callback, and to be able to hand a
corresponding Python object a Python-callable callback that ends up
invoking the C++ callback for a particular C++ class instance...


Ah, you want to make "bound methods" from C++...? That's a tall order
indeed. In C++ itself, in fact, you couldn't.

Fortunately, you can exploit the 2nd argument of PyCFunction_New. Place
the pointer to the C++ object there, wrapped into a PyObject (or cast
into a PyObject* if you feel frisky...!-). Your C function unwraps it
or casts it back, and then can call the C++ method on the suitable
instance thus recovered.
Alex
Jul 18 '05 #4

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

Similar topics

12
by: Thomas Heller | last post by:
I once knew how to do it, but I cannot find or remember it anymore: How can I attach *class* methods to a type in C code, when I have a function PyObject *func(PyObject *type, PyObject *arg);...
0
by: Bo Peng | last post by:
Dear list, My SWIG generated module (myModule) needs an array-like object (carray) to work. Carray objects are created both internally (in C++ level) and through Python so I have to load it when...
0
by: Iker Arizmendi | last post by:
Hello all. Is there a convenient scheme within a C extension to add methods to a type in such a way as to allow me to transparently add a "proxy" around them? For example: typedef PyObject*...
3
by: Hugh Macdonald | last post by:
I've got a pure python module that parses a certain type of file. It has a load() function that allows a callback function to be passed for getting progress information. In straight python, this...
0
by: SkyRanger | last post by:
Hi! I make class extension from Delphi, but i have problem. Here my code: //==================================================================== {pyClassMethod} function pyClassMethod(...
2
by: SkyRanger | last post by:
I create class: FOClassName:= PyString_FromString(ClasName); FClass:= PyClass_New(nil, FDict, FOClassName); PyDict_SetItemString(FDict, ClasName, FClass); Py_DECREF(FOClassName);...
3
by: Torsten Bronger | last post by:
Hallöchen! I'd like to script C++ funtions by an embedded Python interpreter. So far, my C++ main() function contains: Py_Initialize(); Py_InitModule("pp3", PythonMethods);...
2
by: Ben Sizer | last post by:
I have Python embedded in a C++ application (yes, yes, I know, I'd prefer it the other way around too) and essentially need to expose some read-only values and functions to Python so it can be used...
0
by: Ben Sizer | last post by:
In following the example given at <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/54352>, I find that if I instead try to create PyMethodDef instances on the stack and create methods that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.