Skip to content

Commit

Permalink
Regenerate util_import
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Jun 8, 2024
1 parent 9d8efaa commit cb690fd
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions src/xdoctest/utils/util_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ def _importlib_import_modpath(modpath): # nocover
return module


def _pkgutil_modname_to_modpath(modname): # nocover
"""
faster version of :func:`_syspath_modname_to_modpath` using builtin python
mechanisms, but unfortunately it doesn't play nice with pytest.
Note:
pkgutil.find_loader is deprecated in 3.12 and removed in 3.14
Args:
modname (str): the module name.
Example:
>>> # xdoctest: +SKIP
>>> modname = 'xdoctest.static_analysis'
>>> _pkgutil_modname_to_modpath(modname)
...static_analysis.py
>>> # xdoctest: +REQUIRES(CPython)
>>> _pkgutil_modname_to_modpath('_ctypes')
..._ctypes...
Ignore:
>>> _pkgutil_modname_to_modpath('cv2')
"""
import pkgutil
loader = pkgutil.find_loader(modname)
if loader is None:
raise Exception('No module named {} in the PYTHONPATH'.format(modname))
modpath = loader.get_filename().replace('.pyc', '.py')
return modpath


class PythonPathContext(object):
"""
Context for temporarily adding a dir to the PYTHONPATH.
Expand Down Expand Up @@ -293,13 +324,14 @@ def import_module_from_path(modpath, index=-1):
>>> assert module.testvar == 1
Example:
>>> # xdoctest: +SKIP("ubelt dependency")
>>> import pytest
>>> # xdoctest: +SKIP("ubelt dependency")
>>> with pytest.raises(IOError):
>>> ub.import_module_from_path('does-not-exist')
>>> with pytest.raises(IOError):
>>> ub.import_module_from_path('does-not-exist.zip/')
"""
modpath = os.fspath(modpath)
if not os.path.exists(modpath):
import re
import zipimport
Expand Down Expand Up @@ -453,6 +485,13 @@ def _static_parse(varname, fpath):
"""
Statically parse the a constant variable from a python file
Args:
varname (str): variable name to extract
fpath (str | PathLike): path to python file to parse
Returns:
Any: the static value
Example:
>>> # xdoctest: +SKIP("ubelt dependency")
>>> dpath = ub.Path.appdir('tests/import/staticparse').ensuredir()
Expand All @@ -476,6 +515,10 @@ def _static_parse(varname, fpath):
>>> with pytest.raises(AttributeError):
>>> fpath.write_text('a = list(range(10))')
>>> assert _static_parse('c', fpath) is None
>>> if sys.version_info[0:2] >= (3, 6):
>>> # Test with type annotations
>>> fpath.write_text('b: int = 10')
>>> assert _static_parse('b', fpath) == 10
"""
import ast

Expand All @@ -488,9 +531,16 @@ def _static_parse(varname, fpath):
class StaticVisitor(ast.NodeVisitor):
def visit_Assign(self, node):
for target in node.targets:
if getattr(target, 'id', None) == varname:
target_id = getattr(target, 'id', None)
if target_id == varname:
self.static_value = _parse_static_node_value(node.value)

def visit_AnnAssign(self, node):
target = node.target
target_id = getattr(target, 'id', None)
if target_id == varname:
self.static_value = _parse_static_node_value(node.value)

visitor = StaticVisitor()
visitor.visit(pt)
try:
Expand Down Expand Up @@ -744,51 +794,6 @@ def check_dpath(dpath):
return found_modpath


def _importlib_modname_to_modpath(modname): # nocover
import importlib.util
spec = importlib.util.find_spec(modname)
print(f'spec={spec}')
modpath = spec.origin.replace('.pyc', '.py')
return modpath


def _pkgutil_modname_to_modpath(modname): # nocover
"""
faster version of :func:`_syspath_modname_to_modpath` using builtin python
mechanisms, but unfortunately it doesn't play nice with pytest.
Note:
pkgutil.find_loader is deprecated in 3.12 and removed in 3.14
Args:
modname (str): the module name.
Example:
>>> # xdoctest: +SKIP
>>> modname = 'xdoctest.static_analysis'
>>> _pkgutil_modname_to_modpath(modname)
...static_analysis.py
>>> # xdoctest: +REQUIRES(CPython)
>>> _pkgutil_modname_to_modpath('_ctypes')
..._ctypes...
Ignore:
>>> _pkgutil_modname_to_modpath('cv2')
"""
import pkgutil
loader = pkgutil.find_loader(modname)
if loader is None:
raise Exception('No module named {} in the PYTHONPATH'.format(modname))
try:
modpath = loader.get_filename().replace('.pyc', '.py')
except Exception:
print('Issue in _pkgutil_modname_to_modpath')
print(f'loader = {loader!r}')
print(f'modname = {modname!r}')
raise
return modpath


def modname_to_modpath(modname, hide_init=True, hide_main=False, sys_path=None):
"""
Finds the path to a python module from its name.
Expand Down

0 comments on commit cb690fd

Please sign in to comment.