Changing environment variables into python variables

Thomas Wouters thomas at xs4all.nl
Fri Jun 4 07:47:44 CEST 1999


On Fri, Jun 04, 1999 at 07:27:28AM +0200, Thomas Wouters wrote:

> > What am I doing wrong here?  How can I, at runtime, define a variable
> > and assign it a value?

> You need to use exec(), not eval(). This is probably a FAQ, but i'm not sure
> if it's in the FAQ. From the Python Documentation however:

Oh, i forgot to warn you: You probably want to be very sure you dont have an
environment variable containing, for instance:

'You\'re a loony.", posix.system("rm -rf /"), "'

Dont forget -- you already imported posix or os so you can get to the
environment variables, which means you give them access to the 'remove' and
'system' functions, as well.

$ echo $test
You're a loony.", posix._exit(1), "
$ python
Python 1.5.1 (#1, Sep  3 1998, 22:51:17)  [GCC 2.7.2.3] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import posix
>>> for var in posix.environ.keys():
...     command = var + '="' + posix.environ[var] + '"'
...     print command
...     exec(command)
... 
MACHTYPE="i386"
MAIL="/var/spool/mail/thomas"
OSTYPE="linux"
WINDOWID="25165838"
WINDOW="3"
VENDOR="intel"
test="You're a loony.", posix._exit(1), ""
$ echo $?
1

being-paranoid-doesn't-mean-the-bastards-aren't-out-to-get-you-ly y'rs, Thomas

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list