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

C API : Creating a Py_Method object from a C function.

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 works fine.

However, I'm now trying to use this from a C++ program. The current
flow that I'm trying to get is as follows:

C++ calls python interface function, passing a C++ function pointer
Python interface function stores C++ function pointer
Python interface function generates new Py_Object method pointer
pointing to a different C python function. (This different function
calls the stored C++ function pointer)
Python interface function calls python function
Python function calls the python method pointer it was passed
C python function then calls the stored C++ function pointer.
The problem in this workflow is taking the C python function that I've
defined (using the standard "static PyObject *someFunction(PyObject
*self, PyObject *args)" method) and converting this into a Py_Object.
Any ideas?

Py_Method doesn't seem to allow you to generate a new one with your own
pointers inside... and I can't see anything else in the docs that might
allow me to do this...
Thanks for any advice!

--
Hugh Macdonald

Jul 21 '05 #1
3 1798
Hugh Macdonald wrote:
The problem in this workflow is taking the C python function that I've
defined (using the standard "static PyObject *someFunction(PyObject
*self, PyObject *args)" method) and converting this into a Py_Object.
Any ideas?


You should use PyCFunction_New(Ex), passing a static PyMethodDef
variable that you define along with your function definition.

Regards,
Martin
Jul 21 '05 #2
Thanks Martin - that worked wonderfully....
For the record (and for anyone searching for this in future), here's
the code that worked (with names changed to protect my job...)
myPython is the C++/Python interface class containing static methods
which pass on calls to the underlying python module. It's not really
commented, but if you're trying to do this kind of thing, it should
give you what you need.

----------------------------------------------------------------------------------------------------
// myPython.h

#ifndef ___MYPYTHON_H___
#define ___MYPYTHON_H___

#include <list>

using namespace std;

typedef struct _object PyObject;
typedef void (*loadCallback)(string message, int progress, void
*callbackData);

static PyObject *myPython_doLoadCallback(PyObject *self,
PyObject *args);

class myPython {
public:
static list<string> *loadDetails(string file, loadCallback
callbackFunc = NULL, void *callbackData = NULL);
static void doLoadCallback(string message, int
progress);
private:
static PyObject *getResults(char *moduleName, char
*functionName, PyObject *args);

static loadCallback callbackFunc;
static void *callbackData;
};

#endif // ___MYPYTHON_H___
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
// myPython.cpp

#include <myPython.h>
#include <Python.h>

loadCallback myPython::callbackFunc = NULL;
void *myPython::callbackData = NULL;

list<string> *myPython::loadDetails(string file, loadCallback
newCallbackFunc, void *newCallbackData)
{
PyObject *pArgs, *pResult;

callbackFunc = newCallbackFunc;
callbackData = newCallbackData;

PyMethodDef *callbackFunctionDef = new PyMethodDef;
callbackFunctionDef->ml_name = "doLoadCallback";
callbackFunctionDef->ml_meth = &myPython_doLoadCallback;
callbackFunctionDef->ml_flags = 1;

PyObject *pyCallbackFunc = PyCFunction_New(callbackFunctionDef,
NULL);

pArgs = Py_BuildValue("(sO)", file.c_str(), pyCallbackFunc);

pResult = getResults("myPythonModule", "loadDetails", pArgs);

if(!pResult)
{
Py_DECREF(pArgs);
return NULL;
}

if(PyList_Check(pResult))
{
// Convert pResult into a list<string> and return that.
}

return NULL;
}

PyObject *myPython_doLoadCallback(PyObject *self, PyObject *args)
{
char *message;
int progress;

if(!PyArg_ParseTuple(args, "si", &message, &progress))
return NULL;

myPython::doLoadCallback(message, progress);

return Py_None;
}

void myPython::doLoadCallback(string message, int progress)
{
if(callbackFunc)
callbackFunc(message, progress, callbackData);
}
----------------------------------------------------------------------------------------------------

Jul 21 '05 #3
Hugh Macdonald wrote:
PyMethodDef *callbackFunctionDef = new PyMethodDef;
callbackFunctionDef->ml_name = "doLoadCallback";
callbackFunctionDef->ml_meth = &myPython_doLoadCallback;
callbackFunctionDef->ml_flags = 1;


I think this gives a memory leak. I was rather thinking of

static PyMethodDef doLoadCallback = {
"doLoadCallback", &myPython_doLoadCallback, 1
};

PyObject *pyCallbackFunc = PyCFunction_New(&doLoadCallback,
NULL);

Since you have to write the C(++) functions statically, you can also
provide the PyMethodDef objects statically.

Regards,
Martin
Jul 21 '05 #4

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

Similar topics

1
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set...
6
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come from object pascal where everything object is...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
9
by: Brian | last post by:
I have a question that some may consider silly, but it has me a bit stuck and I would appreciate some help in understanding what is going on. For example, lets say that I have a class that...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.