Commit b76ad512 authored by Xiang Zhang's avatar Xiang Zhang Committed by GitHub

bpo-29714: Fix a regression that bytes format may fail when containing zero bytes inside. (GH-499)

parent 86aa2696
...@@ -515,6 +515,11 @@ class BaseBytesTest: ...@@ -515,6 +515,11 @@ class BaseBytesTest:
a = b % (b'seventy-nine', 79) a = b % (b'seventy-nine', 79)
self.assertEqual(a, b'seventy-nine / 100 = 79%') self.assertEqual(a, b'seventy-nine / 100 = 79%')
self.assertIs(type(a), self.type2test) 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): def test_imod(self):
b = self.type2test(b'hello, %b!') b = self.type2test(b'hello, %b!')
...@@ -527,6 +532,11 @@ class BaseBytesTest: ...@@ -527,6 +532,11 @@ class BaseBytesTest:
b %= (b'seventy-nine', 79) b %= (b'seventy-nine', 79)
self.assertEqual(b, b'seventy-nine / 100 = 79%') self.assertEqual(b, b'seventy-nine / 100 = 79%')
self.assertIs(type(b), self.type2test) self.assertIs(type(b), self.type2test)
# issue 29714
b = self.type2test(b'hello,\x00%b!')
b %= b'world'
self.assertEqual(b, b'hello,\x00world!')
self.assertIs(type(b), self.type2test)
def test_rmod(self): def test_rmod(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
......
...@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1? ...@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- bpo-29714: Fix a regression that bytes format may fail when containing zero
bytes inside.
- bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and - bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
using "sequence" as a keyword argument in list() and tuple() are deprecated. using "sequence" as a keyword argument in list() and tuple() are deprecated.
Specify the value as a positional argument instead. Specify the value as a positional argument instead.
......
...@@ -619,11 +619,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len, ...@@ -619,11 +619,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
Py_ssize_t len; Py_ssize_t len;
char *pos; char *pos;
pos = strchr(fmt + 1, '%'); pos = (char *)memchr(fmt + 1, '%', fmtcnt);
if (pos != NULL) if (pos != NULL)
len = pos - fmt; len = pos - fmt;
else else
len = format_len - (fmt - format); len = fmtcnt + 1;
assert(len != 0); assert(len != 0);
memcpy(res, fmt, len); memcpy(res, fmt, len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment