Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pybind/ceph_argparse: handle non ascii unicode args #8943

Merged
merged 1 commit into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/pybind/ceph_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,16 @@ def __init__(self, prefix=''):
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):
Expand Down
12 changes: 9 additions & 3 deletions src/test/pybind/test_ceph_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ 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.
# 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