[pypy-svn] r48638 - in pypy/dist/pypy/translator/jvm: . src/pypy test

antocuni at codespeak.net antocuni at codespeak.net
Tue Nov 13 11:01:21 CET 2007


Author: antocuni
Date: Tue Nov 13 11:01:21 2007
New Revision: 48638

Modified:
   pypy/dist/pypy/translator/jvm/generator.py
   pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java
   pypy/dist/pypy/translator/jvm/test/test_string.py
   pypy/dist/pypy/translator/jvm/test/test_unicode.py
Log:
proper handling of unicode strings for genjvm



Modified: pypy/dist/pypy/translator/jvm/generator.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/generator.py	(original)
+++ pypy/dist/pypy/translator/jvm/generator.py	Tue Nov 13 11:01:21 2007
@@ -1100,7 +1100,7 @@
             if value == ootype.null(TYPE):
                 self.emit(ACONST_NULL)
             else:
-                self.load_string(str(value._str))
+                self.load_string(value._str)
         else:
             assert False, 'Unknown constant type: %s' % TYPE
 

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	Tue Nov 13 11:01:21 2007
@@ -304,23 +304,15 @@
             return "False";
     }
 
-    public static void _append_char(StringBuffer sb, char c) {
-        if (c == '"') 
-            sb.append("\\\"");
-        else
-            sb.append(c);
+    private static String format_char(char c) {
+        String res = "\\x";
+        if (c <= 0x0F) res = res + "0";
+        res = res + Integer.toHexString(c);
+        return res;
     }
 
     public static String escaped_char(char c) {
-        StringBuffer sb = new StringBuffer();
-        sb.append('"');
-        _append_char(sb, c);
-        sb.append('"');
-        return sb.toString();
-    }
-
-    public static String escaped_unichar(char c) {
-        return "u" + escaped_char(c);
+        return "'" + format_char(c) + "'";
     }
 
     public static String escaped_string(String b) {
@@ -330,14 +322,37 @@
         sb.append('"');
         for (int i = 0; i < b.length(); i++) {
             char c = b.charAt(i);
-            _append_char(sb, c);
+            sb.append(format_char(c));
         }
         sb.append('"');
         return sb.toString();
     }
 
+    private static String format_unichar(char c) {
+        String res = "\\u";
+        if (c <= 0xF)   res = res + "0";
+        if (c <= 0xFF)  res = res + "0";
+        if (c <= 0xFFF) res = res + "0";
+        res = res + Integer.toHexString(c);
+        return res;
+    }
+
+    public static String escaped_unichar(char c)
+    {
+        return "u'" + format_unichar(c) + "'";
+    }
+
     public static String escaped_unicode(String b) {
-        return "u" + escaped_string(b);
+        if (b == null)
+            return "None";
+        StringBuffer sb = new StringBuffer();
+        sb.append("u'");
+        for (int i = 0; i < b.length(); i++) {
+            char c = b.charAt(i);
+            sb.append(format_unichar(c));
+        }
+        sb.append("'");
+        return sb.toString();
     }
 
     // used in running unit tests

Modified: pypy/dist/pypy/translator/jvm/test/test_string.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/test/test_string.py	(original)
+++ pypy/dist/pypy/translator/jvm/test/test_string.py	Tue Nov 13 11:01:21 2007
@@ -24,3 +24,10 @@
         # but we don't bother to make runtest understand how to parse that,
         # so we just skip the test.
         py.test.skip("test fails in JVM specific way")
+
+    def test_string_constant(self):
+        const = ''.join(map(chr, range(0, 256)))
+        def fn():
+            return const
+        res = self.interpret(fn, [])
+        assert res == const

Modified: pypy/dist/pypy/translator/jvm/test/test_unicode.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/test/test_unicode.py	(original)
+++ pypy/dist/pypy/translator/jvm/test/test_unicode.py	Tue Nov 13 11:01:21 2007
@@ -9,12 +9,24 @@
     EMPTY_STRING_HASH = 0
 
     def test_unichar_const(self):
+        def fn():
+            return u'\u03b1'
+        assert self.interpret(fn, []) == u'\u03b1'
+
+    def test_unichar_eq(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
-    test_char_unichar_eq = test_unichar_const
-    test_char_unichar_eq_2 = test_unichar_const
+    test_unichar_ord = test_unichar_eq
+    test_unichar_hash = test_unichar_eq
+    test_char_unichar_eq = test_unichar_eq
+    test_char_unichar_eq_2 = test_unichar_eq
 
     def test_getitem_exc(self):
         py.test.skip('fixme!')
+
+    def test_unicode_constant(self):
+        const = u''.join(map(unichr, range(0, 256)))
+        const = const + u'\ufffd'
+        def fn():
+            return const
+        res = self.interpret(fn, [])
+        assert res == const



More information about the Pypy-commit mailing list