Repository navigation
Deprecate PyObject_SetAttr(obj, name, NULL): PyObject_DelAttr(obj, name) must be used instead #106572
Description
Activity
python-dev discussion in 2015: https://mail.python.org/archives/list/python-dev@python.org/thread/OSKS23WDHU67NQCANPNSAPHATZ2J3W7T/
I wrote PR #105373 to remove the deprecation in the documentation, but @serhiy-storchaka wrote that the deprecation should stay: #105374 (comment)
Copy of his message:
The Gmane link no longer work, the working links to the discussion (it was split on several threads) are
https://mail.python.org/archives/list/python-dev@python.org/thread/OSKS23WDHU67NQCANPNSAPHATZ2J3W7T/
https://mail.python.org/archives/list/python-dev@python.org/thread/UJI3KJQVOY7VELTDJOBTBWM3TYDHEYGD/The problems with the current behavior:
- It looks confusing that some of
*Set*functions do removing, while other raise SystemError or crash. Ideally, all such functions should either support deletion by specifying NULL, or raise SystemError. A crash is acceptable too, for performance. - It is a bug magnet. It is easy to get the NULL pointer instead of a pointer to object (just forgot to check for error) and delete instead of set.
Changing the current behavior is a long process. M.-A. Lemburg specified the required procedure. It is interesting, that if we started it in 3.6, it would already be finished.
- It looks confusing that some of
I reported this error prone API as capi-workgroup/problems#47
We should consider deprecating similar APIs used to delete an item/attribute:
PyObject_SetItem(obj, key, NULL): usePyObject_DelItem(obj, key)insteadPyMapping_SetItemString(obj, key, NULL): usePyMapping_DelItemString(obj, key)insteadPySequence_SetItem(obj, index, NULL): usePySequence_DelItem(obj, index)insteadPySequence_SetSlice(obj, i1, i2, NULL): usePySequence_DelSlice(obj, i1, i2)instead
Unfortunately, adding a warning will break binary compatibility. Code compiled with older Python version which uses
PyObject_DelAttr()callsPyObject_SetAttr(), becausePyObject_DelAttr()was implemented as a macro.We should first implement
PyObject_DelAttr()as a separate function, then in some distant future we decide to break binary compatibility with Python older than 3.13 and add a warning, then error, inPyObject_SetAttr().- added 7 commits that reference this issue
on Jul 10, 2023 I close the issue: see #106573 (comment)
When
PyObject_SetAttr()is called with aNULLvalue, it's unclear if the caller wants to remove the attribute on purpose, or if the value isNULLbecause of an error. Such API is error prone and should be avoided.In 2005, this feature was already deprecated in the documentation in issue #69887 by commit 45be8d6.
I propose to now emit a DeprecationWarning and schedule the removal of this feature in Python 3.15. If too many C extensions rely on this current behavior, we can consider removing the feature later.
Linked PRs