[issue45826] unittest.assertRaisesRegex is broken in Python 3.11 and leading to crashing if tested regex does not match name.

Dennis Sweeney report at bugs.python.org
Tue Nov 16 23:37:46 EST 2021


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

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 ```assert(Py_TYPE(exc) == PyExc_NameError);``` fails, so somehow something is getting cast to ```PyNameErrorObject *``` when it shouldn't be.

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:
# with manager:
#      aab

try:
    aab
except:

    # inline __exit__
    exc_type, exc_value, tb = sys.exc_info()
    traceback.clear_frames(tb)
    manager.exception = exc_value.with_traceback(None)
    output = '"{}" does not match "{}"'.format(
                     manager.expected_regex.pattern, str(exc_value))

    # 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:
# A: msg='"aaa" does not match "name \'aab\' is not defined"'
# B: e=AssertionError('"aaa" does not match "name \'aab\' is not defined"')
-----------------------------------------------

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45826>
_______________________________________


More information about the Python-bugs-list mailing list