mirror of https://github.com/python/cpython.git
gh-102799: Let pydoc use the exception instead of sys.exc_info (#102830)
This commit is contained in:
parent
5c471f3f2a
commit
868490e327
36
Lib/pydoc.py
36
Lib/pydoc.py
|
@ -389,8 +389,17 @@ def synopsis(filename, cache={}):
|
||||||
class ErrorDuringImport(Exception):
|
class ErrorDuringImport(Exception):
|
||||||
"""Errors that occurred while trying to import something to document it."""
|
"""Errors that occurred while trying to import something to document it."""
|
||||||
def __init__(self, filename, exc_info):
|
def __init__(self, filename, exc_info):
|
||||||
self.filename = filename
|
if not isinstance(exc_info, tuple):
|
||||||
|
assert isinstance(exc_info, BaseException)
|
||||||
|
self.exc = type(exc_info)
|
||||||
|
self.value = exc_info
|
||||||
|
self.tb = exc_info.__traceback__
|
||||||
|
else:
|
||||||
|
warnings.warn("A tuple value for exc_info is deprecated, use an exception instance",
|
||||||
|
DeprecationWarning)
|
||||||
|
|
||||||
self.exc, self.value, self.tb = exc_info
|
self.exc, self.value, self.tb = exc_info
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
exc = self.exc.__name__
|
exc = self.exc.__name__
|
||||||
|
@ -411,8 +420,8 @@ def importfile(path):
|
||||||
spec = importlib.util.spec_from_file_location(name, path, loader=loader)
|
spec = importlib.util.spec_from_file_location(name, path, loader=loader)
|
||||||
try:
|
try:
|
||||||
return importlib._bootstrap._load(spec)
|
return importlib._bootstrap._load(spec)
|
||||||
except:
|
except BaseException as err:
|
||||||
raise ErrorDuringImport(path, sys.exc_info())
|
raise ErrorDuringImport(path, err)
|
||||||
|
|
||||||
def safeimport(path, forceload=0, cache={}):
|
def safeimport(path, forceload=0, cache={}):
|
||||||
"""Import a module; handle errors; return None if the module isn't found.
|
"""Import a module; handle errors; return None if the module isn't found.
|
||||||
|
@ -440,21 +449,20 @@ def safeimport(path, forceload=0, cache={}):
|
||||||
cache[key] = sys.modules[key]
|
cache[key] = sys.modules[key]
|
||||||
del sys.modules[key]
|
del sys.modules[key]
|
||||||
module = __import__(path)
|
module = __import__(path)
|
||||||
except:
|
except BaseException as err:
|
||||||
# Did the error occur before or after the module was found?
|
# Did the error occur before or after the module was found?
|
||||||
(exc, value, tb) = info = sys.exc_info()
|
|
||||||
if path in sys.modules:
|
if path in sys.modules:
|
||||||
# An error occurred while executing the imported module.
|
# An error occurred while executing the imported module.
|
||||||
raise ErrorDuringImport(sys.modules[path].__file__, info)
|
raise ErrorDuringImport(sys.modules[path].__file__, err)
|
||||||
elif exc is SyntaxError:
|
elif type(err) is SyntaxError:
|
||||||
# A SyntaxError occurred before we could execute the module.
|
# A SyntaxError occurred before we could execute the module.
|
||||||
raise ErrorDuringImport(value.filename, info)
|
raise ErrorDuringImport(err.filename, err)
|
||||||
elif issubclass(exc, ImportError) and value.name == path:
|
elif isinstance(err, ImportError) and err.name == path:
|
||||||
# No such module in the path.
|
# No such module in the path.
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
# Some other error occurred during the importing process.
|
# Some other error occurred during the importing process.
|
||||||
raise ErrorDuringImport(path, sys.exc_info())
|
raise ErrorDuringImport(path, err)
|
||||||
for part in path.split('.')[1:]:
|
for part in path.split('.')[1:]:
|
||||||
try: module = getattr(module, part)
|
try: module = getattr(module, part)
|
||||||
except AttributeError: return None
|
except AttributeError: return None
|
||||||
|
@ -1997,8 +2005,8 @@ def __call__(self, request=_GoInteractive):
|
||||||
if request is not self._GoInteractive:
|
if request is not self._GoInteractive:
|
||||||
try:
|
try:
|
||||||
self.help(request)
|
self.help(request)
|
||||||
except ImportError as e:
|
except ImportError as err:
|
||||||
self.output.write(f'{e}\n')
|
self.output.write(f'{err}\n')
|
||||||
else:
|
else:
|
||||||
self.intro()
|
self.intro()
|
||||||
self.interact()
|
self.interact()
|
||||||
|
@ -2405,8 +2413,8 @@ def run(self):
|
||||||
docsvr = DocServer(self.host, self.port, self.ready)
|
docsvr = DocServer(self.host, self.port, self.ready)
|
||||||
self.docserver = docsvr
|
self.docserver = docsvr
|
||||||
docsvr.serve_until_quit()
|
docsvr.serve_until_quit()
|
||||||
except Exception as e:
|
except Exception as err:
|
||||||
self.error = e
|
self.error = err
|
||||||
|
|
||||||
def ready(self, server):
|
def ready(self, server):
|
||||||
self.serving = True
|
self.serving = True
|
||||||
|
|
Loading…
Reference in New Issue