From d0d47d41cf142db541c5592e48aa5f6d10aa7b97 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 14 Oct 2020 11:47:45 +0200 Subject: [PATCH 1/3] Use PyModule_Add*() in _sre iso. direct module dict manipulation --- Modules/_sre.c | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/Modules/_sre.c b/Modules/_sre.c index fbabeb7c9f3054..dd4009b3fb63ab 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2776,8 +2776,6 @@ static struct PyModuleDef sremodule = { PyMODINIT_FUNC PyInit__sre(void) { PyObject* m; - PyObject* d; - PyObject* x; /* Patch object types */ if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) || @@ -2787,37 +2785,27 @@ PyMODINIT_FUNC PyInit__sre(void) m = PyModule_Create(&sremodule); if (m == NULL) return NULL; - d = PyModule_GetDict(m); - x = PyLong_FromLong(SRE_MAGIC); - if (x) { - PyDict_SetItemString(d, "MAGIC", x); - Py_DECREF(x); + if (PyModule_AddIntConstant(m, "MAGIC", SRE_MAGIC) < 0) { + return NULL; } - x = PyLong_FromLong(sizeof(SRE_CODE)); - if (x) { - PyDict_SetItemString(d, "CODESIZE", x); - Py_DECREF(x); + if (PyModule_AddIntConstant(m, "CODESIZE", sizeof(SRE_CODE)) < 0) { + return NULL; } - x = PyLong_FromUnsignedLong(SRE_MAXREPEAT); - if (x) { - PyDict_SetItemString(d, "MAXREPEAT", x); - Py_DECREF(x); + if (PyModule_AddIntConstant(m, "MAXREPEAT", SRE_MAXREPEAT) < 0) { + return NULL; } - x = PyLong_FromUnsignedLong(SRE_MAXGROUPS); - if (x) { - PyDict_SetItemString(d, "MAXGROUPS", x); - Py_DECREF(x); + if (PyModule_AddIntConstant(m, "MAXGROUPS", SRE_MAXGROUPS) < 0) { + return NULL; } - x = PyUnicode_FromString(copyright); - if (x) { - PyDict_SetItemString(d, "copyright", x); - Py_DECREF(x); + if (PyModule_AddStringConstant(m, "copyright", copyright) < 0) { + return NULL; } + return m; } From ab13ae641891d5b55ba880c21ff523840a6c494b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 2 Nov 2020 11:18:02 +0100 Subject: [PATCH 2/3] Address review comment: SRE_MAXREPEAT and SRE_MAXGROUPS are unsigned --- Modules/_sre.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Modules/_sre.c b/Modules/_sre.c index dd4009b3fb63ab..3aa69752a997bb 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2773,6 +2773,17 @@ static struct PyModuleDef sremodule = { NULL }; +#define ADD_ULONG_CONSTANT(module, name, value) \ + do { \ + PyObject *o = PyLong_FromUnsignedLong(value); \ + if (!o) \ + return NULL; \ + if (PyModule_AddObject(module, name, o) < 0) { \ + Py_DECREF(o); \ + return NULL; \ + } \ +} while (0) + PyMODINIT_FUNC PyInit__sre(void) { PyObject* m; @@ -2794,13 +2805,8 @@ PyMODINIT_FUNC PyInit__sre(void) return NULL; } - if (PyModule_AddIntConstant(m, "MAXREPEAT", SRE_MAXREPEAT) < 0) { - return NULL; - } - - if (PyModule_AddIntConstant(m, "MAXGROUPS", SRE_MAXGROUPS) < 0) { - return NULL; - } + ADD_ULONG_CONSTANT(m, "MAXREPEAT", SRE_MAXREPEAT); + ADD_ULONG_CONSTANT(m, "MAXGROUPS", SRE_MAXGROUPS); if (PyModule_AddStringConstant(m, "copyright", copyright) < 0) { return NULL; From 85616286216092826f3b9aaee2edebfd4efa5dfc Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 3 Nov 2020 09:38:56 +0100 Subject: [PATCH 3/3] Test that MAXREPEAT and MAXGROUPS are unsigned --- Lib/test/test_re.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 1bfbcb853c4ed1..c1d02cfaf0dcb6 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2197,6 +2197,10 @@ def test_overlap_table(self): self.assertEqual(f("ababba"), [0, 0, 1, 2, 0, 1]) self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0]) + def test_signedness(self): + self.assertGreaterEqual(sre_compile.MAXREPEAT, 0) + self.assertGreaterEqual(sre_compile.MAXGROUPS, 0) + class ExternalTests(unittest.TestCase):