-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
85 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from importlib import import_module | ||
from typing import Any | ||
|
||
from sphinx.errors import ExtensionError | ||
|
||
|
||
def import_object(object_name: str, /, source: str = '') -> Any: | ||
"""Import python object by qualname.""" | ||
obj_path = object_name.split('.') | ||
module_name = obj_path.pop(0) | ||
try: | ||
obj = import_module(module_name) | ||
for name in obj_path: | ||
module_name += '.' + name | ||
try: | ||
obj = getattr(obj, name) | ||
except AttributeError: | ||
obj = import_module(module_name) | ||
except (AttributeError, ImportError) as exc: | ||
if source: | ||
msg = f'Could not import {object_name} (needed for {source})' | ||
raise ExtensionError(msg, exc) from exc | ||
msg = f'Could not import {object_name}' | ||
raise ExtensionError(msg, exc) from exc | ||
return obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Test sphinx.util._importer.""" | ||
|
||
import pytest | ||
|
||
from sphinx.errors import ExtensionError | ||
from sphinx.util._importer import import_object | ||
|
||
|
||
def test_import_object(): | ||
module = import_object('sphinx') | ||
assert module.__name__ == 'sphinx' | ||
|
||
module = import_object('sphinx.application') | ||
assert module.__name__ == 'sphinx.application' | ||
|
||
obj = import_object('sphinx.application.Sphinx') | ||
assert obj.__name__ == 'Sphinx' | ||
|
||
with pytest.raises(ExtensionError) as exc: | ||
import_object('sphinx.unknown_module') | ||
assert exc.value.args[0] == 'Could not import sphinx.unknown_module' | ||
|
||
with pytest.raises(ExtensionError) as exc: | ||
import_object('sphinx.unknown_module', 'my extension') | ||
expected = 'Could not import sphinx.unknown_module (needed for my extension)' | ||
assert exc.value.args[0] == expected |