Commit e914d413 authored by Victor Stinner's avatar Victor Stinner

Issue #26766: Fix _PyBytesWriter_Finish()

Return a bytearray object when bytearray is requested and when the small buffer
is used.

Fix also test_bytes: bytearray%args must return a bytearray type.
parent 12bb6f4c
...@@ -492,7 +492,7 @@ class BaseBytesTest: ...@@ -492,7 +492,7 @@ class BaseBytesTest:
b = self.type2test(b'%s / 100 = %d%%') b = self.type2test(b'%s / 100 = %d%%')
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), bytes) self.assertIs(type(a), self.type2test)
def test_imod(self): def test_imod(self):
b = self.type2test(b'hello, %b!') b = self.type2test(b'hello, %b!')
...@@ -504,7 +504,7 @@ class BaseBytesTest: ...@@ -504,7 +504,7 @@ class BaseBytesTest:
b = self.type2test(b'%s / 100 = %d%%') b = self.type2test(b'%s / 100 = %d%%')
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), bytes) self.assertIs(type(b), self.type2test)
def test_rmod(self): def test_rmod(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
......
...@@ -4150,8 +4150,13 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str) ...@@ -4150,8 +4150,13 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
result = PyBytes_FromStringAndSize(NULL, 0); result = PyBytes_FromStringAndSize(NULL, 0);
} }
else if (writer->use_small_buffer) { else if (writer->use_small_buffer) {
if (writer->use_bytearray) {
result = PyByteArray_FromStringAndSize(writer->small_buffer, size);
}
else {
result = PyBytes_FromStringAndSize(writer->small_buffer, size); result = PyBytes_FromStringAndSize(writer->small_buffer, size);
} }
}
else { else {
result = writer->buffer; result = writer->buffer;
writer->buffer = NULL; writer->buffer = NULL;
......
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