PROTON-1325: Repair broken fix for python "buffer" type.

Commit 05deba72edebd0cf1e7bf148ba7e99ab2fb62bfe was incorrect and did not
address the bug, this commit does.

Add an automatic mapping from a python buffer or memoryview object to the AMQP 
binary type.

In the other direction, AMQP "binary" maps to the python "bytes" or "str" type, 
as before.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/e2357e2c
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/e2357e2c
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/e2357e2c

Branch: refs/heads/go1
Commit: e2357e2cc8af94cf038d31936f19fea2e46a323b
Parents: 440131e
Author: Alan Conway <[email protected]>
Authored: Thu Jan 5 10:06:28 2017 -0500
Committer: Alan Conway <[email protected]>
Committed: Thu Jan 5 16:33:04 2017 -0500

----------------------------------------------------------------------
 proton-c/bindings/python/proton/__init__.py | 21 +++++++----
 tests/python/proton_tests/codec.py          | 44 ++++++++++++++----------
 2 files changed, 39 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e2357e2c/proton-c/bindings/python/proton/__init__.py
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/proton/__init__.py 
b/proton-c/bindings/python/proton/__init__.py
index d3f6922..cfac01e 100644
--- a/proton-c/bindings/python/proton/__init__.py
+++ b/proton-c/bindings/python/proton/__init__.py
@@ -1840,7 +1840,15 @@ class Data:
     @type b: binary
     @param b: a binary value
     """
-    self._check(pn_data_put_binary(self._data, bytes(b)))
+    self._check(pn_data_put_binary(self._data, b))
+
+  def put_memoryview(self, mv):
+    """Put a python memoryview object as an AMQP binary value"""
+    self.put_binary(mv.tobytes())
+
+  def put_buffer(self, buff):
+    """Put a python buffer object as an AMQP binary value"""
+    self.put_binary(bytes(buff))
 
   def put_string(self, s):
     """
@@ -2236,12 +2244,11 @@ class Data:
   # we need to add an explicit int since it is a different type
   if int not in put_mappings:
     put_mappings[int] = put_int
-  # For python 3.x use 'memoryview', for <=2.5 use 'buffer'. Python >=2.6 has 
both.
-  if getattr(__builtins__, 'memoryview', None):
-    put_mappings[memoryview] = put_binary
-  if getattr(__builtins__, 'buffer', None):
-    put_mappings[buffer] = put_binary
-
+  # Python >=3.0 has 'memoryview', <=2.5 has 'buffer', >=2.6 has both.
+  try: put_mappings[memoryview] = put_memoryview
+  except NameError: pass
+  try: put_mappings[buffer] = put_buffer
+  except NameError: pass
   get_mappings = {
     NULL: lambda s: None,
     BOOL: get_bool,

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/e2357e2c/tests/python/proton_tests/codec.py
----------------------------------------------------------------------
diff --git a/tests/python/proton_tests/codec.py 
b/tests/python/proton_tests/codec.py
index 4d3d906..49a57e0 100644
--- a/tests/python/proton_tests/codec.py
+++ b/tests/python/proton_tests/codec.py
@@ -357,25 +357,31 @@ class DataTest(Test):
     copy = data.get_object()
     assert copy == obj, (copy, obj)
 
-  if getattr(__builtins__, 'buffer', None):
-    def testBuffer(self):
-      self.data.put_object(buffer("foo"))
-      data = Data()
-      data.decode(self.data.encode())
-      data.rewind()
-      assert data.next()
-      assert data.type() == Data.BINARY
-      assert data.get_object() == "foo"
-
-  if getattr(__builtins__, 'memoryview', None):
-    def testBuffer(self):
-      self.data.put_object(memoryview("foo"))
-      data = Data()
-      data.decode(self.data.encode())
-      data.rewind()
-      assert data.next()
-      assert data.type() == Data.BINARY
-      assert data.get_object() == "foo"
+  def testBuffer(self):
+    try:
+      self.data.put_object(buffer(str2bin("foo")))
+    except NameError:
+      # python >= 3.0 does not have `buffer`
+      return
+    data = Data()
+    data.decode(self.data.encode())
+    data.rewind()
+    assert data.next()
+    assert data.type() == Data.BINARY
+    assert data.get_object() == str2bin("foo")
+
+  def testMemoryView(self):
+    try:
+      self.data.put_object(memoryview(str2bin("foo")))
+    except NameError:
+      # python <= 2.6 does not have `memoryview`
+      return
+    data = Data()
+    data.decode(self.data.encode())
+    data.rewind()
+    assert data.next()
+    assert data.type() == Data.BINARY
+    assert data.get_object() == str2bin("foo")
 
   def testLookup(self):
     obj = {symbol("key"): str2unicode("value"),


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to