rkern / line_profiler

(OLD REPO) Line-by-line profiling for Python - Current repo ->
https://github.com/pyutils/line_profiler
Other
3.6k stars 254 forks source link

UnboundLocalError on invalid module name #43

Closed anntzer closed 8 years ago

anntzer commented 8 years ago
In [1]: %lprun -mfoo bar
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/usr/lib/python3.5/site-packages/line_profiler.py in magic_lprun(self, parameter_s)
    302         try:
--> 303             mod = __import__(modname, fromlist=[''])
    304             profile.add_module(mod)

ImportError: No module named 'foo'

During handling of the above exception, another exception occurred:

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-1-52fba8d475dc> in <module>()
----> 1 get_ipython().magic('lprun -mfoo bar')

/home/antony/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2334         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2335         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2336         return self.run_line_magic(magic_name, magic_arg_s)
   2337 
   2338     #-------------------------------------------------------------------------

/home/antony/.local/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2255                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2256             with self.builtin_trap:
-> 2257                 result = fn(*args,**kwargs)
   2258             return result
   2259 

/usr/lib/python3.5/site-packages/line_profiler.py in magic_lprun(self, parameter_s)
    304             profile.add_module(mod)
    305         except Exception as e:
--> 306             raise UsageError('Could not find module %r.\n%s: %s' % (name,
    307                 e.__class__.__name__, e))
    308 

UnboundLocalError: local variable 'name' referenced before assignment

The fix is probably just name->modname in line_profiler.py line 306.