classification
Title: 3.10a5 regression: AttributeError: 'NoneType' object has no attribute '__suppress_context__' in traceback.py
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: The Compiler, ZackerySpytz, gvanrossum, hroncok, iritkatriel, pablogsal, terry.reedy, vinay.sajip
Priority: release blocker Keywords: 3.10regression, patch

Created on 2021-02-06 15:54 by The Compiler, last changed 2021-02-23 19:39 by iritkatriel. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24463 merged iritkatriel, 2021-02-06 21:12
PR 24629 merged iritkatriel, 2021-02-23 17:16
Messages (18)
msg386562 - (view) Author: Florian Bruhin (The Compiler) * Date: 2021-02-06 15:54
After upgrading to 3.10a5, calling logging.exception("test") results in:

    --- Logging error ---
    Traceback (most recent call last):
      File "/usr/lib/python3.10/logging/__init__.py", line 1094, in emit
        msg = self.format(record)
      File "/usr/lib/python3.10/logging/__init__.py", line 938, in format
        return fmt.format(record)
      File "/usr/lib/python3.10/logging/__init__.py", line 682, in format
        record.exc_text = self.formatException(record.exc_info)
      File "/usr/lib/python3.10/logging/__init__.py", line 632, in formatException
        traceback.print_exception(ei[0], ei[1], tb, None, sio)
      File "/usr/lib/python3.10/traceback.py", line 113, in print_exception
        te = TracebackException(type(value), value, tb, limit=limit, compact=True)
      File "/usr/lib/python3.10/traceback.py", line 531, in __init__
        need_context = cause is None and not e.__suppress_context__
    AttributeError: 'NoneType' object has no attribute '__suppress_context__'

That method is documented as "This function should only be called from an exception handler.", but the same also happens when e.g. calling traceback.print_exc() (without a current exception) or traceback.print_exception(None, None, None). Both worked fine before 3.10 Alpha 5, the code seems to do None-checks in other places, and typeshed also marks those arguments as Optional.

This seems to have been introduced in https://github.com/python/cpython/pull/24179 for bpo-42877.
msg386563 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-06 16:02
Thanks, I’ll add the None check.
msg386568 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-06 20:17
There's another such issue due to PR 22610 // issue 26389:

