prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Improve parsing of result line
  The parsing of the input Python to find the last line which will be evaluated (rather than executed) to provide the result is probably a little brittle in certain corner cases.  We should look at what IPython does here and copy that.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/pyodide.py`
  Content:
  ```
  1 """
  2 A library of helper utilities for connecting Python to the browser environment.
  3 """
  4 
  5 from js import XMLHttpRequest
  6 
  7 import io
  8 
  9 
  10 def open_url(url):
  11     """
  12     Fetches a given *url* and returns a io.StringIO to access its contents.
  13     """
  14     req = XMLHttpRequest.new()
  15     req.open('GET', url, False)
  16     req.send(None)
  17     return io.StringIO(req.response)
  18 
  19 
  20 __all__ = ['open_url']
  21
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/pyodide.py b/src/pyodide.py
  --- a/src/pyodide.py
  +++ b/src/pyodide.py
  @@ -4,6 +4,7 @@
   
   from js import XMLHttpRequest
   
  +import ast
   import io
   
   
  @@ -17,4 +18,23 @@
       return io.StringIO(req.response)
   
   
  -__all__ = ['open_url']
  +def eval_code(code, ns):
  +    """
  +    Runs a string of code, the last part of which may be an expression.
  +    """
  +    mod = ast.parse(code)
  +    if isinstance(mod.body[-1], ast.Expr):
  +        expr = ast.Expression(mod.body[-1].value)
  +        del mod.body[-1]
  +    else:
  +        expr = None
  +
  +    if len(mod.body):
  +        exec(compile(mod, '<exec>', mode='exec'), ns, ns)
  +    if expr is not None:
  +        return eval(compile(expr, '<eval>', mode='eval'), ns, ns)
  +    else:
  +        return None
  +
  +
  +__all__ = ['open_url', 'eval_code']
  
verification_info: {"golden_diff": "diff --git a/src/pyodide.py b/src/pyodide.py\n--- a/src/pyodide.py\n+++ b/src/pyodide.py\n@@ -4,6 +4,7 @@\n \n from js import XMLHttpRequest\n \n+import ast\n import io\n \n \n@@ -17,4 +18,23 @@\n     return io.StringIO(req.response)\n \n \n-__all__ = ['open_url']\n+def eval_code(code, ns):\n+    \"\"\"\n+    Runs a string of code, the last part of which may be an expression.\n+    \"\"\"\n+    mod = ast.parse(code)\n+    if isinstance(mod.body[-1], ast.Expr):\n+        expr = ast.Expression(mod.body[-1].value)\n+        del mod.body[-1]\n+    else:\n+        expr = None\n+\n+    if len(mod.body):\n+        exec(compile(mod, '<exec>', mode='exec'), ns, ns)\n+    if expr is not None:\n+        return eval(compile(expr, '<eval>', mode='eval'), ns, ns)\n+    else:\n+        return None\n+\n+\n+__all__ = ['open_url', 'eval_code']\n", "issue": "Improve parsing of result line\nThe parsing of the input Python to find the last line which will be evaluated (rather than executed) to provide the result is probably a little brittle in certain corner cases.  We should look at what IPython does here and copy that.\n", "before_files": [{"content": "\"\"\"\nA library of helper utilities for connecting Python to the browser environment.\n\"\"\"\n\nfrom js import XMLHttpRequest\n\nimport io\n\n\ndef open_url(url):\n    \"\"\"\n    Fetches a given *url* and returns a io.StringIO to access its contents.\n    \"\"\"\n    req = XMLHttpRequest.new()\n    req.open('GET', url, False)\n    req.send(None)\n    return io.StringIO(req.response)\n\n\n__all__ = ['open_url']\n", "path": "src/pyodide.py"}], "after_files": [{"content": "\"\"\"\nA library of helper utilities for connecting Python to the browser environment.\n\"\"\"\n\nfrom js import XMLHttpRequest\n\nimport ast\nimport io\n\n\ndef open_url(url):\n    \"\"\"\n    Fetches a given *url* and returns a io.StringIO to access its contents.\n    \"\"\"\n    req = XMLHttpRequest.new()\n    req.open('GET', url, False)\n    req.send(None)\n    return io.StringIO(req.response)\n\n\ndef eval_code(code, ns):\n    \"\"\"\n    Runs a string of code, the last part of which may be an expression.\n    \"\"\"\n    mod = ast.parse(code)\n    if isinstance(mod.body[-1], ast.Expr):\n        expr = ast.Expression(mod.body[-1].value)\n        del mod.body[-1]\n    else:\n        expr = None\n\n    if len(mod.body):\n        exec(compile(mod, '<exec>', mode='exec'), ns, ns)\n    if expr is not None:\n        return eval(compile(expr, '<eval>', mode='eval'), ns, ns)\n    else:\n        return None\n\n\n__all__ = ['open_url', 'eval_code']\n", "path": "src/pyodide.py"}]}