Skip to content

Commit

Permalink
Merge pull request #316 from Carreau/tt
Browse files Browse the repository at this point in the history
Maint: Misc typing and test
  • Loading branch information
Carreau authored Mar 6, 2024
2 parents 10023b8 + 9828818 commit 2b4c0eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
20 changes: 11 additions & 9 deletions lib/python/pyflyby/_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class ModuleHandle(object):
A handle to a module.
"""

name: DottedIdentifier

def __new__(cls, arg):
if isinstance(arg, cls):
return arg
Expand Down Expand Up @@ -206,14 +208,8 @@ def exists(self):
if self.parent and not self.parent.exists:
return False

# pkgutil.find_loader returns None for unimported Python 3
# namespace packages, so prefer importlib
try:
import importlib.util
find = importlib.util.find_spec
except ImportError:
import pkgutil
find = pkgutil.find_loader
import importlib.util
find = importlib.util.find_spec

try:
pkg = find(name)
Expand Down Expand Up @@ -241,6 +237,7 @@ def filename(self):
# module, which may be undesirable.
import pkgutil
try:
#TODO: deprecated and will be removed in 3.14
loader = pkgutil.get_loader(str(self.name))
except ImportError:
return None
Expand Down Expand Up @@ -366,6 +363,7 @@ def exports(self):

# If __all__ is defined, try to use it
all_is_good = False # pun intended
all_members = []
if "__all__" in members:
# Iterate through the nodes and reconstruct the
# value of __all__
Expand Down Expand Up @@ -482,7 +480,11 @@ def containing(cls, identifier):
result = module.module
except Exception as e:
raise ImportError(e)
for part, prefix in zip(identifier, prefixes(identifier))[1:]:
# TODO: as far as I can tell the code here is never reached, or haven't
# been in quite some time as the line below was invalid on Python 3 since 2011
# zip(...)[...] fails as zip is not indexable.
# the only place that seem to be using this method is XrefScanner.
for part, prefix in list(zip(identifier, prefixes(identifier)))[1:]:
try:
result = getattr(result, str(part))
except Exception:
Expand Down
48 changes: 31 additions & 17 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import pytest


def test_ModuleHandle_1():
m = ModuleHandle("sys")
assert m.name == DottedIdentifier("sys")
Expand Down Expand Up @@ -65,32 +64,47 @@ def test_module_1():
assert m.module is logging


@pytest.mark.xfail(reason="Fails on CI not locally")
def test_filename_noload_1():
@pytest.mark.parametrize('modname', ['statistics', 'decimal', 'netrc'])
def test_filename_noload_1(modname):

# PRE_TEST

# Here we make sure that everything works properly before the actual test to
# not get false positive


# ensure there is no problem with sys.exit itself.
retcode = subprocess.call([sys.executable, '-c', dedent('''
ret = subprocess.run([sys.executable, '-c', dedent('''
import sys
sys.exit(0)
''')])
assert retcode == 0
''')], capture_output=True)
assert ret.returncode == 0, (ret, ret.stdout, ret.stderr)

# Ensure there is no error with byflyby itself
retcode = subprocess.call([sys.executable, '-c', dedent('''
# Ensure there is no error with pyflyby itself
ret = subprocess.run([sys.executable, '-c', dedent(f'''
from pyflyby._modules import ModuleHandle
import sys
ModuleHandle("multiprocessing").filename
ModuleHandle("{modname}").filename
sys.exit(0)
''')])
assert retcode == 0
''')], capture_output=True)
assert ret.returncode == 0, (ret, ret.stdout, ret.stderr)

# ACTUAL TEST

# don't exit with 1, as something else may exit with 1.
retcode = subprocess.call([sys.executable, '-c', dedent('''
from pyflyby._modules import ModuleHandle
ret = subprocess.run([sys.executable, '-c', dedent(f'''
import sys
ModuleHandle("multiprocessing").filename
if "multiprocessing" in sys.modules:
if "{modname}" in sys.modules:
sys.exit(120)
from pyflyby._modules import ModuleHandle
if "{modname}" in sys.modules:
sys.exit(121)
ModuleHandle("{modname}").filename
if "{modname}" in sys.modules:
sys.exit(123)
else:
sys.exit(0)
''')])
assert retcode == 0
''')], capture_output=True)
assert ret.returncode != 121, f"{modname} imported by pyflyby import"
assert ret.returncode != 120, f"{modname} in sys.modules at startup"
assert ret.returncode == 0, (ret, ret.stdout, ret.stderr)

0 comments on commit 2b4c0eb

Please sign in to comment.