Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-1635741: Convert _sre types to heap types and establish module state (PEP 384) #23393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 20, 2020
Merged
Prev Previous commit
Next Next commit
Merge GH-23101
  • Loading branch information
Erlend E. Aasland committed Nov 19, 2020
commit 12bf771c5d931a67e6360e49c265ad189d06a00e
4 changes: 4 additions & 0 deletions 4 Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
44 changes: 20 additions & 24 deletions 44 Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,18 @@ do { \
} \
} while (0)

#define ADD_ULONG_CONSTANT(module, name, value) \
do { \
PyObject *o = PyLong_FromUnsignedLong(value); \
if (!o) \
goto error; \
int res = PyModule_AddObjectRef(module, name, o); \
Py_DECREF(o); \
if (res <) { \
goto error; \
} \
} while (0)

static int
sre_exec(PyObject *m)
{
Expand All @@ -2847,37 +2859,21 @@ sre_exec(PyObject *m)
CREATE_TYPE(m, state->Match_Type, &match_spec);
CREATE_TYPE(m, state->Scanner_Type, &scanner_spec);

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) {
goto error;
}

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) {
goto error;
}

x = PyLong_FromUnsignedLong(SRE_MAXREPEAT);
if (x) {
PyDict_SetItemString(d, "MAXREPEAT", x);
Py_DECREF(x);
}
ADD_ULONG_CONSTANT(m, "MAXREPEAT", SRE_MAXREPEAT);
ADD_ULONG_CONSTANT(m, "MAXGROUPS", SRE_MAXGROUPS);

x = PyLong_FromUnsignedLong(SRE_MAXGROUPS);
if (x) {
PyDict_SetItemString(d, "MAXGROUPS", x);
Py_DECREF(x);
if (PyModule_AddStringConstant(m, "copyright", copyright) < 0) {
goto error;
}

x = PyUnicode_FromString(copyright);
if (x) {
PyDict_SetItemString(d, "copyright", x);
Py_DECREF(x);
}
return 0;

error:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.