[pypy-svn] r36558 - in pypy/dist/pypy/translator: cli/test jvm jvm/src/pypy jvm/test oosupport/test_template

niko at codespeak.net niko at codespeak.net
Fri Jan 12 13:25:04 CET 2007


Author: niko
Date: Fri Jan 12 13:25:03 2007
New Revision: 36558

Added:
   pypy/dist/pypy/translator/jvm/test/test_string.py
   pypy/dist/pypy/translator/oosupport/test_template/string.py
Modified:
   pypy/dist/pypy/translator/cli/test/test_dict.py
   pypy/dist/pypy/translator/cli/test/test_string.py
   pypy/dist/pypy/translator/jvm/builtin.py
   pypy/dist/pypy/translator/jvm/generator.py
   pypy/dist/pypy/translator/jvm/genjvm.py
   pypy/dist/pypy/translator/jvm/node.py
   pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java
   pypy/dist/pypy/translator/jvm/typesystem.py
   pypy/dist/pypy/translator/oosupport/test_template/dict.py
Log:
(antocuni, niko)

add support for most of test_string, and refactor the cli test_dict
so it doesn't duplicate info



Modified: pypy/dist/pypy/translator/cli/test/test_dict.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dict.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dict.py	Fri Jan 12 13:25:03 2007
@@ -1,41 +1,18 @@
 import py
 from pypy.translator.cli.test.runtest import CliTest
-from pypy.rpython.test.test_rdict import TestOOtype as _TestOOtype
-from pypy.rpython.test.test_remptydict import BaseTestRemptydict
-from pypy.rpython.test.test_rconstantdict import BaseTestRconstantdict
-
-class TestCliDict(CliTest, _TestOOtype):
-    def test_dict_of_void(self):
-        class A: pass
-        def f():
-            d2 = {A(): None, A(): None}
-            return len(d2)
-        res = self.interpret(f, [])
-        assert res == 2
-
-    def test_dict_of_void_iter(self):
-        def f():
-            d = {1: None, 2: None, 3: None}
-            total = 0
-            for key, value in d.iteritems():
-                total += key
-            return total
-        assert self.interpret(f, []) == 6
+import pypy.translator.oosupport.test_template.dict as oodict
 
+class TestCliDict(CliTest, oodict.BaseTestDict):
     def test_dict_of_dict(self):
         py.test.skip("CLI doesn't support recursive dicts")
 
     def test_recursive(self):
         py.test.skip("CLI doesn't support recursive dicts")
 
-class TestCliEmptyDict(CliTest, BaseTestRemptydict):
+
+class TestCliEmptyDict(CliTest, oodict.BaseTestEmptyDict):
     def test_iterate_over_empty_dict(self):
         py.test.skip("Iteration over empty dict is not supported, yet")
 
-class TestCliConstantDict(CliTest, BaseTestRconstantdict):
-
-    def test_tuple_as_key(self):
-        mydict = {('r',): 42}
-        def fn(ch):
-            return mydict[(ch,)]
-        assert self.interpret(fn, ['r']) == 42
+class TestCliConstantDict(CliTest, oodict.BaseTestConstantDict):
+    pass

Modified: pypy/dist/pypy/translator/cli/test/test_string.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_string.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_string.py	Fri Jan 12 13:25:03 2007
@@ -1,25 +1,11 @@
 import py
 from pypy.translator.cli.test.runtest import CliTest
-from pypy.rpython.test.test_rstr import BaseTestRstr
+import pypy.translator.oosupport.test_template.string as oostring
 
-class TestCliString(CliTest, BaseTestRstr):
+class TestCliString(CliTest, oostring.BaseTestString):
 
     EMPTY_STRING_HASH = 0
 
-    def test_char_isxxx(self):
-        def fn(s):
-            return (s.isspace()      |
-                    s.isdigit() << 1 |
-                    s.isalpha() << 2 |
-                    s.isalnum() << 3 |
-                    s.isupper() << 4 |
-                    s.islower() << 5)
-        # need to start from 1, because we cannot pass '\x00' as a command line parameter        
-        for i in range(1, 128):
-            ch = chr(i)
-            res = self.interpret(fn, [ch])
-            assert res == fn(ch)
-
     def test_unichar_const(self):
         py.test.skip("CLI interpret doesn't support unicode for input arguments")
     test_unichar_eq = test_unichar_const
@@ -30,25 +16,9 @@
         py.test.skip("CLI doens't support backquotes inside string literals")
     test_lower = test_upper
 
-    def test_replace_TyperError(self):
-        pass # it doesn't make sense here
-
     def test_hlstr(self):
         py.test.skip("CLI tests can't have string as input arguments")
 
