From 30e57891779f5a5e8d673308685b541b9dc2724c Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Thu, 25 Apr 2019 13:25:42 +0200 Subject: [PATCH] Basic fixes for linters: remove unused variables and imports, plus whitespace and other style improvements for PEP8 Edited by Ram. # Conflicts: # pysnooper/pysnooper.py # tests/test_pysnooper.py --- misc/generate_authors.py | 4 ++-- pysnooper/pysnooper.py | 9 --------- pysnooper/tracer.py | 9 ++++----- tests/test_pysnooper.py | 29 ++++++++++++++++++----------- tests/utils.py | 13 ++++++++----- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/misc/generate_authors.py b/misc/generate_authors.py index 51c9694..6ce76ea 100644 --- a/misc/generate_authors.py +++ b/misc/generate_authors.py @@ -3,7 +3,7 @@ # This program is distributed under the MIT license. -""" +''' Generate an AUTHORS file for your Git repo. This will list the authors by chronological order, from their first @@ -13,7 +13,7 @@ You probably want to run it this way: ./generate_authors > AUTHORS -""" +''' import subprocess diff --git a/pysnooper/pysnooper.py b/pysnooper/pysnooper.py index b70c932..ad6e689 100644 --- a/pysnooper/pysnooper.py +++ b/pysnooper/pysnooper.py @@ -2,12 +2,6 @@ # This program is distributed under the MIT license. import sys -import os -import inspect -import types -import datetime as datetime_module -import re -import collections from .third_party import decorator @@ -38,7 +32,6 @@ def get_write_and_truncate_functions(output): return (write, truncate) - def snoop(output=None, variables=(), depth=1, prefix='', overwrite=False): ''' Snoop on the function, writing everything it's doing to stderr. @@ -83,5 +76,3 @@ def snoop(output=None, variables=(), depth=1, prefix='', overwrite=False): return decorator.decorate(function, inner) return decorate - - diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index cc75fd5..6303083 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -1,7 +1,6 @@ # Copyright 2019 Ram Rachum and collaborators. # This program is distributed under the MIT license. -import types import sys import re import collections @@ -28,6 +27,7 @@ def get_shortish_repr(item): r = '{truncated_r}...'.format(truncated_r=r[:MAX_VARIABLE_LENGTH]) return r + def get_local_reprs(frame, variables=()): result = {key: get_shortish_repr(value) for key, value in frame.f_locals.items()} @@ -52,6 +52,8 @@ class UnavailableSource(object): source_cache_by_module_name = {} source_cache_by_file_name = {} + + def get_source_from_frame(frame): module_name = frame.f_globals.get('__name__') or '' if module_name: @@ -65,7 +67,6 @@ def get_source_from_frame(frame): return source_cache_by_file_name[file_name] except KeyError: pass - function = frame.f_code.co_name loader = frame.f_globals.get('__loader__') source = None @@ -118,6 +119,7 @@ def get_source_from_frame(frame): source_cache_by_file_name[file_name] = source return source + class Tracer: def __init__(self, target_code_object, write, truncate, variables=(), depth=1, prefix='', overwrite=False): @@ -149,7 +151,6 @@ class Tracer: def __exit__(self, exc_type, exc_value, exc_traceback): sys.settrace(self.original_trace_function) - def trace(self, frame, event, arg): ### Checking whether we should trace this line: ####################### @@ -242,5 +243,3 @@ class Tracer: format(**locals())) return self.trace - - diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 3cb7df5..195926e 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -2,28 +2,27 @@ # This program is distributed under the MIT license. import io -import re -import abc -from python_toolbox import caching from python_toolbox import sys_tools from python_toolbox import temp_file_tools from pysnooper.third_party import six import pytest import pysnooper - +from pysnooper.third_party import six from .utils import (assert_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, ReturnValueEntry, ExceptionEntry) def test_string_io(): string_io = io.StringIO() + @pysnooper.snoop(string_io) def my_function(foo): x = 7 y = 8 return y + x + result = my_function('baba') assert result == 15 output = string_io.getvalue() @@ -42,6 +41,7 @@ def test_string_io(): ) ) + def test_variables(): class Foo(object): @@ -51,7 +51,7 @@ def test_variables(): def square(self): self.x **= 2 - @pysnooper.snoop(variables=('foo.x', 're')) + @pysnooper.snoop(variables=('foo.x', 'io')) def my_function(): foo = Foo() for i in range(2): @@ -85,6 +85,7 @@ def test_variables(): ) ) + def test_depth(): string_io = io.StringIO() @@ -145,7 +146,6 @@ def test_depth(): def test_method_and_prefix(): - class Baz(object): def __init__(self): self.x = 2 @@ -179,14 +179,17 @@ def test_method_and_prefix(): prefix='ZZZ' ) + def test_file_output(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder: path = folder / 'foo.log' + @pysnooper.snoop(str(path)) - def my_function(foo): + def my_function(_foo): x = 7 y = 8 return y + x + result = my_function('baba') assert result == 15 with path.open() as output_file: @@ -194,8 +197,8 @@ def test_file_output(): assert_output( output, ( - VariableEntry('foo', value_regex="u?'baba'"), - CallEntry('def my_function(foo):'), + VariableEntry('_foo', value_regex="u?'baba'"), + CallEntry('def my_function(_foo):'), LineEntry('x = 7'), VariableEntry('x', '7'), LineEntry('y = 8'), @@ -206,20 +209,23 @@ def test_file_output(): ) ) + def test_confusing_decorator_lines(): string_io = io.StringIO() def empty_decorator(function): return function + @empty_decorator @pysnooper.snoop(string_io, - depth=2) # Multi-line decorator for extra confusion! + depth=2) # Multi-line decorator for extra confusion! @empty_decorator @empty_decorator def my_function(foo): x = lambda bar: 7 y = 8 return y + x(foo) + result = my_function('baba') assert result == 15 output = string_io.getvalue() @@ -263,9 +269,10 @@ def test_lambda(): ) ) + def test_unavailable_source(): with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder, \ - sys_tools.TempSysPathAdder(str(folder)): + sys_tools.TempSysPathAdder(str(folder)): module_name = 'iaerojajsijf' python_file_path = folder / ('%s.py' % (module_name,)) content = ('import pysnooper\n' diff --git a/tests/utils.py b/tests/utils.py index 5c26e5f..191d1dc 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -30,6 +30,7 @@ class _BaseEntry(pysnooper.pycompat.ABC): def check(self, s): pass + class _BaseValueEntry(_BaseEntry): def __init__(self, prefix=''): _BaseEntry.__init__(self, prefix=prefix) @@ -68,7 +69,6 @@ class _BaseValueEntry(_BaseEntry): ) - class VariableEntry(_BaseValueEntry): def __init__(self, name=None, value=None, stage=None, prefix='', name_regex=None, value_regex=None, ): @@ -98,7 +98,6 @@ class VariableEntry(_BaseValueEntry): stage = match.group('stage') return self._check_stage(stage) - _content_pattern = re.compile( r"""^(?P[^ ]+) = (?P.+)$""" ) @@ -133,6 +132,7 @@ class VariableEntry(_BaseValueEntry): else: return stage == self.stage + class ReturnValueEntry(_BaseValueEntry): def __init__(self, value=None, value_regex=None, prefix=''): _BaseValueEntry.__init__(self, prefix=prefix) @@ -150,7 +150,6 @@ class ReturnValueEntry(_BaseValueEntry): def _check_preamble(self, preamble): return bool(self._preamble_pattern.match(preamble)) - def _check_content(self, content): return self._check_value(content) @@ -162,6 +161,7 @@ class ReturnValueEntry(_BaseValueEntry): else: return True + class _BaseEventEntry(_BaseEntry): def __init__(self, source=None, source_regex=None, prefix=''): _BaseEntry.__init__(self, prefix=prefix) @@ -179,7 +179,6 @@ class _BaseEventEntry(_BaseEntry): self.source_regex = (None if source_regex is None else re.compile(source_regex)) - @caching.CachedProperty def event_name(self): return re.match('^[A-Z][a-z_]*', type(self).__name__).group(0).lower() @@ -201,22 +200,26 @@ class _BaseEventEntry(_BaseEntry): self._check_source(source)) - class CallEntry(_BaseEventEntry): pass + class LineEntry(_BaseEventEntry): pass + class ReturnEntry(_BaseEventEntry): pass + class ExceptionEntry(_BaseEventEntry): pass + class OpcodeEntry(_BaseEventEntry): pass + class OutputFailure(Exception): pass