This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Avoid suppressing all exceptions in PyObject_HasAttr()
Type: behavior Stage: patch review
Components: Extension Modules, Interpreter Core Versions:
process
Status: open Resolution:
Dependencies: 32787 32788 Superseder:
Assigned To: Nosy List: pitrou, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-09-24 17:46 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 3723 merged serhiy.storchaka, 2017-09-24 18:12
PR 3724 merged serhiy.storchaka, 2017-09-24 18:13
PR 3725 merged serhiy.storchaka, 2017-09-24 18:15
PR 3726 merged serhiy.storchaka, 2017-09-24 18:16
PR 3727 merged serhiy.storchaka, 2017-09-24 18:16
PR 3728 merged serhiy.storchaka, 2017-09-24 18:21
PR 3729 merged serhiy.storchaka, 2017-09-24 18:21
PR 3731 merged serhiy.storchaka, 2017-09-24 18:45
PR 4081 merged serhiy.storchaka, 2017-10-23 10:09
Messages (12)
msg302873 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-24 17:46
Initially hasattr() suppressed all raised exceptions. In issue2196 hasattr() was changed to suppress only Exception exceptions and propagate exceptions like SystemExit and KeyboardInterrupt. In issue9666 hasattr() was changed to suppress only AttributeError.

But C API functions, PyObject_HasAttr() and like, were not changed. PyObject_HasAttr() is documented as an equivalent of hasattr(), but there is undocumented difference. The C code that uses PyObject_HasAttr() starves from the same problem as the Python code that used old hasattr().

The only solution of this problem is getting rid of PyObject_HasAttr() if favor of PyObject_GetAttr(). In this issue I'm going to propose a set of PRs that replace PyObject_HasAttr() invocations in different components with more correct code.
msg304759 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-22 18:31
New changeset 04e36af9b88cd5e80fc818e51d46f07252a2ff3f by Serhiy Storchaka in branch 'master':
bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (#3729)
https://github.com/python/cpython/commit/04e36af9b88cd5e80fc818e51d46f07252a2ff3f
msg304764 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-10-22 21:26
> The only solution of this problem is getting rid of PyObject_HasAttr() if favor of PyObject_GetAttr().

I don't understand why.  Why not fix PyObject_HasAttr() like hasattr() was?
msg304787 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-23 09:29
hasattr() can return True, False, or raise an exception. But PyObject_HasAttr() just returns an integer: 0 for False, not 0 for True. There is no way to return an error, and existing code doesn't expect that PyObject_HasAttr() returns an error. Leaking an exception from PyObject_HasAttr() will break existing code.
msg304795 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-23 12:00
New changeset 77af0a3bcab666a356eea2927a8031a7578d60d2 by Serhiy Storchaka in branch '3.6':
[3.6] bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (GH-3729). (#4081)
https://github.com/python/cpython/commit/77af0a3bcab666a356eea2927a8031a7578d60d2
msg306084 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-11 13:19
New changeset d4f8480dfe89447587550a85b61d4e9faf827e98 by Serhiy Storchaka in branch 'master':
bpo-31572: Don't silence unexpected errors in the _warnings module. (#3731)
https://github.com/python/cpython/commit/d4f8480dfe89447587550a85b61d4e9faf827e98
msg306085 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-11 13:51
New changeset 1707e4020fa8dca8e6a3ac4f9da105b54d597b66 by Serhiy Storchaka in branch 'master':
bpo-31572: Silence only AttributeError when get the __copy__ attribute in itertools.tee(). (#3724)
https://github.com/python/cpython/commit/1707e4020fa8dca8e6a3ac4f9da105b54d597b66
msg306086 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-11 14:20
New changeset 60c3d3551a96febac7b6016fb44605643842c686 by Serhiy Storchaka in branch 'master':
bpo-31572: Get rid of _PyObject_HasAttrId() in dict and OrderedDict. (#3728)
https://github.com/python/cpython/commit/60c3d3551a96febac7b6016fb44605643842c686
msg306087 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-11 14:41
New changeset bba2239c17a404fc99524bfbf8126c9b3b7fb343 by Serhiy Storchaka in branch 'master':
bpo-31572: Get rid of _PyObject_HasAttrId() in the ASDL parser. (#3725)
https://github.com/python/cpython/commit/bba2239c17a404fc99524bfbf8126c9b3b7fb343
msg310091 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-16 16:34
New changeset 4d9aec022063dcfc4cf40ae46b1c4a968297e664 by Serhiy Storchaka in branch 'master':
bpo-31572: Get rid of PyObject_HasAttr() and _PyObject_HasAttrId() in the _io module. (#3726)
https://github.com/python/cpython/commit/4d9aec022063dcfc4cf40ae46b1c4a968297e664
msg316580 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-05-14 21:11
It seems like this issue caused a regression in _warnings: please see bpo-33509.
msg331121 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-05 14:44
New changeset 398bd27967690f2c1a8cbf8d47a5613edd9cfb2a by Serhiy Storchaka in branch 'master':
bpo-32787: Better error handling in ctypes. (#3727)
https://github.com/python/cpython/commit/398bd27967690f2c1a8cbf8d47a5613edd9cfb2a
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75753
2018-12-05 14:44:23serhiy.storchakasetmessages: + msg331121
2018-05-14 21:11:28vstinnersetnosy: + vstinner
messages: + msg316580
2018-02-07 12:04:43serhiy.storchakasetdependencies: + Better error handling in ctypes, Better error handling in sqlite3
2018-01-16 16:34:26serhiy.storchakasetmessages: + msg310091
2017-11-11 14:41:34serhiy.storchakasetmessages: + msg306087
2017-11-11 14:20:01serhiy.storchakasetmessages: + msg306086
2017-11-11 13:51:44serhiy.storchakasetmessages: + msg306085
2017-11-11 13:19:54serhiy.storchakasetmessages: + msg306084
2017-10-23 12:00:46serhiy.storchakasetmessages: + msg304795
2017-10-23 10:09:51serhiy.storchakasetpull_requests: + pull_request4051
2017-10-23 09:29:42serhiy.storchakasetmessages: + msg304787
2017-10-22 21:26:37pitrousetnosy: + pitrou
messages: + msg304764
2017-10-22 18:31:37serhiy.storchakasetmessages: + msg304759
2017-09-24 18:45:51serhiy.storchakasetpull_requests: + pull_request3717
2017-09-24 18:21:58serhiy.storchakasetpull_requests: + pull_request3715
2017-09-24 18:21:14serhiy.storchakasetpull_requests: + pull_request3714
2017-09-24 18:16:31serhiy.storchakasetpull_requests: + pull_request3713
2017-09-24 18:16:03serhiy.storchakasetpull_requests: + pull_request3712
2017-09-24 18:15:18serhiy.storchakasetpull_requests: + pull_request3711
2017-09-24 18:13:50serhiy.storchakasetpull_requests: + pull_request3710
2017-09-24 18:12:51serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request3709
2017-09-24 17:46:45serhiy.storchakacreate