Skip to content

Commit

Permalink
pybind/ceph_argparse: handle non ascii unicode args
Browse files Browse the repository at this point in the history
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 <tchaikov@gmail.com>
  • Loading branch information
tchaikov committed May 5, 2016
1 parent 64da723 commit 9cdb999
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
23 changes: 10 additions & 13 deletions src/pybind/ceph_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,23 +529,20 @@ def __init__(self, prefix=''):

def valid(self, s, partial=False):
try:
if partial:
if self.prefix.startswith(s):
self.val = s
return
else:
if s == self.prefix:
self.val = s
return
raise ArgumentPrefix(u'no match for {0}'.format(s))
except UnicodeDecodeError:
# `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
return
else:
if s == self.prefix:
self.val = s
return

raise ArgumentPrefix("no match for {0}".format(s))

def __str__(self):
return self.prefix

Expand Down
16 changes: 11 additions & 5 deletions src/test/pybind/test_ceph_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,17 @@ def check_no_arg(self, prefix, command):

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, ['章鱼和鱿鱼']))
def test_non_ascii_in_non_options(self):
# 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):
Expand Down

0 comments on commit 9cdb999

Please sign in to comment.