Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve errors from pyodide by using filename #244

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

WardBrian
Copy link
Collaborator

This also uses the async call function, as this lets users call micropip

Copy link
Collaborator

@jsoules jsoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable; are there particular behavior changes we should verify?

@WardBrian
Copy link
Collaborator Author

Two changes:

  1. Tracebacks now mention the name of the file, rather than the <exec> string:
    Before:

    File "<exec>", line 2
    ,
    ^
    SyntaxError: invalid syntax
    

    Now:

     File "data.py", line 2
     ,
     ^
    SyntaxError: invalid syntax
    
  2. You can use top-level awaits, which lets you use micropip

    import micropip
    await micropip.install("scikit-learn")

@jsoules
Copy link
Collaborator

jsoules commented Nov 5, 2024

Tried the following code in data.py:

import asyncio

async def foo():
  print("Hello")
  await asyncio.sleep(1)
  print("...world!")


await foo()

Before the change:

PythonError: sys:1: RuntimeWarning: coroutine 'foo' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/lib/python312.zip/_pyodide/_base.py", line 501, in eval_code
    .compile()
     ^^^^^^^^^
  File "/lib/python312.zip/_pyodide/_base.py", line 280, in compile
    self._gen.send(self.ast)
  File "/lib/python312.zip/_pyodide/_base.py", line 163, in _parse_and_compile_gen
    return compile(mod, filename, mode, flags=flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 10
SyntaxError: 'await' outside function

Note error is listed as originating in File "<exec>".

After the change:

Hello

...world!

PythonError: Traceback (most recent call last):
  File "/lib/python312.zip/_pyodide/_base.py", line 574, in eval_code_async
    await CodeRunner(
  File "/lib/python312.zip/_pyodide/_base.py", line 396, in run_async
    await coroutine
  File "data.py", line 14, in <module>
    raise ValueError("data is not defined")
ValueError: data is not defined

Still an error, but only because I didn't actually generate any data for data.json. Source of the error is File: "data.py".

I think this is a good change and checks out--let's ship it.

@WardBrian WardBrian merged commit a8f439c into main Nov 5, 2024
2 checks passed
@WardBrian WardBrian deleted the improve-pyodide-errors branch November 5, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants