mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 02:14:04 +00:00
Avoid snooping on the base class
This commit is contained in:
parent
d89099aadd
commit
b4b425c652
2 changed files with 37 additions and 2 deletions
|
|
@ -233,7 +233,7 @@ class Tracer:
|
|||
return self._wrap_function(function_or_class)
|
||||
|
||||
def _wrap_class(self, cls):
|
||||
for attr_name in dir(cls):
|
||||
for attr_name in cls.__dict__:
|
||||
attr = getattr(cls, attr_name)
|
||||
if inspect.isfunction(attr):
|
||||
setattr(cls, attr_name, self._wrap_function(attr))
|
||||
|
|
|
|||
|
|
@ -1447,4 +1447,39 @@ def test_class_with_property():
|
|||
ReturnEntry('pass'),
|
||||
ReturnValueEntry('None'),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_snooping_on_class_does_not_cause_base_class_to_be_snooped():
|
||||
string_io = io.StringIO()
|
||||
|
||||
class UnsnoopedBaseClass(object):
|
||||
def __init__(self):
|
||||
self.method_on_base_class_was_called = False
|
||||
|
||||
def method_on_base_class(self):
|
||||
self.method_on_base_class_was_called = True
|
||||
|
||||
@pysnooper.snoop(string_io)
|
||||
class MyClass(UnsnoopedBaseClass):
|
||||
def method_on_child_class(self):
|
||||
self.method_on_base_class()
|
||||
|
||||
instance = MyClass()
|
||||
|
||||
assert not instance.method_on_base_class_was_called
|
||||
instance.method_on_child_class()
|
||||
assert instance.method_on_base_class_was_called
|
||||
|
||||
output = string_io.getvalue()
|
||||
assert_output(
|
||||
output,
|
||||
(
|
||||
SourcePathEntry(),
|
||||
VariableEntry('self', value_regex="u?.*<locals>.MyClass object at"),
|
||||
CallEntry('def method_on_child_class(self):'),
|
||||
LineEntry('self.method_on_base_class()'),
|
||||
ReturnEntry('self.method_on_base_class()'),
|
||||
ReturnValueEntry('None'),
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue