easy eval() fix?

Geoff Gerrietts geoff at gerrietts.net
Wed Oct 15 20:53:15 EDT 2003


Quoting John Roth (newsgroups at jhrothjr.com):
> 
> I don't know of a module that does this, but I'm not altogether
> certain it wouldn't be possible to put something together that would
> suit what you need in around the same time it took to write the
> message.

You might be surprised how quickly I type. ;)

> What are the primitive types you need to convert from repr() string
> format back to their object format?

Literal statements.

A list of integers:
  [1, 2, 3, 4] 
A list of strings:
  ['1', '2', '3', '4']
A string/string dict:
  {'a': 'b', 'c': 'd'}

Imagine the variations; they are plentiful.

On the other hand, anything that actually performs "operations" is not
permissible.

On the other hand, an error case:
  [10 ** (10 *** 10)]
  
This should not, for instance, choke the box for a day evaluating the
expression; it should (probably) throw an exception but any scenario
that does not allow the code to chew CPU time is a win over eval().

Also, eval and exec do all their work inside a namespace where names
get resolved to bound objects etcetera. That's not desirable. Nor is
it desirable to permit an object to be called.

What I'm interested in -- what eval seems most used for, certainly in
this project -- is a general-purpose tool for transforming a string
containing a literal statement into the Python data structure.

I toyed with using the parser module to do this. I still may try to do
that, but I don't know enough about ASN parse trees to understand why
so many apparently unrelated symbols show up in the parse tree, and so
I'm reluctant to start down this road without an ample budget of time
to come to an understanding of such things.

I don't have that ample budget of time in my project schedule, so I
thought I would check to see if there was a quick fix available.

Thanks,
--G.

-- 
Geoff Gerrietts <geoff at gerrietts dot net>     http://www.gerrietts.net/
 "Politics, as a practice, whatever its professions, has always been the 
          systematic organization of hatreds." --Henry Adams





More information about the Python-list mailing list