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: assertion failure in PyErr_WriteUnraisable() in case of an exception with a bad __module__
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-09-11 12:49 by Oren Milman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3539 merged Oren Milman, 2017-09-13 10:26
PR 3556 merged python-dev, 2017-09-13 22:30
Messages (7)
msg301872 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-09-11 12:49
The following code causes an assertion failure in PyErr_WriteUnraisable() (in
Python/errors.c):

class BadException(Exception):
    __module__ = None

class BadClass:
    def __del__(self):
        raise BadException

foo = BadClass()
del foo


this is because PyErr_WriteUnraisable() assumes that __module__ is a string,
and passes it to _PyUnicode_EqualToASCIIId(), which asserts it received a string.


what is the wanted behavior in such a case?
should we ignore the bad __module__ and print '<unknown>' as the module name
in the traceback?
msg301873 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-11 13:25
Printing '<unknown>' LGTM.

Implicit converting to str can raise a warning or exception if __module__ is a bytes object.
msg301874 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-11 13:27
I think this patch doesn't need a test (which would require using a subprocess).
msg302042 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-09-13 09:48
what do you mean by 'Implicit converting to str can raise a warning or exception if __module__ is a bytes object.'?

should we treat __module__ differently in case it is a bytes object?
msg302047 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-13 10:02
PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) implicitly converts its argument to string. I mean that treating non-string moduleName the same way as string moduleName not equal to string "builtins" and calling PyFile_WriteObject() would cause other problem. Treating non-string moduleName the same way as moduleName==NULL LGTM.
msg302126 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-13 22:30
New changeset f6e61df01536493f1280cd07639c7ff9bffb2cdc by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31418: Fix an assertion failure in PyErr_WriteUnraisable() in case of an exception with a bad __module__ attribute. (#3539)
https://github.com/python/cpython/commit/f6e61df01536493f1280cd07639c7ff9bffb2cdc
msg302145 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-14 06:41
New changeset 5dbb28ececdd0382d85b164aaf37bec1ae08036c by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31418: Fix an assertion failure in PyErr_WriteUnraisable() in case of an exception with a bad __module__ attribute. (GH-3539) (#3556)
https://github.com/python/cpython/commit/5dbb28ececdd0382d85b164aaf37bec1ae08036c
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75599
2017-09-14 06:42:14serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-09-14 06:41:41serhiy.storchakasetmessages: + msg302145
2017-09-13 22:30:16python-devsetpull_requests: + pull_request3548
2017-09-13 22:30:08serhiy.storchakasetmessages: + msg302126
2017-09-13 22:27:35serhiy.storchakasetversions: + Python 3.6
2017-09-13 10:26:02Oren Milmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request3534
2017-09-13 10:02:15serhiy.storchakasetmessages: + msg302047
2017-09-13 09:48:48Oren Milmansetmessages: + msg302042
2017-09-11 13:27:17serhiy.storchakasetmessages: + msg301874
2017-09-11 13:25:24serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg301873
2017-09-11 12:49:19Oren Milmancreate