From 1e78ed6825701029aa45a68f9e62dd3bb8d4e928 Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Fri, 20 Oct 2017 07:41:29 -0700
Subject: bpo-31825: Fixed OverflowError in the 'unicode-escape' codec
 (GH-4058) (#4059)

and in codecs.escape_decode() when decode an escaped non-ascii byte.
(cherry picked from commit 56cb465cc93dcb35aaf7266ca3dbe2dcff1fac5f)
---
 Lib/test/test_codecs.py                                               | 4 ++++
 .../next/Core and Builtins/2017-10-20-14-07-46.bpo-31825.gJvmGW.rst   | 2 ++
 Objects/bytesobject.c                                                 | 2 +-
 Objects/unicodeobject.c                                               | 2 +-
 Python/ast.c                                                          | 2 +-
 5 files changed, 9 insertions(+), 3 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-10-20-14-07-46.bpo-31825.gJvmGW.rst

diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 1e63ed8..de6868a 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -1203,6 +1203,8 @@ class EscapeDecodeTest(unittest.TestCase):
             check(br"\8", b"\\8")
         with self.assertWarns(DeprecationWarning):
             check(br"\9", b"\\9")
+        with self.assertWarns(DeprecationWarning):
+            check(b"\\\xfa", b"\\\xfa")
 
     def test_errors(self):
         decode = codecs.escape_decode
@@ -2474,6 +2476,8 @@ class UnicodeEscapeTest(unittest.TestCase):
             check(br"\8", "\\8")
         with self.assertWarns(DeprecationWarning):
             check(br"\9", "\\9")
+        with self.assertWarns(DeprecationWarning):
+            check(b"\\\xfa", "\\\xfa")
 
     def test_decode_errors(self):
         decode = codecs.unicode_escape_decode
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-20-14-07-46.bpo-31825.gJvmGW.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-20-14-07-46.bpo-31825.gJvmGW.rst
new file mode 100644
index 0000000..18e81af
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-20-14-07-46.bpo-31825.gJvmGW.rst	
@@ -0,0 +1,2 @@
+Fixed OverflowError in the 'unicode-escape' codec and in
+codecs.escape_decode() when decode an escaped non-ascii byte.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 4c55294..489062e 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -1255,7 +1255,7 @@ PyObject *PyBytes_DecodeEscape(const char *s,
     if (first_invalid_escape != NULL) {
         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
                              "invalid escape sequence '\\%c'",
-                             *first_invalid_escape) < 0) {
+                             (unsigned char)*first_invalid_escape) < 0) {
             Py_DECREF(result);
             return NULL;
         }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index e9fc658..64905e8 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6174,7 +6174,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
     if (first_invalid_escape != NULL) {
         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
                              "invalid escape sequence '\\%c'",
-                             *first_invalid_escape) < 0) {
+                             (unsigned char)*first_invalid_escape) < 0) {
             Py_DECREF(result);
             return NULL;
         }
diff --git a/Python/ast.c b/Python/ast.c
index 2973b9f..d271025 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -4127,7 +4127,7 @@ decode_utf8(struct compiling *c, const char **sPtr, const char *end)
 
 static int
 warn_invalid_escape_sequence(struct compiling *c, const node *n,
-                             char first_invalid_escape_char)
+                             unsigned char first_invalid_escape_char)
 {
     PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
                                          first_invalid_escape_char);
-- 
cgit v0.12