[pypy-commit] pypy default: Stop relying on the host stdlib's sre_*.py files, which can and do

arigo noreply at buildbot.pypy.org
Tue Sep 10 14:29:55 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r66891:7b0cafed5689
Date: 2013-09-10 14:22 +0200
http://bitbucket.org/pypy/pypy/changeset/7b0cafed5689/

Log:	Stop relying on the host stdlib's sre_*.py files, which can and do
	change details (like in 2.7.4). Instead, copy directly the version
	we want to support (here 2.7.3) into the path
	rpython.rlib.rsre.rpy.sre_*.

diff --git a/rpython/rlib/rsre/rpy/__init__.py b/rpython/rlib/rsre/rpy/__init__.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/rsre/rpy/__init__.py
@@ -0,0 +1,1 @@
+from ._sre import get_code
diff --git a/rpython/rlib/rsre/rpy.py b/rpython/rlib/rsre/rpy/_sre.py
rename from rpython/rlib/rsre/rpy.py
rename to rpython/rlib/rsre/rpy/_sre.py
--- a/rpython/rlib/rsre/rpy.py
+++ b/rpython/rlib/rsre/rpy/_sre.py
@@ -1,46 +1,24 @@
 
 from rpython.rlib.rsre import rsre_char
-from rpython.rlib.rsre.rsre_core import match
 from rpython.rlib.rarithmetic import intmask
 
-def get_hacked_sre_compile(my_compile):
-    """Return a copy of the sre_compile module for which the _sre
-    module is a custom module that has _sre.compile == my_compile
-    and CODESIZE == rsre_char.CODESIZE.
-    """
-    import sre_compile, sre_constants, __builtin__, new
-    sre_hacked = new.module("_sre_hacked")
-    sre_hacked.compile = my_compile
-    sre_hacked.MAGIC = sre_compile.MAGIC
-    sre_hacked.CODESIZE = rsre_char.CODESIZE
-    sre_hacked.MAXREPEAT = sre_constants.MAX_REPEAT
-    sre_hacked.getlower = rsre_char.getlower
-    def my_import(name, *args):
-        if name == '_sre':
-            return sre_hacked
-        else:
-            return default_import(name, *args)
-    src = sre_compile.__file__
-    if src.lower().endswith('.pyc') or src.lower().endswith('.pyo'):
-        src = src[:-1]
-    mod = new.module("sre_compile_hacked")
-    default_import = __import__
-    try:
-        __builtin__.__import__ = my_import
-        execfile(src, mod.__dict__)
-    finally:
-        __builtin__.__import__ = default_import
-    return mod
+
+MAGIC = 20031017
+CODESIZE = rsre_char.CODESIZE
+getlower = rsre_char.getlower
+
 
 class GotIt(Exception):
     pass
-def my_compile(pattern, flags, code, *args):
+
+def compile(pattern, flags, code, *args):
     raise GotIt([intmask(i) for i in code], flags, args)
-sre_compile_hacked = get_hacked_sre_compile(my_compile)
+
 
 def get_code(regexp, flags=0, allargs=False):
+    from . import sre_compile
     try:
-        sre_compile_hacked.compile(regexp, flags)
+        sre_compile.compile(regexp, flags)
     except GotIt, e:
         pass
     else:
diff --git a/lib-python/2.7/sre_compile.py b/rpython/rlib/rsre/rpy/sre_compile.py
copy from lib-python/2.7/sre_compile.py
copy to rpython/rlib/rsre/rpy/sre_compile.py
--- a/lib-python/2.7/sre_compile.py
+++ b/rpython/rlib/rsre/rpy/sre_compile.py
@@ -8,11 +8,11 @@
 # See the sre.py file for information on usage and redistribution.
 #
 
-"""Internal support module for sre"""
+"""Internal support module for sre (copied from CPython 2.7.3)"""
 
-import _sre, sys
-import sre_parse
-from sre_constants import *
+import sys
+from . import _sre, sre_parse
+from .sre_constants import *
 
 assert _sre.MAGIC == MAGIC, "SRE module mismatch"
 
diff --git a/lib-python/2.7/sre_constants.py b/rpython/rlib/rsre/rpy/sre_constants.py
copy from lib-python/2.7/sre_constants.py
copy to rpython/rlib/rsre/rpy/sre_constants.py
--- a/lib-python/2.7/sre_constants.py
+++ b/rpython/rlib/rsre/rpy/sre_constants.py
@@ -9,7 +9,7 @@
 # See the sre.py file for information on usage and redistribution.
 #
 
-"""Internal support module for sre"""
+"""Internal support module for sre (copied from CPython 2.7.3)"""
 
 # update when constants are added or removed
 
@@ -20,10 +20,8 @@
 MAXREPEAT = 65535
 
 # SRE standard exception (access as sre.error)
-# should this really be here?
-
-class error(Exception):
-    pass
+# (use the real re.error exception class)
+from re import error
 
 # operators
 
diff --git a/lib-python/2.7/sre_parse.py b/rpython/rlib/rsre/rpy/sre_parse.py
copy from lib-python/2.7/sre_parse.py
copy to rpython/rlib/rsre/rpy/sre_parse.py
--- a/lib-python/2.7/sre_parse.py
+++ b/rpython/rlib/rsre/rpy/sre_parse.py
@@ -8,19 +8,13 @@
 # See the sre.py file for information on usage and redistribution.
 #
 
-"""Internal support module for sre"""
+"""Internal support module for sre (copied from CPython 2.7.3)"""
 
 # XXX: show string offset and offending character for all errors
 
 import sys
 
-from sre_constants import *
-
-try:
-    from __pypy__ import newdict
-except ImportError:
-    assert '__pypy__' not in sys.builtin_module_names
-    newdict = lambda _ : {}
+from .sre_constants import *
 
 SPECIAL_CHARS = ".\\[{()*+?^$|"
 REPEAT_CHARS = "*+?{"
@@ -74,7 +68,7 @@
         self.flags = 0
         self.open = []
         self.groups = 1
-        self.groupdict = newdict("module")
+        self.groupdict = {}
     def opengroup(self, name=None):
         gid = self.groups
         self.groups = gid + 1


More information about the pypy-commit mailing list