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_dict_attr_access_disabled(self):
# nested dict
self.s.ATTR_INDEX_FALLBACK = False
self.assertEqual(self.s.ATTR_INDEX_FALLBACK, False)
self.s.names = {'a': {'b': {'c': 42}}}
with self.assertRaises(simpleeval.AttributeDoesNotExist):
self.t("a.b.c * 2", 84)
self.t("a['b']['c'] * 2", 84)
self.assertEqual(self.s.names['a']['b']['c'], 42)
def test_attributedoesnotexist(self):
try:
raise AttributeDoesNotExist("foo", "foo in bar")
except AttributeDoesNotExist as e:
assert hasattr(e, 'attr')
assert getattr(e, 'attr') == 'foo'
assert hasattr(e, 'expression')
assert getattr(e, 'expression') == 'foo in bar'
with self.assertRaises(NameNotDefined):
self.t("a == 2", None)
self.s.names["s"] = 21
with self.assertRaises(NameNotDefined):
self.t("s += a", 21)
self.s.names = None
with self.assertRaises(InvalidExpression):
self.t('s', 21)
self.s.names = {'a': {'b': {'c': 42}}}
with self.assertRaises(AttributeDoesNotExist):
self.t('a.b.d**2', 42)
# Maybe the base object is an actual object, not just a dict
try:
return getattr(node_evaluated, node.attr)
except (AttributeError, TypeError):
pass
# TODO: is this a good idea? Try and look for [x] if .x doesn't work?
if self.ATTR_INDEX_FALLBACK:
try:
return node_evaluated[node.attr]
except (KeyError, TypeError):
pass
# If it is neither, raise an exception
raise AttributeDoesNotExist(node.attr, self.expr)