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.
This commit is contained in:
Victor Stinner 2016-04-15 17:52:27 +02:00
parent 12bb6f4c7d
commit e914d41312
2 changed files with 8 additions and 3 deletions

View File

@ -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:
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):

View File

@ -4150,7 +4150,12 @@ _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) {
result = PyBytes_FromStringAndSize(writer->small_buffer, size); if (writer->use_bytearray) {
result = PyByteArray_FromStringAndSize(writer->small_buffer, size);
}
else {
result = PyBytes_FromStringAndSize(writer->small_buffer, size);
}
} }
else { else {
result = writer->buffer; result = writer->buffer;