Skip to content

Commit

Permalink
Add a test module for run_in_pyodide functions
Browse files Browse the repository at this point in the history
This makes name resolution a bit nicer. If we define a class A in the test,
1. A.__module__ is no longer "builtins"
2. sys.modules[A.__module__] points back to our current global scope
3. __file__ now has the correct value
  • Loading branch information
hoodmane committed Apr 17, 2024
1 parent 0b78ac0 commit f7f60aa
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pytest_pyodide/_decorator_in_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"""

import pickle
import sys
from base64 import b64decode, b64encode
from inspect import isclass
from io import BytesIO
from types import ModuleType
from typing import Any

import pyodide_js
Expand Down Expand Up @@ -108,9 +110,15 @@ async def run_in_pyodide_main(
mod = decode(mod64)
args: tuple[Any] = decode(args64)

# Set up test module
MODULE_NAME = "test_module"
module = ModuleType(module_filename)
module.__file__ = module_filename
sys.modules[MODULE_NAME] = module
d = module.__dict__

# Compile and execute the ast
co = compile(mod, module_filename, "exec")
d: dict[str, Any] = {}
exec(co, d)

try:
Expand Down Expand Up @@ -142,6 +150,9 @@ def get_locals(frame):
except ImportError:
pass
return (1, encode(e))
finally:
# Remove test module
del sys.modules[MODULE_NAME]


__all__ = ["PyodideHandle", "encode"]

0 comments on commit f7f60aa

Please sign in to comment.