[pypy-svn] r78161 - in pypy/trunk/pypy/rlib/rsre: . test

arigo at codespeak.net arigo at codespeak.net
Thu Oct 21 13:23:06 CEST 2010


Author: arigo
Date: Thu Oct 21 13:23:04 2010
New Revision: 78161

Modified:
   pypy/trunk/pypy/rlib/rsre/rsre_core.py
   pypy/trunk/pypy/rlib/rsre/test/test_match.py
Log:
Fix the tests, at least on Mac.


Modified: pypy/trunk/pypy/rlib/rsre/rsre_core.py
==============================================================================
--- pypy/trunk/pypy/rlib/rsre/rsre_core.py	(original)
+++ pypy/trunk/pypy/rlib/rsre/rsre_core.py	Thu Oct 21 13:23:04 2010
@@ -170,6 +170,8 @@
     def __init__(self, pattern, string, match_start, end, flags):
         AbstractMatchContext.__init__(self, pattern, match_start, end, flags)
         self._string = string
+        if not we_are_translated() and isinstance(string, unicode):
+            self.flags |= rsre_char.SRE_FLAG_UNICODE   # for rsre_re.py
 
     def str(self, index):
         check_nonneg(index)

Modified: pypy/trunk/pypy/rlib/rsre/test/test_match.py
==============================================================================
--- pypy/trunk/pypy/rlib/rsre/test/test_match.py	(original)
+++ pypy/trunk/pypy/rlib/rsre/test/test_match.py	Thu Oct 21 13:23:04 2010
@@ -1,24 +1,49 @@
-import _sre, re, sre_compile
-from pypy.rlib.rsre import rsre_core
+import re
+from pypy.rlib.rsre import rsre_core, rsre_char
 
 
-def get_code(regexp, flags=0, allargs=False):
-    class GotIt(Exception):
-        pass
-    def my_compile(pattern, flags, code, *args):
-        print code
-        raise GotIt(code, flags, args)
-    saved = _sre.compile
-    try:
-        _sre.compile = my_compile
-        try:
-            sre_compile.compile(regexp, flags)
-        except GotIt, e:
-            pass
+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, __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.getlower = rsre_char.getlower
+    def my_import(name, *args):
+        if name == '_sre':
+            return sre_hacked
         else:
-            raise ValueError("did not reach _sre.compile()!")
+            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:
-        _sre.compile = saved
+        __builtin__.__import__ = default_import
+    return mod
+
+class GotIt(Exception):
+    pass
+def my_compile(pattern, flags, code, *args):
+    print code
+    raise GotIt(code, flags, args)
+sre_compile_hacked = get_hacked_sre_compile(my_compile)
+
+def get_code(regexp, flags=0, allargs=False):
+    try:
+        sre_compile_hacked.compile(regexp, flags)
+    except GotIt, e:
+        pass
+    else:
+        raise ValueError("did not reach _sre.compile()!")
     if allargs:
         return e.args
     else:



More information about the Pypy-commit mailing list