catch argc-argv

Discussion in 'Python' started by mg, Jun 20, 2005.

  1. mg

    mg Guest

    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,
    mg, Jun 20, 2005
    #1
    1. Advertising

  2. 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
    Wolfram Kraus, Jun 20, 2005
    #2
    1. Advertising

  3. mg

    mg Guest

    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
    mg, Jun 20, 2005
    #3
  4. mg

    John Machin Guest

    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.
    John Machin, Jun 20, 2005
    #4
  5. mg

    Duncan Booth Guest

    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);
    Duncan Booth, Jun 20, 2005
    #5
  6. mg

    John Machin Guest

    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);
    John Machin, Jun 20, 2005
    #6
  7. mg

    mg Guest

    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 ;-)
    mg, Jun 21, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. userfriendly

    argc and argv

    userfriendly, Feb 29, 2004, in forum: C++
    Replies:
    3
    Views:
    6,000
    Buster
    Mar 1, 2004
  2. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,130
    Howard
    Oct 28, 2004
  3. Vinu
    Replies:
    9
    Views:
    615
  4. Vinu
    Replies:
    4
    Views:
    4,793
    Ron Natalie
    May 10, 2005
  5. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
Loading...

Share This Page