Skip to content

Commit 7ab8b37

Browse files
sobolevnmiss-islington
authored andcommitted
pythongh-110590: Fix a bug where _sre.compile would overwrite exceptions (pythonGH-110591)
TypeError would be overwritten by OverflowError if 'code' param contained non-ints. (cherry picked from commit 344d3a2) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 190660a commit 7ab8b37

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/test/test_re.py

+3
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,9 @@ def test_dealloc(self):
26942694
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
26952695
with self.assertRaises(TypeError):
26962696
_sre.compile({}, 0, [], 0, [], [])
2697+
# gh-110590: `TypeError` was overwritten with `OverflowError`:
2698+
with self.assertRaises(TypeError):
2699+
_sre.compile('', 0, ['abc'], 0, {}, ())
26972700

26982701
@cpython_only
26992702
def test_repeat_minmax_overflow_maxrepeat(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug in :meth:`!_sre.compile` where :exc:`TypeError`
2+
would be overwritten by :exc:`OverflowError` when
3+
the *code* argument was a list of non-ints.

Modules/_sre/sre.c

+3
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,9 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
14621462
for (i = 0; i < n; i++) {
14631463
PyObject *o = PyList_GET_ITEM(code, i);
14641464
unsigned long value = PyLong_AsUnsignedLong(o);
1465+
if (value == (unsigned long)-1 && PyErr_Occurred()) {
1466+
break;
1467+
}
14651468
self->code[i] = (SRE_CODE) value;
14661469
if ((unsigned long) self->code[i] != value) {
14671470
PyErr_SetString(PyExc_OverflowError,

0 commit comments

Comments
 (0)