changeset: 105864:277b36596a54 branch: 3.5 parent: 105859:29d46d29e169 user: Serhiy Storchaka date: Wed Dec 28 09:54:22 2016 +0200 files: Lib/test/test_format.py Misc/NEWS Objects/bytearrayobject.c description: Issue #29073: bytearray formatting no longer truncates on first null byte. diff -r 29d46d29e169 -r 277b36596a54 Lib/test/test_format.py --- a/Lib/test/test_format.py Wed Dec 28 09:19:15 2016 +0200 +++ b/Lib/test/test_format.py Wed Dec 28 09:54:22 2016 +0200 @@ -388,6 +388,13 @@ else: raise TestFailed('"%*d"%(maxsize, -127) should fail') + def test_nul(self): + # test the null character + testcommon("a\0b", (), 'a\0b') + testcommon("a%cb", (0,), 'a\0b') + testformat("a%sb", ('c\0d',), 'ac\0db') + testcommon(b"a%sb", (b'c\0d',), b'ac\0db') + def test_non_ascii(self): testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000") diff -r 29d46d29e169 -r 277b36596a54 Misc/NEWS --- a/Misc/NEWS Wed Dec 28 09:19:15 2016 +0200 +++ b/Misc/NEWS Wed Dec 28 09:54:22 2016 +0200 @@ -10,6 +10,8 @@ Core and Builtins ----------------- +- Issue #29073: bytearray formatting no longer truncates on first null byte. + - Issue #28932: Do not include if it does not exist. - Issue #28147: Fix a memory leak in split-table dictionaries: setattr() diff -r 29d46d29e169 -r 277b36596a54 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Wed Dec 28 09:19:15 2016 +0200 +++ b/Objects/bytearrayobject.c Wed Dec 28 09:54:22 2016 +0200 @@ -283,13 +283,15 @@ { PyObject *bytes_in, *bytes_out, *res; char *bytestring; + Py_ssize_t bytesize; if (self == NULL || !PyByteArray_Check(self) || args == NULL) { PyErr_BadInternalCall(); return NULL; } bytestring = PyByteArray_AS_STRING(self); - bytes_in = PyBytes_FromString(bytestring); + bytesize = PyByteArray_GET_SIZE(self); + bytes_in = PyBytes_FromStringAndSize(bytestring, bytesize); if (bytes_in == NULL) return NULL; bytes_out = _PyBytes_Format(bytes_in, args);