Yet another attempt at a safe eval() call

Alister alister.ware at ntlworld.com
Fri Jan 4 13:13:08 EST 2013


On Fri, 04 Jan 2013 13:33:41 +0000, Steven D'Aprano 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.

as a thought why not processes the input string by exploding the literal 
constants into their numerical values as stage 1
this should result in a string that only contains numbers & mathematical 
symbols then reject any resultant string that contains any alpha 
characters.

I have to agree that it is still a risky process.

 



-- 
Loneliness is a terrible price to pay for independence.



More information about the Python-list mailing list