Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9ad8b7

Browse files
Issue #29073: bytearray formatting no longer truncates on first null byte.
1 parent af9181a commit c9ad8b7

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_format.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ def test_exc(formatstr, args, exception, excmsg):
388388
else:
389389
raise TestFailed('"%*d"%(maxsize, -127) should fail')
390390

391+
def test_nul(self):
392+
# test the null character
393+
testcommon("a\0b", (), 'a\0b')
394+
testcommon("a%cb", (0,), 'a\0b')
395+
testformat("a%sb", ('c\0d',), 'ac\0db')
396+
testcommon(b"a%sb", (b'c\0d',), b'ac\0db')
397+
391398
def test_non_ascii(self):
392399
testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000")
393400

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #29073: bytearray formatting no longer truncates on first null byte.
14+
1315
- Issue #28932: Do not include <sys/random.h> if it does not exist.
1416

1517
- Issue #28147: Fix a memory leak in split-table dictionaries: setattr()

Objects/bytearrayobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,15 @@ bytearray_format(PyByteArrayObject *self, PyObject *args)
283283
{
284284
PyObject *bytes_in, *bytes_out, *res;
285285
char *bytestring;
286+
Py_ssize_t bytesize;
286287

287288
if (self == NULL || !PyByteArray_Check(self) || args == NULL) {
288289
PyErr_BadInternalCall();
289290
return NULL;
290291
}
291292
bytestring = PyByteArray_AS_STRING(self);
292-
bytes_in = PyBytes_FromString(bytestring);
293+
bytesize = PyByteArray_GET_SIZE(self);
294+
bytes_in = PyBytes_FromStringAndSize(bytestring, bytesize);
293295
if (bytes_in == NULL)
294296
return NULL;
295297
bytes_out = _PyBytes_Format(bytes_in, args);

0 commit comments

Comments
 (0)