Python eval: is it still dangerous if I disable builtins and attribute access?
<p>We all know that <a href="http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html"><code>eval</code> is dangerous</a>, even if you hide dangerous functions, because you can use Python's introspection features to dig down into things and re-extract them. For example, even if you delete <code>__builtins__</code>, you can retrieve them with </p>

<pre><code>[c for c in ().__class__.__base__.__subclasses__()  
 if c.__name__ == 'catch_warnings'][0]()._module.__builtins__
</code></pre>

<p>However, every example I've seen of this uses attribute access. What if I disable all builtins, <em>and</em> disable attribute access (by tokenizing the input with a Python tokenizer and rejecting it if it has an attribute access token)? </p>

<p>And before you ask, no, for my use-case, I do not need either of these, so it isn't too crippling. </p>

<p>What I'm trying to do is make SymPy's <a href="http://docs.sympy.org/latest/modules/core.html#id1">sympify</a> function more safe. Currently it tokenizes the input, does some transformations on it, and evals it in a namespace. But it's unsafe because it allows attribute access (even though it really doesn't need it).</p>