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.
2 parents de3f48a + 4a72a7b commit 7b4bcd2Copy full SHA for 7b4bcd2
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
@@ -2542,6 +2542,26 @@ def test_decode_errors(self):
2542
self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
2543
2544
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
2565
class SurrogateEscapeTest(unittest.TestCase):
2566
2567
def test_utf8(self):
Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
27
Library
28
-------
29
30
+- Issue #25270: Prevent codecs.escape_encode() from raising SystemError when
31
+ an empty bytestring is passed.
32
33
- Issue #28181: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.
34
35
- Issue #25895: Enable WebSocket URL schemes in urllib.parse.urljoin.
Objects/bytesobject.c
@@ -2910,11 +2910,15 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
2910
PyObject *v;
2911
PyBytesObject *sv;
2912
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;
+ if (!PyBytes_Check(v) || newsize < 0) {
+ goto error;
+ }
+ if (Py_SIZE(v) == newsize) {
+ /* return early if newsize equals to v->ob_size */
2918
+ return 0;
2919
2920
+ if (Py_REFCNT(v) != 1) {
2921
2922
}
2923
/* XXX UNREF/NEWREF interface should be more symmetrical */
2924
_Py_DEC_REFTOTAL;
@@ -2932,6 +2936,11 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
2932
2936
sv->ob_sval[newsize] = '\0';
2933
2937
sv->ob_shash = -1; /* invalidate cached hash value */
2934
2938
return 0;
2939
+error:
2940
+ *pv = 0;
2941
+ Py_DECREF(v);
2942
+ PyErr_BadInternalCall();
2943
+ return -1;
2935
2944
2945
2946
void
0 commit comments