From 05f3cff508c5db0f188460c407c825bc8c9a909f Mon Sep 17 00:00:00 2001 From: joncrall Date: Wed, 22 May 2024 21:21:25 -0400 Subject: [PATCH] Something is causing issues --- src/xdoctest/utils/util_import.py | 44 ++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/xdoctest/utils/util_import.py b/src/xdoctest/utils/util_import.py index b81f1419..66e77ea4 100644 --- a/src/xdoctest/utils/util_import.py +++ b/src/xdoctest/utils/util_import.py @@ -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) @@ -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): @@ -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 @@ -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. @@ -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