{"action":"created","issue":{"url":"https://api.github.com/repos/mrj0/jep/issues/55","repository_url":"https://api.github.com/repos/mrj0/jep","labels_url":"https://api.github.com/repos/mrj0/jep/issues/55/labels{/name}","comments_url":"https://api.github.com/repos/mrj0/jep/issues/55/comments","events_url":"https://api.github.com/repos/mrj0/jep/issues/55/events","html_url":"https://github.com/mrj0/jep/issues/55","id":167704760,"number":55,"title":"Add support for execing multiple python statements","user":{"login":"bsteffensmeier","id":13338101,"avatar_url":"https://avatars.githubusercontent.com/u/13338101?v=3","gravatar_id":"","url":"https://api.github.com/users/bsteffensmeier","html_url":"https://github.com/bsteffensmeier","followers_url":"https://api.github.com/users/bsteffensmeier/followers","following_url":"https://api.github.com/users/bsteffensmeier/following{/other_user}","gists_url":"https://api.github.com/users/bsteffensmeier/gists{/gist_id}","starred_url":"https://api.github.com/users/bsteffensmeier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsteffensmeier/subscriptions","organizations_url":"https://api.github.com/users/bsteffensmeier/orgs","repos_url":"https://api.github.com/users/bsteffensmeier/repos","events_url":"https://api.github.com/users/bsteffensmeier/events{/privacy}","received_events_url":"https://api.github.com/users/bsteffensmeier/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mrj0/jep/labels/improvement","name":"improvement","color":"009800"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-26T20:33:22Z","updated_at":"2016-07-28T13:38:33Z","closed_at":null,"body":"Jep has the ability to eval a single python statement and it has the ability to run a script from a file but if a script is loaded into a Java String then there is no way to reliably execute the script. Jep should provide an exec method that matches the behavior of the python builtin exec and allows the caller to exec an entire script from a String.\r\n\r\nHere are the workarounds I have experimented with to get around this missing capability:\r\n1. The most reliable way is to simply exec the entire script in python through something like this:\r\n```java\r\njep.set(\"_pythonScript\", script.toString());\r\njep.eval(\"exec _pythonScript\");\r\njep.eval(\"del _pythonScript\");\r\n```\r\nor\r\n```java\r\njep.eval(\"exec \\\"\\\"\\\"\" + pythonScript + \"\\\"\\\"\\\"\");\r\n```\r\n2. If the python script only contains a single python statement, such as a function or a class definition then it is possible to eval the entire script. This actually causes confusion because things seem to work for simple test cases but eventually a more complicated script breaks it. Even if there aren't multiple python statements it is still possible for things like trailing whitespace to cause syntax errors\r\n3. If jep is in interactive mode you can feed the script to jep one line at a time. This breaks if there are blank lines in indented blocks because the block is evaluated early. Also if there are no blank lines between unindented lines then only the first statement is evaluated.\r\n"},"comment":{"url":"https://api.github.com/repos/mrj0/jep/issues/comments/235896769","html_url":"https://github.com/mrj0/jep/issues/55#issuecomment-235896769","issue_url":"https://api.github.com/repos/mrj0/jep/issues/55","id":235896769,"user":{"login":"ndjensen","id":5747885,"avatar_url":"https://avatars.githubusercontent.com/u/5747885?v=3","gravatar_id":"","url":"https://api.github.com/users/ndjensen","html_url":"https://github.com/ndjensen","followers_url":"https://api.github.com/users/ndjensen/followers","following_url":"https://api.github.com/users/ndjensen/following{/other_user}","gists_url":"https://api.github.com/users/ndjensen/gists{/gist_id}","starred_url":"https://api.github.com/users/ndjensen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ndjensen/subscriptions","organizations_url":"https://api.github.com/users/ndjensen/orgs","repos_url":"https://api.github.com/users/ndjensen/repos","events_url":"https://api.github.com/users/ndjensen/events{/privacy}","received_events_url":"https://api.github.com/users/ndjensen/received_events","type":"User","site_admin":false},"created_at":"2016-07-28T13:38:33Z","updated_at":"2016-07-28T13:38:33Z","body":"I'm not convinced `eval(String)` is written as well as it could be, in particular [this area](https://github.com/mrj0/jep/blob/master/src/jep/Jep.java#L465).  We might be able to fix it there.\r\n\r\nAlso some of the code in the C could possibly be improved [here](https://github.com/mrj0/jep/blob/master/src/jep/pyembed.c#L818).  In particular I'm not sure `Py_single_input` is the most appropriate option there.  One of [these other options](https://docs.python.org/2/c-api/veryhigh.html#c.Py_eval_input) might be better."}}
{"actor":{"display_login":"ndjensen"}}