astitcher commented on a change in pull request #192: Changed Python API to 
better handle strings where symbo…
URL: https://github.com/apache/qpid-proton/pull/192#discussion_r334078205
 
 

 ##########
 File path: python/proton/_data.py
 ##########
 @@ -308,6 +308,222 @@ def __eq__(self, o):
             return False
 
 
+def _check_type(s, allow_ulong=False, throw=True):
+    if isinstance(s, symbol):
+        return s
+    if allow_ulong and isinstance(s, ulong):
+        return s
+    if isinstance(s, str):
+        # Must be py2 or py3 str
+        return symbol(s)
+    if isinstance(s, unicode):
+        # This must be python2 unicode as we already detected py3 str above
+        return symbol(s.encode('utf-8'))
+    if throw:
+        raise TypeError('Non-symbol type %s: %s' % (type(s), s))
+    return s
+
+def check_is_symbol(s, throw=True):
+    """
+    Check if s is a symbol type. If s is a string, convert to a symbol
+    and return it. If s is already a symbol, just return it.
+    For other types of s, if throw == True, raise a ``TypeError``,
+    otherwise return s without any changes.
+
+    :param s: Any object that should be checked
+    :type s: any
+    :param throw: If true, raise exception if non-allowed type is found.
+                  Otherwise, return non-allowed types without any changes.
+    :type throw: ``bool``
+    """
+    return _check_type(s, allow_ulong=False, throw=throw)
+
+def check_is_symbol_or_ulong(s, throw=True):
 
 Review comment:
   I think 'check' is the wrong name for these operations.
   In fact I think these operations are incoherent - they both optionally check 
the types conform **and** convert strings to symbols.
   This would matter less if they weren't intended to be user visible, but 
given the lack of leading '_' I assume they are intended to be user API. I 
recommend making these non API for the present with a leading '_'.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to