-    def test_hash_value(self):
-        # make that hash are computed by value and not by reference
-        def fn(x, y):
-            s1 = ''.join([x, 'e', 'l', 'l', 'o'])
-            s2 = ''.join([y, 'e', 'l', 'l', 'o'])
-            return (hash(s1) == hash(s2)) and (s1 is not s2)
-        assert self.interpret(fn, ['h', 'h']) == True
-
-    def test_int_formatting(self):
-        def fn(answer):
-            return 'the answer is %s' % answer
-        assert self.ll_to_string(self.interpret(fn, [42])) == 'the answer is 42'
-
     def test_getitem_exc(self):
         py.test.skip('fixme!')
 

Modified: pypy/dist/pypy/translator/jvm/builtin.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/builtin.py	(original)
+++ pypy/dist/pypy/translator/jvm/builtin.py	Fri Jan 12 13:25:03 2007
@@ -3,7 +3,7 @@
 from pypy.rpython.ootypesystem import ootype
 from pypy.translator.jvm.typesystem import \
      jInt, jVoid, jStringBuilder, jString, jPyPy, jChar, jArrayList, jObject, \
-     jBool, jHashMap, jPyPyDictItemsIterator, Generifier
+     jBool, jHashMap, jPyPyDictItemsIterator, Generifier, jCharSequence
 
 # ______________________________________________________________________
 # Mapping of built-in OOTypes to JVM types
@@ -94,6 +94,27 @@
     (ootype.String.__class__, "ll_stritem_nonneg"):
     jvmgen.Method.v(jString, "charAt", (jInt,), jChar),
 
+    (ootype.String.__class__, "ll_startswith"):
+    jvmgen.Method.v(jString, "startsWith", (jString,), jBool),
+
+    (ootype.String.__class__, "ll_endswith"):
+    jvmgen.Method.v(jString, "endsWith", (jString,), jBool),
+
+    (ootype.String.__class__, "ll_strcmp"):
+    jvmgen.Method.v(jString, "compareTo", (jString,), jInt),
+
+    (ootype.String.__class__, "ll_upper"):
+    jvmgen.Method.v(jString, "toUpperCase", (), jString),
+
+    (ootype.String.__class__, "ll_lower"):
+    jvmgen.Method.v(jString, "toLowerCase", (), jString),
+
+    (ootype.String.__class__, "ll_contains"):
+    jvmgen.Method.v(jString, "contains", (jCharSequence,), jBool),
+
+    (ootype.String.__class__, "ll_replace_chr_chr"):
+    jvmgen.Method.v(jString, "replace", (jChar, jChar), jString),
+
     (ootype.Dict, "ll_set"):
     jvmgen.Method.v(jHashMap, "put", (jObject, jObject), jObject),
     

Modified: pypy/dist/pypy/translator/jvm/generator.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/generator.py	(original)
+++ pypy/dist/pypy/translator/jvm/generator.py	Fri Jan 12 13:25:03 2007
@@ -600,6 +600,8 @@
         def escape(char):
             if char == '"': return r'\"'
             if char == '\n': return r'\n'
+            if char == "\\": return r'\\'
+            if ord(char) > 127: return r'\u%04x' % ord(char)
             return char
         res = ('"' + 
                "".join(escape(c) for c in str) +
@@ -1072,7 +1074,7 @@
     not_equals = lambda self: self._compare_op(IF_ICMPNE)
     less_than = lambda self: self._compare_op(IF_ICMPLT)
     greater_than = lambda self: self._compare_op(IF_ICMPGT)
-    less_equals = lambda self: self._compare_op(IF_ICMPLT)
+    less_equals = lambda self: self._compare_op(IF_ICMPLE)
     greater_equals = lambda self: self._compare_op(IF_ICMPGE)
 
     def _uint_compare_op(self, cmpopcode):

Modified: pypy/dist/pypy/translator/jvm/genjvm.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/genjvm.py	(original)
+++ pypy/dist/pypy/translator/jvm/genjvm.py	Fri Jan 12 13:25:03 2007
@@ -15,6 +15,7 @@
 from pypy.translator.jvm.log import log
 from pypy.translator.jvm.node import EntryPoint, Function
 from pypy.translator.jvm.opcodes import opcodes
+from pypy.rpython.ootypesystem import ootype
 from pypy.translator.jvm.constant import \
      JVMConstantGenerator, JVMStaticMethodConst
 
@@ -122,12 +123,18 @@
 
         print "Invoking jasmin on %s" % self.jasmin_files
         self._invoke(jascmd+list(self.jasmin_files), False)
+        print "... completed!"
                            
         self.compiled = True
         self._compile_helper(('DictItemsIterator',
                               'PyPy',
                               'ExceptionWrapper'))
 
+    def _make_str(self, a):
+        if isinstance(a, ootype._string):
+            return a._str
+        return str(a)
+
     def execute(self, args):
         """
         Executes the compiled sources in a separate process.  Returns the
@@ -135,12 +142,14 @@
         and will be converted to strings.
         """
         assert self.compiled
-        strargs = [str(a) for a in args]
+        strargs = [self._make_str(a) for a in args]
         cmd = [getoption('java'),
                '-cp',
                str(self.javadir),
                self.package+".Main"] + strargs
+        print "Invoking java to run the code"
         stdout, stderr = self._invoke(cmd, True)
+        print "...done!"
         sys.stderr.write(stderr)
         return stdout
         

Modified: pypy/dist/pypy/translator/jvm/node.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/node.py	(original)
+++ pypy/dist/pypy/translator/jvm/node.py	Fri Jan 12 13:25:03 2007
@@ -82,7 +82,9 @@
         lltype.UnsignedLongLong:jvmgen.PYPYSTRTOULONG,
         ootype.Bool:jvmgen.PYPYSTRTOBOOL,
         ootype.Float:jvmgen.PYPYSTRTODOUBLE,
-        ootype.Char:jvmgen.PYPYSTRTOCHAR
+        ootype.Char:jvmgen.PYPYSTRTOCHAR,
+        ootype.UniChar:jvmgen.PYPYSTRTOCHAR,
+        ootype.String:None
         }
 
     def render(self, gen):
