Author: fijal
Branch: unicode-utf8
Changeset: r93158:467a32f09dd6
Date: 2017-11-24 11:16 +0100
http://bitbucket.org/pypy/pypy/changeset/467a32f09dd6/

Log:    start fixing _rawffi

diff --git a/pypy/module/_rawffi/alt/interp_funcptr.py 
b/pypy/module/_rawffi/alt/interp_funcptr.py
--- a/pypy/module/_rawffi/alt/interp_funcptr.py
+++ b/pypy/module/_rawffi/alt/interp_funcptr.py
@@ -167,8 +167,8 @@
         addr = rffi.cast(rffi.ULONG, buf)
         self.argchain.arg(addr)
 
-    def handle_unichar_p(self, w_ffitype, w_obj, unicodeval):
-        buf = rffi.unicode2wcharp(unicodeval)
+    def handle_unichar_p(self, w_ffitype, w_obj, utf8val, utf8len):
+        buf = rffi.utf82wcharp(utf8val, utf8len)
         self.w_func.to_free.append(rffi.cast(rffi.VOIDP, buf))
         addr = rffi.cast(rffi.ULONG, buf)
         self.argchain.arg(addr)
diff --git a/pypy/module/_rawffi/alt/test/test_type_converter.py 
b/pypy/module/_rawffi/alt/test/test_type_converter.py
--- a/pypy/module/_rawffi/alt/test/test_type_converter.py
+++ b/pypy/module/_rawffi/alt/test/test_type_converter.py
@@ -6,7 +6,7 @@
 
 class DummyFromAppLevelConverter(FromAppLevelConverter):
 
-    def handle_all(self, w_ffitype, w_obj, val):
+    def handle_all(self, w_ffitype, w_obj, val, lgt=None):
         self.lastval = val
 
     handle_signed = handle_all
@@ -120,8 +120,8 @@
     def test_strings(self):
         # first, try automatic conversion from applevel
         self.check(app_types.char_p, self.space.newbytes('foo'), 'foo')
-        self.check(app_types.unichar_p, self.space.wrap(u'foo\u1234'), 
u'foo\u1234')
-        self.check(app_types.unichar_p, self.space.wrap('foo'), u'foo')
+        self.check(app_types.unichar_p, self.space.wrap(u'foo\u1234'), 
u'foo\u1234'.encode('utf8'))
+        self.check(app_types.unichar_p, self.space.wrap('foo'), 'foo')
         # then, try to pass explicit pointers
         self.check(app_types.char_p, self.space.wrap(42), 42)
         self.check(app_types.unichar_p, self.space.wrap(42), 42)
diff --git a/pypy/module/_rawffi/alt/type_converter.py 
b/pypy/module/_rawffi/alt/type_converter.py
--- a/pypy/module/_rawffi/alt/type_converter.py
+++ b/pypy/module/_rawffi/alt/type_converter.py
@@ -1,6 +1,6 @@
 from rpython.rlib import libffi
-from rpython.rlib import jit
-from rpython.rlib.rarithmetic import r_uint
+from rpython.rlib import jit, rutf8
+from rpython.rlib.rarithmetic import r_uint, intmask
 from pypy.interpreter.error import oefmt
 from pypy.module._rawffi.structure import W_StructureInstance, W_Structure
 from pypy.module._rawffi.alt.interp_ffitype import app_types
@@ -85,8 +85,8 @@
             return True
         elif w_ffitype.is_unichar_p() and (w_type is self.space.w_bytes or
                                            w_type is self.space.w_unicode):
-            unicodeval = self.space.unicode_w(w_obj)
-            self.handle_unichar_p(w_ffitype, w_obj, unicodeval)
+            utf8, lgt = self.space.utf8_len_w(w_obj)
+            self.handle_unichar_p(w_ffitype, w_obj, utf8, lgt)
             return True
         return False
 
@@ -147,7 +147,7 @@
         """
         self.error(w_ffitype, w_obj)
 
-    def handle_unichar_p(self, w_ffitype, w_obj, unicodeval):
+    def handle_unichar_p(self, w_ffitype, w_obj, utf8val, utf8len):
         """
         unicodeval: interp-level unicode
         """
@@ -228,7 +228,8 @@
             return space.newbytes(chr(ucharval))
         elif w_ffitype.is_unichar():
             wcharval = self.get_unichar(w_ffitype)
-            return space.newunicode(unichr(wcharval))
+            return space.newutf8(rutf8.unichr_as_utf8(wcharval), 1,
+                                 rutf8.get_flag_from_code(intmask(wcharval)))
         elif w_ffitype.is_double():
             return self._float(w_ffitype)
         elif w_ffitype.is_singlefloat():
diff --git a/pypy/module/_rawffi/interp_rawffi.py 
b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -10,6 +10,7 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rtyper.tool import rffi_platform
 from rpython.rlib.unroll import unrolling_iterable
+from rpython.rlib import rutf8
 from rpython.rlib.objectmodel import specialize
 import rpython.rlib.rposix as rposix
 
@@ -416,13 +417,13 @@
         val = s[0]
         push_func(add_arg, argdesc, val)
     elif letter == 'u':
-        s = space.unicode_w(w_arg)
-        if len(s) != 1:
+        s, lgt = space.utf8_len_w(w_arg)
+        if lgt != 1:
             raise oefmt(space.w_TypeError,
                         "Expected unicode string of length one as wide "
                         "character")
-        val = s[0]
-        push_func(add_arg, argdesc, val)
+        val = rutf8.codepoint_at_pos(s, 0)
+        push_func(add_arg, argdesc, rffi.cast(rffi.WCHAR_T, val))
     else:
         for c in unroll_letters_for_numbers:
             if letter == c:
diff --git a/rpython/rtyper/lltypesystem/rffi.py 
b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -1024,13 +1024,14 @@
 def utf82wcharp(utf8, utf8len):
     from rpython.rlib import rutf8
 
-    w = lltype.malloc(CWCHARP.TO, utf8len, flavor='raw')
+    w = lltype.malloc(CWCHARP.TO, utf8len + 1, flavor='raw')
     i = 0
     index = 0
     while i < len(utf8):
         w[index] = unichr(rutf8.codepoint_at_pos(utf8, i))
         i = rutf8.next_codepoint_pos(utf8, i)
         index += 1
+    w[index] = unichr(0)
     return w
 
 # char**
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to