From e3e0a77a95919fc1f847ac34fa1f49f1fd3cd65d Mon Sep 17 00:00:00 2001 From: Domenic Barbuzzi Date: Tue, 1 Oct 2019 12:11:11 -0400 Subject: [PATCH 1/3] Update utils.latinify to support Python 3 --- evennia/utils/tests/test_utils.py | 33 +++++++++++++++++++++++++++++++ evennia/utils/utils.py | 8 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index 8cde74f349..11a4f3b3d5 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -223,3 +223,36 @@ class TestImportFunctions(TestCase): test_path = self._t_dir_file("invalid_filename.py") loaded_mod = utils.mod_import_from_path(test_path) self.assertIsNone(loaded_mod) + + +class LatinifyTest(TestCase): + """ + utils._UNICODE_MAP may need some additional entries to resolve these tests-- + + LEFT DOUBLE QUOTATION MARK: " + RIGHT DOUBLE QUOTATION MARK: " + + """ + def setUp(self): + super().setUp() + + self.example_str = 'It says, “plugh.”' + self.example_ustr = u'It says, “plugh.”' + + self.expected_output = 'It says, "plugh."' + + def test_plain_string(self): + result = utils.latinify(self.example_str) + self.assertEqual(result, self.expected_output) + + def test_unicode_string(self): + result = utils.latinify(self.example_ustr) + self.assertEqual(result, self.expected_output) + + def test_encoded_string(self): + result = utils.latinify(self.example_str.encode('utf8')) + self.assertEqual(result, self.expected_output) + + def test_byte_string(self): + result = utils.latinify(utils.to_bytes(self.example_str)) + self.assertEqual(result, self.expected_output) diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index b39c1857ec..49c8af1e7b 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -761,7 +761,10 @@ _UNICODE_MAP = { "EN DASH": "-", "HORIZONTAL BAR": "-", "HORIZONTAL ELLIPSIS": "...", + "LEFT SINGLE QUOTATION MARK": "'", "RIGHT SINGLE QUOTATION MARK": "'", + "LEFT DOUBLE QUOTATION MARK": '"', + "RIGHT DOUBLE QUOTATION MARK": '"', } @@ -788,10 +791,13 @@ def latinify(string, default="?", pure_ascii=False): from unicodedata import name + if isinstance(string, bytes): + string = string.decode("utf8") + converted = [] for unich in iter(string): try: - ch = unich.decode("ascii") + ch = unich.encode("utf8").decode("ascii") except UnicodeDecodeError: # deduce a latin letter equivalent from the Unicode data # point name; e.g., since `name(u'á') == 'LATIN SMALL From aca5c20b336234989fbbd0ec2f9938903d166252 Mon Sep 17 00:00:00 2001 From: Domenic Barbuzzi Date: Tue, 1 Oct 2019 12:59:15 -0400 Subject: [PATCH 2/3] Remove resolved comment --- evennia/utils/tests/test_utils.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index 11a4f3b3d5..74d82e202e 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -226,13 +226,6 @@ class TestImportFunctions(TestCase): class LatinifyTest(TestCase): - """ - utils._UNICODE_MAP may need some additional entries to resolve these tests-- - - LEFT DOUBLE QUOTATION MARK: " - RIGHT DOUBLE QUOTATION MARK: " - - """ def setUp(self): super().setUp() From 6bc437b6d82e6514f90a2a0acb9980f874c10366 Mon Sep 17 00:00:00 2001 From: Domenic Barbuzzi Date: Tue, 1 Oct 2019 14:42:26 -0400 Subject: [PATCH 3/3] Update unit tests for utils.latinify --- evennia/utils/tests/test_utils.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index 74d82e202e..090126b67f 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -229,23 +229,14 @@ class LatinifyTest(TestCase): def setUp(self): super().setUp() - self.example_str = 'It says, “plugh.”' - self.example_ustr = u'It says, “plugh.”' - - self.expected_output = 'It says, "plugh."' + self.example_str = 'It naïvely says, “plugh.”' + self.expected_output = 'It naively says, "plugh."' def test_plain_string(self): result = utils.latinify(self.example_str) self.assertEqual(result, self.expected_output) - def test_unicode_string(self): - result = utils.latinify(self.example_ustr) - self.assertEqual(result, self.expected_output) - - def test_encoded_string(self): - result = utils.latinify(self.example_str.encode('utf8')) - self.assertEqual(result, self.expected_output) - def test_byte_string(self): - result = utils.latinify(utils.to_bytes(self.example_str)) + byte_str = utils.to_bytes(self.example_str) + result = utils.latinify(byte_str) self.assertEqual(result, self.expected_output)