>>> traceback.format_exception(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython-dev\lib\traceback.py", line 128, in format_exception
    value, tb = _parse_value_tb(exc, value, tb)
  File "C:\Users\User\src\cpython-dev\lib\traceback.py", line 94, in _parse_value_tb
    return exc, exc.__traceback__
AttributeError: 'NoneType' object has no attribute '__traceback__'
msg386796 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-10 17:34
Marking as blocker because these are 3.10 regressions.
msg386885 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-12 22:24
It seems to me that print_exception(None), etc, *should* raise something.  Printing "NoneType: None\n" makes no sense to me since NoneType is not an exception.  In 3.9, it raised TypeError for # of arguments.

I do note that in 3.9
>>> traceback.print_exception(None, None, None)
NoneType: None

I wonder what the rationale was.  It isn't because these functions never raise.
msg386886 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-12 22:33
In 3.9 you had to give exc,val,tb. In 3.10 the shortcut was added. Issue 26389.
msg386917 - (view) Author: Miro Hrončok (hroncok) * Date: 2021-02-13 09:10
JFYI, there are 2 affected Fedora packages (that we know of):

visidata fails to build with Python 3.10: AttributeError: 'NoneType' object has no attribute '__suppress_context__'
https://bugzilla.redhat.com/show_bug.cgi?id=1928145

python-utils fails to build with Python 3.10: AttributeError: 'NoneType' object has no attribute '__suppress_context__'
https://bugzilla.redhat.com/show_bug.cgi?id=1928081
msg386928 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-13 20:09
> I wonder what the rationale was.  It isn't because these functions never raise.

I think it's because sys.exc_info() can return None, None, None when there is no exceptions.
msg387159 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-17 14:57
Let's split the print_exception(None) case (new issue) from the regression. I've updated the PR to fix only the latter, which is not controversial. 

I don't know what print_exception(None) should do.
msg387573 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-23 14:59
New changeset 26f18b8540b49d592af66361f8df1a03953d1768 by Irit Katriel in branch 'master':
bpo-43146: fix regression in traceback.print_exception(None) (GH-24463)
https://github.com/python/cpython/commit/26f18b8540b49d592af66361f8df1a03953d1768
msg387574 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-23 15:01
Are you going to create a separate PR or a new issue for the other thing? (And what exactly *is* the other thing? Please summarize.)
msg387575 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-23 15:26
The other thing is not related to the one reported here, but I found it while writing the test for this. 

We have a new API in 3.10, where you can do traceback.print_exception(ex)   (so you don't need to give the whole type,val,tb triplet).

But:

>>> traceback.print_exception(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython-dev\lib\traceback.py", line 110, in print_exception
    value, tb = _parse_value_tb(exc, value, tb)
  File "C:\Users\User\src\cpython-dev\lib\traceback.py", line 94, in _parse_value_tb
    return exc, exc.__traceback__
AttributeError: 'NoneType' object has no attribute '__traceback__'
msg387579 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-23 16:23
So the PR just merged fixes that, right? Color me confused.
msg387580 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-23 16:29
The PR just merged fixes the issue that was reported by Florian (what I broke). That impacted pre-3.10 APIs so was an actual regression.

The issue with traceback.print_exception(None) is new to 3.10, and was introduced in issue 26389.
msg387581 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-23 16:45
So the remaining question is what to do for print_exception(None)? Honestly
the current (new) behavior of printing `NoneType: None` seems fine.
msg387582 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-23 16:58
> So the remaining question is what to do for print_exception(None)? 

Yes.

> Honestly the current (new) behavior of printing `NoneType: None` seems fine.

That's not the current behaviour - I removed that fix from the PR till we agree what to do. Currently we get the AttributeError.
msg387586 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-23 17:21
The new PR makes it print `NoneType: None`, which is at least consistent with what the same functions do without the Issue26389 shortcut.
msg387587 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-23 17:43
New changeset b798ab06937f8bb24b444a49dd42e11fff15e654 by Irit Katriel in branch 'master':
bpo-43146: fix None-handling in single-arg traceback.print_exception(None) (GH-24629)
https://github.com/python/cpython/commit/b798ab06937f8bb24b444a49dd42e11fff15e654
History
Date User Action Args
2021-02-23 19:39:25iritkatrielsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-23 17:43:27gvanrossumsetmessages: + msg387587
2021-02-23 17:21:40iritkatrielsetmessages: + msg387586
2021-02-23 17:16:33iritkatrielsetpull_requests: + pull_request23414
2021-02-23 16:58:33iritkatrielsetmessages: + msg387582
2021-02-23 16:45:40gvanrossumsetmessages: + msg387581
2021-02-23 16:29:50iritkatrielsetmessages: + msg387580
2021-02-23 16:23:38gvanrossumsetmessages: + msg387579
2021-02-23 15:26:26iritkatrielsetmessages: + msg387575
2021-02-23 15:01:38gvanrossumsetmessages: + msg387574
2021-02-23 14:59:12gvanrossumsetnosy: + gvanrossum
messages: + msg387573
2021-02-17 14:57:18iritkatrielsetmessages: + msg387159
2021-02-15 15:13:49iritkatrielsetkeywords: + 3.10regression
2021-02-13 20:09:49iritkatrielsetmessages: + msg386928
2021-02-13 09:10:02hroncoksetnosy: + hroncok
messages: + msg386917
2021-02-12 22:33:08iritkatrielsetmessages: + msg386886
2021-02-12 22:24:48terry.reedysetnosy: + terry.reedy
messages: + msg386885
2021-02-10 17:34:57iritkatrielsetpriority: normal -> release blocker

messages: + msg386796
2021-02-06 21:12:52iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23261
2021-02-06 20:35:07iritkatrielsetnosy: + ZackerySpytz, pablogsal
2021-02-06 20:17:45iritkatrielsetmessages: + msg386568
2021-02-06 16:02:38iritkatrielsetmessages: + msg386563
2021-02-06 15:54:16The Compilercreate