Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a4961e5 commit 4a72a7bCopy full SHA for 4a72a7b
4 files changed
Doc/c-api/bytes.rst
@@ -198,5 +198,5 @@ called with a non-bytes parameter.
198
desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
199
returned; the address in *\*bytes* may differ from its input value. If the
200
reallocation fails, the original bytes object at *\*bytes* is deallocated,
201
- *\*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
202
returned.
Lib/test/test_codecs.py
@@ -2501,6 +2501,26 @@ def test_decode_errors(self):
2501
self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
2502
2503
2504
+class EscapeEncodeTest(unittest.TestCase):
2505
+
2506
+ def test_escape_encode(self):
2507
+ tests = [
2508
+ (b'', (b'', 0)),
2509
+ (b'foobar', (b'foobar', 6)),
2510
+ (b'spam\0eggs', (b'spam\\x00eggs', 9)),
2511
+ (b'a\'b', (b"a\\'b", 3)),
2512
+ (b'b\\c', (b'b\\\\c', 3)),
2513
+ (b'c\nd', (b'c\\nd', 3)),
2514
+ (b'd\re', (b'd\\re', 3)),
2515
+ (b'f\x7fg', (b'f\\x7fg', 3)),
2516
+ ]
2517
+ for data, output in tests:
2518
+ with self.subTest(data=data):
2519
+ self.assertEqual(codecs.escape_encode(data), output)
2520
+ self.assertRaises(TypeError, codecs.escape_encode, 'spam')
2521
+ self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
2522
2523
2524
class SurrogateEscapeTest(unittest.TestCase):
2525
2526
def test_utf8(self):
Misc/NEWS
@@ -71,6 +71,9 @@ Core and Builtins
71
Library
72
-------
73
74
+- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
75
+ an empty bytestring is passed.
76
77
- Issue #28181: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.
78
79
- Issue #25895: Enable WebSocket URL schemes in urllib.parse.urljoin.
Objects/bytesobject.c
@@ -3550,11 +3550,15 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
3550
PyObject *v;
3551
PyBytesObject *sv;
3552
v = *pv;
3553
- if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
3554
- *pv = 0;
3555
- Py_DECREF(v);
3556
- PyErr_BadInternalCall();
3557
- return -1;
+ if (!PyBytes_Check(v) || newsize < 0) {
+ goto error;
+ }
+ if (Py_SIZE(v) == newsize) {
+ /* return early if newsize equals to v->ob_size */
3558
+ return 0;
3559
3560
+ if (Py_REFCNT(v) != 1) {
3561
3562
}
3563
/* XXX UNREF/NEWREF interface should be more symmetrical */
3564
_Py_DEC_REFTOTAL;
@@ -3572,6 +3576,11 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
3572
3576
sv->ob_sval[newsize] = '\0';
3573
3577
sv->ob_shash = -1; /* invalidate cached hash value */
3574
3578
return 0;
3579
+error:
3580
+ *pv = 0;
3581
+ Py_DECREF(v);
3582
+ PyErr_BadInternalCall();
3583
+ return -1;
3575
3584
3585
3586
void
0 commit comments