From c2e44fb5835535df61c2777cd8accda36fadbbd5 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Wed, 17 Jul 2019 09:19:37 +0300 Subject: [PATCH] Improve ensure_tuple --- pysnooper/pycompat.py | 5 +++++ pysnooper/tracer.py | 4 +++- pysnooper/utils.py | 10 ++++++---- pysnooper/variables.py | 11 +++++++++++ tests/test_utils/__init__.py | 0 tests/test_utils/test_ensure_tuple.py | 19 +++++++++++++++++++ 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/test_utils/__init__.py create mode 100644 tests/test_utils/test_ensure_tuple.py diff --git a/pysnooper/pycompat.py b/pysnooper/pycompat.py index de0a472..1748ccd 100644 --- a/pysnooper/pycompat.py +++ b/pysnooper/pycompat.py @@ -54,3 +54,8 @@ if PY3: else: string_types = (basestring,) text_type = unicode + +try: + from collections import abc as collections_abc +except ImportError: # Python 2.7 + import collections as collections_abc diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index c4a6413..5d91aeb 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -261,7 +261,9 @@ class Tracer: calling_frame.f_trace = self.trace self.target_frames.add(calling_frame) - stack = self.thread_local.__dict__.setdefault('original_trace_functions', []) + stack = self.thread_local.__dict__.setdefault( + 'original_trace_functions', [] + ) stack.append(sys.gettrace()) sys.settrace(self.trace) diff --git a/pysnooper/utils.py b/pysnooper/utils.py index 8c27006..b569698 100644 --- a/pysnooper/utils.py +++ b/pysnooper/utils.py @@ -4,7 +4,7 @@ import abc import sys -from .pycompat import ABC, string_types +from .pycompat import ABC, string_types, collections_abc MAX_VARIABLE_LENGTH = 100 MAX_EXCEPTION_LENGTH = 200 @@ -78,9 +78,11 @@ def truncate(string, max_length): def ensure_tuple(x): - if isinstance(x, string_types): - x = (x,) - return tuple(x) + if isinstance(x, collections_abc.Iterable) and \ + not isinstance(x, string_types): + return tuple(x) + else: + return (x,) diff --git a/pysnooper/variables.py b/pysnooper/variables.py index 8468c6b..fe0176a 100644 --- a/pysnooper/variables.py +++ b/pysnooper/variables.py @@ -38,6 +38,17 @@ class BaseVariable(pycompat.ABC): def _items(self, key): raise NotImplementedError + @property + def _fingerprint(self): + return (type(self), self.source, self.exclude) + + def __hash__(self): + return hash(self._fingerprint) + + def __eq__(self, other): + return (isinstance(other, BaseVariable) and + self._fingerprint == other._fingerprint) + class CommonVariable(BaseVariable): def _items(self, main_value): diff --git a/tests/test_utils/__init__.py b/tests/test_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_utils/test_ensure_tuple.py b/tests/test_utils/test_ensure_tuple.py new file mode 100644 index 0000000..aa6e526 --- /dev/null +++ b/tests/test_utils/test_ensure_tuple.py @@ -0,0 +1,19 @@ +# Copyright 2019 Ram Rachum and collaborators. +# This program is distributed under the MIT license. + +import pysnooper +from pysnooper.utils import ensure_tuple + +def test_ensure_tuple(): + x1 = ('foo', ('foo',), ['foo'], {'foo'}) + assert set(map(ensure_tuple, x1)) == {('foo',)} + + x2 = (pysnooper.Keys('foo'), (pysnooper.Keys('foo'),), + [pysnooper.Keys('foo')], {pysnooper.Keys('foo')}) + + assert set(map(ensure_tuple, x2)) == {(pysnooper.Keys('foo'),)} + + + + +