[Python-checkins] cpython (3.5): Issue #29073: bytearray formatting no longer truncates on first null byte.

serhiy.storchaka python-checkins at python.org
Wed Dec 28 02:57:26 EST 2016


https://hg.python.org/cpython/rev/277b36596a54
changeset:   105864:277b36596a54
branch:      3.5
parent:      105859:29d46d29e169
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Dec 28 09:54:22 2016 +0200
summary:
  Issue #29073: bytearray formatting no longer truncates on first null byte.

files:
  Lib/test/test_format.py   |  7 +++++++
  Misc/NEWS                 |  2 ++
  Objects/bytearrayobject.c |  4 +++-
  3 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #29073: bytearray formatting no longer truncates on first null byte.
+
 - Issue #28932: Do not include <sys/random.h> if it does not exist.
 
 - Issue #28147: Fix a memory leak in split-table dictionaries: setattr()
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -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);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list