Skip to content

Commit 7e68d49

Browse files
sobolevnGlyphack
authored andcommitted
pythongh-110590: Fix a bug where _sre.compile would overwrite exceptions (python#110591)
TypeError would be overwritten by OverflowError if 'code' param contained non-ints.
1 parent cb98434 commit 7e68d49

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
@@ -2735,6 +2735,9 @@ def test_dealloc(self):
27352735
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
27362736
with self.assertRaises(TypeError):
27372737
_sre.compile({}, 0, [], 0, [], [])
2738+
# gh-110590: `TypeError` was overwritten with `OverflowError`:
2739+
with self.assertRaises(TypeError):
2740+
_sre.compile('', 0, ['abc'], 0, {}, ())
27382741

27392742
@cpython_only
27402743
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
@@ -1508,6 +1508,9 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
15081508
for (i = 0; i < n; i++) {
15091509
PyObject *o = PyList_GET_ITEM(code, i);
15101510
unsigned long value = PyLong_AsUnsignedLong(o);
1511+
if (value == (unsigned long)-1 && PyErr_Occurred()) {
1512+
break;
1513+
}
15111514
self->code[i] = (SRE_CODE) value;
15121515
if ((unsigned long) self->code[i] != value) {
15131516
PyErr_SetString(PyExc_OverflowError,

0 commit comments

Comments
 (0)