From 0a131d6dec724e934ef1db8977a9ac04cb2848aa Mon Sep 17 00:00:00 2001
From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl>
Date: Tue, 10 Jan 2017 05:50:13 +0100
Subject: [PATCH] memoryview madness

---
 coolamqp/attaches/publisher.py                | 13 +++----------
 .../compilation/compile_definitions.py        | 10 ++++++++++
 coolamqp/framing/definitions.py               | 11 ++++++++++-
 coolamqp/framing/field_table.py               |  6 ++++--
 coolamqp/uplink/connection/recv_framer.py     | 19 +++++++------------
 coolamqp/uplink/handshake.py                  |  4 ++--
 coolamqp/uplink/listener/socket.py            |  5 +----
 .../test_definitions/test_frames.py           |  5 +----
 tests/test_framing/test_field_table.py        |  4 ++--
 9 files changed, 40 insertions(+), 37 deletions(-)

diff --git a/coolamqp/attaches/publisher.py b/coolamqp/attaches/publisher.py
index b5fa4df..b8e3136 100644
--- a/coolamqp/attaches/publisher.py
+++ b/coolamqp/attaches/publisher.py
@@ -115,18 +115,11 @@ class Publisher(Channeler, Synchronized):
         # Break down large bodies
         bodies = []
 
-        if six.PY3:     # memoryview
-            buffer = memoryview
-
-        body = buffer(message.body)
+        body = memoryview(message.body)
         max_body_size = self.connection.frame_max - AMQPBodyFrame.FRAME_SIZE_WITHOUT_PAYLOAD
         while len(body) > 0:
