mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 10:15:11 +00:00
Fail gracefully when can't find code
This commit is contained in:
parent
fbb2dbdd9d
commit
3a8c0c35a2
2 changed files with 43 additions and 1 deletions
|
|
@ -44,6 +44,14 @@ def get_local_reprs(frame, variables=()):
|
|||
return result
|
||||
|
||||
|
||||
class UnavailableSource(object):
|
||||
def __getitem__(self, i):
|
||||
content = 'SOURCE IS UNAVAILABLE'
|
||||
if six.PY2:
|
||||
content = content.decode()
|
||||
return content
|
||||
|
||||
|
||||
source_cache_by_module_name = {}
|
||||
source_cache_by_file_name = {}
|
||||
def get_source_from_frame(frame):
|
||||
|
|
@ -77,7 +85,7 @@ def get_source_from_frame(frame):
|
|||
except (OSError, IOError):
|
||||
pass
|
||||
if source is None:
|
||||
raise NotImplementedError
|
||||
source = UnavailableSource()
|
||||
|
||||
# If we just read the source from a file, or if the loader did not
|
||||
# apply tokenize.detect_encoding to decode the source into a
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import abc
|
|||
from python_toolbox import caching
|
||||
from python_toolbox import sys_tools
|
||||
from python_toolbox import temp_file_tools
|
||||
import six
|
||||
|
||||
import pysnooper
|
||||
|
||||
|
|
@ -260,3 +261,36 @@ def test_lambda():
|
|||
ReturnValueEntry('49'),
|
||||
)
|
||||
)
|
||||
|
||||
def test_unavailable_source():
|
||||
with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder, \
|
||||
sys_tools.TempSysPathAdder(str(folder)):
|
||||
module_name = 'iaerojajsijf'
|
||||
python_file_path = folder / ('%s.py' % (module_name,))
|
||||
content = ('import pysnooper\n'
|
||||
'\n'
|
||||
'@pysnooper.snoop()\n'
|
||||
'def f(x):\n'
|
||||
' return x\n')
|
||||
if six.PY2:
|
||||
content = content.decode()
|
||||
with python_file_path.open('w') as python_file:
|
||||
python_file.write(content)
|
||||
module = __import__(module_name)
|
||||
python_file_path.unlink()
|
||||
with sys_tools.OutputCapturer(stdout=False,
|
||||
stderr=True) as output_capturer:
|
||||
result = getattr(module, 'f')(7)
|
||||
assert result == 7
|
||||
output = output_capturer.output
|
||||
assert_output(
|
||||
output,
|
||||
(
|
||||
VariableEntry(stage='starting'),
|
||||
CallEntry('SOURCE IS UNAVAILABLE'),
|
||||
LineEntry('SOURCE IS UNAVAILABLE'),
|
||||
ReturnEntry('SOURCE IS UNAVAILABLE'),
|
||||
ReturnValueEntry('7'),
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue