Skip to content

Commit 4f6d547

Browse files
orivejstefanseefeld
authored andcommitted
clear python exception after expected attribute lookup failure
Python 3.7.0 asserts that attribute lookup functions are called without outstanding exceptions: https://github.com/python/cpython/blob/v3.7.0a2/Objects/typeobject.c#L3037 Motivation: https://bugs.python.org/issue34068#msg321262 Therefore the error set by the first PyObject_GetItem should be cleared before calling PyObject_GetAttrString.
1 parent b644946 commit 4f6d547

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/object/function.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ void function::add_to_namespace(
444444
if (dict == 0)
445445
throw_error_already_set();
446446

447+
assert(!PyErr_Occurred());
447448
handle<> existing(allow_null(::PyObject_GetItem(dict.get(), name.ptr())));
449+
PyErr_Clear();
448450

449451
if (existing)
450452
{
@@ -485,16 +487,15 @@ void function::add_to_namespace(
485487
if (new_func->name().is_none())
486488
new_func->m_name = name;
487489

490+
assert(!PyErr_Occurred());
488491
handle<> name_space_name(
489492
allow_null(::PyObject_GetAttrString(name_space.ptr(), const_cast<char*>("__name__"))));
493+
PyErr_Clear();
490494

491495
if (name_space_name)
492496
new_func->m_namespace = object(name_space_name);
493497
}
494498

495-
// The PyObject_GetAttrString() or PyObject_GetItem calls above may
496-
// have left an active error
497-
PyErr_Clear();
498499
if (PyObject_SetAttr(ns, name.ptr(), attribute.ptr()) < 0)
499500
throw_error_already_set();
500501

0 commit comments

Comments
 (0)