Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another case of exception swallowing from PyObject_GenericGetAttr #960

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -2972,7 +2972,7 @@ static PyObject *
trait_getattro(trait_object *obj, PyObject *name)
{
PyObject *value = PyObject_GenericGetAttr((PyObject *)obj, name);
if (value != NULL) {
if (value != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
return value;
}

Expand Down
19 changes: 19 additions & 0 deletions traits/tests/test_ctraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ def _bar_changed(self, new):
self.assertEqual(len(foo.bar_changed), 1)
self.assertEqual(foo.bar_changed[0], "baz")

def test_failed_attribute_access(self):
ctrait = CTrait(0)
self.assertIsNone(ctrait.non_existent)

def test_exception_from_attribute_access(self):
# Regression test for enthought/traits#946.

# Danger: we're (temporarily) mutating global state here! Check that
# we're not touching an attribute that actually exists.
self.assertFalse(hasattr(CTrait, "badattr_test"))

CTrait.badattr_test = property(lambda self: 1 / 0)
try:
ctrait = CTrait(0)
with self.assertRaises(ZeroDivisionError):
ctrait.badattr_test
finally:
del CTrait.badattr_test


class TestCTraitNotifiers(unittest.TestCase):
""" Test calling trait notifiers and object notifiers. """
Expand Down