Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
filename = None
while context >= 0:
frame = frame.f_back
filename = inspect.getsourcefile(frame)
if filename is not None:
context -= 1
filename = os.path.abspath(filename)
if frame.f_globals.get('__name__') != '__main__':
if PY3 and self._treetrace_hidden_with_stmt.__name__ not in frame.f_globals:
raise RuntimeError(
'To trace an imported module, you must import birdseye before '
'importing that module.')
return
lines = read_source_file(filename).splitlines()
lines[:frame.f_lineno] = [''] * frame.f_lineno
source = '\n'.join(lines)
self.exec_string(source, filename, frame.f_globals, frame.f_locals, deep)
sys.exit(0)
def visit_stmt(self, node):
# type: (ast.stmt) -> ast.With
"""
Every statement in the original code becomes:
with _treetrace_hidden_with_stmt(_tree_index):
where the _treetrace_hidden_with_stmt function is the the corresponding method with the
TreeTracerBase and traced_file arguments already filled in (see _trace_methods_dict)
"""
context_expr = self._create_simple_marker_call(
super(_NodeVisitor, self).generic_visit(node),
TreeTracerBase._treetrace_hidden_with_stmt)
if PY3:
wrapped = ast.With(
items=[ast.withitem(context_expr=context_expr)],
body=[node],
)
else:
wrapped = ast.With(
context_expr=context_expr,
body=[node],
)
ast.copy_location(wrapped, node)
ast.fix_missing_locations(wrapped)
return wrapped
@eye
def __repr__(self):
return '%s(label=%r)' % (self.__class__.__name__, self.label)
def test_future_imports(self):
from tests.future_tests import with_future, without_future
self.assertEqual(with_future.foo(), eye(with_future.foo)())
self.assertEqual(without_future.foo(), eye(without_future.foo)())
def test_future_imports(self):
from tests.future_tests import with_future, without_future
self.assertEqual(with_future.foo(), eye(with_future.foo)())
self.assertEqual(without_future.foo(), eye(without_future.foo)())
def main():
call_id = eye._last_call_id
foo()
assert call_id is eye._last_call_id
@eye
def error():
raise ValueError()
@eye
def test_A():
self.assertEqual(a.label, 'hello')
self.assertEqual(a.length, 3)
self.assertEqual(a.thing, 'thing')
self.assertEqual(repr(a), "A(label='hello')")
def _trace(self, func, *args, **kwargs):
# noinspection PyUnresolvedReferences
from birdseye import eye
traced = eye(func)
traced = self.config.snoop(*args, **kwargs)(traced)
@functools.wraps(func)
def wrapper(*func_args, **func_kwargs):
if self.config.enabled:
final_func = traced
else:
final_func = func
return final_func(*func_args, **func_kwargs)
return wrapper
def find_code(root_code):
# type: (CodeType) -> None
for const in root_code.co_consts: # type: CodeType
if not inspect.iscode(const):
continue
matches = (const.co_firstlineno == func.__code__.co_firstlineno and
const.co_name == func.__code__.co_name)
if matches:
code_options.append(const)
find_code(const)
find_code(traced_file.code)
if len(code_options) > 1:
# Currently lambdas aren't allowed anyway, but should be in the future
assert is_lambda(func)
raise ValueError("Failed to trace lambda. Convert the function to a def.")
new_func_code = code_options[0] # type: CodeType
# Give the new function access to the hooks
# We have to use the original __globals__ and not a copy
# because it's the actual module namespace that may get updated by other code
func.__globals__.update(self._trace_methods_dict(traced_file))
# http://stackoverflow.com/a/13503277/2482744
# noinspection PyArgumentList
new_func = FunctionType(new_func_code, func.__globals__, func.__name__, func.__defaults__, func.__closure__)
update_wrapper(new_func, func) # type: FunctionType
if PY3:
new_func.__kwdefaults__ = getattr(func, '__kwdefaults__', None)
new_func.traced_file = traced_file
return new_func