@@ -102,7 +104,8 @@
                 gen.load_jvm_var(jStringArray, 0)
                 gen.emit(jvmgen.ICONST, i)
                 gen.load_from_array(jString)
-                gen.emit(self._type_conversion_methods[arg.concretetype])
+                conv = self._type_conversion_methods[arg.concretetype]
+                if conv: gen.emit(conv)
         else:
             # Convert the array of strings to a List<String> as the
             # python method expects

Modified: pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java
==============================================================================
--- pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java	(original)
+++ pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java	Fri Jan 12 13:25:03 2007
@@ -268,6 +268,107 @@
         return str1 + str2;
     }
 
+    public static int ll_find(String haystack, String needle, int start, int end) {
+        // if it is impossible for the needle to occur:
+        //   this deals w/ a disparity in how java and python handle when needle=""
+        if (start > haystack.length())
+            return -1;
+
+        int res = haystack.indexOf(needle, start);
+        System.err.println("haystack="+haystack+" needle="+needle+" start="+start+
+                           " end="+end+" res="+res);
+        if (res + needle.length() > end) 
+            return -1;
+        return res;
+    }
+
+    public static int ll_rfind(String haystack, String needle, int start, int end) {
+        int res = haystack.lastIndexOf(needle, end-1);
+        System.err.println("haystack="+haystack+" needle="+needle+" start="+start+
+                           " end="+end+" res="+res);
+        if (res >= start) 
+            return res;
+        return -1;
+    }
+
+    public static int ll_count(String haystack, String needle, int start, int end) {
+        haystack = haystack.substring(start, end);
+
+        if (needle.length() == 0) {
+            return haystack.length()+1;
+        }
+
+        int cnt = 0;
+        int lastidx = 0, idx = -1;
+        while ((idx = haystack.indexOf(needle, lastidx)) != -1) {
+            cnt++;
+            lastidx = idx + needle.length(); // avoid overlapping occurrences
+        }
+        return cnt;
+    }
+
+    public static int ll_find_char(String haystack, char needle, int start, int end) {
+        // see ll_find
+        if (start > haystack.length())
+            return -1;
+
+        int res = haystack.indexOf(needle, start);
+        if (res >= end) 
+            return -1;
+        return res;
+    }
+
+    public static int ll_rfind_char(String haystack, char needle, int start, int end) {
+        int res = haystack.lastIndexOf(needle, end-1);
+        System.err.println("haystack="+haystack+" needle="+needle+" start="+start+
+                           " end="+end+" res="+res);
+        if (res >= start) 
+            return res;
+        return -1;
+    }
+
+    public static int ll_count_char(String haystack, char needle, int start, int end) {
+        haystack = haystack.substring(start, end);
+        int cnt = 0;
+        int idx = -1;
+        while ((idx = haystack.indexOf(needle, idx+1)) != -1) {
+            cnt++;
+        }
+        return cnt;
+    }
+
+    public static String ll_strip(String str, char ch, boolean left, boolean right) {
+        int start = 0;
+        int end = str.length();
+
+        if (left) {
+            while (start <= str.length() && str.charAt(start) == ch) start++;
+        }
+
+        if (right) {
+            while (end > start && str.charAt(end-1) == ch) end--;
+        }
+
+        return str.substring(start, end);
+    }
+
+    public static ArrayList ll_split_chr(String str, char c) {
+        ArrayList list = new ArrayList();
+        int lastidx = 0, idx = 0;
+        while ((idx = str.indexOf(c, lastidx)) != -1)
+        {
+            String sub = str.substring(lastidx, idx);
+            list.add(sub);
+            lastidx = idx+1;
+        }
+        list.add(str.substring(lastidx));
+        return list;
+    }
+
+    public static String ll_substring(String str, int start, int cnt) {
+        return str.substring(start,start+cnt);
+    }
+
     // ----------------------------------------------------------------------
     // StringBuffer
 

