Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b4bcd2

Browse files
committed
Issue #25270: Merge from 3.5
2 parents de3f48a + 4a72a7b commit 7b4bcd2

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

Doc/c-api/bytes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,5 @@ called with a non-bytes parameter.
198198
desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
199199
returned; the address in *\*bytes* may differ from its input value. If the
200200
reallocation fails, the original bytes object at *\*bytes* is deallocated,
201-
*\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is
201+
*\*bytes* is set to *NULL*, :exc:`MemoryError` is set, and ``-1`` is
202202
returned.

Lib/test/test_codecs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,26 @@ def test_decode_errors(self):
25422542
self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
25432543

25442544

2545+
class EscapeEncodeTest(unittest.TestCase):
2546+
2547+
def test_escape_encode(self):
2548+
tests = [
2549+
(b'', (b'', 0)),
2550+
(b'foobar', (b'foobar', 6)),
2551+
(b'spam\0eggs', (b'spam\\x00eggs', 9)),
2552+
(b'a\'b', (b"a\\'b", 3)),
2553+
(b'b\\c', (b'b\\\\c', 3)),
2554+
(b'c\nd', (b'c\\nd', 3)),
2555+
(b'd\re', (b'd\\re', 3)),
2556+
(b'f\x7fg', (b'f\\x7fg', 3)),
2557+
]
2558+
for data, output in tests:
2559+
with self.subTest(data=data):
2560+
self.assertEqual(codecs.escape_encode(data), output)
2561+
self.assertRaises(TypeError, codecs.escape_encode, 'spam')
2562+
self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
2563+
2564+
25452565
class SurrogateEscapeTest(unittest.TestCase):
25462566

25472567
def test_utf8(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
31+
an empty bytestring is passed.
32+
3033
- Issue #28181: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.
3134

3235
- Issue #25895: Enable WebSocket URL schemes in urllib.parse.urljoin.

Objects/bytesobject.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,11 +2910,15 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
29102910
PyObject *v;
29112911
PyBytesObject *sv;
29122912
v = *pv;
2913-
if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
2914-
*pv = 0;
2915-
Py_DECREF(v);
2916-
PyErr_BadInternalCall();
2917-
return -1;
2913+
if (!PyBytes_Check(v) || newsize < 0) {
2914+
goto error;
2915+
}
2916+
if (Py_SIZE(v) == newsize) {
2917+
/* return early if newsize equals to v->ob_size */
2918+
return 0;
2919+
}
2920+
if (Py_REFCNT(v) != 1) {
2921+
goto error;
29182922
}
29192923
/* XXX UNREF/NEWREF interface should be more symmetrical */
29202924
_Py_DEC_REFTOTAL;
@@ -2932,6 +2936,11 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
29322936
sv->ob_sval[newsize] = '\0';
29332937
sv->ob_shash = -1; /* invalidate cached hash value */
29342938
return 0;
2939+
error:
2940+
*pv = 0;
2941+
Py_DECREF(v);
2942+
PyErr_BadInternalCall();
2943+
return -1;
29352944
}
29362945

29372946
void

0 commit comments

Comments
 (0)