Skip to content

Commit cd09484

Browse files
authored
Merge pull request #354 from TTsangSC/logging-fixes
FIX: debug-mode and logging bugs in #337
2 parents 7d2c415 + b75065c commit cd09484

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

kernprof.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def _gather_preimport_targets(options, exclude):
832832
'' if len(invalid_targets) == 1 else 's',
833833
invalid_targets))
834834
warnings.warn(msg)
835-
diagnostics.log.warn(msg)
835+
diagnostics.log.warning(msg)
836836

837837
return filtered_targets, recurse_targets
838838

@@ -930,13 +930,19 @@ def _dump_filtered_stats(tmpdir, prof, filename):
930930

931931

932932
def _format_call_message(func, *args, **kwargs):
933+
if isinstance(func, functools.partial):
934+
return _format_call_message(
935+
func.func, *func.args, *args, **{**func.keywords, **kwargs})
933936
if isinstance(func, MethodType):
934937
obj = func.__self__
935938
func_repr = (
936939
'{0.__module__}.{0.__qualname__}(...).{1.__name__}'
937940
.format(type(obj), func.__func__))
938941
else:
939-
func_repr = '{0.__module__}.{0.__qualname__}'.format(func)
942+
try:
943+
func_repr = '{0.__module__}.{0.__qualname__}'.format(func)
944+
except Exception: # Fallback
945+
func_repr = repr(func)
940946
args_repr = dedent(' ' + pformat(args)[len('['):-len(']')])
941947
lprefix = len('namespace(')
942948
kwargs_repr = dedent(

tests/test_kernprof.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,52 @@ def test_kernprof_bad_temp_script(stdin):
338338
assert not os.path.exists(os.path.dirname(tmp_script))
339339

340340

341+
@pytest.mark.parametrize('debug', [True, False])
342+
def test_bad_prof_mod_target(debug):
343+
"""
344+
Test the handling of bad paths in `--prof-mod` targets.
345+
"""
346+
with contextlib.ExitStack() as stack:
347+
enter = stack.enter_context
348+
enter(ub.ChDir(enter(tempfile.TemporaryDirectory())))
349+
proc = ub.cmd(['kernprof', '-l', '-p', './nonexistent.py',
350+
'-c', 'print("Output: foo")'],
351+
env={**os.environ, 'LINE_PROFILER_DEBUG': str(debug)})
352+
print(proc.stdout)
353+
print(proc.stderr, file=sys.stderr)
354+
proc.check_returncode()
355+
assert os.listdir() # Profile results
356+
assert 'Output: foo' in proc.stdout
357+
assert re.search(r"1 .* target .*: \['\./nonexistent\.py'\]", proc.stderr)
358+
359+
360+
@pytest.mark.parametrize('builtin', [True, False])
361+
@pytest.mark.parametrize('module', [True, False])
362+
def test_call_with_diagnostics(module, builtin):
363+
"""
364+
Test the output of call signatures in debug messages.
365+
"""
366+
to_run = ['-m', 'calendar'] if module else ['-c', 'print("Output: foo")']
367+
with contextlib.ExitStack() as stack:
368+
enter = stack.enter_context
369+
enter(ub.ChDir(enter(tempfile.TemporaryDirectory())))
370+
cmd = ['kernprof']
371+
if builtin:
372+
cmd += ['-b']
373+
proc = ub.cmd(cmd + to_run,
374+
env={**os.environ, 'LINE_PROFILER_DEBUG': 'true'})
375+
print(proc.stdout)
376+
print(proc.stderr, file=sys.stderr)
377+
proc.check_returncode()
378+
assert os.listdir() # Profile results
379+
has_runctx_call = re.search(
380+
r'Calling: .+\.runctx\(.+\)', proc.stdout, flags=re.DOTALL)
381+
has_execfile_call = re.search(
382+
r'execfile\(.+\)', proc.stdout, flags=re.DOTALL)
383+
assert bool(has_runctx_call) == (not builtin)
384+
assert bool(has_execfile_call) == (not module)
385+
386+
341387
class TestKernprof(unittest.TestCase):
342388

343389
def test_enable_disable(self):

0 commit comments

Comments
 (0)