Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b35ffc0

Browse files
author
Fredrik Lundh
committed
added "magic" number to the _sre module, to avoid weird errors caused
by compiler/engine mismatches
1 parent 142297a commit b35ffc0

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/sre_compile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from sre_constants import *
1414

15+
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
16+
1517
MAXCODE = 65535
1618

1719
def _compile(code, pattern, flags):

Lib/sre_constants.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99
# See the sre.py file for information on usage and redistribution.
1010
#
1111

12+
# update when constants are added or removed
13+
14+
MAGIC = 20010115
15+
16+
# max code word in this release
17+
1218
MAXREPEAT = 65535
1319

20+
# SRE standard exception (access as sre.error)
1421
# should this really be here?
1522

1623
class error(Exception):
@@ -211,6 +218,8 @@ def dump(f, d, prefix):
211218
212219
""")
213220

221+
f.write("#define SRE_MAGIC %d\n" % MAGIC)
222+
214223
dump(f, OPCODES, "SRE_OP")
215224
dump(f, ATCODES, "SRE")
216225
dump(f, CHCODES, "SRE")

Modules/_sre.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,11 +2355,19 @@ __declspec(dllexport)
23552355
#endif
23562356
init_sre(void)
23572357
{
2358+
PyObject* m;
2359+
PyObject* d;
2360+
23582361
/* Patch object types */
23592362
Pattern_Type.ob_type = Match_Type.ob_type =
23602363
Scanner_Type.ob_type = &PyType_Type;
23612364

2362-
Py_InitModule("_" MODULE, _functions);
2365+
m = Py_InitModule("_" MODULE, _functions);
2366+
d = PyModule_GetDict(m);
2367+
2368+
PyDict_SetItemString(
2369+
d, "MAGIC", (PyObject*) PyInt_FromLong(SRE_MAGIC)
2370+
);
23632371
}
23642372

23652373
#endif /* !defined(SRE_RECURSIVE) */

Modules/sre_constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* See the _sre.c file for information on usage and redistribution.
1212
*/
1313

14+
#define SRE_MAGIC 20010115
1415
#define SRE_OP_FAILURE 0
1516
#define SRE_OP_SUCCESS 1
1617
#define SRE_OP_ANY 2

0 commit comments

Comments
 (0)