Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix bytes format regression when containing zero bytes inside
  • Loading branch information
zhangyangyu committed Mar 6, 2017
commit 80a6e7eecc2e5d284f5aef6573042fce4d23975d
10 changes: 10 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ def test_mod(self):
a = b % (b'seventy-nine', 79)
self.assertEqual(a, b'seventy-nine / 100 = 79%')
self.assertIs(type(a), self.type2test)
# issue 29714
b = self.type2test(b'hello,\x00%b!')
b = b % b'world'
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)

def test_imod(self):
b = self.type2test(b'hello, %b!')
Expand All @@ -527,6 +532,11 @@ def test_imod(self):
b %= (b'seventy-nine', 79)
self.assertEqual(b, b'seventy-nine / 100 = 79%')
self.assertIs(type(b), self.type2test)
# issue 29714
b = self.type2test(b'hello,\x00%b!')
b = b % b'world'
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)

def test_rmod(self):
with self.assertRaises(TypeError):
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
Py_ssize_t len;
char *pos;

pos = strchr(fmt + 1, '%');
pos = (char *)memchr(fmt + 1, '%', fmtcnt);
if (pos != NULL)
len = pos - fmt;
else
len = format_len - (fmt - format);
len = fmtcnt + 1;
assert(len != 0);

memcpy(res, fmt, len);
Expand Down