]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/ceph_argparse: do not choke on non-ascii prefix 5275/head
authorKefu Chai <kchai@redhat.com>
Fri, 17 Jul 2015 07:57:04 +0000 (15:57 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 17 Jul 2015 08:04:16 +0000 (16:04 +0800)
* add a test for it
* add the comments for utf-8 encoding, which is needed by python
  module loader. because the new test has a non-ascii string in
  it. it's the Chinese translation of "octopus and squid".

Fixes: #12287
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/ceph_argparse.py
src/test/pybind/test_ceph_argparse.py

index 762ba604e88a7344223d30b4b9d5ae2ffb176b63..9a830575d44856e7ad386e6fa56816744ce8174b 100644 (file)
@@ -513,6 +513,13 @@ class CephPrefix(CephArgtype):
         self.prefix = prefix
 
     def valid(self, s, partial=False):
+        try:
+            # `prefix` can always be converted into unicode when being compared,
+            # but `s` could be anything passed by user.
+            s = unicode(s)
+        except UnicodeDecodeError:
+            raise ArgumentPrefix("no match for {0}".format(s))
+
         if partial:
             if self.prefix.startswith(s):
                 self.val = s
index 6bd2b08352636154208d628d8bc198a6f8fba5f6..62a9a2497b2c7060a04298c9856318aed998ca49 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/nosetests --nocapture
-# -*- mode:python; tab-width:4; indent-tabs-mode:t -*-
-# vim: ts=4 sw=4 smarttab expandtab
+# -*- mode:python; tab-width:4; indent-tabs-mode:t; coding:utf-8 -*-
+# vim: ts=4 sw=4 smarttab expandtab fileencoding=utf-8
 #
 # Ceph - scalable distributed file system
 #
@@ -86,6 +86,15 @@ class TestArgparse:
                                                     'toomany']))
 
 
+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.
+               assert_is_none(validate_command(sigdict, ['章鱼和鱿鱼']))
+
+
 class TestPG(TestArgparse):
 
     def test_stat(self):