eval fails in list comprehension [duplicate]

By using list comprehension, you actually define a new scope. Indeed if we alter the list comprehension to: out2 = [print(globals()) or print(locals()) or eval(cmd) for cmd in [‘self.b’]] we force Python to print the local and global variables before making the eval(..) call, and we obtain something like: {‘__builtins__’: <module ‘builtins’ (built-in)>, ‘__name__’: ‘__main__’, … Read more

Alternatives to JavaScript eval() for parsing JSON

Well, safe or not, when you are using jQuery, you’re better to use the $.getJSON() method, not $.ajax(): $.getJSON(url, function(data){ alert(data.exampleType); }); eval() is usually considered safe for JSON parsing when you are only communicating with your own server and especially when you use a good JSON library on server side that guarantees that generated … Read more

Context-preserving eval

The article you linked contains a crazy approach that actally works: during each eval() call, we create a new closure inside that eval scope and export it so that to we can use it evaluate the next statement. var __EVAL = s => eval(`void (__EVAL = ${__EVAL.toString()}); ${s}`); function evaluate(expr) { try { const result … Read more

Execute PHP code in a string [duplicate]

Needless to say you should find another solution ASAP. In the meantime you can eval the code like this: $str=”<h1>Welcome</h1><?php echo $motto?><br/>”; // Your DB content eval(“?> $str <?php “); Demo: http://codepad.org/ao2PPHN7 I can’t stress that enough: eval is dangerous, and application code shouldn’t be in the database. Try a template parser like Smarty, Dwoo, … Read more