[pypy-commit] pypy py3.5-ssl: it is ffi.gc, not lib.gc, use ffi.new("type[]", count) instead of ffi.new("type[%d]"%count), special case _str_to_ffi_buffer

plan_rich pypy.commits at gmail.com
Mon Nov 28 10:23:25 EST 2016


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

Log:	it is ffi.gc, not lib.gc, use ffi.new("type[]", count) instead of
	ffi.new("type[%d]"%count), special case _str_to_ffi_buffer

diff --git a/lib_pypy/_cffi_ssl/_stdssl/__init__.py b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
--- a/lib_pypy/_cffi_ssl/_stdssl/__init__.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
@@ -203,9 +203,6 @@
 SOCKET_TOO_LARGE_FOR_SELECT = 4
 SOCKET_OPERATION_OK = 5
 
-def _buffer_new(length):
-    return ffi.new("char[%d]"%length)
-
 class _SSLSocket(object):
 
     @staticmethod
@@ -218,7 +215,7 @@
 
         lib.ERR_get_state()
         lib.ERR_clear_error()
-        self.ssl = ssl = lib.gc(lib.SSL_new(ctx), lib.SSL_free)
+        self.ssl = ssl = ffi.gc(lib.SSL_new(ctx), lib.SSL_free)
 
         self._app_data_handle = ffi.new_handle(self)
         lib.SSL_set_app_data(ssl, ffi.cast("char*", self._app_data_handle))
@@ -347,7 +344,7 @@
 
         peer_cert = lib.SSL_get_peer_certificate(ssl)
         if peer_cert != ffi.NULL:
-            peer_cert = lib.gc(peer_cert, lib.X509_free)
+            peer_cert = ffi.gc(peer_cert, lib.X509_free)
         self.peer_cert = peer_cert
 
         #PySSL_END_ALLOW_THREADS
@@ -437,7 +434,7 @@
         sock = self.get_socket_or_connection_gone()
 
         if not buffer_into:
-            dest = _buffer_new(length)
+            dest = ffi.new("char[]", length)
             mem = dest
         else:
             mem = ffi.from_buffer(buffer_into)
@@ -654,7 +651,7 @@
             return count
 
     def tls_unique_cb(self):
-        buf = ffi.new("char[%d]" % SSL_CB_MAXLEN)
+        buf = ffi.new("char[]", SSL_CB_MAXLEN)
 
         if lib.SSL_session_reused(self.ssl) ^ (not self.socket_type):
             # if session is resumed XOR we are the client
@@ -1259,7 +1256,7 @@
         raise ValueError("Unknown object")
     sn = _str_from_buf(lib.OBJ_nid2sn(nid))
     ln = _str_from_buf(lib.OBJ_nid2ln(nid))
-    buf = ffi.new("char[255]")
+    buf = ffi.new("char[]", 255)
     length = lib.OBJ_obj2txt(buf, len(buf), obj, 1)
     if length < 0:
         ssl_error(None)
@@ -1301,7 +1298,7 @@
         lib.BIO_set_retry_read(bio);
         lib.BIO_set_mem_eof_return(bio, -1);
 
-        self.bio = lib.gc(bio, lib.BIO_free)
+        self.bio = ffi.gc(bio, lib.BIO_free)
         self.eof_written = False
 
     @property
@@ -1339,7 +1336,7 @@
         if count < 0 or count > avail:
             count = avail;
 
-        buf = ffi.new("char[%d]" % count)
+        buf = ffi.new("char[]", count)
 
         nbytes = lib.BIO_read(self.bio, buf, count);
         #  There should never be any short reads but check anyway.
@@ -1358,7 +1355,7 @@
 def _RAND_bytes(count, pseudo):
     if count < 0:
         raise ValueError("num must be positive")
-    buf = ffi.new("unsigned char[%d]" % count)
+    buf = ffi.new("unsigned char[]", count)
     if pseudo:
         # note by reaperhulk, RAND_pseudo_bytes is deprecated in 3.6 already,
         # it is totally fine to just call RAND_bytes instead
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,19 @@
     return ffi.buffer(char_ptr, length)[:]
 
 def _str_to_ffi_buffer(view, zeroterm=False):
+    # XXX incomplete and does not work if e.g. view in (True, 0, 1, ...)
     if zeroterm:
         # only two cases use zeroterm=True, those are rather 'short' strings
+        if isinstance(view, str):
+            return ffi.from_buffer(bytes(view+'\x00', 'utf-8'))
         return ffi.from_buffer(bytes(view)+b'\x00')
 
     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))
 
 def _str_from_buf(buf):


More information about the pypy-commit mailing list