[pypy-svn] r10211 - in pypy/dist/pypy: interpreter module/_sre_pypy objspace/std
mcherm at codespeak.net
mcherm at codespeak.net
Fri Apr 1 04:17:50 CEST 2005
Author: mcherm
Date: Fri Apr 1 04:17:49 2005
New Revision: 10211
Added:
pypy/dist/pypy/module/_sre_pypy/ (props changed)
pypy/dist/pypy/module/_sre_pypy/__init__.py (contents, props changed)
pypy/dist/pypy/module/_sre_pypy/application_code.py (contents, props changed)
pypy/dist/pypy/module/_sre_pypy/interpreter_code.py (contents, props changed)
pypy/dist/pypy/module/_sre_pypy/test.py (contents, props changed)
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/objspace/std/fake.py
Log:
Created _sre module which delegates to the C implementation of _sre from the underlying python interpreter.
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Fri Apr 1 04:17:49 2005
@@ -82,6 +82,12 @@
self.setitem(w_modules, w_name, w_builtin)
self.setitem(self.builtin.w_dict, self.wrap('__builtins__'), w_builtin)
+ from pypy.module._sre_pypy import Module
+ w_name = self.wrap('_sre')
+ mod = Module(self, w_name)
+ w_mod = self.wrap(mod)
+ self.setitem(w_modules, w_name, w_mod)
+
self.setbuiltinmodule('parser')
# initialize with "bootstrap types" from objspace (e.g. w_None)
Added: pypy/dist/pypy/module/_sre_pypy/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_sre_pypy/__init__.py Fri Apr 1 04:17:49 2005
@@ -0,0 +1,53 @@
+"""
+ _sre_pypy
+
+ A pypy interpreter-level implementation of the _sre module which is
+ implemented by invoking _sre from the underlying interpreter and
+ wrapping the results.
+
+ We hope someday to replace the C dependency; hopefully this module
+ allows us to do so one piece at a time.
+"""
+
+from pypy.interpreter.lazymodule import LazyModule
+
+
+class Module(LazyModule):
+ """_sre_pypy module"""
+
+ appleveldefs = {
+ '__name__': 'application_code.__name__',
+ '__doc__': 'application_code.__doc__',
+ 'getcodesize': 'application_code.getcodesize',
+ 'compile': 'application_code.compile',
+ 'SRE_Pattern': 'application_code.SRE_Pattern',
+ 'SRE_Match': 'application_code.SRE_Match',
+ }
+
+ interpleveldefs = {
+ # constants
+ 'MAGIC' : '(space.wrap(interpreter_code._sre.MAGIC))',
+ 'CODESIZE' : '(space.wrap(interpreter_code._sre.CODESIZE))',
+ 'getlower' : 'interpreter_code.getlower',
+ '_compile' : 'interpreter_code._compile',
+ '_fget' : 'interpreter_code._fget',
+ '_SRE_Pattern_match' : 'interpreter_code._SRE_Pattern_match',
+ '_SRE_Pattern_search' : 'interpreter_code._SRE_Pattern_search',
+ '_SRE_Pattern_findall' : 'interpreter_code._SRE_Pattern_findall',
+ '_SRE_Pattern_sub' : 'interpreter_code._SRE_Pattern_sub',
+ '_SRE_Pattern_subn' : 'interpreter_code._SRE_Pattern_subn',
+ '_SRE_Pattern_split' : 'interpreter_code._SRE_Pattern_split',
+ '_SRE_Pattern_finditer' : 'interpreter_code._SRE_Pattern_finditer',
+ '_SRE_Pattern_scanner' : 'interpreter_code._SRE_Pattern_scanner',
+ '_SRE_Match_start' : 'interpreter_code._SRE_Match_start',
+ '_SRE_Match_end' : 'interpreter_code._SRE_Match_end',
+ '_SRE_Match_expand' : 'interpreter_code._SRE_Match_expand',
+ '_SRE_Match_span' : 'interpreter_code._SRE_Match_span',
+ '_SRE_Match_groups' : 'interpreter_code._SRE_Match_groups',
+ '_SRE_Match_groupdict' : 'interpreter_code._SRE_Match_groupdict',
+ '_SRE_Match_group' : 'interpreter_code._SRE_Match_group',
+ '_SRE_Finditer_next' : 'interpreter_code._SRE_Finditer_next',
+ '_SRE_Scanner_match' : 'interpreter_code._SRE_Scanner_match',
+ '_SRE_Scanner_search' : 'interpreter_code._SRE_Scanner_search',
+ }
+
Added: pypy/dist/pypy/module/_sre_pypy/application_code.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_sre_pypy/application_code.py Fri Apr 1 04:17:49 2005
@@ -0,0 +1,162 @@
+"""The application layer implementation of the _sre module."""
+
+import sys
+import _sre # Here we are importing the pypy version of _sre, (which is being
+ # defined within this very file!). This is done in order to access
+ # some methods written at interpreter level (they all start with
+ # underscores).
+
+__name__ = '_sre'
+__doc__ = '_sre module: the core of the regular expression engine, implemented in C.'
+
+def getcodesize():
+ return _sre.CODESIZE
+
+
+def compile(pattern, flags, code, groups=0, groupindex={}, indexgroup=[None]):
+ import _sre
+ result = _sre._compile(pattern, flags, code, groups, groupindex, indexgroup)
+ return SRE_Pattern(result)
+
+
+def _read_only_attribute(attr_name):
+ """This is used to construct read-only attributes."""
+ def fget(self):
+ return _sre._fget(self._wrapped, attr_name)
+ return property(fget)
+
+
+class SRE_Pattern(object):
+ def __init__(self, _wrapped):
+ self._wrapped = _wrapped
+
+ pattern = _read_only_attribute('pattern')
+ flags = _read_only_attribute('flags')
+ groups = _read_only_attribute('groups')
+ groupindex = _read_only_attribute('groupindex')
+
+ def match(self, pattern, pos=0, endpos=sys.maxint):
+ """Searches through the string 'pattern' (which is the string
+ to be searched, NOT the regular expression pattern) starting at
+ pos and ending at endpos looking for matches to the pattern. If
+ any are found it returns a SRE_Match match object; if no matches
+ are found it returns None."""
+ result = _sre._SRE_Pattern_match(self._wrapped, pattern, pos, endpos)
+ if result is None:
+ return None
+ else:
+ return SRE_Match(result, self)
+
+ def search(self, pattern, pos=0, endpos=sys.maxint):
+ result = _sre._SRE_Pattern_search(self._wrapped, pattern, pos, endpos)
+ if result is None:
+ return None
+ else:
+ return SRE_Match(result, self)
+
+ def findall(self, string):
+ return _sre._SRE_Pattern_findall(self._wrapped, string)
+
+ def sub(self, repl, string, count=0):
+ return _sre._SRE_Pattern_sub(self._wrapped, repl, string, count)
+
+ def subn(self, repl, string, count=0):
+ return _sre._SRE_Pattern_subn(self._wrapped, repl, string, count)
+
+ def split(self, string, maxsplit=0):
+ return _sre._SRE_Pattern_split(self._wrapped, string, maxsplit)
+
+ def finditer(self, string):
+ result = _sre._SRE_Pattern_finditer(self._wrapped, string)
+ return SRE_Finditer(result, self)
+
+ def scanner(self, string, start=0, end=sys.maxint):
+ result = _sre._SRE_Pattern_scanner(self._wrapped, string, start, end)
+ return SRE_Scanner(result, self)
+
+ def __copy__():
+ raise TypeError, 'cannot copy this pattern object'
+
+ def __deepcopy__():
+ raise TypeError, 'cannot copy this pattern object'
+
+
+
+class SRE_Finditer(object):
+ def __init__(self, _wrapped, _re):
+ self._wrapped = _wrapped
+ self._re = _re
+ def __iter__(self):
+ return self
+ def next(self):
+ match_obj = _sre._SRE_Finditer_next(self._wrapped)
+ return SRE_Match( match_obj, self._re)
+
+
+class SRE_Scanner(object):
+ def __init__(self, _wrapped, pattern):
+ self._wrapped = _wrapped
+ self.pattern = pattern
+
+ def match(self):
+ """Returns a match object if the string matches the pattern
+ starting at the current position, and advances the current position to
+ the end of the match. Returns None if the string fails to match the
+ pattern at the current position."""
+ match_obj = _sre._SRE_Scanner_match(self._wrapped)
+ if match_obj is None:
+ return None
+ else:
+ return SRE_Match( match_obj, self.pattern )
+
+ def search(self):
+ """Returns a match object if the string matches the pattern anywhere
+ on or after the current position, and advances the curent position to
+ the end of the match. Returns None if the string fails to match the
+ pattern at the current position."""
+ match_obj = _sre._SRE_Scanner_search(self._wrapped)
+ if match_obj is None:
+ return None
+ else:
+ return SRE_Match( match_obj, self.pattern )
+
+
+class SRE_Match(object):
+ def __init__(self, _wrapped, re):
+ self._wrapped = _wrapped
+ self.re = re
+
+ string = _read_only_attribute('string')
+ pos = _read_only_attribute('pos')
+ endpos = _read_only_attribute('endpos')
+ lastindex = _read_only_attribute('lastindex')
+ lastgroup = _read_only_attribute('lastgroup')
+ regs = _read_only_attribute('regs')
+
+ def start(self, group=0):
+ return _sre._SRE_Match_start(self._wrapped, group)
+
+ def end(self, group=0):
+ return _sre._SRE_Match_end(self._wrapped, group)
+
+ def span(self, group=0):
+ return _sre._SRE_Match_span(self._wrapped, group)
+
+ def expand(self, template):
+ return _sre._SRE_Match_expand(self._wrapped, template)
+
+ def groups(self, default=None):
+ return _sre._SRE_Match_groups(self._wrapped, default)
+
+ def groupdict(self, default=None):
+ return _sre._SRE_Match_groupdict(self._wrapped, default)
+
+ def group(self, *args):
+ return _sre._SRE_Match_group(self._wrapped, args)
+
+ def __copy__():
+ raise TypeError, 'cannot copy this pattern object'
+
+ def __deepcopy__():
+ raise TypeError, 'cannot copy this pattern object'
+
Added: pypy/dist/pypy/module/_sre_pypy/interpreter_code.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_sre_pypy/interpreter_code.py Fri Apr 1 04:17:49 2005
@@ -0,0 +1,270 @@
+"""
+This file contains the interpreter-level code for pypy's _sre module.
+Really, it's nothing but a very thin wrapper around the underlying
+interpreter's _sre module (ie, the one written in C). Since the C
+code can't be meaningfully optimized and this is nothing more than
+a minimally thin layer that deals with wrapping and unwrapping,
+there is no particular reason for any of this code to be RPython
+
+
+ NOT RPython
+
+"""
+
+# NOTE: In some places, after setting f.unwrap_spec, the property
+# f.return_type has also been set. This has no effect whatsoever,
+# it was just a convenient way of documenting the return type,
+# which can be difficult to look up in _sre.c.
+
+
+from pypy.interpreter.baseobjspace import Wrappable, W_Root, ObjSpace
+from pypy.interpreter.gateway import NoneNotWrapped
+from pypy.interpreter.error import OperationError
+import _sre # This runs at interpreter level, so here we are importing the
+ # C version of _sre provided by underlying virtual machine.
+
+
+
+def _unwrap_list_of_ints(space, w_list):
+ list_w = space.unpackiterable(w_list)
+ list = [space.int_w(w_x) for w_x in list_w]
+ return list
+
+def _unwrap_dict_of_strings_to_ints(space, w_dict):
+ w_items = space.call_method(w_dict, 'iteritems')
+ items_w = space.unpackiterable(w_items)
+ result = {}
+ for item_w in items_w:
+ w_key, w_value = space.unpackiterable(item_w)
+ key = space.str_w(w_key)
+ value = space.int_w(w_value)
+ result[key] = value
+ return result
+
+def _unwrap_list_of_None_or_strings(space, w_list):
+ list_w = space.unpackiterable(w_list)
+ result = []
+ for w_item in list_w:
+ if space.is_w(w_item, space.w_None):
+ result.append(None)
+ else:
+ result.append( space.str_w(w_item) )
+ return result
+
+def _unwrap_int_or_str(space, w_item):
+ try:
+ return space.int_w(w_item)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ return space.str_w(w_item)
+
+def _unwrap_list_of_ints_and_strings(space, w_list):
+ list_w = space.unpackiterable(w_list)
+ return [_unwrap_int_or_str(space, w_item) for w_item in list_w]
+
+
+
+def _fget(space, w_wrapped, w_attr):
+ """A generalized attribute accessor, this is passed a wrapped
+ OpaqueObject and an attribute name. It performs getattr(wrapped, attr)
+ and returns the wrapped version of the result."""
+ wrapped = space.interpclass_w(w_wrapped).value
+ attr = space.str_w(w_attr)
+ result = getattr(wrapped, attr)
+ return space.wrap(result)
+
+
+class OpaqueObject(Wrappable):
+ """This stores a single interpreter-level object ('value') and can be
+ wrapped (via "o_w = space.wrap(o)") and unwrapped (via
+ "o = space.interpclass_w(o_w)")."""
+ def __init__(self, value):
+ self.value = value
+
+
+def getlower(space, character, flags):
+ return space.wrap( _sre.getlower(character, flags) )
+getlower.unwrap_spec = [ObjSpace, int, int]
+getlower.return_type = int # just for documentation
+
+
+def _compile(space, pattern, flags, w_code, groups=0,
+ w_groupindex=NoneNotWrapped, w_indexgroup=NoneNotWrapped):
+ """"compile" pattern descriptor to pattern object"""
+ code = _unwrap_list_of_ints(space, w_code)
+ groupindex = _unwrap_dict_of_strings_to_ints(space, w_groupindex)
+ indexgroup = _unwrap_list_of_None_or_strings(space, w_indexgroup)
+
+ pattern_obj = _sre.compile(pattern, flags, code, groups, groupindex, indexgroup)
+ w_opaque_pattern_obj = space.wrap(OpaqueObject(pattern_obj))
+ return w_opaque_pattern_obj
+_compile.unwrap_spec = [ObjSpace, str, int, W_Root, int, W_Root, W_Root]
+
+
+def _SRE_Pattern_match(space, w_wrapped, pattern, pos, endpos):
+ wrapped = space.interpclass_w(w_wrapped).value
+ match_obj = wrapped.match(pattern, pos, endpos)
+ if match_obj is None:
+ return space.w_None
+ else:
+ w_opaque_match_obj = space.wrap(OpaqueObject(match_obj))
+ return w_opaque_match_obj
+_SRE_Pattern_match.unwrap_spec = [ObjSpace, W_Root, str, int, int]
+
+def _SRE_Pattern_search(space, w_wrapped, pattern, pos, endpos):
+ wrapped = space.interpclass_w(w_wrapped).value
+ match_obj = wrapped.search(pattern, pos, endpos)
+ if match_obj is None:
+ return space.w_None
+ else:
+ w_opaque_match_obj = space.wrap(OpaqueObject(match_obj))
+ return w_opaque_match_obj
+_SRE_Pattern_search.unwrap_spec = [ObjSpace, W_Root, str, int, int]
+
+def _SRE_Pattern_findall(space, w_wrapped, string):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.findall(string)
+ return space.wrap(result)
+_SRE_Pattern_findall.unwrap_spec = [ObjSpace, W_Root, str]
+_SRE_Pattern_findall.return_type = 'list<str>' # just for documentation
+
+def _SRE_Pattern_sub(space, w_wrapped, repl, string, count):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.sub(repl, string, count)
+ return space.wrap(result)
+_SRE_Pattern_sub.unwrap_spec = [ObjSpace, W_Root, str, str, int]
+_SRE_Pattern_sub.return_type = str # just for documentation
+
+def _SRE_Pattern_subn(space, w_wrapped, repl, string, count):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.subn(repl, string, count)
+ return space.wrap(result)
+_SRE_Pattern_subn.unwrap_spec = [ObjSpace, W_Root, str, str, int]
+_SRE_Pattern_subn.return_type = 'tuple<str,int>' # just for documentation
+
+def _SRE_Pattern_split(space, w_wrapped, string, maxsplit):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.split(string, maxsplit)
+ return space.wrap(result)
+_SRE_Pattern_split.unwrap_spec = [ObjSpace, W_Root, str, int]
+_SRE_Pattern_split.return_type = 'list<int>' # just for documentation
+
+def _SRE_Pattern_finditer(space, w_wrapped, string):
+ wrapped = space.interpclass_w(w_wrapped).value
+ iter_obj = wrapped.finditer(string)
+ return space.wrap(OpaqueObject(iter_obj))
+_SRE_Pattern_finditer.unwrap_spec = [ObjSpace, W_Root, str]
+
+def _SRE_Pattern_scanner(space, w_wrapped, string, start, end):
+ wrapped = space.interpclass_w(w_wrapped).value
+ scanner_obj = wrapped.scanner(string)
+ return space.wrap(OpaqueObject(scanner_obj))
+_SRE_Pattern_scanner.unwrap_spec = [ObjSpace, W_Root, str, int, int]
+
+
+def _SRE_Match_start(space, w_wrapped, w_group):
+ wrapped = space.interpclass_w(w_wrapped).value
+ group = _unwrap_int_or_str(space, w_group)
+ try:
+ return space.wrap(wrapped.start(group))
+ except IndexError, err:
+ raise OperationError(space.w_IndexError, space.wrap(str(err)))
+_SRE_Match_start.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_start.return_type = int # just for documentation
+
+def _SRE_Match_end(space, w_wrapped, w_group):
+ wrapped = space.interpclass_w(w_wrapped).value
+ group = _unwrap_int_or_str(space, w_group)
+ try:
+ return space.wrap(wrapped.end(group))
+ except IndexError, err:
+ raise OperationError(space.w_IndexError, space.wrap(str(err)))
+_SRE_Match_end.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_end.return_type = int # just for documentation
+
+def _SRE_Match_span(space, w_wrapped, w_group):
+ wrapped = space.interpclass_w(w_wrapped).value
+ group = _unwrap_int_or_str(space, w_group)
+ try:
+ result_tuple = wrapped.span(group)
+ except IndexError, err:
+ raise OperationError(space.w_IndexError, space.wrap(str(err)))
+ return space.newtuple( [space.wrap(x) for x in result_tuple] )
+_SRE_Match_span.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_span.return_type = '2-tuple<int>' # just for documentation
+
+def _SRE_Match_expand(space, w_wrapped, template):
+ wrapped = space.interpclass_w(w_wrapped).value
+ return space.wrap(wrapped.expand(template))
+_SRE_Match_expand.unwrap_spec = [ObjSpace, W_Root, str]
+_SRE_Match_expand.return_type = str # just for documentation
+
+
+def _SRE_Match_groups(space, w_wrapped, w_default):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.groups(None)
+ result_w = []
+ for item in result:
+ if item is None:
+ result_w.append(w_default)
+ else:
+ result_w.append(space.wrap(item))
+ return space.newtuple(result_w)
+_SRE_Match_groups.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_groups.return_type = 'tuple<str|default>' # just for documentation
+
+def _SRE_Match_groupdict(space, w_wrapped, w_default):
+ wrapped = space.interpclass_w(w_wrapped).value
+ result = wrapped.groupdict(None)
+ result_items_w = []
+ for key, value in result.iteritems():
+ w_key = space.wrap(key)
+ if value is None:
+ w_value = w_default
+ else:
+ w_value = space.wrap(value)
+ result_items_w.append( (w_key, w_value) )
+ w_result = space.newdict(result_items_w)
+ return w_result
+_SRE_Match_groupdict.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_groupdict.return_type = 'dict<str: (str|default)>' # just for documentation
+
+def _SRE_Match_group(space, w_wrapped, w_args):
+ wrapped = space.interpclass_w(w_wrapped).value
+ args = _unwrap_list_of_ints_and_strings(space, w_args)
+ try:
+ return space.wrap( wrapped.group(*args) )
+ except IndexError, err:
+ raise OperationError(space.w_IndexError, space.wrap(str(err)))
+_SRE_Match_group.unwrap_spec = [ObjSpace, W_Root, W_Root]
+_SRE_Match_group.return_type = 'str|tuple<str>|w_None' # just for documentation
+
+
+def _SRE_Finditer_next(space, w_wrapped):
+ wrapped = space.interpclass_w(w_wrapped).value
+ try:
+ match_obj = wrapped.next()
+ return space.wrap(OpaqueObject(match_obj))
+ except StopIteration, err:
+ raise OperationError(space.w_StopIteration, space.wrap(""))
+_SRE_Finditer_next.unwrap_spec = [ObjSpace, W_Root]
+
+
+def _SRE_Scanner_match(space, w_wrapped):
+ wrapped = space.interpclass_w(w_wrapped).value
+ match_obj = wrapped.match()
+ if match_obj is None:
+ return space.w_None
+ else:
+ return space.wrap(OpaqueObject(match_obj))
+_SRE_Scanner_match.unwrap_spec = [ObjSpace, W_Root]
+
+def _SRE_Scanner_search(space, w_wrapped):
+ wrapped = space.interpclass_w(w_wrapped).value
+ match_obj = wrapped.search()
+ if match_obj is None:
+ return space.w_None
+ else:
+ return space.wrap(OpaqueObject(match_obj))
+_SRE_Scanner_search.unwrap_spec = [ObjSpace, W_Root]
Added: pypy/dist/pypy/module/_sre_pypy/test.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_sre_pypy/test.py Fri Apr 1 04:17:49 2005
@@ -0,0 +1,154 @@
+#
+# These are tests for the _sre module. Specifically, they test that it
+# it *EXACTLY* conforms to the C implementation of _sre. This is not
+# something we want to maintain in the future... it was useful only
+# while developing the first version of _sre. Since _sre is considered
+# a "private" module, we can modify its behavior as long as the re
+# module continues to work the same, so we do not WANT to be running
+# tests which ensure that every single non-documented interface of
+# _sre works exactly as it does in the C version. Therefore, we should
+# NOT be running these tests. In fact, they probably shouldn't even
+# get checked in... except that it may be convenient someday to be
+# able to look back in svn history and see the contents of this file.
+# So just for the moment this file is getting checked in despite the
+# fact that it's not a proper test file and we probably don't want
+# it anyway... we can delete it soon.
+#
+# This does not test unicode behavior at all (and, in fact, the module
+# doesn't support unicode properly).
+#
+
+import _sre
+
+assert _sre.CODESIZE == 4
+assert _sre.getcodesize() == 4
+assert _sre.getlower(97,2) == 97
+
+def assert_raises(err_type, f, *args, **kwargs):
+ try:
+ f(*args, **kwargs)
+ assert False # Should have raised
+ except IndexError:
+ pass
+ except:
+ assert False # Should have raised a different type
+
+
+# Simple pattern
+pat = _sre.compile('a', 0, [16, 8, 3, 1, 1, 1, 1, 97, 0, 18, 97, 1])
+assert pat.pattern == 'a'
+assert pat.flags == 0
+assert pat.groups == 0
+assert pat.groupindex == {}
+assert pat.match('xxx') is None
+
+m = pat.match('ax')
+assert m is not None
+assert m
+assert m.string == 'ax'
+assert m.re is pat
+assert m.pos == 0
+assert m.endpos == 2
+assert m.lastindex is None
+assert m.lastgroup is None
+assert m.regs == ((0,1),)
+assert m.start() == 0
+assert m.end() == 1
+assert m.span() == (0,1)
+assert m.expand('xx') == 'xx'
+assert m.groupdict() == {}
+assert m.group() == 'a'
+assert m.group(0) == 'a'
+
+assert_raises(IndexError, m.start, 1)
+assert_raises(IndexError, m.end, 1)
+assert_raises(IndexError, m.span, 1)
+assert_raises(IndexError, m.group, 1)
+assert_raises(IndexError, m.group, -1)
+
+assert pat.search('xxx') is None
+ma = pat.search('xxax')
+assert ma
+assert ma.pos == 0
+assert ma.endpos == 4
+assert ma.span() == (2,3)
+
+assert pat.findall('__a__a_') == ['a','a']
+assert pat.sub('qq', '_a__') == '_qq__'
+assert pat.sub('qq', '_aa_a_', 2) == '_qqqq_a_'
+assert pat.subn('qq', '_a__') == ('_qq__', 1)
+assert pat.subn('qq', '_aa_a_', 2) == ('_qqqq_a_', 2)
+assert pat.split('12a34a5') == ['12','34','5']
+
+assert [(x.start(), x.group()) for x in pat.finditer('_a_a_')] == [(1,'a'), (3,'a')]
+
+def test_scanner_match():
+ scan = pat.scanner('x')
+ assert scan.match() is None
+ scan = pat.scanner('a')
+ assert scan.match().start() == 0
+ assert scan.match() is None
+ scan = pat.scanner('aaxa')
+ assert scan.match().start() == 0
+ assert scan.match().start() == 1
+ assert scan.match() is None
+test_scanner_match()
+
+def test_scanner_search():
+ scan = pat.scanner('x')
+ assert scan.search() is None
+ scan = pat.scanner('a')
+ assert scan.search().start() == 0
+ assert scan.search() is None
+ scan = pat.scanner('aaxa')
+ assert scan.search().start() == 0
+ assert scan.search().start() == 1
+ assert scan.search().start() == 3
+ assert scan.search() is None
+test_scanner_search()
+
+# Pattern with groups
+pat2 = _sre.compile('(a)(b)?', 0,
+ [16, 8, 1, 1, 2, 1, 0, 97, 0, 20, 0, 18, 97, 20, 1, 27, 9, 0, 1, 20, 2, 18, 98, 20, 3, 21, 1],
+ 2, {}, [None, None, None])
+m2 = pat2.match('abc')
+assert m2.lastindex == 2
+assert m2.groups() == ('a', 'b')
+assert m2.group(1,2,0) == ('a', 'b', 'ab')
+assert_raises(IndexError, m2.start, 99)
+
+
+m2b = pat2.match('axx')
+assert m2b.groups() == ('a', None)
+assert m2b.groups(43) == ('a', 43)
+
+assert pat2.findall('_a__abb') == [('a', ''), ('a', 'b')]
+
+# Pattern with named group
+pat3 = _sre.compile('(?P<first>a)', 0,
+ [16, 8, 1, 1, 1, 1, 0, 97, 0, 20, 0, 18, 97, 20, 1, 1],
+ 1, {'first': 1}, [None, 'first'])
+assert pat3.groupindex == {'first': 1}
+m3 = pat3.match('abc')
+assert m3.start(0) == 0
+assert m3.start(1) == 0
+assert m3.start('first') == 0
+assert m3.end('first') == 1
+assert m3.span('first') == (0,1)
+assert_raises(IndexError, m3.start, 2)
+assert_raises(IndexError, m3.start, 'second')
+assert m3.lastindex == 1
+assert m3.lastgroup == 'first'
+assert m3.group('first') == 'a'
+assert m3.groupdict() == {'first': 'a'}
+
+# Pattern with a named group that doesn't participate in the match
+pat4 = _sre.compile('(?P<first>a)(?P<second>b)?', 0, [16, 8, 1, 1, 2, 1, 0, 97, 0, 20, 0, 18, 97, 20, 1, 27, 9, 0, 1, 20, 2, 18, 98, 20, 3, 21, 1], 2, {'second': 2, 'first': 1}, [None, 'first', 'second'])
+
+assert pat4.match('ax').group('second') == None
+assert pat4.match('ab').groupdict() == {'first': 'a', 'second': 'b'}
+assert pat4.match('ax').groupdict() == {'first': 'a', 'second': None}
+assert pat4.match('ax').groupdict(42) == {'first': 'a', 'second': 42}
+
+
+print 'Tests OK'
Modified: pypy/dist/pypy/objspace/std/fake.py
==============================================================================
--- pypy/dist/pypy/objspace/std/fake.py (original)
+++ pypy/dist/pypy/objspace/std/fake.py Fri Apr 1 04:17:49 2005
@@ -42,23 +42,10 @@
debug_print('faking %r'%(cpy_type,))
kw = {}
- if cpy_type.__name__ == 'SRE_Pattern':
- import re
- import __builtin__
- p = re.compile("foo")
- for meth_name in p.__methods__:
- kw[meth_name] = __builtin__.eval("lambda p,*args,**kwds: p.%s(*args,**kwds)" % meth_name)
- elif cpy_type.__name__ == 'SRE_Match':
- import re
- import __builtin__
- m = re.compile("foo").match('foo')
- for meth_name in m.__methods__:
- kw[meth_name] = __builtin__.eval("lambda m,*args,**kwds: m.%s(*args,**kwds)" % meth_name)
- else:
- for s, v in cpy_type.__dict__.items():
- if not (cpy_type is unicode and s in ['__add__', '__contains__']):
- if s != '__getattribute__' or cpy_type is type(sys):
- kw[s] = v
+ for s, v in cpy_type.__dict__.items():
+ if not (cpy_type is unicode and s in ['__add__', '__contains__']):
+ if s != '__getattribute__' or cpy_type is type(sys):
+ kw[s] = v
kw['__module__'] = cpy_type.__module__
More information about the pypy-svn
mailing list