6 Commits

Author SHA1 Message Date
Victor Stinner
de84b5ba03 Add encodeutils.to_utf8() function
The function replaces a very common pattern in code base supporting
Python 2 and Python 3:

    if isinstance(text, six.text_type):
        text = text.encode('utf-8')

to_utf8() accepts subtypes of bytes and six.text_type. For example,
oslo.i18n Message objects are accepted and encoded to UTF-8 as
expected.

Using encodeutils.safe_encode(text) is not reliable because it relies
on the current locale encoding which can be ASCII, whereas UTF-8 is
expected. Having to write encodeutils.safe_encode(text,
incoding='utf-8') is not obvious and error-prone (it's easy to forget
the incoming parameter).

Change-Id: I00463716b6012cbef383855999f63f99f2f52540
2016-01-20 16:47:23 +01:00
Cyril Roelandt
92661e47ba Add a bandit target to tox.ini
Some parts of the code have been annoted with "# nosec" as they made bandit
raise false positives.

Change-Id: I9d8a8ab79e8b5de98e7d291ac132f8971a44216d
2015-11-20 15:52:51 +01:00
Victor Stinner
7f57de5bb2 Use versionadded and versionchanged in doc
Document in which version new types and functions were added using
".. versionadded:: x.y". Document changes using
".. versionchanged:: x.y."

For new modules, add the versionadded tag in the module top
docstring, not on each type/function.

Add fileutils to documentation. The doc part was forgotten during the
graduation.

Add docstrings to convert versions of versionutils.

I used "git blame" + "git tag --contains=SHA1" to find these version,
and then I checked manually each version.

Change-Id: Ia2f00aa29eb36410a49fc1d350896a569a7737a1
2015-10-17 10:53:34 +02:00
Victor Stinner
ac308341f6 Fix exception_to_unicode() for oslo_i18n Message
Message instances created by oslo_i18n are subclasses of the Unicode
type (unicode on Python 2, str on Python 3) and have no __unicode__()
method. exception_to_unicode() raises an AttributeError when trying to
convert it to Unicode.

This change fixes this issue and adds an unit test.

Change-Id: Ica67429ac64f74e5c636b6d74d71910a26511378
2015-07-02 23:53:41 +02:00
Victor Stinner
7f2406874e Add exception_to_unicode() function
There is no simple way working on Python 2 and Python 3 to get the
message of an exception as a Unicode string. This new functions uses an
heuristic to get the encoding of the exception message. It tries UTF-8
(which is a superset of ASCII), the locale encoding, or fallback to
ISO-8859-1 (which never fails).

This function is required to log exceptions using the logging module
when the exception message contains non-ASCII characters.

- logging.log() only works with non-ASCII characters on Python 2 and
  Python 3 if the message is formatted as Unicode. For example,
  logging.log(b'error: %s', b'\xe9') fails in most cases.
- unicode % bytes doesn't work with non-ASCII characters
- logging.log(u'error: %s', exc) doesn't work if the exception message
  contains a non-ASCII character

Only logging.log(u'error: %s', exception_to_unicode(exc)) works in all
cases: ASCII or non-ASCII exception message, Python 2 and Python 3.

Co-Authored-By: Joshua Harlow <harlowja@gmail.com>

Change-Id: I241b7c81c7ae3d26f81790e9180678dc9af81e22
2015-05-05 23:50:00 -07:00
Doug Hellmann
ca76fdcb52 Move files out of the namespace package
Move the public API out of oslo.utils to oslo_utils. Retain the ability
to import from the old namespace package for backwards compatibility for
this release cycle.

bp/drop-namespace-packages

Change-Id: Ic6dd62097399bf75e3d11b4d8a6400971069c415
2015-01-06 17:07:36 -05:00