PyArg_Parse vs PyArg_ParseTuple
Ignacio Vazquez-Abrams
ignacio at openservices.net
Tue Oct 16 21:42:04 EDT 2001
On Wed, 17 Oct 2001, David Brady wrote:
> Hello,
>
> I'm new to Python, and I've searched the archives for more info but can't
> seem to find the info I need. I am having trouble using PyArg_ParseTuple().
> Here is the python code I'm embedding:
>
> # TestModule.py
> def NumberFunction(x):
> return x+1
>
> As you see, it's not too complex. :-) This is just a proof of concept I'm
> working on to prove it can be done.
>
> Here is the code I'm trying to embed it in. Note: this is C++, does that
> make a difference in this case?
>
> // TestEmbed.cpp
> #include <stdio.h>
> #include <Python.h>
>
> void main(void)
> {
> long val = 7;
> PyObject *pmod, *pfunc, *pargs, *pstr;
>
> PyInitialize();
> printf( "Processing data: %ld\n", val );
>
> pmod = PyImport_ImportModule( "TestModule" );
> pfunc = PyObject_GetAttrString( pmod, "NumberFunction" );
>
> pargs = Py_BuildValue( "(l)", val );
> pstr = PyEval_CallObject( pfunc, pargs );
>
> // *** This code works ***
> PyArg_Parse( pstr, "l", &val );
>
> // *** This code does NOT work ***
> //PyArg_ParseTuple( pstr, "l", &val );
>
> printf( "Val returned was %d\n", val );
> }
>
>
> If I use the ParseTuple version, the code appears to run fine, but val is
> unchanged. I've tried using "(l)" as the string but I'm grasping at straws
> here.
>
> I read in the documentatian that PyArg_Parse() is deprecated, so I would
> like to get this working correctly.
You're misunderstanding what PyArg_Parse() and PyArg_ParseTuple() are used
for. They are used for when a C function is called by a Python program, not
the other way around. In your case you should probably be using one of the
PyInt_As*() or PyLong_As*() functions.
Also, a C signed long on a 32-bit machine translates into a Python Integer, so
you can just as easily use "(i)" in Py_BuildValue().
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
"As far as I can tell / It doesn't matter who you are /
If you can believe there's something worth fighting for."
- "Parade", Garbage
More information about the Python-list
mailing list