catch argc-argv

John Machin sjmachin at lexicon.net
Mon Jun 20 09:14:27 EDT 2005


Duncan Booth wrote:
> John Machin wrote:
> 
> 
>>>So, my question is: does the Python API containe fonctions like 
>>>'get_argc()' and 'get_argv()' ?
>>>
>>
>>If you can't see them in the documentation, they aren't there. If they
>>aren't there, that's probably for a good reason -- no demand, no use
>>case. 
>>
>>
> 
> 
> Leaving aside whether or not there is a use-case for this, the reason they 
> aren't there is that they aren't needed.

"no use-case" == "no need" in my book

> As the OP was already told, to 
> access argv, you simply import the 'sys' module and access sys.argv.

Simple in Python, not in C.

> 
> There are apis both to import modules and to get an attribute of an 
> existing Python object.

I know that; my point was why should you do something tedious like that 
when you shouldn't be interested in accessing sys.argv from a C 
extension anyway.

>  So all you need is something like (untested):
> 
> PyObject *sys = PyImport_ImportModule("sys");
> PyObject *argv = PyObject_GetAttrString(sys, "argv");
> int argc = PyObject_Length(argv);
> if (argc != -1) {
>    ... use argc, argv ...
> }
> Py_DECREF(argv);
> Py_DECREF(sys);



More information about the Python-list mailing list