[Python-checkins] bpo-42065: Fix incorrectly formatted _codecs.charmap_decode error message (GH-19940)

Miss Skeleton (bot) webhook-mailer at python.org
Sun Oct 18 02:00:36 EDT 2020


https://github.com/python/cpython/commit/6a2aa4994e99ae8e43011a0290646fde4724a003
commit: 6a2aa4994e99ae8e43011a0290646fde4724a003
branch: 3.9
author: Miss Skeleton (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-10-18T09:00:18+03:00
summary:

bpo-42065: Fix incorrectly formatted _codecs.charmap_decode error message (GH-19940)

(cherry picked from commit 3635388f52b42e5280229104747962117104c453)

Co-authored-by: Max Bernstein <tekknolagi at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2020-10-17-23-17-18.bpo-42065.85BsRA.rst
M Lib/test/test_codecs.py
M Objects/unicodeobject.c

diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 54a3520802a4f..8d112a171d7c4 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -2183,6 +2183,18 @@ def test_decode_with_int2str_map(self):
             ("", len(allbytes))
         )
 
+        self.assertRaisesRegex(TypeError,
+            "character mapping must be in range\\(0x110000\\)",
+            codecs.charmap_decode,
+            b"\x00\x01\x02", "strict", {0: "A", 1: 'Bb', 2: -2}
+        )
+
+        self.assertRaisesRegex(TypeError,
+            "character mapping must be in range\\(0x110000\\)",
+            codecs.charmap_decode,
+            b"\x00\x01\x02", "strict", {0: "A", 1: 'Bb', 2: 999999999}
+        )
+
     def test_decode_with_int2int_map(self):
         a = ord('a')
         b = ord('b')
diff --git a/Misc/NEWS.d/next/Library/2020-10-17-23-17-18.bpo-42065.85BsRA.rst b/Misc/NEWS.d/next/Library/2020-10-17-23-17-18.bpo-42065.85BsRA.rst
new file mode 100644
index 0000000000000..83c86c0799ebf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-10-17-23-17-18.bpo-42065.85BsRA.rst
@@ -0,0 +1,3 @@
+Fix an incorrectly formatted error from :meth:`_codecs.charmap_decode` when
+called with a mapped value outside the range of valid Unicode code points.
+PR by Max Bernstein.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 3c2383d57c863..d5fbf158d50b4 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8160,7 +8160,7 @@ charmap_decode_mapping(const char *s,
                 goto Undefined;
             if (value < 0 || value > MAX_UNICODE) {
                 PyErr_Format(PyExc_TypeError,
-                             "character mapping must be in range(0x%lx)",
+                             "character mapping must be in range(0x%x)",
                              (unsigned long)MAX_UNICODE + 1);
                 goto onError;
             }



More information about the Python-checkins mailing list