mirror of https://github.com/cool-RR/PySnooper.git
Improve ensure_tuple
This commit is contained in:
parent
76c739a958
commit
c2e44fb583
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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,)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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'),)}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue