gh-129813, PEP 782: Add PyBytesWriter_Format() (#138824)

Modify PyBytes_FromFormatV() to use the public PyBytesWriter API
rather than the _PyBytesWriter private API.
This commit is contained in:
Victor Stinner 2025-09-12 14:21:57 +02:00 committed by GitHub
parent 419441a6e1
commit c3fca5d478
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 130 additions and 30 deletions

View file

@ -361,12 +361,26 @@ def test_resize(self):
writer.resize(len(b'number=123456'), b'456')
self.assertEqual(writer.finish(), self.result_type(b'number=123456'))
def test_format_i(self):
# Test PyBytesWriter_Format()
writer = self.create_writer()
writer.format_i(b'x=%i', 123456)
self.assertEqual(writer.finish(), self.result_type(b'x=123456'))
writer = self.create_writer()
writer.format_i(b'x=%i, ', 123)
writer.format_i(b'y=%i', 456)
self.assertEqual(writer.finish(), self.result_type(b'x=123, y=456'))
def test_example_abc(self):
self.assertEqual(_testcapi.byteswriter_abc(), b'abc')
def test_example_resize(self):
self.assertEqual(_testcapi.byteswriter_resize(), b'Hello World')
def test_example_highlevel(self):
self.assertEqual(_testcapi.byteswriter_highlevel(), b'Hello World!')
class ByteArrayWriterTest(BytesWriterTest):
result_type = bytearray
@ -374,5 +388,6 @@ class ByteArrayWriterTest(BytesWriterTest):
def create_writer(self, alloc=0, string=b''):
return _testcapi.PyBytesWriter(alloc, string, 1)
if __name__ == "__main__":
unittest.main()