[Python-checkins] cpython (merge 3.5 -> 3.6): Issue #25270: Merge from 3.5

berker.peksag python-checkins at python.org
Fri Sep 16 10:31:37 EDT 2016


https://hg.python.org/cpython/rev/8a649009a0e9
changeset: 103864:8a649009a0e9
branch: 3.6
parent: 103860:0820d023077e
parent: 103863:2a4fb01fa1a3
user: Berker Peksag <berker.peksag at gmail.com>
date: Fri Sep 16 17:32:06 2016 +0300
summary:
 Issue #25270: Merge from 3.5
files:
 Doc/c-api/bytes.rst | 2 +-
 Lib/test/test_codecs.py | 20 ++++++++++++++++++++
 Misc/NEWS | 3 +++
 Objects/bytesobject.c | 19 ++++++++++++++-----
 4 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst
--- a/Doc/c-api/bytes.rst
+++ b/Doc/c-api/bytes.rst
@@ -198,5 +198,5 @@
 desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
 returned; the address in *\*bytes* may differ from its input value. If the
 reallocation fails, the original bytes object at *\*bytes* is deallocated,
- *\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is
+ *\*bytes* is set to *NULL*, :exc:`MemoryError` is set, and ``-1`` is
 returned.
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -2542,6 +2542,26 @@
 self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
 
 
+class EscapeEncodeTest(unittest.TestCase):
+
+ def test_escape_encode(self):
+ tests = [
+ (b'', (b'', 0)),
+ (b'foobar', (b'foobar', 6)),
+ (b'spam0円eggs', (b'spam\\x00eggs', 9)),
+ (b'a\'b', (b"a\\'b", 3)),
+ (b'b\\c', (b'b\\\\c', 3)),
+ (b'c\nd', (b'c\\nd', 3)),
+ (b'd\re', (b'd\\re', 3)),
+ (b'f\x7fg', (b'f\\x7fg', 3)),
+ ]
+ for data, output in tests:
+ with self.subTest(data=data):
+ self.assertEqual(codecs.escape_encode(data), output)
+ self.assertRaises(TypeError, codecs.escape_encode, 'spam')
+ self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
+
+
 class SurrogateEscapeTest(unittest.TestCase):
 
 def test_utf8(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 Library
 -------
 
+- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
+ an empty bytestring is passed.
+
 - Issue #28181: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.
 
 - Issue #25895: Enable WebSocket URL schemes in urllib.parse.urljoin.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2910,11 +2910,15 @@
 PyObject *v;
 PyBytesObject *sv;
 v = *pv;
- if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
- *pv = 0;
- Py_DECREF(v);
- PyErr_BadInternalCall();
- return -1;
+ if (!PyBytes_Check(v) || newsize < 0) {
+ goto error;
+ }
+ if (Py_SIZE(v) == newsize) {
+ /* return early if newsize equals to v->ob_size */
+ return 0;
+ }
+ if (Py_REFCNT(v) != 1) {
+ goto error;
 }
 /* XXX UNREF/NEWREF interface should be more symmetrical */
 _Py_DEC_REFTOTAL;
@@ -2932,6 +2936,11 @@
 sv->ob_sval[newsize] = '0円';
 sv->ob_shash = -1; /* invalidate cached hash value */
 return 0;
+error:
+ *pv = 0;
+ Py_DECREF(v);
+ PyErr_BadInternalCall();
+ return -1;
 }
 
 void
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /