[pypy-svn] r13566 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Sat Jun 18 00:10:31 CEST 2005


Author: arigo
Date: Sat Jun 18 00:10:27 2005
New Revision: 13566

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
Character comparisons.


Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Sat Jun 18 00:10:27 2005
@@ -247,6 +247,17 @@
                 return func(x)
         """ % locals()).compile()
 
+for opname in ('gt', 'lt', 'ge', 'ne', 'le', 'eq'):
+    assert opname in opimpls
+    exec py.code.Source("""
+        def char_%(opname)s(x, y):
+            assert isinstance(x, str) and len(x) == 1
+            assert isinstance(y, str) and len(y) == 1
+            func = opimpls[%(opname)r]
+            return func(x, y)
+    """ % locals()).compile()
+
+
 # by default we route all logging messages to nothingness
 # e.g. tests can then switch on logging to get more help
 # for failing tests

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Sat Jun 18 00:10:27 2005
@@ -34,6 +34,9 @@
 
 
 CONST_STR_CACHE = WeakValueDictionary()
+string_repr = StringRepr()
+char_repr   = CharRepr()
+
 
 class __extend__(StringRepr):
     lowleveltype = Ptr(STR)
@@ -112,6 +115,23 @@
         return hop.genop('cast_char_to_int', vlist, resulttype=Signed)
 
 
+class __extend__(pairtype(CharRepr, CharRepr)):
+    def rtype_eq(_, hop): return _rtype_compare_template(hop, 'eq')
+    def rtype_ne(_, hop): return _rtype_compare_template(hop, 'ne')
+    def rtype_lt(_, hop): return _rtype_compare_template(hop, 'lt')
+    def rtype_le(_, hop): return _rtype_compare_template(hop, 'le')
+    def rtype_gt(_, hop): return _rtype_compare_template(hop, 'gt')
+    def rtype_ge(_, hop): return _rtype_compare_template(hop, 'ge')
+
+#Helper functions for comparisons
+
+def _rtype_compare_template(hop, func):
+    vlist = hop.inputargs(char_repr, char_repr)
+    return hop.genop('char_'+func, vlist, resulttype=Bool)
+
+#
+# _________________________ Conversions _________________________
+
 class __extend__(pairtype(CharRepr, StringRepr)):
     def convert_from_to((r_from, r_to), v, llops):
         if r_from == char_repr and r_to == string_repr:
@@ -125,13 +145,6 @@
             return llops.gendirectcall(ll_stritem_nonneg, v, c_zero)
         return NotImplemented
 
-
-string_repr = StringRepr()
-char_repr   = CharRepr()
-
-#
-# _________________________ Conversions _________________________
-
 ##class __extend__(pairtype(PyObjRepr, StringRepr)):
 ##    def convert_from_to((r_from, r_to), v, llops):
 ##        XXX

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Sat Jun 18 00:10:27 2005
@@ -62,3 +62,11 @@
     assert len(res.chars) == 2
     assert res.chars[0] == 'x'
     assert res.chars[1] == '.'
+
+def test_char_compare():
+    res = interpret(lambda c1, c2: c1 == c2,  ['a', 'b'])
+    assert res is False
+    res = interpret(lambda c1, c2: c1 == c2,  ['a', 'a'])
+    assert res is True
+    res = interpret(lambda c1, c2: c1 <= c2,  ['z', 'a'])
+    assert res is False



More information about the Pypy-commit mailing list