catch argc-argv

M

mg

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,
 
W

Wolfram Kraus

mg said:
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
 
M

mg

Wolfram said:
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 :
 
J

John Machin

mg said:
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.
 
D

Duncan Booth

John said:
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);
 
J

John Machin

Duncan said:
John Machin wrote:




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.
 
M

mg

John said:
Duncan Booth wrote:



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




Simple in Python, not in C.




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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top