[pypy-commit] pypy py3.5-ssl: revert _str_to_ffi_buffer back to parts of it's original impl. str and memoryview are special cased!

plan_rich pypy.commits at gmail.com
Mon Nov 28 10:52:18 EST 2016


Author: Richard Plangger <planrichi at gmail.com>
Branch: py3.5-ssl
Changeset: r88704:e44522edcb3a
Date: 2016-11-28 16:51 +0100
http://bitbucket.org/pypy/pypy/changeset/e44522edcb3a/

Log:	revert _str_to_ffi_buffer back to parts of it's original impl. str
	and memoryview are special cased!

diff --git a/lib_pypy/_cffi_ssl/_stdssl/utility.py b/lib_pypy/_cffi_ssl/_stdssl/utility.py
--- a/lib_pypy/_cffi_ssl/_stdssl/utility.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/utility.py
@@ -14,14 +14,15 @@
     return ffi.buffer(char_ptr, length)[:]
 
 def _str_to_ffi_buffer(view):
-    # XXX incomplete and does not work if e.g. view in (True, 0, 1, ...)
-    try:
-        buf = ffi.from_buffer(view)
-        return buf
-    except TypeError:
-        if isinstance(view, str):
-            return ffi.from_buffer(bytes(view, 'utf-8'))
-        return ffi.from_buffer(bytes(view))
+    if isinstance(view, str):
+        return ffi.from_buffer(view.encode())
+    elif isinstance(view, memoryview):
+        # NOTE pypy limitation StringBuffer does not allow
+        # to get a raw address to the string!
+        view = bytes(view)
+    # dont call call ffi.from_buffer(bytes(view)), arguments
+    # like ints/bools should result in a TypeError
+    return ffi.from_buffer(view)
 
 def _str_from_buf(buf):
     return ffi.string(buf).decode('utf-8')


More information about the pypy-commit mailing list