Python system properties Re: PEP 271: Prefixing sys.path by command line option

Frederic Giacometti frederic.giacometti at arakne.com
Sat Sep 22 20:51:59 EDT 2001


This PEP is too narrow.
In effect, I am now coming up for needs to override other configuration
system variables (e.g. PYTHONHOME, as well as other configuration variables
Python extension modules depend on); and we can't work this out just by
adding options.

Basically, the objective is to make it possible to run python without
relying upon environment variables.

So, I'll propose that the PEP be redisigned around the idea of a dictionary
of system properties; somehow along lines similar as in Java (cf. java
command line -D option, and the Java SDK's system property API).

In Java for instance, the java.class.path and java.library.path properties
are commonly used for adjusting the class and native extension directory
searches.

In Python, this could be mapped into something as follows:

** Three new command line options would be introduced (-D, -N, and -P) such
that:

'-D propertyname' : set system property to '' (empty string) [equivalent
to -D propertyname= ]
'-D propertyname=value' : set system property 'propertyname' to 'value'.

'-N propertyname' : unset system property 'propertyname'  (-U is already in
use in Python...).

'-P propertyfile': run python with the given property filename

where 'propertyname' can be any printable string (e.g.: 'python.path' is a
valid property name).

Property options would be evaluated from left to right.


** At the C API level, three functions would be introduced:

int PySys_SetProperty( char const* name, char const* value);
     /* value=NULL is treated as if value="" */

int PySys_UnsetProperty( char const* name);

/* these Set/Unset  functions can only be called before Python
initialization - they are designed for use when embeding Python -*/

char const* PySys_GetProperty( char const* name);
  /* return NULL if 'name' not set */


** At the Python API level, a new attribute would be attached to the sys
module: sys.property would be the 'read-only' Python dictionary of Python
system properties.


** Interaction with existing system environment variables (PYTHONPATH,
PYTHONHOME...):

- PYTHONPATH would correspond to 'python.path', PYTHONHOME to 'python.home'
...
- when its environment variable is not defined, the property is 'not set'
[not in property dictionary], and the python default applies.
- the command line options (or C API calls in embedded Python) override
Python environment variables.


** Application to Python module configuration:

The system properties are accessible to Python modules, and can be used as
Python configuration parameters.

Examples:

The 're.engine' property can be defined to override the 'sre' default
re.engine
(e.g. python -D re.engine=pre would run python with the pre engine, instead
of the 'sre' current default).

'java.options' would pass options overriding the default options for
starting the JVM in Python
(e.g. python -D java.options="-verbose -classpath=dir1:dir2").

'socket.something' could be used to define the 'something' configuration
value of use to the Python socket module...

** Property files

Three type of property files can be defined: site properties, user
properties, and command line properties; they would override python default
properties.
When present, site and user property files would be read one after the
other, and before command line property options; all this being done before
initializing the python engine.
The user property filename would be $HOME/.pythondef, if defined.

The property file format would be most simple, with four types of lines:

- blank: empty line
- comment line: line starting with '#'
- definition line: other line with at least one '=' char. Everything left of
the '=' is the property name; everything right is the property value
- extension line: any property line finishing with a '\' char just before
the eol is concatenated with the next line

Every other line would be rejected with a proper error message (with faulty
property filename and line number :)).

** Property naming convention

All python core variables are prefixed with 'python.' (e.g.: python.home,
python.path...).

All variables of interest to a particular module are prefixed with this
module name (e.g.: re.engine, java.options, metadynamic.root, oracle.home
....).

FG


"Frederic Giacometti" <frederic.giacometti at arakne.com> wrote in message
news:mFxp7.7444$RS3.4571126 at news1.elcjn1.sdca.home.com...
> I'm pasting below the PEP 271 for python 2.2
> (http://python.sourceforge.net/peps/pep-0271.html), for public debate.
>
> The purpose of the PEP is the introduction of new command line option to
> python (-P), whose role would be similar to java's -classpath option:
> providing the ability to explicitly set the PYTHONPATH value from the
> command line.
> This feature can be immensely useful when calling python from scripts
> (shell, Makefiles...), in situations for which either multiple PYTHONPATH
> values are needed, or where the PYTHONPATH inherited from the parent
> environment is not reliable.
> Last, the -P option can also be used to expose in the scripts the value of
> PYTHONPATH in a highly readable manner.
>
> Typical applications are in development of build, test, and configuration
> scripts.
>
> Comments and suggestions for improvement, and questions are welcome.
>
> Thanks!
>
> Frederic Giacometti
> fred at arakne.com
> fredg at scripps.edu
>
> -----------------------------------------------------
> Abstract
>
>     At present, setting the PYTHONPATH environment variable is the
>     only method for defining additional Python module search
>     directories.
>
>     This PEP introduces the '-P' valued option to the python command
>     as an alternative to PYTHONPATH.
>
>
> Rationale
>
>     On Unix:
>
>         python -P $SOMEVALUE
>
>     will be equivalent to
>
>         env PYTHONPATH=$SOMEVALUE python
>
>     On Windows 2K:
>
>         python -P %SOMEVALUE%
>
>     will (almost) be equivalent to
>
>         set __PYTHONPATH=%PYTHONPATH% && set PYTHONPATH=%SOMEVALUE%\
>             && python && set PYTHONPATH=%__PYTHONPATH%
>
>
> Other Information
>
>     This option is equivalent to the 'java -classpath' option.
>
>
> When to use this option
>
>     This option is intended to ease and make more robust the use of
>     Python in test or build scripts, for instance.
>
>
> Reference Implementation
>
>     A patch implementing this is available from SourceForge:
>
>
>
http://sourceforge.net/tracker/download.php?group_id=5470&atid=305470&file_i
> d=6916&aid=429614
>
>     with the patch discussion at:
>
>
>
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=429614&group_id=
> 5470
>
>
>
>
>





More information about the Python-list mailing list