Python/C++ interface
idfx
idfx at my-deja.com
Fri Feb 9 14:29:14 EST 2001
In article <3A79D28B.1BEB46A0 at cybermesa.com>,
Jay O'Connor <joconnor at cybermesa.com> wrote:
> All,
>
> I was doing some testing with the Python->C++ interface and ran into
> something odd.
>
> The example shows calling a C function from python with
>
> import spam
> spam.system ("ls -l")
>
> and on the C++ side... the signature reads
> PyObject* spam_system(PyObject *self, PyObject * args) {
> ...
> }
>
> Now. On the C++ side, args is considered, and parsed, as a Tuple.
> However, what I found was that a Tuple was not being passed, just a
> String. The only time a Tuple was passed was if there was more than
one
> argument. The whole argument list would be converted to a Tuple,
then.
>
> Is this an error in the documentation? Or has the passing method
> changed between 1.5.x and 2.0 (I'm using 2.0) and the documentation is
> just out of date?
>
> Also, is there a good way of printing the name of the type of a
> PyObject?
I may be wrong, or over-simplifying, but I think you've gotten
confused by the terms, understandably. Yes, args is read into a tuple,
always, but if there's only one argument, then it becomes a tuple of
length 1.
ie: tpl = ("yourstring",)
that way, you can refer to as many arguments as there are,
( numargs=len(tpl) )
instead of worrying about special cases for 1 or 0 arguments.
Your args is still a string, it's just that it's a string that happens
to be an element of a tuple.
OTOH, I may be totally mis-reading your problem. The other
possibility is that, for a system call, (e.g. 'ls -l'), C++ makes a
string of the argument(s) to preserve the command you are passing to
the system, which should be a string anyway, or so I'm told.
As for the printing of type names, this is something I had trouble
with, too. Eventually, I came up with this, although I'm sure there
are more elegant ways to address the problem:
def printype( obj ):
naym = `type(obj)` # string it,
i = naym.find("'")
naym = naym[(i+1):] # trim it,
i = naym.find("'")
naym = naym[:i]
return naym # return it! (as a string)
Hey, what can I say? It works. Usually. ; )
Hope that helps.
--
yrs,
in_sanity,
idfx
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list