gh-135075: Deprecate PyObject_SetAttr(obj, name, NULL) with exc#135082
gh-135075: Deprecate PyObject_SetAttr(obj, name, NULL) with exc#135082vstinner wants to merge 1 commit into
Conversation
Deprecate calling PyObject_SetAttr() and PyObject_SetAttrString() with NULL value and an exception set.
| if (PyErr_WarnFormat(PyExc_DeprecationWarning, 0, | ||
| "calling PyObject_SetAttrString() with NULL value " | ||
| "and an exception set is deprecated; " | ||
| "use PyObject_DelAttrString() instead")) { |
There was a problem hiding this comment.
I think that in most cases, this is the wrong advice. PyObject_SetAttrString will only be called with NULL and an exception set when someone was misusing the C API, not when they were trying to delete an attribute.
Is it also possible to display the exception or chain it here?
There was a problem hiding this comment.
PyObject_SetAttr() does chain the exception, PyObject_SetAttrString() doesn't.
There was a problem hiding this comment.
An alternative is to modify PyObject_SetAttr() to fail with an exception (SystemError) if called with NULL value and an exception set.
There was a problem hiding this comment.
Failing with SystemError makes more sense to me. There's already an exception present, so I don't think we'll be breaking code.
There was a problem hiding this comment.
I don't think we'll be breaking code.
It seems like currently, it's not possible to call PyObject_SetAttr() with an exception set: #135075 (comment) At least, for some setters.
There was a problem hiding this comment.
A SystemError should be safe then.
There was a problem hiding this comment.
Ok, I created #136180 to raise SystemError if called with NULL value and an exception set.
|
#136180 looks like a better approach, I close this PR. |
Deprecate calling PyObject_SetAttr() and PyObject_SetAttrString() with NULL value and an exception set.
📚 Documentation preview 📚: https://cpython-previews--135082.org.readthedocs.build/