extending w/o creating a new module??
Martin v. Loewis
martin at v.loewis.de
Sun May 19 07:10:26 EDT 2002
"Johan Hahn" <johahn at delta.telenordia.se> writes:
> Is there a more dynamic way of binding functions in python to C than to
> create a PyMethodDef and use it when creating a new module with
> PyInit_InitModule ??.
Certainly. You just need to create the function objects yourself.
> I would like to add the function send(text) to an existing python module
> (b4.py) and have that function call the C function:
> static PyObject* sendc(PyObject* self, PyObject* args)
> {
> /*...*/
> }
I'm not sure I understand that desire. b4.py is a pure-Python module?
You surely need some C module as well. Lets call it _b4.
By "add the function ... to ...", I understand you want that
b4.sendc()
calls your code in _b4. To do that, define a normal function in _b4,
using the standard PyMethodDef approach. Then, inside b4.py, write
from _b4 import sendc
> My attempts so far are failing because I can't create a PyFunction
> object that I can pass to PyObject_SetAttrString(b4, "send", ?) as
> the last argument. There is no documentation for PyFuntion_New and
> as I understand it takes a code object. And to create a code object
> that binds to a c-function seemed like a dead end.
Ah, so you want to achieve that without modifying b4's source code?
Then, do
py_sendc = PyObject_GetItemString(_b4, "sendc");
PyObject_SetAttrString(b4, "send", py_sendc);
Py_DECREF(py_sendc);
where _b4 is the module object of _b4.
> Am I missing something?
Most certainly. More importantly, it seems we are missing a precise
description of the problem.
Regards,
Martin
More information about the Python-list
mailing list