473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

catch argc-argv

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
6 7279
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
2
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
1
by: subramanian100in | last post by:
The C standard specifies that argc value should be non-negative. Given this, is there any reason for keeping the type of argc in main( ) as int instead of unsigned int ? For learning purpose I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.