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

2021-11-17 Thread Łukasz Langa

Łukasz Langa  added the comment:

Thanks, Dennis! ✨  ✨

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed
versions: +Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-17 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset 8eabe60108b536b942c791b5d3dc3c3020497aac by Łukasz Langa in 
branch '3.10':
[3.10] bpo-45826: Fix a crash in suggestions.c by checking for `traceback is 
None` (GH-29590) (GH-29602)
https://github.com/python/cpython/commit/8eabe60108b536b942c791b5d3dc3c3020497aac


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-17 Thread Łukasz Langa

Change by Łukasz Langa :


--
pull_requests: +27845
pull_request: https://github.com/python/cpython/pull/29602

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-17 Thread Łukasz Langa

Łukasz Langa  added the comment:


New changeset 5d90c467c02ffefdb13c1abc83a171db1a99ffad by Dennis Sweeney in 
branch 'main':
bpo-45826: Fix a crash in suggestions.c by checking for `traceback is None` 
(GH-29590)
https://github.com/python/cpython/commit/5d90c467c02ffefdb13c1abc83a171db1a99ffad


--
nosy: +lukasz.langa

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Dennis Sweeney


Change by Dennis Sweeney :


--
keywords: +patch
pull_requests: +27833
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/29590

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Even shorter reproducer:

-
try:
aab
except BaseException as E:
E.with_traceback(None)
raise ZeroDivisionError()
-


Bisection points to the initial implementation of suggestions.c:

5bf8bf2267cd109970b2d946d43b2e9f71379ba2 is the first bad commit
commit 5bf8bf2267cd109970b2d946d43b2e9f71379ba2
Author: Pablo Galindo 
Date:   Wed Apr 14 15:10:33 2021 +0100

bpo-38530: Offer suggestions on NameError (GH-25397)

When printing NameError raised by the interpreter, PyErr_Display
will offer suggestions of simmilar variable names in the function that the 
exception
was raised from:

>>> schwarzschild_black_hole = None
>>> schwarschild_black_hole
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'schwarschild_black_hole' is not defined. Did you mean: 
schwarzschild_black_hole?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Here's shorter reproducer not involving unittest:


import sys

try:
aab
except:
exc_type, exc_value, tb = sys.exc_info()
exc_value.with_traceback(None)
raise ZeroDivisionError()

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Dennis Sweeney


Dennis Sweeney  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Running it in debug build mode

./python -X dev ../bpo45826.py
python: Python/suggestions.c:215: offer_suggestions_for_name_error: Assertion 
`frame != NULL' failed.
Fatal Python error: Aborted

Current thread 0x7f4c717f3280 (most recent call first):
  
[1]15180 abort (core dumped)  ./python -X dev ../bpo45826.py

--
nosy: +pablogsal, xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2021-11-16 Thread Xinmeng Xia


New submission from Xinmeng Xia :

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:
--
NameError: name 'aab' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xxm/Desktop/test.py", line 29, in 
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:
Segmentation fault (core dumped)

System: Ubuntu 16.04

--
components: Library (Lib)
messages: 406445
nosy: xxm
priority: normal
severity: normal
status: open
title: unittest.assertRaisesRegex is broken in Python 3.11 and leading to 
crashing if tested regex does not match name.
type: crash
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com