]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/cephfs: avoid unicode check
authorPatrick Donnelly <pdonnell@redhat.com>
Wed, 19 Jun 2019 03:38:26 +0000 (20:38 -0700)
committerPatrick Donnelly <pdonnell@redhat.com>
Thu, 20 Jun 2019 22:39:43 +0000 (15:39 -0700)
py3 does not have the unicode built-in. Instead, simply try to do the encoding
and catch failures appropriately. If it quacks like a duck...

(Note that cython apparently cheats and allows the unicode check but this is
simpler.)

Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
(cherry picked from commit 9bc49fefcd81fbb1c0ce62b1b81f3be420e81663)

src/pybind/cephfs/cephfs.pyx

index 98932fb9e9d56029d54e5e9966251fb660056280..68da6a1934fe0f8d2c0713b9671e7729a6ec1b9e 100644 (file)
@@ -293,11 +293,13 @@ def cstr(val, name, encoding="utf-8", opt=False):
         return None
     if isinstance(val, bytes):
         return val
-    elif isinstance(val, unicode):
-        return val.encode(encoding)
     else:
-        raise TypeError('%s must be a string' % name)
-
+        try:
+            v = val.encode(encoding)
+        except:
+            raise TypeError('%s must be encodeable as a bytearray' % name)
+        assert isinstance(v, bytes)
+        return v
 
 def cstr_list(list_str, name, encoding="utf-8"):
     return [cstr(s, name) for s in list_str]