You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kp...@apache.org on 2020/08/18 14:59:32 UTC

[qpid-proton] branch python-check-property-keys updated: PROTON-2237: Rearrange logic so as to avoid python version check. Minor re-arrange of tests.

This is an automated email from the ASF dual-hosted git repository.

kpvdr pushed a commit to branch python-check-property-keys
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git


The following commit(s) were added to refs/heads/python-check-property-keys by this push:
     new 877d2df  PROTON-2237: Rearrange logic so as to avoid python version check. Minor re-arrange of tests.
877d2df is described below

commit 877d2dfff5f2fb0ed75f82fe88c61d2f589b70c2
Author: Kim van der Riet <kp...@apache.org>
AuthorDate: Tue Aug 18 10:59:09 2020 -0400

    PROTON-2237: Rearrange logic so as to avoid python version check. Minor re-arrange of tests.
---
 python/proton/_message.py            | 17 ++++++++++-------
 python/tests/proton_tests/message.py | 30 ++++++++++++++++--------------
 2 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/python/proton/_message.py b/python/proton/_message.py
index d087163..ed791c9 100644
--- a/python/proton/_message.py
+++ b/python/proton/_message.py
@@ -38,8 +38,6 @@ from ._data import char, Data, decimal128, symbol, ulong, AnnotationDict
 from ._endpoints import Link
 from ._exceptions import EXCEPTIONS, MessageException
 
-from sys import version_info
-
 #
 # Hack to provide Python2 <---> Python3 compatibility
 try:
@@ -109,14 +107,19 @@ class Message(object):
         # must save and make the changes afterwards
         changed_keys = []
         for k in self.properties.keys():
-            # If key is a subclass of unicode, convert to unicode. Exclude unicode subclasses symbol and char.
-            if isinstance(k, unicode) and not (type(k) is symbol or type(k) is char):
+            if isinstance(k, unicode):
+                # Py2 & Py3 strings and their subclasses
+                if type(k) is symbol or type(k) is char:
+                    # Exclude symbol and char
+                    raise MessageException('Application property key is not string type: key=%s %s' % (str(k), type(k)))
                 if type(k) is not unicode:
+                    # Only for string subclasses, convert to string
                     changed_keys.append((k, unicode(k)))
-            # Py2 only: If key is binary then convert to unicode. Exclude bytes subclass decimal128.
-            elif version_info[0] == 2 and isinstance(k, bytes) and not (type(k) is decimal128):
+            elif isinstance(k, str) and not (type(k) is decimal128):
+                # Py2 only: If key is binary string then convert to unicode. Exclude bytes subclass decimal128.
                 changed_keys.append((k, k.decode('utf-8')))
             else:
+                # Anything else: raise exception
                 raise MessageException('Application property key is not string type: key=%s %s' % (str(k), type(k)))
         # Make the key changes
         for old_key, new_key in changed_keys:
@@ -449,7 +452,7 @@ class Message(object):
         The group-id for any replies.
 
         :type: ``str``
-        :raise: :exc:`MessageException` if there is any Proton error when using the setter.        
+        :raise: :exc:`MessageException` if there is any Proton error when using the setter.
         """)
 
     def _get_instructions(self):
diff --git a/python/tests/proton_tests/message.py b/python/tests/proton_tests/message.py
index 4fcaa1b..6f407ca 100644
--- a/python/tests/proton_tests/message.py
+++ b/python/tests/proton_tests/message.py
@@ -145,18 +145,6 @@ class CodecTest(Test):
     for k in msg2.properties:
         assert type(k) is unicode, 'non-string key %s %s' % (k, type(k))
 
-  @skipIf(version_info[0] != 2, 'Property key binary to string conversion only supported for Py2')
-  def testBinaryPropertyKey(self):
-    self.msg.properties = {'abc': 123, b'def': 456}
-    data = self.msg.encode()
-
-    msg2 = Message()
-    msg2.decode(data)
-
-    assert msg2.properties == self.msg.properties
-    for k in msg2.properties:
-        assert type(k) is unicode, 'non-string key %s %s' % (k, type(k))
-
   def _testNonStringPropertyKey(self, k):
     self.msg.properties = {k: 'abc'}
     try:
@@ -183,8 +171,6 @@ class CodecTest(Test):
     self._testNonStringPropertyKey(decimal64(12))
     self._testNonStringPropertyKey(timestamp(1234567890))
     self._testNonStringPropertyKey(uuid4())
-    if version_info[0] == 3: # Py3 only
-        self._testNonStringPropertyKey(b'abc123')
 
   def testCharPropertyKey(self):
     self._testNonStringPropertyKey(char('A'))
@@ -195,6 +181,22 @@ class CodecTest(Test):
   def testSymbolPropertyKey(self):
     self._testNonStringPropertyKey(symbol('abcdef'))
 
+  def testBinaryPropertyKey(self):
+    if version_info[0] > 2:
+      # Py3: Binary conversions not supported
+      self._testNonStringPropertyKey(b'abc123')
+    else:
+      # Py2: Check for correct key conversion
+      self.msg.properties = {'abc': 123, b'def': 456}
+      data = self.msg.encode()
+
+      msg2 = Message()
+      msg2.decode(data)
+
+      assert msg2.properties == self.msg.properties
+      for k in msg2.properties:
+        assert type(k) is unicode, 'non-string key %s %s' % (k, type(k))
+
   def testAnnotationsSymbolicAndUlongKey(self, a={symbol('one'): 1, 'two': 2, ulong(3): 'three'}):
     self.msg.annotations = a
     data = self.msg.encode()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org