Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except TypeError:
attrs = None
else:
if isinstance(obj, BaseException):
attrs['args'] = obj.args
has_slots = getattr(obj, '__slots__', not_there) is not not_there
if has_slots:
slots = set()
for cls in type(obj).__mro__:
slots.update(getattr(cls, '__slots__', ()))
if slots and attrs is None:
attrs = {}
for n in slots:
value = getattr(obj, n, not_there)
if value is not not_there:
attrs[n] = value
if attrs is None:
return None
if ignore is not None:
for attr in ignore:
attrs.pop(attr, None)
return attrs
method = 'a' # pragma: no branch
except AttributeError:
try:
__import__(used)
except ImportError:
method = 'i'
try:
found = found[n] # pragma: no branch
except KeyError:
found = not_there # pragma: no branch
except TypeError:
try:
n = int(n)
except ValueError:
method = 'a'
found = not_there
else:
found = found[n] # pragma: no branch
else:
found = getattr(found, n)
method = 'a' # pragma: no branch
return container, method, n, found
def _extract_attrs(obj, ignore=None):
try:
attrs = vars(obj).copy()
except TypeError:
attrs = None
else:
if isinstance(obj, BaseException):
attrs['args'] = obj.args
has_slots = getattr(obj, '__slots__', not_there) is not not_there
if has_slots:
slots = set()
for cls in type(obj).__mro__:
slots.update(getattr(cls, '__slots__', ()))
if slots and attrs is None:
attrs = {}
for n in slots:
value = getattr(obj, n, not_there)
if value is not not_there:
attrs[n] = value
if attrs is None:
return None
if ignore is not None:
for attr in ignore:
try:
found = found.__dict__[n]
method = 'a'
except (AttributeError, KeyError):
try:
found = getattr(found, n)
method = 'a' # pragma: no branch
except AttributeError:
try:
__import__(used)
except ImportError:
method = 'i'
try:
found = found[n] # pragma: no branch
except KeyError:
found = not_there # pragma: no branch
except TypeError:
try:
n = int(n)
except ValueError:
method = 'a'
found = not_there
else:
found = found[n] # pragma: no branch
else:
found = getattr(found, n)
method = 'a' # pragma: no branch
return container, method, n, found
def __init__(self,
object_or_type,
attribute_dict=None,
strict=True,
**attributes):
if attributes:
if attribute_dict is None:
attribute_dict = attributes
else:
attribute_dict.update(attributes)
if isinstance(object_or_type, basestring):
container, method, name, c = resolve(object_or_type)
if c is not_there:
raise AttributeError(
'%r could not be resolved' % object_or_type
)
elif isinstance(object_or_type, (ClassType, type)):
c = object_or_type
else:
c = object_or_type.__class__
if attribute_dict is None:
attribute_dict = _extract_attrs(object_or_type)
self.c = c
self.v = attribute_dict
self.strict = strict
else:
v = {}
while remaining_keys:
k = remaining_keys.pop()
try:
v[k] = getattr(other, k)
except AttributeError:
pass
kw = {'x_label': 'Comparison', 'y_label': 'actual'}
context = CompareContext(kw)
self.failed = _compare_mapping(self.v,
v,
context,
obj_for_class=not_there,
prefix='attributes ',
breadcrumb='.%s',
check_y_not_x=self.strict)
return not self.failed
def extract_args(self, args):
possible = []
expected = self.options.pop('expected', not_there)
if expected is not not_there:
possible.append(expected)
possible.extend(args)
actual = self.options.pop('actual', not_there)
if actual is not not_there:
possible.append(actual)
x = self.options.pop('x', not_there)
if x is not not_there:
possible.append(x)
y = self.options.pop('y', not_there)
if y is not not_there:
possible.append(y)
if len(possible) != 2:
message = 'Exactly two objects needed, you supplied:'
if possible:
message += ' {}'.format(possible)
if self.options:
message += ' {}'.format(self.options)
raise TypeError(message)
return possible