Added: pypy/dist/pypy/translator/jvm/test/test_string.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/jvm/test/test_string.py	Fri Jan 12 13:25:03 2007
@@ -0,0 +1,23 @@
+import py
+from pypy.translator.jvm.test.runtest import JvmTest
+import pypy.translator.oosupport.test_template.string as oostring
+
+class TestJvmString(JvmTest, oostring.BaseTestString):
+
+    EMPTY_STRING_HASH = 0
+    
+    def test_unichar_const(self):
+        py.test.skip("JVM doesn't support unicode for command line arguments")
+    test_unichar_eq = test_unichar_const
+    test_unichar_ord = test_unichar_const
+    test_unichar_hash = test_unichar_const
+
+    def test_upper(self):
+        py.test.skip("eval has trouble with evaluation of null literals")
+    test_lower = test_upper
+
+    def test_float(self):
+        py.test.skip("JVM does not yet support ooparse_float")
+        
+    def test_getitem_exc(self):
+        py.test.skip("JVM has trouble with exceptions")

Modified: pypy/dist/pypy/translator/jvm/typesystem.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/typesystem.py	(original)
+++ pypy/dist/pypy/translator/jvm/typesystem.py	Fri Jan 12 13:25:03 2007
@@ -152,6 +152,7 @@
 jThrowable = JvmClassType('java.lang.Throwable')
 jObject = JvmClassType('java.lang.Object')
 jString = JvmClassType('java.lang.String')
+jCharSequence = JvmClassType('java.lang.CharSequence')
 jArrayList = JvmClassType('java.util.ArrayList')
 jHashMap = JvmClassType('java.util.HashMap')
 jIterator = JvmClassType('java.util.Iterator')

Modified: pypy/dist/pypy/translator/oosupport/test_template/dict.py
==============================================================================
--- pypy/dist/pypy/translator/oosupport/test_template/dict.py	(original)
+++ pypy/dist/pypy/translator/oosupport/test_template/dict.py	Fri Jan 12 13:25:03 2007
@@ -21,20 +21,10 @@
             return total
         assert self.interpret(f, []) == 6
 
-    def test_dict_of_dict(self):
-        py.test.skip("CLI doesn't support recursive dicts")
-
-    def test_recursive(self):
-        py.test.skip("CLI doesn't support recursive dicts")
-
 class BaseTestEmptyDict(BaseTestRemptydict):
-    def test_iterate_over_empty_dict(self):
-        py.test.skip("Iteration over empty dict is not supported, yet")
+    pass
 
 class BaseTestConstantDict(BaseTestRconstantdict):
-    def test_constant_r_dict(self):
-        py.test.skip('r_dict is not supported, yet')
-
     def test_tuple_as_key(self):
         mydict = {('r',): 42}
         def fn(ch):

Added: pypy/dist/pypy/translator/oosupport/test_template/string.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/oosupport/test_template/string.py	Fri Jan 12 13:25:03 2007
@@ -0,0 +1,35 @@
+import py
+from pypy.rpython.test.test_rstr import BaseTestRstr
+
+class BaseTestString(BaseTestRstr):
+
+    def test_char_isxxx(self):
+        def fn(s):
+            return (s.isspace()      |
+                    s.isdigit() << 1 |
+                    s.isalpha() << 2 |
+                    s.isalnum() << 3 |
+                    s.isupper() << 4 |
+                    s.islower() << 5)
+        # need to start from 1, because we cannot pass '\x00' as a
+        # command line parameter
+        for i in range(1, 128):
+            ch = chr(i)
+            res = self.interpret(fn, [ch])
+            assert res == fn(ch)
+
+    def test_replace_TyperError(self):
+        pass # it doesn't make sense here
+
+    def test_hash_value(self):
+        # make that hash are computed by value and not by reference
+        def fn(x, y):
+            s1 = ''.join([x, 'e', 'l', 'l', 'o'])
+            s2 = ''.join([y, 'e', 'l', 'l', 'o'])
+            return (hash(s1) == hash(s2)) and (s1 is not s2)
+        assert self.interpret(fn, ['h', 'h']) == True
+
+    def test_int_formatting(self):
+        def fn(answer):
+            return 'the answer is %s' % answer
+        assert self.ll_to_string(self.interpret(fn, [42])) == 'the answer is 42'



More information about the Pypy-commit mailing list