[pypy-commit] pypy numpy-multidim: shuffle code around. Now get_code lives outside of tests
fijal
noreply at buildbot.pypy.org
Thu Nov 3 19:51:01 CET 2011
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-multidim
Changeset: r48713:1582795d14f5
Date: 2011-11-03 19:50 +0100
http://bitbucket.org/pypy/pypy/changeset/1582795d14f5/
Log: shuffle code around. Now get_code lives outside of tests
diff --git a/pypy/rlib/rsre/rpy.py b/pypy/rlib/rsre/rpy.py
new file mode 100644
--- /dev/null
+++ b/pypy/rlib/rsre/rpy.py
@@ -0,0 +1,49 @@
+
+from pypy.rlib.rsre import rsre_char
+from pypy.rlib.rsre.rsre_core import match
+
+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:
+ 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
+
+class GotIt(Exception):
+ pass
+def my_compile(pattern, flags, code, *args):
+ 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:
+ return e.args[0]
diff --git a/pypy/rlib/rsre/test/test_match.py b/pypy/rlib/rsre/test/test_match.py
--- a/pypy/rlib/rsre/test/test_match.py
+++ b/pypy/rlib/rsre/test/test_match.py
@@ -1,54 +1,8 @@
import re
-from pypy.rlib.rsre import rsre_core, rsre_char
+from pypy.rlib.rsre import rsre_core
+from pypy.rlib.rsre.rpy import get_code
-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:
- 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
-
-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:
- return e.args[0]
-
def get_code_and_re(regexp):
return get_code(regexp), re.compile(regexp)
More information about the pypy-commit
mailing list