Skip to content

Commit 12bf771

Browse files
author
Erlend E. Aasland
committed
Merge GH-23101
1 parent 1c925b9 commit 12bf771

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

Lib/test/test_re.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,10 @@ def test_overlap_table(self):
21972197
self.assertEqual(f("ababba"), [0, 0, 1, 2, 0, 1])
21982198
self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])
21992199

2200+
def test_signedness(self):
2201+
self.assertGreaterEqual(sre_compile.MAXREPEAT, 0)
2202+
self.assertGreaterEqual(sre_compile.MAXGROUPS, 0)
2203+
22002204

22012205
class ExternalTests(unittest.TestCase):
22022206

Modules/_sre.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,18 @@ do { \
28342834
} \
28352835
} while (0)
28362836

2837+
#define ADD_ULONG_CONSTANT(module, name, value) \
2838+
do { \
2839+
PyObject *o = PyLong_FromUnsignedLong(value); \
2840+
if (!o) \
2841+
goto error; \
2842+
int res = PyModule_AddObjectRef(module, name, o); \
2843+
Py_DECREF(o); \
2844+
if (res <) { \
2845+
goto error; \
2846+
} \
2847+
} while (0)
2848+
28372849
static int
28382850
sre_exec(PyObject *m)
28392851
{
@@ -2847,37 +2859,21 @@ sre_exec(PyObject *m)
28472859
CREATE_TYPE(m, state->Match_Type, &match_spec);
28482860
CREATE_TYPE(m, state->Scanner_Type, &scanner_spec);
28492861

2850-
d = PyModule_GetDict(m);
2851-
2852-
x = PyLong_FromLong(SRE_MAGIC);
2853-
if (x) {
2854-
PyDict_SetItemString(d, "MAGIC", x);
2855-
Py_DECREF(x);
2862+
if (PyModule_AddIntConstant(m, "MAGIC", SRE_MAGIC) < 0) {
2863+
goto error;
28562864
}
28572865

2858-
x = PyLong_FromLong(sizeof(SRE_CODE));
2859-
if (x) {
2860-
PyDict_SetItemString(d, "CODESIZE", x);
2861-
Py_DECREF(x);
2866+
if (PyModule_AddIntConstant(m, "CODESIZE", sizeof(SRE_CODE)) < 0) {
2867+
goto error;
28622868
}
28632869

2864-
x = PyLong_FromUnsignedLong(SRE_MAXREPEAT);
2865-
if (x) {
2866-
PyDict_SetItemString(d, "MAXREPEAT", x);
2867-
Py_DECREF(x);
2868-
}
2870+
ADD_ULONG_CONSTANT(m, "MAXREPEAT", SRE_MAXREPEAT);
2871+
ADD_ULONG_CONSTANT(m, "MAXGROUPS", SRE_MAXGROUPS);
28692872

2870-
x = PyLong_FromUnsignedLong(SRE_MAXGROUPS);
2871-
if (x) {
2872-
PyDict_SetItemString(d, "MAXGROUPS", x);
2873-
Py_DECREF(x);
2873+
if (PyModule_AddStringConstant(m, "copyright", copyright) < 0) {
2874+
goto error;
28742875
}
28752876

2876-
x = PyUnicode_FromString(copyright);
2877-
if (x) {
2878-
PyDict_SetItemString(d, "copyright", x);
2879-
Py_DECREF(x);
2880-
}
28812877
return 0;
28822878

28832879
error:

0 commit comments

Comments
 (0)