summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--oslo_utils/encodeutils.py8
-rw-r--r--oslo_utils/tests/tests_encodeutils.py20
2 files changed, 11 insertions, 17 deletions
diff --git a/oslo_utils/encodeutils.py b/oslo_utils/encodeutils.py
index 3827631..761aef7 100644
--- a/oslo_utils/encodeutils.py
+++ b/oslo_utils/encodeutils.py
@@ -18,6 +18,12 @@ import sys
import six
+# NOTE(blk-u): This provides a symbol that can be overridden just for this
+# module during testing. sys.getfilesystemencoding() is called by coverage so
+# mocking it globally caused the coverage job to fail.
+_getfilesystemencoding = sys.getfilesystemencoding
+
+
def safe_decode(text, incoming=None, errors='strict'):
"""Decodes incoming text/bytes string using `incoming` if they're not
already unicode.
@@ -169,7 +175,7 @@ def exception_to_unicode(exc):
# Try the locale encoding, most error messages are encoded to this encoding
# (ex: os.strerror(errno))
- encoding = sys.getfilesystemencoding()
+ encoding = _getfilesystemencoding()
try:
return msg.decode(encoding)
except UnicodeDecodeError: # nosec
diff --git a/oslo_utils/tests/tests_encodeutils.py b/oslo_utils/tests/tests_encodeutils.py
index 8cceb8e..ded44c2 100644
--- a/oslo_utils/tests/tests_encodeutils.py
+++ b/oslo_utils/tests/tests_encodeutils.py
@@ -15,8 +15,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import sys
-
import mock
from oslotest import base as test_base
import six
@@ -137,18 +135,6 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
def __str__(self):
return self.value
- def mock_getfilesystemencoding(test_value):
- # NOTE(blk-u): sys.getfilesystemencoding is called by coverage
- # while processing the functions/files called by the test so
- # mocking getfilesystemencoding was causing the coverage job to
- # fail since it doesn't know about koi8_r encoding, and the
- # encoding might be incorrect when set in other cases. The
- # workaround is to return the test value when it's called by
- # oslo.utils and the original value when called later by coverage.
- return mock.patch.object(
- sys, 'getfilesystemencoding',
- side_effect=[test_value, sys.getfilesystemencoding()])
-
# On Python 3, an exception which returns bytes with is __str__()
# method (like StrException(bytes)) is probably a bug, but it was not
# harder to support this silly case in exception_to_unicode().
@@ -164,7 +150,8 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
u'utf-8 \xe9\u20ac')
# Force the locale encoding to ASCII to test the fallback
- with mock_getfilesystemencoding('ascii'):
+ with mock.patch.object(encodeutils, '_getfilesystemencoding',
+ return_value='ascii'):
# Fallback: decode from ISO-8859-1
exc = StrException(b'rawbytes \x80\xff')
self.assertEqual(encodeutils.exception_to_unicode(exc),
@@ -181,7 +168,8 @@ class ExceptionToUnicodeTest(test_base.BaseTestCase):
u'unicode \xe9\u20ac')
# Test the locale encoding
- with mock_getfilesystemencoding('koi8_r'):
+ with mock.patch.object(encodeutils, '_getfilesystemencoding',
+ return_value='koi8_r'):
exc = StrException(b'\xf2\xd5\xd3\xd3\xcb\xc9\xca')
# Decode from the locale encoding
# (the message cannot be decoded from ASCII nor UTF-8)