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_r334081186
 
 

 ##########
 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 a name more like 'ensure' is a better guide to what these functions 
do (but they would still be a bit incoherent in purpose)

----------------------------------------------------------------
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