Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_safe_repr():
data = {
'a': [set('b')],
('c',): deque(['d']),
'e': _socket.socket(),
1: array('d', [1, 2]),
frozenset('f'): socket(),
'g': Dict({
'a': List('123'),
'b': Set([Decimal('1.0')]),
'c': Stuff(1, 2),
'd': Exception(1, 2, {
'a': safe_repr,
'b': Foobar,
'c': Bad2(),
'ct': Bad2,
})
}),
'od': OrderedDict({'a': 'b'}),
'nt': MyTuple(1, 2),
'bad1': Bad1().method,
'bad2': Bad2().method
}
print(safe_repr(data))
print(safe_repr([data]))
print(safe_repr([[data]]))
print(safe_repr([[[data]]]))
print(safe_repr([[[[data]]]]))
print(safe_repr([[[[[data]]]]]))
'ct': Bad2,
})
}),
'od': OrderedDict({'a': 'b'}),
'nt': MyTuple(1, 2),
'bad1': Bad1().method,
'bad2': Bad2().method
}
print(safe_repr(data))
print(safe_repr([data]))
print(safe_repr([[data]]))
print(safe_repr([[[data]]]))
print(safe_repr([[[[data]]]]))
print(safe_repr([[[[[data]]]]]))
assert safe_repr(py.io).startswith('
'a': List('123'),
'b': Set([Decimal('1.0')]),
'c': Stuff(1, 2),
'd': Exception(1, 2, {
'a': safe_repr,
'b': Foobar,
'c': Bad2(),
'ct': Bad2,
})
}),
'od': OrderedDict({'a': 'b'}),
'nt': MyTuple(1, 2),
'bad1': Bad1().method,
'bad2': Bad2().method
}
print(safe_repr(data))
print(safe_repr([data]))
print(safe_repr([[data]]))
print(safe_repr([[[data]]]))
print(safe_repr([[[[data]]]]))
print(safe_repr([[[[[data]]]]]))
assert safe_repr(py.io).startswith('
'b': Foobar,
'c': Bad2(),
'ct': Bad2,
})
}),
'od': OrderedDict({'a': 'b'}),
'nt': MyTuple(1, 2),
'bad1': Bad1().method,
'bad2': Bad2().method
}
print(safe_repr(data))
print(safe_repr([data]))
print(safe_repr([[data]]))
print(safe_repr([[[data]]]))
print(safe_repr([[[[data]]]]))
print(safe_repr([[[[[data]]]]]))
assert safe_repr(py.io).startswith('
issubclass(Plain, type(type(Foobar)))
assert side_effects == ['Meta.__class__', 'MetaMeta.__class__', 'Meta.__subclasscheck__', 'MetaMeta.__subclasscheck__']
del side_effects[:]
issubclass(getattr_static(SubFoobar(), '__class__'), Foobar)
assert side_effects[-1] == 'Meta.__subclasscheck__'
del side_effects[:]
getattr_static(type(SubFoobar()), '__instancecheck__')
getattr_static(type(SubFoobar()), '__subclasscheck__')
assert not side_effects
safe_repr(Foobar())
safe_repr(Foobar)
assert not side_effects
issubclass(Plain, type(Foobar))
issubclass(Plain, type(type(Foobar)))
assert side_effects == ['Meta.__class__', 'MetaMeta.__class__', 'Meta.__subclasscheck__', 'MetaMeta.__subclasscheck__']
del side_effects[:]
issubclass(getattr_static(SubFoobar(), '__class__'), Foobar)
assert side_effects[-1] == 'Meta.__subclasscheck__'
del side_effects[:]
getattr_static(type(SubFoobar()), '__instancecheck__')
getattr_static(type(SubFoobar()), '__subclasscheck__')
assert not side_effects
safe_repr(Foobar())
safe_repr(Foobar)
assert not side_effects