Yet another attempt at a safe eval() call

Grant Edwards invalid at invalid.invalid
Fri Jan 4 10:59:22 EST 2013


On 2013-01-04, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> On Fri, 04 Jan 2013 07:24:04 -0500, Terry Reedy wrote:
>
>> On 1/3/2013 6:25 PM, Grant Edwards wrote:
>>>
>>> I've written a small assembler in Python 2.[67], and it needs to
>>> evaluate integer-valued arithmetic expressions in the context of a
>>> symbol table that defines integer values for a set of names.  The
>>> "right" thing is probably an expression parser/evaluator using ast, but
>>> it looked like that would take more code that the rest of the assembler
>>> combined, and I've got other higher-priority tasks to get back to.
>> 
>> Will ast.literal_eval do what you want?
>
> No. Grant needs to support variables, not just literal constants, hence 
> the symbol table.

Right.  ast.literal_eval() doesn't even support arithmetic expressions
involving only literal constats such as "3+1" (that's the bare minimum
requirement).  It would also be very highly desirable to allow
expressions involving symblic constants such as "PC+1".

Google has found me exapmles of ast-based code that does pretty much
what I want, but I haven't tried it yet because of that solution's 
size and complexity.

-- 
Grant Edwards               grant.b.edwards        Yow! I brought my BOWLING
                                  at               BALL -- and some DRUGS!!
                              gmail.com            



More information about the Python-list mailing list