diff --git a/Lib/_codecs.py b/Lib/_codecs.py index 26439b061..78da31d3f 100644 --- a/Lib/_codecs.py +++ b/Lib/_codecs.py @@ -239,6 +239,9 @@ def charmap_encode(obj, errors='strict', mapping='latin-1'): res = bytes(res) return res, len(res) +def charmap_build(s): + return {ord(c): i for i, c in enumerate(s)} + if sys.maxunicode == 65535: unicode_bytes = 2 else: @@ -1525,11 +1528,11 @@ def charmapencode_output(c, mapping): rep = mapping[c] if isinstance(rep, int) or isinstance(rep, int): if rep < 256: - return chr(rep) + return rep else: raise TypeError("character mapping must be in range(256)") elif isinstance(rep, str): - return rep + return ord(rep) elif rep == None: raise KeyError("character maps to ") else: @@ -1579,7 +1582,7 @@ def PyUnicode_DecodeCharmap(s, size, mapping, errors): #/* Get mapping (char ordinal -> integer, Unicode char or None) */ ch = s[inpos] try: - x = mapping[ord(ch)] + x = mapping[ch] if isinstance(x, int): if x < 65536: p += chr(x) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 476540e21..88843dedc 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -2219,8 +2219,6 @@ class UnicodeTest(string_tests.CommonTest, for encoding in ('utf-8',): self.assertEqual(str(u.encode(encoding),encoding), u) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_codecs_charmap(self): # 0-127 s = bytes(range(128))