12

I am embedding Python in a C++ library which I am making. I would like users to be able to pass C functions in the form of function pointers PyObject* (fpFunc*)(PyObject*,PyObject*); so that I can use those functions in the embedded Python.

So I have a function pointer and I know that it is possible to put this function as a module's method using a PyMethodDef struct and passing it to Py_InitModule("module", ModMethods); and thus obtaining a PyObject* module which I can easily grab functions from.

But, what I would really appreciate is to be able to create this function on the fly without necessarily recreating another module each time.

I have looked into the Python docs, and some of the Python headers to think about a hacky way of doing this without real success... but I'm wondering if's there's a more conventional way of doing this.

From what I understood, every function belongs to a module, even __builtin__, so I guess I would require at least on module.

Any idea on how to achieve this?

2
  • It's not clear to me what you mean. Why are you using a function pointer? Who's setting it? Commented Jul 8, 2011 at 14:59
  • I'm making a C++ library which embeds Python. I'm using a function pointer because it's handy to pass around. (edited my post) Commented Jul 8, 2011 at 15:16

1 Answer 1

27

Found it. Though it's not in the docs and it's hardly explicit in the source.

PyObject* (*fpFunc)(PyObject*,PyObject*) = someFunction;
PyMethodDef methd = {"methd",fpFunc,METH_VARARGS,"A new function"};
PyObject* name = PyString_FromString(methd.ml_name);
PyObject* pyfoo = PyCFunction_NewEx(&methd,NULL,name);
Py_DECREF(name);

It works. I can call the function like I normally would call a Python function object.

If you're curious, all I found in the doc was about Py_InitModule4 and loading modules, so I went to check Python's source, and found out about PyCFunction, then I looked around in the doc but I couldn't find anything about PyCFunction_NewEx, so I had to check the source to make sure it was as I thought.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, not sure though you really need name as PyCFunction_New macro just calls PyCFunction_NewEx with NULL as third parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.