Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > catch argc-argv

Reply
Thread Tools

catch argc-argv

 
 
mg
Guest
Posts: n/a
 
      06-20-2005
Hello,

I am writting bindings for a FEM application. In one of my function
'initModulename', called when the module is imported, I would like to
get the argc and argv arguments used in the main function of Python.
So, my question is: does the Python API containe fonctions like
'get_argc()' and 'get_argv()' ?

Thanks,



 
Reply With Quote
 
 
 
 
Wolfram Kraus
Guest
Posts: n/a
 
      06-20-2005
mg wrote:
> Hello,
>
> I am writting bindings for a FEM application. In one of my function
> 'initModulename', called when the module is imported, I would like to
> get the argc and argv arguments used in the main function of Python.
> So, my question is: does the Python API containe fonctions like
> 'get_argc()' and 'get_argv()' ?
>
> Thanks,
>
>
>

Use sys.argv:
http://python.org/doc/2.4.1/lib/module-sys.html

HTH,
Wolfram
 
Reply With Quote
 
 
 
 
mg
Guest
Posts: n/a
 
      06-20-2005
Wolfram Kraus wrote:

>mg wrote:
>
>
>>Hello,
>>
>>I am writting bindings for a FEM application. In one of my function
>>'initModulename', called when the module is imported, I would like to
>>get the argc and argv arguments used in the main function of Python.
>>So, my question is: does the Python API containe fonctions like
>>'get_argc()' and 'get_argv()' ?
>>
>>Thanks,
>>
>>
>>
>>
>>

>Use sys.argv:
>http://python.org/doc/2.4.1/lib/module-sys.html
>
>HTH,
>Wolfram
>
>

I know this module and this function. But, I search the same function in
the C-API because a would like to know these variables directly in my
bindings written in C++.

As an example, I try to create the following fonction :

PyMODINIT_FUNC initMyModule( void )
{
int argc = Py_GetArgc() ; // I don't know if this function exist : I
would like to know...
char** argv = Py_GetArgv() ; // I don't know if this function exist
: I would like to know...

myfunction( argc, argv ) ;

PyObject* module = Py_InitModule3( "MyModule", 0, "module of my FEM
application" ) ;
}

Thanks to this C function written with the C-API of Python, I will
create a new Python module and import it in the Python interpreter :

>>> import MyModule



 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-20-2005
mg wrote:
> Hello,
>
> I am writting bindings for a FEM application. In one of my function
> 'initModulename', called when the module is imported, I would like to
> get the argc and argv arguments used in the main function of Python.


This is an "interesting" way of writing bindings. Most people would
provide an interface in terms of either a library of functions or a
class or two. I don't recall ever seeing a module or package that did
what you say you want to do.

Consider that your module should NOT be tied to command-line arguments.
Abstract out what are the essential inputs to whatever a "FEM
application" is. Then the caller of your module can parse those inputs
off the command line using e.g. optparse and/or can collect them via a
GUI and/or hard code them in a test module and/or read them from a test
data file or database.


> 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.
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      06-20-2005
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. As the OP was already told, to
access argv, you simply import the 'sys' module and access sys.argv.

There are apis both to import modules and to get an attribute of an
existing Python object. 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);
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-20-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);

 
Reply With Quote
 
mg
Guest
Posts: n/a
 
      06-21-2005
John Machin wrote:

>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);
>>
>>

I understand all the good arguments explained before, and I am agree
with them.
Nevertheless, I implement Python bindings from a generic parallel
framework and a new application based on this framework needs to call a
kind of initilization class : the constructor arguments are argc and
argv... Then, the previous solution can be a work around to test some
behavours of my bindings.
Thanks for your answers
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
Catch pid for a process that created Mav Perl 2 08-20-2004 08:56 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM
Trace: Can anyone suggest a good tool to catch trace messages? Rukmal Fernando ASP .Net 4 10-27-2003 09:03 PM



Advertisments