-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unittest.assertRaisesRegex is broken in Python 3.11 and leading to crashing if tested regex does not match name. #89984
Comments
In Python 3.11, unittest.assertRaisesRegex is broken and leading to crashing if tested regex does not match name. See the following example: test.py import unittest
class uTest(unittest.TestCase):
pass
uTest = uTest()
with uTest.assertRaisesRegex(Exception, 'aaa'):
aab ========================================= Output in Python3.9.2, 3.10: During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/xxm/Desktop/test.py", line 29, in <module>
aab
File "/usr/local/python310/lib/python3.10/unittest/case.py", line 239, in __exit__
self._raiseFailure('"{}" does not match "{}"'.format(
File "/usr/local/python310/lib/python3.10/unittest/case.py", line 163, in _raiseFailure
raise self.test_case.failureException(msg)
AssertionError: "aaa" does not match "name 'aab' is not defined Actual output in Python3.11.0a1,Python3.11.0a2: System: Ubuntu 16.04 |
Running it in debug build mode ./python -X dev ../bpo45826.py Current thread 0x00007f4c717f3280 (most recent call first): |
I got a segfault in a similar location: static PyObject *
offer_suggestions_for_name_error(PyNameErrorObject *exc)
{
PyObject *name = exc->name; // borrowed reference
PyTracebackObject *traceback = (PyTracebackObject *) exc->traceback; // borrowed reference
// Abort if we don't have a variable name or we have an invalid one
// or if we don't have a traceback to work with
if (name == NULL || traceback == NULL || !PyUnicode_CheckExact(name)) {
return NULL;
}
// Move to the traceback of the exception
while (traceback->tb_next != NULL) { <<<<<<<<<<<<<<< segfault: traceback is junk (but not null) pointer
traceback = traceback->tb_next;
}
... Adding Here is some debugging code I used that also causes the crash: ---------------------------------------------- from unittest import TestCase
from unittest.case import _AssertRaisesContext
import sys
import traceback
manager = _AssertRaisesContext(Exception, TestCase(), 'aaa') # inline this: try:
# inline manager._raiseFailure(output)
msg = manager.test_case._formatMessage(manager.msg, output)
print("A:", f"{msg=!r}")
e = manager.test_case.failureException(msg)
print("B:", f"{e=!r}")
raise e # Output: |
Here's shorter reproducer not involving unittest: import sys try: |
Even shorter reproducer: ----------------------------- Bisection points to the initial implementation of suggestions.c: 5bf8bf2 is the first bad commit
>>> schwarzschild_black_hole = None
>>> schwarschild_black_hole
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole? |
Thanks, Dennis! ✨ 🍰 ✨ |
traceback is None
#29590traceback is None
(GH-29590) #29602Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: