Add also tests for PyUnicode_FromFormat() and PyBytes_FromFormat()
with empty result.
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
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):
--- /dev/null
+Fixed :c:func:`_PyBytes_Resize` for empty bytes objects.
/* 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);