[pypy-commit] pypy py3.5: hopefully fix the str/bytes mismatches on py3.5

wlav pypy.commits at gmail.com
Thu Mar 2 18:11:40 EST 2017


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: py3.5
Changeset: r90503:6c8f0848fcf2
Date: 2017-03-02 15:00 -0800
http://bitbucket.org/pypy/pypy/changeset/6c8f0848fcf2/

Log:	hopefully fix the str/bytes mismatches on py3.5

diff --git a/pypy/module/cppyy/capi/loadable_capi.py b/pypy/module/cppyy/capi/loadable_capi.py
--- a/pypy/module/cppyy/capi/loadable_capi.py
+++ b/pypy/module/cppyy/capi/loadable_capi.py
@@ -355,7 +355,7 @@
     return rffi.cast(rffi.UCHAR, space.c_uint_w(call_capi(space, 'call_b', args)))
 def c_call_c(space, cppmethod, cppobject, nargs, cargs):
     args = [_ArgH(cppmethod), _ArgH(cppobject), _ArgL(nargs), _ArgP(cargs)]
-    return rffi.cast(rffi.CHAR, space.text_w(call_capi(space, 'call_c', args))[0])
+    return rffi.cast(rffi.CHAR, space.bytes_w(call_capi(space, 'call_c', args))[0])
 def c_call_h(space, cppmethod, cppobject, nargs, cargs):
     args = [_ArgH(cppmethod), _ArgH(cppobject), _ArgL(nargs), _ArgP(cargs)]
     return rffi.cast(rffi.SHORT, space.int_w(call_capi(space, 'call_h', args)))
diff --git a/pypy/module/cppyy/executor.py b/pypy/module/cppyy/executor.py
--- a/pypy/module/cppyy/executor.py
+++ b/pypy/module/cppyy/executor.py
@@ -132,7 +132,11 @@
         if ccpresult == rffi.cast(rffi.CCHARP, 0):
             return space.newbytes("")
         result = rffi.charp2str(ccpresult)   # TODO: make it a choice to free
-        return space.newbytes(result)
+        # debatable whether this should be newtext or newbytes; they are bytes
+        # but will be more likely used as text after binding ... probably need
+        # to make this configurable on a per-function bases (same as the old
+        # char* v.s. byte* problem)
+        return space.newtext(result)
 
 
 class ConstructorExecutor(FunctionExecutor):
diff --git a/pypy/module/cppyy/ffitypes.py b/pypy/module/cppyy/ffitypes.py
--- a/pypy/module/cppyy/ffitypes.py
+++ b/pypy/module/cppyy/ffitypes.py
@@ -81,13 +81,15 @@
                 raise oefmt(space.w_ValueError, "char arg not in range(256)")
 
             value = rffi.cast(rffi.CHAR, space.c_int_w(w_value))
+        elif space.isinstance_w(w_value, space.w_text):
+            value = space.text_w(w_value)
         else:
             value = space.bytes_w(w_value)
 
         if len(value) != 1:  
             raise oefmt(space.w_ValueError,
                         "char expected, got string of size %d", len(value))
-        return value[0] # turn it into a "char" to the annotator
+        return rffi.cast(rffi.CHAR, value[0]) # help the annotator
 
     def cffi_type(self, space):
         state = space.fromcache(State)
diff --git a/pypy/module/cppyy/test/test_datatypes.py b/pypy/module/cppyy/test/test_datatypes.py
--- a/pypy/module/cppyy/test/test_datatypes.py
+++ b/pypy/module/cppyy/test/test_datatypes.py
@@ -38,9 +38,9 @@
         assert not c.get_bool(); assert not c.get_bool_cr(); assert not c.get_bool_r()
 
         # reading char types
-        assert c.m_char  == 'a'
-        assert c.m_schar == 'b'
-        assert c.m_uchar == 'c'
+        assert c.m_char  == b'a'
+        assert c.m_schar == b'b'
+        assert c.m_uchar == b'c'
 
         # reading integer types
         assert c.m_short   == -11; assert c.get_short_cr()   == -11; assert c.get_short_r()   == -11
@@ -133,18 +133,20 @@
         raises(ValueError, 'c.set_bool(10)')
 
         # char types through functions
-        c.set_char('c');   assert c.get_char()  == 'c'
-        c.set_uchar('e');  assert c.get_uchar() == 'e'
+        c.set_char('c');   assert c.get_char()  == b'c'
+        c.set_char(b'b');  assert c.get_char()  == b'b'
+        c.set_uchar('e');  assert c.get_uchar() == b'e'
+        c.set_uchar(b'd'); assert c.get_uchar() == b'd'
 
         # char types through data members
-        c.m_char = 'b';    assert c.get_char()  ==     'b'
-        c.m_char = 40;     assert c.get_char()  == chr(40)
-        c.set_char('c');   assert c.m_char      ==     'c'
-        c.set_char(41);    assert c.m_char      == chr(41)
-        c.m_uchar = 'd';   assert c.get_uchar() ==     'd'
-        c.m_uchar = 42;    assert c.get_uchar() == chr(42)
-        c.set_uchar('e');  assert c.m_uchar     ==     'e'
-        c.set_uchar(43);   assert c.m_uchar     == chr(43)
+        c.m_char = 'b';    assert c.get_char()  == b'b'
+        c.m_char = 40;     assert c.get_char()  == str.encode(chr(40))
+        c.set_char('c');   assert c.m_char      == b'c'
+        c.set_char(41);    assert c.m_char      == str.encode(chr(41))
+        c.m_uchar = 'd';   assert c.get_uchar() == b'd'
+        c.m_uchar = 42;    assert c.get_uchar() == str.encode(chr(42))
+        c.set_uchar('e');  assert c.m_uchar     == b'e'
+        c.set_uchar(43);   assert c.m_uchar     == str.encode(chr(43))
 
         raises(ValueError, 'c.set_char("string")')
         raises(ValueError, 'c.set_char(500)')
