Mailing List Archive

More Python command-line features
While we're talking about Python command-line features, I have a couple
of requests that would make life alot easier for us.

1. I'd like the options given before the script name to
be accessable to Python scripts. For example, in:

python -O foo.py bar spam

I'd like to have a sys variable that told me that the arguments
['-O'] were included before the arguments ['foo.py', 'bar', 'spam']
(aka sys.argv). This is needed if I want to fork/exec (or span or
whatever) Python with the same option.

2. I'd like to be able to supply environment variables to Python
on the command line, as in:

python -O -e PYTHONHOME=/Zope foo.py bar spam

This would be very helpful in environment-variable-challenged
environments like windows 9x, and might be handy elswhere as
well.

If people agree that these would be good ideas, I'd ve happy
to supply a patch.

Jim

--
Jim Fulton mailto:jim@digicool.com Python Powered!
Technical Director (888) 344-4332 http://www.python.org
Digital Creations http://www.digicool.com http://www.zope.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission. Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.
Re: More Python command-line features [ In reply to ]
On 08 September 1999, Jim Fulton said:
>
> While we're talking about Python command-line features, I have a couple
> of requests that would make life alot easier for us.
>
> 1. I'd like the options given before the script name to
> be accessable to Python scripts. For example, in:
>
> python -O foo.py bar spam
>
> I'd like to have a sys variable that told me that the arguments
> ['-O'] were included before the arguments ['foo.py', 'bar', 'spam']
> (aka sys.argv). This is needed if I want to fork/exec (or span or
> whatever) Python with the same option.

Regarding -O, it would also be nice to have a higher-level way to find
out what optimization level the current interpreter is running under.
Currently, as I understand it, there's no way to predict whether
py_compile will generate .pyc or .pyo files, which is a minor annoyance
in the Distutils installation code. However, if Jim's mythical sys
variable is guaranteed to canonicalize Python's command-line options so
that something like

"-O" in sys.python_argv

would always answer this question, I'd be satisfied.

Greg
--
Greg Ward - software developer gward@cnri.reston.va.us
Corporation for National Research Initiatives
1895 Preston White Drive voice: +1-703-620-8990
Reston, Virginia, USA 20191-5434 fax: +1-703-620-0913
Re: More Python command-line features [ In reply to ]
Greg Ward writes:
> Regarding -O, it would also be nice to have a higher-level way to find
> out what optimization level the current interpreter is running under.
> Currently, as I understand it, there's no way to predict whether

I agree. In fact, I'd even be willing to add support to compile the
parse-trees produced by the parser module with or without optimization
(pick your favorite level); that would be easy with the current
implementation.
I didn't add this earlier because Guido objected, saying that the
internal optimization flag could change. That would require that the
parser module implementation change accordingly. I don't think that
would be a huge problem, other than for my having to send Guido a
pre-forma gripe that he'd given me more work to do two days before a
release. ;-)
So, should <ast-object>.compile() accept an optional optimization
level, with the default being to use the "current" setting?


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
Re: Accessing internal flag values [ In reply to ]
Greg Ward wrote:
>
> On 08 September 1999, Jim Fulton said:
> >
> > While we're talking about Python command-line features, I have a couple
> > of requests that would make life alot easier for us.
> >
> > 1. I'd like the options given before the script name to
> > be accessable to Python scripts. For example, in:
> >
> > python -O foo.py bar spam
> >
> > I'd like to have a sys variable that told me that the arguments
> > ['-O'] were included before the arguments ['foo.py', 'bar', 'spam']
> > (aka sys.argv). This is needed if I want to fork/exec (or span or
> > whatever) Python with the same option.
>
> Regarding -O, it would also be nice to have a higher-level way to find
> out what optimization level the current interpreter is running under.
> Currently, as I understand it, there's no way to predict whether
> py_compile will generate .pyc or .pyo files, which is a minor annoyance
> in the Distutils installation code.

Check out mxTools (from my Python Pages). It has a function which
lets you control the value of the optimization flag:
"""
The following functions are installed as add-ons to the builtin sys
module.

sys.verbosity([level])
If level is given, the value of the interpreter's verbosity flag is set
to level and the previous value of that flag is returned. Otherwise,
the current value is returned.

You can use this function to e.g. enable verborse lookup output to
stderr for import statements even when the interpreter was not
invoked with '-v' or '-vv' switch or to force verbosity to be
switched off.

sys.debugging([level])
If level is given, the value of the interpreter's debugging flag is set
to level and the previous value of that flag is returned. Otherwise,
the current value is returned.

You can use this function to check whether the interpreter was
called with '-d' flag or not. Some extensions use this flag to
enable/disable debugging log output (e.g. all the mx Extensions).

sys.optimization([level])
If level is given, the value of the interpreter's optimization flag is
set to level and the previous value of that flag is returned.
Otherwise, the current value is returned.

You can use this function to e.g. compile Python scripts in
optimized mode even though the interpreter was not started with
-O.
"""

--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 123 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/