Jay Ocean
Jay Ocean

Reputation: 273

Safe literal_eval with potential for indexing

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?

Upvotes: 0

Views: 124

Answers (0)

Related Questions