Writing a CPython extension - calling another sibbling method ?
MRAB
python at mrabarnett.plus.com
Mon Nov 18 14:32:22 EST 2019
On 2019-11-18 07:52, R.Wieser wrote:
> Hello all,
>
> I'm trying to edit a binary extension to Python, and have a situation where
> I would like to create method which adds a single argument, and than jumps
> to / calls another method. Like this:
>
> static PyObject *py_proc1(PyObject *self, PyObject *args)
> {
> ....
> Py_RETURN_NONE
> }
>
> static PyObject *py_proc2(PyObject *self, PyObject *args)
> {
> // call py_proc1 to with "foo" prepended to "args"
> }
>
> I have no idea how I should do either the call or the adding of that
> argument (and going to a few examples I've found googeling didn't show me
> the answer either).
>
> Than again, I'm not even sure if the "foo" needs to be prepended, or if that
> "py_proc1" method can receive more than a single "args" argument ... Like
> this perhaps:
>
> static PyObject *py_proc1(PyObject *self, int MyNewArgument, PyObject *args)
> {
> ....
> }
>
> I've also tried to go the C way (just calling, from "py_proc2", a C function
> containing "py_proc1"s code), but I got lots of errors, most of them in the
> realm of the different returns not being of the same type (no idea why it
> doesn't complain about it in the origional "py_proc1" code itself though).
>
> tl;dr:
> I could use some examples that show how to work withl PyObject subfunctions.
>
> Regards,
> Rudy Wieser
>
> P.s.
> Yes, this is related to my earlier questions and problems.
>
One possibility is to refactor the code so that py_proc1 and py_proc2
themselves just handle their arguments and then call the function that
does the actual work.
A clunkier way would be to make a new tuple that consists of the
prepended item and the items of args and pass that to py_proc1 as its
args. When py_proc1 returns its result object, DECREF the new tuple to
clean up and then return the result object.
More information about the Python-list
mailing list