[Python-checkins] bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516)
Miss Islington (bot)
webhook-mailer at python.org
Sat Jan 12 02:40:16 EST 2019
https://github.com/python/cpython/commit/d39c19255910b9dce08c595f511597e98b09e91f
commit: d39c19255910b9dce08c595f511597e98b09e91f
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-01-11T23:40:09-08:00
summary:
bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516)
Add also tests for PyUnicode_FromFormat() and PyBytes_FromFormat()
with empty result.
(cherry picked from commit 44cc4822bb3799858201e61294c5863f93ec12e2)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst
M Lib/test/test_bytes.py
M Lib/test/test_unicode.py
M Objects/bytesobject.c
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 145411efbb9d..274616bf998d 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -999,6 +999,12 @@ def ptr_formatter(ptr):
self.assertRaises(OverflowError,
PyBytes_FromFormat, b'%c', c_int(256))
+ # Issue #33817: empty strings
+ self.assertEqual(PyBytes_FromFormat(b''),
+ b'')
+ self.assertEqual(PyBytes_FromFormat(b'%s', b''),
+ b'')
+
def test_bytes_blocking(self):
class IterationBlocked(list):
__bytes__ = None
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 3cc018c0cc2c..1aad9334074c 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -2676,6 +2676,12 @@ def check_format(expected, format, *args):
check_format('%.%s',
b'%.%s', b'abc')
+ # Issue #33817: empty strings
+ check_format('',
+ b'')
+ check_format('',
+ b'%s', b'')
+
# Test PyUnicode_AsWideChar()
@support.cpython_only
def test_aswidechar(self):
diff --git a/Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst b/Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst
new file mode 100644
index 000000000000..ca4ccb26d361
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2019-01-11-11-16-16.bpo-33817.nJ4yIj.rst
@@ -0,0 +1 @@
+Fixed :c:func:`_PyBytes_Resize` for empty bytes objects.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 711faba64548..5f9e1eccf2e4 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2990,9 +2990,22 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
/* return early if newsize equals to v->ob_size */
return 0;
}
+ if (Py_SIZE(v) == 0) {
+ if (newsize == 0) {
+ return 0;
+ }
+ *pv = _PyBytes_FromSize(newsize, 0);
+ Py_DECREF(v);
+ return (*pv == NULL) ? -1 : 0;
+ }
if (Py_REFCNT(v) != 1) {
goto error;
}
+ if (newsize == 0) {
+ *pv = _PyBytes_FromSize(0, 0);
+ Py_DECREF(v);
+ return (*pv == NULL) ? -1 : 0;
+ }
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL;
_Py_ForgetReference(v);
More information about the Python-checkins
mailing list