Skip to content

Commit 1c086a4

Browse files
authored
FIX: check import target names (#397)
* check import target names * Parametrize `tests/test_autoprofile.py::test_autoprofile_module` for coverage
1 parent 6153697 commit 1c086a4

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

line_profiler/autoprofile/eager_preimports.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def walk_packages_static(pkg):
286286
return
287287
for subpath in package_modpaths(path, with_pkg=True):
288288
submod = modpath_to_modname(subpath)
289-
if submod:
289+
if submod and is_dotted_path(submod):
290290
yield submod
291291

292292
def walk_packages_import_sys(pkg):
@@ -297,7 +297,8 @@ def walk_packages_import_sys(pkg):
297297
if not paths:
298298
return
299299
for info in walk_packages(paths, prefix=pkg + '.'):
300-
yield info.name
300+
if is_dotted_path(info.name):
301+
yield info.name
301302

302303
dotted_paths = set(dotted_paths)
303304
if isinstance(recurse, Collection):

tests/test_autoprofile.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ def add_operator(a, b):
235235
return a + b
236236
'''))
237237

238+
# Note: this can't be profiled because `test_mod.more-utils` is not
239+
# a valid dotted path
240+
(temp_dpath / 'test_mod/dev-utils.py').write_text(ub.codeblock(
241+
'''
242+
"""
243+
Just imagine that the file contains some dev tools.
244+
"""
245+
246+
def publish_pkg():
247+
pass
248+
'''))
249+
238250
(temp_dpath / 'test_mod/submod1.py').write_text(ub.codeblock(
239251
'''
240252
from test_mod.util import add_operator
@@ -332,7 +344,8 @@ def test_autoprofile_script_with_module():
332344
assert 'Function: main' in raw_output
333345

334346

335-
def test_autoprofile_module():
347+
@pytest.mark.parametrize('static_resolution', [True, False])
348+
def test_autoprofile_module(static_resolution):
336349
"""
337350
Test that every function in a file is profiled when autoprofile is
338351
enabled.
@@ -342,11 +355,14 @@ def test_autoprofile_module():
342355

343356
script_fpath = _write_demo_module(temp_dpath)
344357

345-
# args = [sys.executable, '-m', 'kernprof', '--prof-imports',
346-
# '-p', 'script.py', '-l', os.fspath(script_fpath)]
347358
args = [sys.executable, '-m', 'kernprof', '-p', 'test_mod', '-l',
348359
os.fspath(script_fpath)]
349-
proc = ub.cmd(args, cwd=temp_dpath, verbose=2)
360+
env = dict(os.environ)
361+
if static_resolution:
362+
env['LINE_PROFILER_STATIC_ANALYSIS'] = '1'
363+
else:
364+
env.pop('LINE_PROFILER_STATIC_ANALYSIS', None)
365+
proc = ub.cmd(args, cwd=temp_dpath, env=env, verbose=2)
350366
print(proc.stdout)
351367
print(proc.stderr)
352368
proc.check_returncode()

0 commit comments

Comments
 (0)