@@ -246,10 +248,10 @@
         assert isinstance(c, CppyyTestData)
 
         # char types
-        assert CppyyTestData.s_char    == 'c'
-        assert c.s_char                == 'c'
-        assert c.s_uchar               == 'u'
-        assert CppyyTestData.s_uchar   == 'u'
+        assert CppyyTestData.s_char    == b'c'
+        assert c.s_char                == b'c'
+        assert c.s_uchar               == b'u'
+        assert CppyyTestData.s_uchar   == b'u'
 
         # integer types
         assert CppyyTestData.s_short    == -101
@@ -259,15 +261,15 @@
         assert CppyyTestData.s_int      == -202
         assert c.s_int                  == -202
         assert c.s_uint                 ==  202
-        assert CppyyTestData.s_uint   ==  202
-        assert CppyyTestData.s_long   == -303
+        assert CppyyTestData.s_uint     ==  202
+        assert CppyyTestData.s_long     == -303
         assert c.s_long                 == -303
         assert c.s_ulong                ==  303
-        assert CppyyTestData.s_ulong  ==  303
-        assert CppyyTestData.s_llong  == -404
+        assert CppyyTestData.s_ulong    ==  303
+        assert CppyyTestData.s_llong    == -404
         assert c.s_llong                == -404
-        assert c.s_ullong               ==  505
-        assert CppyyTestData.s_ullong ==  505
+        assert c.s_ullong               ==  404
+        assert CppyyTestData.s_ullong   ==  404
 
         # floating point types
         assert round(CppyyTestData.s_float  + 606., 5)   == 0
@@ -287,57 +289,57 @@
         assert isinstance(c, CppyyTestData)
 
         # char types
-        CppyyTestData.s_char          = 'a'
-        assert c.s_char                == 'a'
-        c.s_char                        = 'b'
-        assert CppyyTestData.s_char  == 'b'
-        CppyyTestData.s_uchar         = 'c'
-        assert c.s_uchar               == 'c'
-        c.s_uchar                       = 'd'
-        assert CppyyTestData.s_uchar == 'd'
+        CppyyTestData.s_char           =  'a'
+        assert c.s_char               == b'a'
+        c.s_char                       =  'b'
+        assert CppyyTestData.s_char   == b'b'
+        CppyyTestData.s_uchar          =  'c'
+        assert c.s_uchar              == b'c'
+        c.s_uchar                      =  'd'
+        assert CppyyTestData.s_uchar  == b'd'
         raises(ValueError, setattr, CppyyTestData, 's_uchar', -1)
-        raises(ValueError, setattr, c,               's_uchar', -1)
+        raises(ValueError, setattr, c,             's_uchar', -1)
 
         # integer types
         c.s_short                        = -102
-        assert CppyyTestData.s_short  == -102
-        CppyyTestData.s_short          = -203
+        assert CppyyTestData.s_short    == -102
+        CppyyTestData.s_short            = -203
         assert c.s_short                == -203
         c.s_ushort                       =  127
-        assert CppyyTestData.s_ushort ==  127
-        CppyyTestData.s_ushort         =  227
+        assert CppyyTestData.s_ushort   ==  127
+        CppyyTestData.s_ushort           =  227
         assert c.s_ushort               ==  227
-        CppyyTestData.s_int            = -234
+        CppyyTestData.s_int              = -234
         assert c.s_int                  == -234
         c.s_int                          = -321
-        assert CppyyTestData.s_int    == -321
-        CppyyTestData.s_uint           = 1234
+        assert CppyyTestData.s_int      == -321
+        CppyyTestData.s_uint             = 1234
         assert c.s_uint                 == 1234
         c.s_uint                         = 4321
-        assert CppyyTestData.s_uint   == 4321
+        assert CppyyTestData.s_uint     == 4321
         raises(ValueError, setattr, c,               's_uint', -1)
         raises(ValueError, setattr, CppyyTestData, 's_uint', -1)
-        CppyyTestData.s_long           = -87
+        CppyyTestData.s_long             = -87
         assert c.s_long                 == -87
         c.s_long                         = 876
-        assert CppyyTestData.s_long   == 876
-        CppyyTestData.s_ulong          = 876
+        assert CppyyTestData.s_long     == 876
+        CppyyTestData.s_ulong            = 876
         assert c.s_ulong                == 876
         c.s_ulong                        = 678
-        assert CppyyTestData.s_ulong  == 678
+        assert CppyyTestData.s_ulong    == 678
         raises(ValueError, setattr, CppyyTestData, 's_ulong', -1)
-        raises(ValueError, setattr, c,               's_ulong', -1)
+        raises(ValueError, setattr, c,             's_ulong', -1)
 
         # floating point types
         CppyyTestData.s_float                    = -3.1415
-        assert round(c.s_float, 5 )               == -3.1415
-        c.s_float                                  =  3.1415
+        assert round(c.s_float, 5 )             == -3.1415
+        c.s_float                                =  3.1415
         assert round(CppyyTestData.s_float, 5 ) ==  3.1415
         import math
-        c.s_double                                 = -math.pi
+        c.s_double                               = -math.pi
         assert CppyyTestData.s_double           == -math.pi
         CppyyTestData.s_double                   =  math.pi
-        assert c.s_double                         ==  math.pi
+        assert c.s_double                       ==  math.pi
 
         c.destruct()
 


More information about the pypy-commit mailing list