From 079c963ddd4ebfd13a905829bc341dce85d94fbd Mon Sep 17 00:00:00 2001 From: Daniel Holth Date: Sun, 17 Nov 2019 22:45:52 -0500 Subject: use _ffi.from_buffer() to support bytearray (#852) * use _ffi.from_buffer(buf) in send, to support bytearray * add bytearray test * update CHANGELOG.rst * move from_buffer before 'buffer too long' check * context-managed from_buffer + black * don't shadow buf in send() * test return count for sendall * test sending an array * fix test * also use from_buffer in bio_write * de-format _util.py * formatting * add simple bio_write tests * wrap line --- src/OpenSSL/_util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/OpenSSL/_util.py') diff --git a/src/OpenSSL/_util.py b/src/OpenSSL/_util.py index cdcacc8..d8e3f66 100644 --- a/src/OpenSSL/_util.py +++ b/src/OpenSSL/_util.py @@ -145,3 +145,17 @@ def text_to_bytes_and_warn(label, obj): ) return obj.encode('utf-8') return obj + + +try: + # newer versions of cffi free the buffer deterministically + with ffi.from_buffer(b""): + pass + from_buffer = ffi.from_buffer +except AttributeError: + # cffi < 0.12 frees the buffer with refcounting gc + from contextlib import contextmanager + + @contextmanager + def from_buffer(*args): + yield ffi.from_buffer(*args) -- cgit v1.2.1