Safe literal_eval with potential for indexing

117 views Asked by At

I am looking for a way to evaluate string syntax that allows for indexing.

Example:

string = '[1,2,3][0]'
foo = literal_eval( string )

In this case, I want foo to equal 1

ast literal_eval does not support this because

It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

The code is untrusted so I cannot use the python eval() function.

Any thoughts?

0

There are 0 answers