mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Get traceback working in wasm
This commit is contained in:
6 files changed
+44
-64
No files matched your search
Vendored
+6
-5
@@ -7,12 +7,13 @@ try:
|
||||
except ImportError:
|
||||
import abc
|
||||
|
||||
# this will throw an appropriate error if os isn't available and a module
|
||||
# tries to use an os function. If os has become available, then this will
|
||||
# just work.
|
||||
def __getattr__(name):
|
||||
import os
|
||||
return getattr(os, name)
|
||||
raise OSError("no os specific module found")
|
||||
|
||||
def _shim():
|
||||
import _dummy_os, sys
|
||||
sys.modules['os'] = _dummy_os
|
||||
sys.modules['os.path'] = _dummy_os.path
|
||||
|
||||
import posixpath as path
|
||||
import sys
|
||||
|
||||
Vendored
+4
-1
@@ -7,7 +7,10 @@ that name.
|
||||
|
||||
import functools
|
||||
import sys
|
||||
import os
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import _dummy_os as os
|
||||
import tokenize
|
||||
|
||||
__all__ = ["getline", "clearcache", "checkcache"]
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import asyncweb
|
||||
import pypimport
|
||||
import whlimport
|
||||
|
||||
pypimport.setup()
|
||||
whlimport.setup()
|
||||
|
||||
# shim path utilities into the "os" module
|
||||
class os:
|
||||
import posixpath as path
|
||||
import sys
|
||||
sys.modules['os'] = os
|
||||
sys.modules['os.path'] = os.path
|
||||
del sys, os
|
||||
# make sys.modules['os'] a dumb version of the os module, which has posixpath
|
||||
# available as os.path as well as a few other utilities, but will raise an
|
||||
# OSError for anything that actually requires an OS
|
||||
import _dummy_os
|
||||
_dummy_os._shim()
|
||||
|
||||
@asyncweb.main
|
||||
async def main():
|
||||
await pypimport.load_package("pygments")
|
||||
await whlimport.load_package("pygments")
|
||||
import pygments
|
||||
import pygments.lexers
|
||||
import pygments.formatters.html
|
||||
|
||||
Whitespace-only changes.
@@ -48,51 +48,16 @@ def main(async_func):
|
||||
async def _main_wrapper(coro):
|
||||
try:
|
||||
await coro
|
||||
except BaseException as e:
|
||||
for line in _format_exc(e, 1):
|
||||
print(line)
|
||||
|
||||
|
||||
# TODO: get traceback/linecache working in wasm
|
||||
|
||||
|
||||
def _format_exc(exc, skip_tb=0):
|
||||
exc_type, exc_value, exc_traceback = type(exc), exc, exc.__traceback__
|
||||
|
||||
_str = _some_str(exc_value)
|
||||
|
||||
yield "Traceback (most recent call last):"
|
||||
tb = exc_traceback
|
||||
while tb:
|
||||
if skip_tb:
|
||||
skip_tb -= 1
|
||||
else:
|
||||
co = tb.tb_frame.f_code
|
||||
yield f' File "{co.co_filename}", line {tb.tb_lineno}, in {co.co_name}'
|
||||
tb = tb.tb_next
|
||||
|
||||
stype = exc_type.__qualname__
|
||||
smod = exc_type.__module__
|
||||
if smod not in ("__main__", "builtins"):
|
||||
stype = smod + "." + stype
|
||||
|
||||
yield _format_final_exc_line(stype, _str)
|
||||
|
||||
|
||||
def _format_final_exc_line(etype, value):
|
||||
valuestr = _some_str(value)
|
||||
if value is None or not valuestr:
|
||||
line = "%s" % etype
|
||||
else:
|
||||
line = "%s: %s" % (etype, valuestr)
|
||||
return line
|
||||
|
||||
|
||||
def _some_str(value):
|
||||
try:
|
||||
return str(value)
|
||||
except:
|
||||
return "<unprintable %s object>" % type(value).__name__
|
||||
try:
|
||||
import traceback, sys
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
except BaseException as e:
|
||||
tb = e.__traceback__
|
||||
while tb:
|
||||
print(tb.tb_lineno, tb.tb_frame.f_code.co_name, tb.tb_frame.f_code.co_filename)
|
||||
tb = tb.tb_next
|
||||
print(type(e), e)
|
||||
|
||||
|
||||
def _resolve(prom):
|
||||
|
||||
@@ -73,14 +73,27 @@ class WheelFinder:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def exec_module(cls, module):
|
||||
origin = module.__spec__.origin
|
||||
def get_source(cls, fullname):
|
||||
spec = cls.find_spec(fullname)
|
||||
if spec:
|
||||
return cls._get_source(spec)
|
||||
else:
|
||||
raise ImportError('cannot find source for module', name=fullname)
|
||||
|
||||
@classmethod
|
||||
def _get_source(cls, spec):
|
||||
origin = spec.origin
|
||||
if not origin or not origin.startswith("wheel:"):
|
||||
raise ImportError(f'{module.__spec__.name!r} is not a zip module')
|
||||
|
||||
zipname, slash, path = origin[len('wheel:'):].partition('/')
|
||||
source = cls._packages[zipname].read(path)
|
||||
code = _bootstrap._call_with_frames_removed(compile, source, origin, 'exec', dont_inherit=True)
|
||||
return cls._packages[zipname].read(path).decode()
|
||||
|
||||
@classmethod
|
||||
def exec_module(cls, module):
|
||||
spec = module.__spec__
|
||||
source = cls._get_source(spec)
|
||||
code = _bootstrap._call_with_frames_removed(compile, source, spec.origin, 'exec', dont_inherit=True)
|
||||
_bootstrap._call_with_frames_removed(exec, code, module.__dict__)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user