python-openstackclient/openstackclient
John Dennis 21cbc1b14e arguments are not locale decoded into Unicode
When the openstackclient in Python2 passes command line arguments to a
subcommand it fails to pass the arguments as text
(e.g. Unicode). Instead it passes the arguments as binary data encoded
using the current locales encoding.

An easy way to see this is trying to pass a username with a non-ASCII
character.

% openstack user delete ñew
No user with a name or ID of 'ñew' exists.

What occurs internally is when the user data is retrieved it's it
properly represented in a Unicode object. However the username pased
from the command line is still a str object encoded in the locales
encoding (typically UTF-8). A string comparison is attempted between
the encoded data from the command line and the Unicode text found in
the user representation. This seldom ends well, either the comparison
fails to match or a codec error is raised.

There is a hard and fast rule, all text data must be stored in Unicode
objects and the conversion from binary encoded text to Unicode must
occur as close to the I/O boundary as possible. Python3 enforces this
behavior automatically but in Python2 it is the programmers job to do
so.

In the past there have been attempts to fix problems deep inside
internal code by attempting to decode from UTF-8. There are two
problems with this approach. First, internal code has no way to
accurately know what encoding was used to encode the binary data. This
is way it needs to be decoded as close to the I/O source as possible
because that is the best place to know the actual encoding. Guessing
UTF-8 is at best a heuristic. Second, there must be a canonical
representation for data "inside" the program, you don't want dozens of
individual modules, classes, methods, etc. performing conversions,
instead they should be able to make the assumption in what format text
is represented in, the format for text data must be Unicode. This is
another reason to decode as close to the I/O as possible.

In Python3 the argv strings are decoded from the locales encoding by
the interpreter. By the time any Python3 code sees the argv strings
they will be Unicode. However in Python2 there must be explicit code
added to decode the argv strings into Unicode.

The conversion of sys.argv into Unicode only occurs when argv is not
passed to OpenStackShell.run(). If a caller of OpenStackShell.run()
supplies their own arg it is their responsiblity to assure they are
passing actual text objects. Consider this a requirement of the API.

Note: This patch does not contain a unittest to exercise the behavior
because it is difficult to construct a test that depends on command
invocation from a shell. The general structure of the unit tests is to
pass fake argv into OpenStackShell.run() as if it came from a
shell. Because the new code only operates when argv is not passed and
defaults to sys.argv it conflicts with the unittest design.

Change-Id: I779d260744728eae8455ff9dedb6e5c09c165559
Closes-Bug: 1603494
Signed-off-by: John Dennis <jdennis@redhat.com>
(cherry picked from commit 756d2fac67)
2017-01-16 15:54:04 +00:00
..
api Merge "Clean up unnecessary import of urlparse module" 2016-03-01 23:51:00 +00:00
common fix: Exception message includes unnecessary class args 2016-02-29 16:06:05 -06:00
compute Merge "Make SetAggregate inherit from cliff.Command" 2016-03-01 08:29:04 +00:00
identity take_action() method from command.Command shouldn't return 2016-02-29 17:23:29 +00:00
image Fix return value of "image set" command 2016-02-29 16:18:55 +08:00
locale Update translation setup 2016-01-29 08:18:50 +09:00
network Fix SSL/TLS verification for network commands 2017-01-16 06:22:54 +00:00
object Add recursive object delete for containers 2016-02-05 11:18:46 -07:00
tests Merge "Trivial: Reorder unit tests in alphabetical order in volume tests" 2016-03-03 05:00:36 +00:00
volume Fixed a bunch of spacing 2016-02-23 10:38:58 -06:00
__init__.py Change version reporting to use pbr 2013-08-02 12:11:49 -05:00
i18n.py Update translation setup 2016-01-29 08:18:50 +09:00
shell.py arguments are not locale decoded into Unicode 2017-01-16 15:54:04 +00:00