How to eval a file

Björn Lindberg d95-bli at nada.kth.se
Sun Feb 23 14:48:50 CET 2003


Alex Martelli <aleax at aleax.it> writes:

> > Ah, I see now. I thought that by just doing "execfile(filename)", the
> > variables would automagically appear in the local namespace. The
> 
> They do, and that is exactly why you should not use it that way:
> you completely lose control of what's defined in your local namespace 
> after that.  It makes errors likely and debugging far too hard.

Please see my other post of an example where execfile() doesn't import
the variables into the global namespace. Either the behaviour doesn't
conform to the documentation, or (much more likely) I don't understand
Python namespaces well enough.

> > You are right. I was trying to take advantage of the fact that I am
> > using a very high level language. But still, there are fewer security
> > implications and more error checking by parsing it manually. I think I
> > will use the execfile() solution for now, and when it gets closer to
> > being finished, I will write a proper parser instead.
> 
> This is a reasonable compromise.  But make sure you use execfile
> WITH an explicit namespace anyway -- even just to debug &c -- you'll
> be glad you did.

I found the shlex module which made it really easy to parse my
configuration file, so I'm doing that now. 


Björn




More information about the Python-list mailing list