C API: getting sys.argv

Discussion in 'Python' started by williams.jasonscott@gmail.com, May 16, 2006.

  1. Guest

    Hi,

    How would one go about getting sys.argv fom within the context of a C
    API extention module? I want to pass it along to C library that I'm
    calling from my module code.

    TIA~

    I'm trying to load the sys module using:

    PyObject *_argv = PyImport_ImportModule("sys.argv");

    but it is coming back NULL...
     
    , May 16, 2006
    #1
    1. Advertising

  2. John Machin Guest

    > PyObject *_argv = PyImport_ImportModule("sys.argv");

    What does the name of the function tell you? You can't do that in one
    hit. Start with
    PyObject *_sys = PyImport_ImportModule("sys");
    then you need to get the module's argv attribute.

    However a better design might be have your extension module have an arg
    which can be *any* list of strings. That makes it more flexible, at the
    expense of making the caller pass sys.argv. But it would save you
    having to muck about with importing "sys", then plucking out the
    module's argv attribute.
     
    John Machin, May 16, 2006
    #2
    1. Advertising

  3. Guest

    John Machin wrote:
    > > PyObject *_argv = PyImport_ImportModule("sys.argv");

    >
    > What does the name of the function tell you? You can't do that in one
    > hit. Start with
    > PyObject *_sys = PyImport_ImportModule("sys");
    > then you need to get the module's argv attribute.


    I just figured this out, doh. Thanks.

    > However a better design might be have your extension module have an arg
    > which can be *any* list of strings. That makes it more flexible, at the
    > expense of making the caller pass sys.argv. But it would save you
    > having to muck about with importing "sys", then plucking out the
    > module's argv attribute.


    but this is great advice.

    Thanks!
     
    , May 16, 2006
    #3
  4. Georg Brandl Guest

    wrote:
    > John Machin wrote:
    >> > PyObject *_argv = PyImport_ImportModule("sys.argv");

    >>
    >> What does the name of the function tell you? You can't do that in one
    >> hit. Start with
    >> PyObject *_sys = PyImport_ImportModule("sys");
    >> then you need to get the module's argv attribute.

    >
    > I just figured this out, doh. Thanks.
    >
    >> However a better design might be have your extension module have an arg
    >> which can be *any* list of strings. That makes it more flexible, at the
    >> expense of making the caller pass sys.argv. But it would save you
    >> having to muck about with importing "sys", then plucking out the
    >> module's argv attribute.

    >
    > but this is great advice.


    Actually, use can use PySys_GetObject("argv") instead.

    Georg
     
    Georg Brandl, May 16, 2006
    #4
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,701
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,074
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,713
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    1,002
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. Replies:
    2
    Views:
    689
    velle
    Jan 5, 2006
Loading...

Share This Page