-            if six.PY3:
-                bodies.append(body[:max_body_size])
-                body = body[max_body_size:]
-            else:
-                bodies.append(buffer(body, 0, max_body_size))
-                body = buffer(body, max_body_size)
+            bodies.append(body[:max_body_size])
+            body = body[max_body_size:]
 
 
         self.connection.send([
diff --git a/coolamqp/framing/compilation/compile_definitions.py b/coolamqp/framing/compilation/compile_definitions.py
index ec0022e..76558ac 100644
--- a/coolamqp/framing/compilation/compile_definitions.py
+++ b/coolamqp/framing/compilation/compile_definitions.py
@@ -40,6 +40,15 @@ See coolamqp.uplink.framing.compilation for the tool
 
 AMQP is copyright (c) 2016 OASIS
 CoolAMQP is copyright (c) 2016 DMS Serwis s.c.
+
+
+###########################################
+# IMPORTANT NOTE
+# Type of field may depend on the origin of packet.
+# strings will be memoryviews if we received the packet
+# while they may be bytes if we created it
+#
+# this has some use - speed :D
 """
 
 import struct, collections, warnings, logging, six
@@ -415,6 +424,7 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved
 ''')
 
             line(get_from_buffer(method.fields, '', 2, remark=(method.name == 'deliver')))
+
             line("        return %s(%s)",
                  full_class_name,
                  u', '.join(format_field_name(field.name) for field in method.fields if not field.reserved))
diff --git a/coolamqp/framing/definitions.py b/coolamqp/framing/definitions.py
index 83251c5..55b348e 100644
--- a/coolamqp/framing/definitions.py
+++ b/coolamqp/framing/definitions.py
@@ -8,6 +8,15 @@ See coolamqp.uplink.framing.compilation for the tool
 
 AMQP is copyright (c) 2016 OASIS
 CoolAMQP is copyright (c) 2016 DMS Serwis s.c.
+
+
+###########################################
+# IMPORTANT NOTE
+# Type of field may depend on the origin of packet.
+# strings will be memoryviews if we received the packet
+# while they may be bytes if we created it
+#
+# this has some use - speed :D
 """
 
 import struct, collections, warnings, logging, six
@@ -2369,7 +2378,7 @@ class BasicContentPropertyList(AMQPContentPropertyList):
         pfl = 2
         while ord(buf[offset + pfl - 1]) & 1:
             pfl += 2
-        zpf = BasicContentPropertyList.zero_property_flags(buf[offset:offset+pfl])
+        zpf = BasicContentPropertyList.zero_property_flags(buf[offset:offset+pfl]).tobytes()
         if zpf in BasicContentPropertyList.PARTICULAR_CLASSES:
             return BasicContentPropertyList.PARTICULAR_CLASSES[zpf].from_buffer(buf, offset)
         else:
diff --git a/coolamqp/framing/field_table.py b/coolamqp/framing/field_table.py
index b53faa4..db25625 100644
--- a/coolamqp/framing/field_table.py
+++ b/coolamqp/framing/field_table.py
@@ -8,6 +8,8 @@ An array is of form [field-value1, field-value2, ...]
 
 A table is of form ( (name1::bytes, fv1), (name2::bytes, fv2), ...)
 
+
+NOTE: it's not buffers, it's memoryview all along
 """
 from __future__ import absolute_import, division, print_function
 import struct
@@ -31,7 +33,7 @@ def deframe_decimal(buf, offset):
 
 def deframe_shortstr(buf, offset):      # -> value, bytes_eaten
     ln, = struct.unpack_from('!B', buf, offset)
-    return buf[offset+1:offset+1+ln], 1+ln
+    return buf[offset+1:offset+1+ln].tobytes(), 1+ln
 
 
 def enframe_shortstr(buf, value):
@@ -41,7 +43,7 @@ def enframe_shortstr(buf, value):
 
 def deframe_longstr(buf, offset):  # -> value, bytes_eaten
     ln, = struct.unpack_from('!I', buf, offset)
-    return buf[offset+4:offset+4+ln], 4 + ln
+    return buf[offset+4:offset+4+ln].tobytes(), 4 + ln
 
 
 def enframe_longstr(buf, value):
diff --git a/coolamqp/uplink/connection/recv_framer.py b/coolamqp/uplink/connection/recv_framer.py
index 7caf9d6..4e80955 100644
--- a/coolamqp/uplink/connection/recv_framer.py
+++ b/coolamqp/uplink/connection/recv_framer.py
@@ -55,9 +55,7 @@ class ReceivingFramer(object):
         :param data: received data
         """
         self.total_data_len += len(data)
-        if six.PY3:
-            buffer = memoryview
-        self.chunks.append(buffer(data))
+        self.chunks.append(memoryview(data))
 
         while self._statemachine():
             pass
@@ -67,14 +65,11 @@ class ReceivingFramer(object):
         if up_to >= len(self.chunks[0]):
             q =  self.chunks.popleft()
         else:
-            if six.PY3:
-                q = self.chunks[0][:up_to]
-                self.chunks[0] = self.chunks[0][up_to:]
-            else:
-                q = buffer(self.chunks[0], 0, up_to)
-                self.chunks[0] = buffer(self.chunks[0], up_to)
+            q = self.chunks[0][:up_to]
+            self.chunks[0] = self.chunks[0][up_to:]
 
         self.total_data_len -= len(q)
+        print(repr(q))
         return q
 
     def _statemachine(self):
@@ -91,7 +86,7 @@ class ReceivingFramer(object):
         elif (self.frame_type == FRAME_HEARTBEAT) and (self.total_data_len >= AMQPHeartbeatFrame.LENGTH-1):
             data = b''
             while len(data) < AMQPHeartbeatFrame.LENGTH-1:
-                data = data + six.binary_type(self._extract(AMQPHeartbeatFrame.LENGTH-1 - len(data)))
+                data = data + self._extract(AMQPHeartbeatFrame.LENGTH-1 - len(data)).tobytes()
 
             if data != AMQPHeartbeatFrame.DATA[1:]:
                 # Invalid heartbeat frame!
@@ -106,9 +101,9 @@ class ReceivingFramer(object):
         elif (self.frame_type != FRAME_HEARTBEAT) and (self.frame_type is not None) and (self.frame_size is None) and (self.total_data_len > 6):
             hdr = b''
             while len(hdr) < 6:
-                hdr = hdr + six.binary_type(self._extract(6 - len(hdr)))
+                hdr = hdr + self._extract(6 - len(hdr)).tobytes()
 
-            self.frame_channel, self.frame_size = struct.unpack('!HI', hdr)
+            self.frame_channel, self.frame_size = struct.unpack('!HI',hdr)
 
             return True
 
diff --git a/coolamqp/uplink/handshake.py b/coolamqp/uplink/handshake.py
index 18edb52..adcb938 100644
--- a/coolamqp/uplink/handshake.py
+++ b/coolamqp/uplink/handshake.py
@@ -66,8 +66,8 @@ class Handshaker(object):
 
     def on_connection_start(self, payload):
 
-        sasl_mechanisms = payload.mechanisms.split(b' ')
-        locale_supported = payload.locales.split(b' ')
+        sasl_mechanisms = payload.mechanisms.tobytes().split(b' ')
+        locale_supported = payload.locales.tobytes().split(b' ')
 
         # Select a mechanism
         if b'PLAIN' not in sasl_mechanisms:
diff --git a/coolamqp/uplink/listener/socket.py b/coolamqp/uplink/listener/socket.py
index 8fcc0bc..9684243 100644
--- a/coolamqp/uplink/listener/socket.py
+++ b/coolamqp/uplink/listener/socket.py
@@ -126,10 +126,7 @@ class BaseSocket(object):
 
             if sent < len(self.data_to_send[0]):
                 # Not everything could be sent
-                if six.PY3:
-                    self.data_to_send[0] = self.data_to_send[0][sent:]
-                else:
-                    self.data_to_send[0] = buffer(self.data_to_send[0], sent)
+                self.data_to_send[0] = self.data_to_send[0][sent:]
                 return False
             else:
                 # Looks like everything has been sent
diff --git a/tests/test_framing/test_definitions/test_frames.py b/tests/test_framing/test_definitions/test_frames.py
index bb1f6e5..295771c 100644
--- a/tests/test_framing/test_definitions/test_frames.py
+++ b/tests/test_framing/test_definitions/test_frames.py
@@ -8,9 +8,6 @@ from coolamqp.framing.frames import AMQPHeaderFrame
 from coolamqp.framing.definitions import BasicContentPropertyList, FRAME_HEADER, FRAME_END, ConnectionStartOk
 
 
-if six.PY3:
-    buffer = memoryview
-
 class TestShitSerializesRight(unittest.TestCase):
 
     def test_unser_header_frame(self):
@@ -18,7 +15,7 @@ class TestShitSerializesRight(unittest.TestCase):
             b'\x00\x00\x00\x00\x00\x00\x00\x0A' + \
             b'\xC0\x00\x0Atext/plain\x04utf8'
 
-        hf = AMQPHeaderFrame.unserialize(0, buffer(s))
+        hf = AMQPHeaderFrame.unserialize(0, memoryview(s))
 
         self.assertEquals(hf.class_id, 60)
         self.assertEquals(hf.weight, 0)
diff --git a/tests/test_framing/test_field_table.py b/tests/test_framing/test_field_table.py
index 1ffecd2..af5082f 100644
--- a/tests/test_framing/test_field_table.py
+++ b/tests/test_framing/test_field_table.py
@@ -28,7 +28,7 @@ class TestFramingTables(unittest.TestCase):
 
         self.assertEquals(buf, struct.pack('!I', 10) + b'\x05fields\x02yo')
 
-        tab, delta = deframe_table(buffer(buf), 0)
+        tab, delta = deframe_table(memoryview(buf), 0)
 
         self.assertEquals(len(tab), 1)
         self.assertEquals(delta, 14)
@@ -43,7 +43,7 @@ class TestFramingTables(unittest.TestCase):
         buf = buf.getvalue()
         self.assertEquals(b's\x02yo', buf)
 
-        fv, delta = deframe_field_value(buffer(buf), 0)
+        fv, delta = deframe_field_value(memoryview(buf), 0)
         self.assertEquals(fv, (b'yo', b's'))
         self.assertEquals(delta, 4)
 
-- 
GitLab