Skip to content

Commit 85b7c70

Browse files
committed
Merge remote-tracking branch 'origin/master' into edge
2 parents a7ef7ff + 27b410e commit 85b7c70

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

mig/shared/compat.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@
3131
"""
3232

3333
from __future__ import absolute_import
34+
from past.builtins import basestring
3435

3536
import codecs
3637
import sys
3738

3839
PY2 = sys.version_info[0] < 3
40+
_TYPE_UNICODE = type(u"")
3941

40-
from mig.shared.base import STR_KIND, _force_default_coding
42+
43+
def _is_unicode(val):
44+
"""Return boolean indicating if the value is a unicode string.
45+
46+
We avoid the `isinstance(val, unicode)` recommended by PEP8 here since it
47+
breaks when combined with python-future and futurize.
48+
"""
49+
return (type(val) == _TYPE_UNICODE)
4150

4251

4352
def ensure_native_string(string_or_bytes):
@@ -50,13 +59,16 @@ def ensure_native_string(string_or_bytes):
5059
valid textual string) will trigger a UnicodeDecodeError on PY3.
5160
Force the same to occur on PY2.
5261
"""
53-
textual_output = _force_default_coding(string_or_bytes, STR_KIND)
5462
if PY2:
5563
# Simulate decoding done by PY3 to trigger identical exceptions
5664
# note the use of a forced "utf8" encoding value: this function
5765
# is generally used to wrap, for example, substitutions of values
5866
# into strings that are defined in the source code. In Python 3
5967
# these are mandated to be UTF-8, and thus decoding as "utf8" is
6068
# what will be attempted on supplied input. Match it.
61-
codecs.decode(textual_output, "utf8")
69+
textual_output = codecs.encode(string_or_bytes, 'utf8')
70+
elif not _is_unicode(string_or_bytes):
71+
textual_output = str(string_or_bytes, 'utf8')
72+
else:
73+
textual_output = string_or_bytes
6274
return textual_output

tests/test_mig_shared_compat.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
#
2020
# You should have received a copy of the GNU General Public License
2121
# along with this program; if not, write to the Free Software
22-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23+
# USA.
2324
#
24-
# -- END_HEADER ---
25+
# --- END_HEADER ---
2526
#
2627

27-
"""Unit test compat functions"""
28+
"""Unit tests for the migrid module pointed to in the filename"""
2829

2930
import binascii
3031
import os
@@ -33,21 +34,32 @@
3334
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
3435
from support import MigTestCase, testmain
3536

36-
from mig.shared.compat import ensure_native_string
37+
from mig.shared.compat import PY2, ensure_native_string
3738

3839
DUMMY_BYTECHARS = b'DEADBEEF'
3940
DUMMY_BYTESRAW = binascii.unhexlify('DEADBEEF') # 4 bytes
41+
DUMMY_UNICODE = u'UniCode123½¾µßðþđŋħĸþł@ª€£$¥©®'
4042

4143
class MigSharedCompat__ensure_native_string(MigTestCase):
42-
# TODO: Add docstrings to this class and its methods
44+
"""Unit test helper for the migrid code pointed to in class name"""
45+
4346
def test_char_bytes_conversion(self):
4447
actual = ensure_native_string(DUMMY_BYTECHARS)
48+
self.assertIs(type(actual), str)
4549
self.assertEqual(actual, 'DEADBEEF')
4650

4751
def test_raw_bytes_conversion(self):
4852
with self.assertRaises(UnicodeDecodeError):
4953
ensure_native_string(DUMMY_BYTESRAW)
5054

55+
def test_unicode_conversion(self):
56+
actual = ensure_native_string(DUMMY_UNICODE)
57+
self.assertEqual(type(actual), str)
58+
if PY2:
59+
self.assertEqual(actual, DUMMY_UNICODE.encode("utf8"))
60+
else:
61+
self.assertEqual(actual, DUMMY_UNICODE)
62+
5163

5264
if __name__ == '__main__':
5365
testmain()

0 commit comments

Comments
 (0)