bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516) · python/cpython@44cc482

4 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -1001,6 +1001,12 @@ def ptr_formatter(ptr):

10011001

self.assertRaises(OverflowError,

10021002

PyBytes_FromFormat, b'%c', c_int(256))

10031003
1004+

# Issue #33817: empty strings

1005+

self.assertEqual(PyBytes_FromFormat(b''),

1006+

b'')

1007+

self.assertEqual(PyBytes_FromFormat(b'%s', b''),

1008+

b'')

1009+
10041010

def test_bytes_blocking(self):

10051011

class IterationBlocked(list):

10061012

__bytes__ = None

Original file line numberDiff line numberDiff line change

@@ -2680,6 +2680,12 @@ def check_format(expected, format, *args):

26802680

check_format('%.%s',

26812681

b'%.%s', b'abc')

26822682
2683+

# Issue #33817: empty strings

2684+

check_format('',

2685+

b'')

2686+

check_format('',

2687+

b'%s', b'')

2688+
26832689

# Test PyUnicode_AsWideChar()

26842690

@support.cpython_only

26852691

def test_aswidechar(self):

Original file line numberDiff line numberDiff line change

@@ -0,0 +1 @@

1+

Fixed :c:func:`_PyBytes_Resize` for empty bytes objects.

Original file line numberDiff line numberDiff line change

@@ -2991,9 +2991,22 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)

29912991

/* return early if newsize equals to v->ob_size */

29922992

return 0;

29932993

}

2994+

if (Py_SIZE(v) == 0) {

2995+

if (newsize == 0) {

2996+

return 0;

2997+

}

2998+

*pv = _PyBytes_FromSize(newsize, 0);

2999+

Py_DECREF(v);

3000+

return (*pv == NULL) ? -1 : 0;

3001+

}

29943002

if (Py_REFCNT(v) != 1) {

29953003

goto error;

29963004

}

3005+

if (newsize == 0) {

3006+

*pv = _PyBytes_FromSize(0, 0);

3007+

Py_DECREF(v);

3008+

return (*pv == NULL) ? -1 : 0;

3009+

}

29973010

/* XXX UNREF/NEWREF interface should be more symmetrical */

29983011

_Py_DEC_REFTOTAL;

29993012

_Py_ForgetReference(v);