From d2ae61f5d8abb2875aaeb31cc261760109f647b0 Mon Sep 17 00:00:00 2001 From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl> Date: Tue, 10 Jan 2017 05:36:28 +0100 Subject: [PATCH] memoryview madness --- .codeclimate.yml | 1 + coolamqp/attaches/publisher.py | 13 +++++++++++-- coolamqp/uplink/connection/recv_framer.py | 10 ++++++++-- coolamqp/uplink/listener/socket.py | 6 +++++- tests/test_framing/test_definitions/test_frames.py | 4 ++++ tests/test_framing/test_field_table.py | 4 ++++ 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index 249d03d..5c6e916 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -16,6 +16,7 @@ engines: exclude_paths: - examples/** - tests/** +- coolamqp/framing/definitions.py ratings: paths: - coolamqp/** diff --git a/coolamqp/attaches/publisher.py b/coolamqp/attaches/publisher.py index 5266e2e..b5fa4df 100644 --- a/coolamqp/attaches/publisher.py +++ b/coolamqp/attaches/publisher.py @@ -13,6 +13,7 @@ from __future__ import absolute_import, division, print_function import collections import logging import struct +import six import warnings from coolamqp.framing.definitions import ChannelOpenOk, BasicPublish, Basic, BasicAck @@ -114,11 +115,19 @@ class Publisher(Channeler, Synchronized): # Break down large bodies bodies = [] + if six.PY3: # memoryview + buffer = memoryview + body = buffer(message.body) max_body_size = self.connection.frame_max - AMQPBodyFrame.FRAME_SIZE_WITHOUT_PAYLOAD while len(body) > 0: - bodies.append(buffer(body, 0, max_body_size)) - body = buffer(body, max_body_size) + 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) + self.connection.send([ AMQPMethodFrame(self.channel_id, BasicPublish(exchange_name, routing_key, False, False)), diff --git a/coolamqp/uplink/connection/recv_framer.py b/coolamqp/uplink/connection/recv_framer.py index 1075f52..7caf9d6 100644 --- a/coolamqp/uplink/connection/recv_framer.py +++ b/coolamqp/uplink/connection/recv_framer.py @@ -55,6 +55,8 @@ class ReceivingFramer(object): :param data: received data """ self.total_data_len += len(data) + if six.PY3: + buffer = memoryview self.chunks.append(buffer(data)) while self._statemachine(): @@ -65,8 +67,12 @@ class ReceivingFramer(object): if up_to >= len(self.chunks[0]): q = self.chunks.popleft() else: - q = buffer(self.chunks[0], 0, up_to) - self.chunks[0] = buffer(self.chunks[0], up_to) + 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) self.total_data_len -= len(q) return q diff --git a/coolamqp/uplink/listener/socket.py b/coolamqp/uplink/listener/socket.py index e2fd2a8..8fcc0bc 100644 --- a/coolamqp/uplink/listener/socket.py +++ b/coolamqp/uplink/listener/socket.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function import collections import socket +import six class SocketFailed(IOError): @@ -125,7 +126,10 @@ class BaseSocket(object): if sent < len(self.data_to_send[0]): # Not everything could be sent - self.data_to_send[0] = buffer(self.data_to_send[0], 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) 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 8f57d0a..bb1f6e5 100644 --- a/tests/test_framing/test_definitions/test_frames.py +++ b/tests/test_framing/test_definitions/test_frames.py @@ -3,10 +3,14 @@ from __future__ import absolute_import, division, print_function import unittest import io import struct +import six 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): diff --git a/tests/test_framing/test_field_table.py b/tests/test_framing/test_field_table.py index 0f3f7cc..1ffecd2 100644 --- a/tests/test_framing/test_field_table.py +++ b/tests/test_framing/test_field_table.py @@ -3,11 +3,15 @@ from __future__ import absolute_import, division, print_function import unittest import struct import io +import six from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size, \ enframe_field_value, deframe_field_value, frame_field_value_size +if six.PY3: + buffer = memoryview + class TestFramingTables(unittest.TestCase): def test_frame_unframe_table(self): -- GitLab