By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

catch argc-argv

P: n/a
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,

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Jul 19 '05 #2

P: n/a
mg
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

Jul 19 '05 #3

P: n/a
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.
Jul 19 '05 #4

P: n/a
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);
Jul 19 '05 #5

P: n/a
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);

Jul 19 '05 #6

P: n/a
mg
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 ;-)
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.