Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit44cc482

Browse files
bpo-33817: Fix _PyBytes_Resize() for empty bytes object. (GH-11516)
Add also tests for PyUnicode_FromFormat() and PyBytes_FromFormat()with empty result.
1 parentd0d3e99 commit44cc482

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

‎Lib/test/test_bytes.py‎

Lines changed: 6 additions & 0 deletions
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
deftest_bytes_blocking(self):
10051011
classIterationBlocked(list):
10061012
__bytes__=None

‎Lib/test/test_unicode.py‎

Lines changed: 6 additions & 0 deletions
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
deftest_aswidechar(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed:c:func:`_PyBytes_Resize` for empty bytes objects.

‎Objects/bytesobject.c‎

Lines changed: 13 additions & 0 deletions
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
return0;
29932993
}
2994+
if (Py_SIZE(v)==0) {
2995+
if (newsize==0) {
2996+
return0;
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
gotoerror;
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);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp