Skip to content

Commit

Permalink
Something is causing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed May 23, 2024
1 parent d729f72 commit 1a7d9f9
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/xdoctest/utils/util_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def is_modname_importable(modname, sys_path=None, exclude=None):
>>> is_modname_importable('xdoctest', sys_path=[])
False
"""
modpath = _pkgutil_modname_to_modpath(modname)
modpath = _syspath_modname_to_modpath(modname, sys_path=sys_path,
exclude=exclude)
flag = bool(modpath is not None)
Expand Down Expand Up @@ -320,8 +321,8 @@ def import_module_from_path(modpath, index=-1):
>>> assert module.testvar == 1
Example:
>>> import pytest
>>> # xdoctest: +SKIP("ubelt dependency")
>>> import pytest
>>> with pytest.raises(IOError):
>>> ub.import_module_from_path('does-not-exist')
>>> with pytest.raises(IOError):
Expand Down Expand Up @@ -704,17 +705,21 @@ def check_dpath(dpath):
# break with pytest anymore? Nope, pytest still doesn't work right
# with it.
for finder_fpath in new_editable_finder_paths:
mapping = _static_parse('MAPPING', finder_fpath)
try:
target = dirname(mapping[_pkg_name])
except KeyError:
mapping = _static_parse('MAPPING', finder_fpath)
except AttributeError:
...
else:
if not exclude or normalize(target) not in real_exclude: # pragma: nobranch
modpath = check_dpath(target)
if modpath: # pragma: nobranch
found_modpath = modpath
break
try:
target = dirname(mapping[_pkg_name])
except KeyError:
...
else:
if not exclude or normalize(target) not in real_exclude: # pragma: nobranch
modpath = check_dpath(target)
if modpath: # pragma: nobranch
found_modpath = modpath
break
if found_modpath is not None:
break

Expand Down Expand Up @@ -767,6 +772,14 @@ 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 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 Expand Up @@ -808,7 +821,18 @@ def modname_to_modpath(modname, hide_init=True, hide_main=False, sys_path=None):
>>> modpath = basename(modname_to_modpath('_ctypes'))
>>> assert 'ctypes' in modpath
"""
modpath = _syspath_modname_to_modpath(modname, sys_path)
if hide_main or sys_path:
modpath = _syspath_modname_to_modpath(modname, sys_path)
else:
# import xdev
# with xdev.embed_on_exception_context:
try:
modpath = _importlib_modname_to_modpath(modname)
except Exception:
modpath = _syspath_modname_to_modpath(modname, sys_path)
# modpath = _pkgutil_modname_to_modpath(modname, sys_path)
# modpath = _syspath_modname_to_modpath(modname, sys_path)

if modpath is None:
return None

Expand Down

0 comments on commit 1a7d9f9

Please sign in to comment.