]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/ceph_argparse: handle non ascii unicode args 10420/head
authorKefu Chai <kchai@redhat.com>
Fri, 22 Jul 2016 07:55:16 +0000 (15:55 +0800)
committerNathan Cutler <ncutler@suse.com>
Sun, 24 Jul 2016 19:04:41 +0000 (21:04 +0200)
we raise UnicodeDecodeError at seeing non-ascii args if we fail to match
it with any command signatures. instead, we should use a unicode string
for representing the error in that case. please note, the exception is
not printed at all in real-world. =)

Fixes: http://tracker.ceph.com/issues/12287
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 5864626d275c423cdb8d3e52a91fe4cc6b8e6f90)

src/pybind/ceph_argparse.py
src/test/pybind/test_ceph_argparse.py

index 18a87183c2a93e6a00c4c93f737af98c6bf8c200..dc93ddc6d7af2e6b238438ddfadda49a9e356999 100644 (file)
@@ -533,14 +533,16 @@ class CephPrefix(CephArgtype):
         self.prefix = prefix
 
     def valid(self, s, partial=False):
-        s = str(s)
-        if isinstance(s, bytes):
-            try:
+        try:
+            s = str(s)
+            if isinstance(s, bytes):
                 # `prefix` can always be converted into unicode when being compared,
                 # but `s` could be anything passed by user.
                 s = s.decode('ascii')
-            except UnicodeDecodeError:
-                raise ArgumentPrefix("no match for {0}".format(s))
+        except UnicodeEncodeError:
+            raise ArgumentPrefix(u"no match for {0}".format(s))
+        except UnicodeDecodeError:
+            raise ArgumentPrefix("no match for {0}".format(s))
 
         if partial:
             if self.prefix.startswith(s):
index 17cbddd2be742ea19d7896e213e70ff5a51b2aef..00689e3b0e20e187a730d5db6c6bd7b9b689264a 100755 (executable)
@@ -90,10 +90,16 @@ class TestArgparse:
 class TestBasic:
 
     def test_non_ascii_in_non_options(self):
-        # unicode() is not able to convert this str parameter into unicode
-        # using the default encoding 'ascii'. and validate_command() should
-        # not choke on it.
+        # ArgumentPrefix("no match for {0}".format(s)) is not able to convert
+        # unicode str parameter into str. and validate_command() should not
+        # choke on it.
+        assert_is_none(validate_command(sigdict, [u'章鱼和鱿鱼']))
+        assert_is_none(validate_command(sigdict, [u'–w']))
+        # actually we always pass unicode strings to validate_command() in "ceph"
+        # CLI, but we also use bytestrings in our tests, so make sure it does not
+        # break.
         assert_is_none(validate_command(sigdict, ['章鱼和鱿鱼']))
+        assert_is_none(validate_command(sigdict, ['–w']))
 
 
 class TestPG(TestArgparse):