C API: getting sys.argv

C API: getting sys.argv

Post by williams.j » Wed, 17 May 2006 12:04:00


Hi,

How would one go about getting sys.argv fom within the context of a C
API extention module? I want to pass it along to C library that I'm
calling from my module code.

TIA~

I'm trying to load the sys module using:

PyObject *_argv = PyImport_ImportModule("sys.argv");

but it is coming back NULL...
 
 
 

C API: getting sys.argv

Post by John Machi » Wed, 17 May 2006 12:26:37

> PyObject *_argv = PyImport_ImportModule("sys.argv");

What does the name of the function tell you? You can't do that in one
hit. Start with
PyObject *_sys = PyImport_ImportModule("sys");
then you need to get the module's argv attribute.

However a better design might be have your extension module have an arg
which can be *any* list of strings. That makes it more flexible, at the
expense of making the caller pass sys.argv. But it would save you
having to muck about with importing "sys", then plucking out the
module's argv attribute.

 
 
 

C API: getting sys.argv

Post by williams.j » Wed, 17 May 2006 12:34:30


I just figured this out, doh. Thanks.


but this is great advice.

Thanks!
 
 
 

C API: getting sys.argv

Post by Georg Bran » Wed, 17 May 2006 16:08:57


Actually, use can use PySys_GetObject("argv") instead.

Georg