diff -r 34e7b9879e60 Lib/test/test_format.py --- a/Lib/test/test_format.py Sun Dec 25 16:22:23 2016 +0200 +++ b/Lib/test/test_format.py Mon Dec 26 12:30:36 2016 +0200 @@ -388,6 +388,13 @@ class FormatTest(unittest.TestCase): 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 34e7b9879e60 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Sun Dec 25 16:22:23 2016 +0200 +++ b/Objects/bytearrayobject.c Mon Dec 26 12:30:36 2016 +0200 @@ -283,13 +283,15 @@ bytearray_format(PyByteArrayObject *self { 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);