diff options
Diffstat (limited to 'sphinx/util')
-rw-r--r-- | sphinx/util/__init__.py | 14 | ||||
-rw-r--r-- | sphinx/util/i18n.py | 2 | ||||
-rw-r--r-- | sphinx/util/inspect.py | 8 | ||||
-rw-r--r-- | sphinx/util/osutil.py | 4 | ||||
-rw-r--r-- | sphinx/util/pycompat.py | 7 |
5 files changed, 18 insertions, 17 deletions
diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index ca9bb028d..3bf049f98 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -285,21 +285,21 @@ def get_module_source(modname: str) -> Tuple[str, str]: try: mod = import_module(modname) except Exception as err: - raise PycodeError('error importing %r' % modname, err) + raise PycodeError('error importing %r' % modname, err) from err filename = getattr(mod, '__file__', None) loader = getattr(mod, '__loader__', None) if loader and getattr(loader, 'get_filename', None): try: filename = loader.get_filename(modname) except Exception as err: - raise PycodeError('error getting filename for %r' % filename, err) + raise PycodeError('error getting filename for %r' % filename, err) from err if filename is None and loader: try: filename = loader.get_source(modname) if filename: return 'string', filename except Exception as err: - raise PycodeError('error getting source for %r' % modname, err) + raise PycodeError('error getting source for %r' % modname, err) from err if filename is None: raise PycodeError('no source found for module %r' % modname) filename = path.normpath(path.abspath(filename)) @@ -456,8 +456,8 @@ def parselinenos(spec: str, total: int) -> List[int]: items.extend(range(start - 1, end)) else: raise ValueError - except Exception: - raise ValueError('invalid line number spec: %r' % spec) + except Exception as exc: + raise ValueError('invalid line number spec: %r' % spec) from exc return items @@ -596,9 +596,9 @@ def import_object(objname: str, source: str = None) -> Any: except (AttributeError, ImportError) as exc: if source: raise ExtensionError('Could not import %s (needed for %s)' % - (objname, source), exc) + (objname, source), exc) from exc else: - raise ExtensionError('Could not import %s' % objname, exc) + raise ExtensionError('Could not import %s' % objname, exc) from exc def split_full_qualified_name(name: str) -> Tuple[str, str]: diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py index 1cb75637c..499f2316f 100644 --- a/sphinx/util/i18n.py +++ b/sphinx/util/i18n.py @@ -312,7 +312,7 @@ def get_image_filename_for_language(filename: str, env: "BuildEnvironment") -> s try: return filename_format.format(**d) except KeyError as exc: - raise SphinxError('Invalid figure_language_filename: %r' % exc) + raise SphinxError('Invalid figure_language_filename: %r' % exc) from exc def search_image_for_language(filename: str, env: "BuildEnvironment") -> str: diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index d4928c847..3077f9eb2 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -321,7 +321,7 @@ def safe_getattr(obj: Any, name: str, *defargs: Any) -> Any: """A getattr() that turns all exceptions into AttributeErrors.""" try: return getattr(obj, name, *defargs) - except Exception: + except Exception as exc: # sometimes accessing a property raises an exception (e.g. # NotImplementedError), so let's try to read the attribute directly try: @@ -336,7 +336,7 @@ def safe_getattr(obj: Any, name: str, *defargs: Any) -> Any: if defargs: return defargs[0] - raise AttributeError(name) + raise AttributeError(name) from exc def safe_getmembers(object: Any, predicate: Callable[[str], bool] = None, @@ -385,8 +385,8 @@ def object_description(object: Any) -> str: for x in sorted_values) try: s = repr(object) - except Exception: - raise ValueError + except Exception as exc: + raise ValueError from exc # Strip non-deterministic memory addresses such as # ``<__main__.A at 0x7f68cb685710>`` s = memory_address_re.sub('', s) diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py index 23f5b0137..0390b038d 100644 --- a/sphinx/util/osutil.py +++ b/sphinx/util/osutil.py @@ -168,10 +168,10 @@ def abspath(pathdir: str) -> str: if isinstance(pathdir, bytes): try: pathdir = pathdir.decode(fs_encoding) - except UnicodeDecodeError: + except UnicodeDecodeError as exc: raise UnicodeDecodeError('multibyte filename not supported on ' 'this filesystem encoding ' - '(%r)' % fs_encoding) + '(%r)' % fs_encoding) from exc return pathdir diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py index 664387cac..2173fce14 100644 --- a/sphinx/util/pycompat.py +++ b/sphinx/util/pycompat.py @@ -34,11 +34,11 @@ def convert_with_2to3(filepath: str) -> str: try: from lib2to3.refactor import RefactoringTool, get_fixers_from_package from lib2to3.pgen2.parse import ParseError - except ImportError: + except ImportError as exc: # python 3.9.0a6+ emits PendingDeprecationWarning for lib2to3. # Additionally, removal of the module is still discussed at PEP-594. # To support future python, this catches ImportError for lib2to3. - raise SyntaxError + raise SyntaxError from exc fixers = get_fixers_from_package('lib2to3.fixes') refactoring_tool = RefactoringTool(fixers) @@ -49,7 +49,8 @@ def convert_with_2to3(filepath: str) -> str: # do not propagate lib2to3 exceptions lineno, offset = err.context[1] # try to match ParseError details with SyntaxError details - raise SyntaxError(err.msg, (filepath, lineno, offset, err.value)) + + raise SyntaxError(err.msg, (filepath, lineno, offset, err.value)) from err return str(tree) |