summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Paul Calderone <exarkun@twistedmatrix.com>2014-04-19 18:33:23 -0400
committerJean-Paul Calderone <exarkun@twistedmatrix.com>2014-04-19 18:33:23 -0400
commitbdb3986b386d150ff60ad2ee14a78503affe845d (patch)
tree458ee150397525f4e0704ceb00d2f31eef563100
parent5a82db9e1d9215e4fc1308e9f5631f8c2ceb5170 (diff)
parent963153810ae9ef70c1838a4626f00cf2338c96d1 (diff)
downloadpyopenssl-bdb3986b386d150ff60ad2ee14a78503affe845d.tar.gz
Merge branch 'master' into ecdhe
-rw-r--r--.gitignore6
-rw-r--r--ChangeLog5
-rw-r--r--OpenSSL/SSL.py18
-rw-r--r--OpenSSL/test/test_ssl.py32
4 files changed, 56 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 867434b..7b60bb0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,7 @@
build
dist
-*.egg-info \ No newline at end of file
+*.egg-info
+*.pyc
+*.pyo
+__pycache__
+.tox
diff --git a/ChangeLog b/ChangeLog
index 164c9ad..53fb1df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-04-19 Markus Unterwaditzer <markus@unterwaditzer.net>
+
+ * OpenSSL/SSL.py: ``Connection.send`` and ``Connection.sendall``
+ now also accept the ``buffer`` type as data.
+
2014-03-30 Fedor Brunner <fedor.brunner@azet.sk>
* OpenSSL/SSL.py: Add ``get_finished``, ``get_peer_finished``
diff --git a/OpenSSL/SSL.py b/OpenSSL/SSL.py
index ec49c27..58553d6 100644
--- a/OpenSSL/SSL.py
+++ b/OpenSSL/SSL.py
@@ -24,6 +24,12 @@ except NameError:
class _memoryview(object):
pass
+try:
+ _buffer = buffer
+except NameError:
+ class _buffer(object):
+ pass
+
OPENSSL_VERSION_NUMBER = _lib.OPENSSL_VERSION_NUMBER
SSLEAY_VERSION = _lib.SSLEAY_VERSION
SSLEAY_CFLAGS = _lib.SSLEAY_CFLAGS
@@ -952,15 +958,17 @@ class Connection(object):
WantWrite or WantX509Lookup exceptions on this, you have to call the
method again with the SAME buffer.
- :param buf: The string to send
+ :param buf: The string, buffer or memoryview to send
:param flags: (optional) Included for compatibility with the socket
API, the value is ignored
:return: The number of bytes written
"""
if isinstance(buf, _memoryview):
buf = buf.tobytes()
+ if isinstance(buf, _buffer):
+ buf = str(buf)
if not isinstance(buf, bytes):
- raise TypeError("data must be a byte string")
+ raise TypeError("data must be a memoryview, buffer or byte string")
result = _lib.SSL_write(self._ssl, buf, len(buf))
self._raise_ssl_error(self._ssl, result)
@@ -974,15 +982,17 @@ class Connection(object):
all data is sent. If an error occurs, it's impossible to tell how much
data has been sent.
- :param buf: The string to send
+ :param buf: The string, buffer or memoryview to send
:param flags: (optional) Included for compatibility with the socket
API, the value is ignored
:return: The number of bytes written
"""
if isinstance(buf, _memoryview):
buf = buf.tobytes()
+ if isinstance(buf, _buffer):
+ buf = str(buf)
if not isinstance(buf, bytes):
- raise TypeError("buf must be a byte string")
+ raise TypeError("buf must be a memoryview, buffer or byte string")
left_to_send = len(buf)
total_sent = 0
diff --git a/OpenSSL/test/test_ssl.py b/OpenSSL/test/test_ssl.py
index 4c3b12a..5373510 100644
--- a/OpenSSL/test/test_ssl.py
+++ b/OpenSSL/test/test_ssl.py
@@ -2150,6 +2150,23 @@ class ConnectionSendTests(TestCase, _LoopbackMixin):
self.assertEquals(client.recv(2), b('xy'))
+ try:
+ buffer
+ except NameError:
+ "cannot test sending buffer without buffer"
+ else:
+ def test_short_buffer(self):
+ """
+ When passed a buffer containing a small number of bytes,
+ :py:obj:`Connection.send` transmits all of them and returns the number of
+ bytes sent.
+ """
+ server, client = self._loopback()
+ count = server.send(buffer(b('xy')))
+ self.assertEquals(count, 2)
+ self.assertEquals(client.recv(2), b('xy'))
+
+
class ConnectionSendallTests(TestCase, _LoopbackMixin):
"""
@@ -2193,6 +2210,21 @@ class ConnectionSendallTests(TestCase, _LoopbackMixin):
self.assertEquals(client.recv(1), b('x'))
+ try:
+ buffer
+ except NameError:
+ "cannot test sending buffers without buffers"
+ else:
+ def test_short_buffers(self):
+ """
+ When passed a buffer containing a small number of bytes,
+ :py:obj:`Connection.sendall` transmits all of them.
+ """
+ server, client = self._loopback()
+ server.sendall(buffer(b('x')))
+ self.assertEquals(client.recv(1), b('x'))
+
+
def test_long(self):
"""
:py:obj:`Connection.sendall` transmits all of the bytes in the string passed to