Title: [C API] PEP 782: Add PyBytesWriter API · Issue #129813 · python/cpython · GitHub
Open Graph Title: [C API] PEP 782: Add PyBytesWriter API · Issue #129813 · python/cpython
X Title: [C API] PEP 782: Add PyBytesWriter API · Issue #129813 · python/cpython
Description: Feature or enhancement Proposal: I propose adding a PyBytesWriter API to create bytes objects. Efficient API thanks to overallocation in PyBytesWriter_Extend() and usage of a "small buffer" of (around) 256 bytes Avoid creating incomplete...
Open Graph Description: Feature or enhancement Proposal: I propose adding a PyBytesWriter API to create bytes objects. Efficient API thanks to overallocation in PyBytesWriter_Extend() and usage of a "small buffer" of (aro...
X Description: Feature or enhancement Proposal: I propose adding a PyBytesWriter API to create bytes objects. Efficient API thanks to overallocation in PyBytesWriter_Extend() and usage of a "small buffer&quo...
Opengraph URL: https://github.com/python/cpython/issues/129813
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] PEP 782: Add PyBytesWriter API","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nI propose adding a PyBytesWriter API to create `bytes` objects.\n\n* Efficient API thanks to overallocation in `PyBytesWriter_Extend()` and usage of a \"small buffer\" of (around) 256 bytes\n* [Avoid creating incomplete/inconsistent](https://github.com/capi-workgroup/api-evolution/issues/36) `bytes` objects.\n* [Avoid mutating immutable](https://github.com/capi-workgroup/api-evolution/issues/20) `bytes`.\n\nAPI:\n\n```c\ntypedef struct PyBytesWriter PyBytesWriter;\n\nPyAPI_FUNC(void*) PyBytesWriter_Create(\n PyBytesWriter **writer,\n Py_ssize_t alloc);\nPyAPI_FUNC(void) PyBytesWriter_Discard(\n PyBytesWriter *writer);\nPyAPI_FUNC(PyObject*) PyBytesWriter_Finish(\n PyBytesWriter *writer,\n void *buf);\n\nPyAPI_FUNC(Py_ssize_t) PyBytesWriter_GetRemaining(\n PyBytesWriter *writer,\n void *buf);\nPyAPI_FUNC(void*) PyBytesWriter_Extend(\n PyBytesWriter *writer,\n void *buf,\n Py_ssize_t extend);\nPyAPI_FUNC(void*) PyBytesWriter_WriteBytes(\n PyBytesWriter *writer,\n void *buf,\n const void *bytes,\n Py_ssize_t size);\nPyAPI_FUNC(void*) PyBytesWriter_Format(\n PyBytesWriter *writer,\n void *buf,\n const char *format,\n ...);\n```\n\nSimple example creating the string `b\"abc\"`:\n\n```c\nPyObject* create_abc(void)\n{\n PyBytesWriter *writer;\n char *str = PyBytesWriter_Create(&writer, 3);\n if (writer == NULL) return NULL;\n\n memcpy(str, \"abc\", 3);\n str += 3;\n\n return PyBytesWriter_Finish(writer, str);\n}\n```\n\nExample formatting an integer in decimal, the size is not known in advance::\n\n```c\nPyObject* format_int(int value)\n{\n PyBytesWriter *writer;\n char *str = PyBytesWriter_Create(&writer, 20);\n if (writer == NULL) return NULL;\n\n str += PyOS_snprintf(str, 20, \"%i\", value);\n\n return PyBytesWriter_Finish(writer, str);\n}\n```\n\nNote: using `PyBytesWriter_Format()` would make this code simpler.\n\nExample using `PyBytesWriter_Extend()`,smilar to `bytes.center()` with a different API: spaces are number of whitespaces added to the left and to the right:\n\n```c\nstatic PyObject *\nbyteswriter_center_example(Py_ssize_t spaces, char *str, Py_ssize_t str_size)\n{\n PyBytesWriter *writer;\n char *buf = PyBytesWriter_Create(&writer, spaces * 2);\n if (buf == NULL) {\n goto error;\n }\n assert(PyBytesWriter_GetRemaining(writer, buf) == spaces * 2);\n\n // Add left spaces\n memset(buf, ' ', spaces);\n buf += spaces;\n assert(PyBytesWriter_GetRemaining(writer, buf) == spaces);\n\n // Copy string\n buf = PyBytesWriter_Extend(writer, buf, str_size);\n if (buf == NULL) {\n goto error;\n }\n assert(PyBytesWriter_GetRemaining(writer, buf) == spaces + str_size);\n\n memcpy(buf, str, str_size);\n buf += str_size;\n assert(PyBytesWriter_GetRemaining(writer, buf) == spaces);\n\n // Add right spaces\n memset(buf, ' ', spaces);\n buf += spaces;\n assert(PyBytesWriter_GetRemaining(writer, buf) == 0);\n\n return PyBytesWriter_Finish(writer, buf);\n\nerror:\n PyBytesWriter_Discard(writer);\n return NULL;\n}\n```\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\nMy previous attempt in July/August 2024:\n\n* https://github.com/capi-workgroup/decisions/issues/39\n* https://github.com/python/cpython/pull/121726\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-129814\n* gh-131520\n* gh-131681\n* gh-138822\n* gh-138824\n* gh-138825\n* gh-138829\n* gh-138830\n* gh-138831\n* gh-138832\n* gh-138833\n* gh-138835\n* gh-138836\n* gh-138837\n* gh-138838\n* gh-138839\n* gh-138840\n* gh-138874\n* gh-138901\n* gh-138919\n* gh-138920\n* gh-138921\n* gh-138922\n* gh-138923\n* gh-138924\n* gh-138929\n* gh-138930\n* gh-138941\n* gh-138954\n* gh-138955\n* gh-138957\n* gh-138964\n* gh-138986\n* gh-139044\n* gh-139047\n* gh-139048\n* gh-139054\n* gh-139097\n* gh-139101\n* gh-139113\n* gh-138956\n* gh-139121\n* gh-139131\n* gh-139151\n* gh-139264\n* gh-139892\n* gh-140152\n* gh-145900\n* gh-156938\n* gh-156979\n* gh-157226\n* gh-159261\n* gh-162257\n* gh-162259\n* gh-162260\n* gh-162297\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2025-02-07T15:44:08Z","url":"https://github.com/python/cpython/issues/129813"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8712efa0-960e-bbdd-5aed-bfc355692e0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9534:AFBD9:11DF1D3:17A3BD7:6B01E84C |
| html-safe-nonce | 71b805d423ca767746e9d82c410fc95e16166e7d55f98eb662bd927fcbb07314 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTM0OkFGQkQ5OjExREYxRDM6MTdBM0JENzo2QjAxRTg0QyIsInZpc2l0b3JfaWQiOiI2NTgxNzIxMzQ4ODI3MjEyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fc1c44850e2b593f85b0ab3989d6df0417ab0ad8a1996bda3a53ed32069fbd11 |
| hovercard-subject-tag | issue:2838479949 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/129813/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b982931fc34b2e4a578c9d33db19d644ce4c1f3f6572cdfe30bff324c581e53e/python/cpython/issues/129813 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b982931fc34b2e4a578c9d33db19d644ce4c1f3f6572cdfe30bff324c581e53e/python/cpython/issues/129813 |
| og:image:alt | Feature or enhancement Proposal: I propose adding a PyBytesWriter API to create bytes objects. Efficient API thanks to overallocation in PyBytesWriter_Extend() and usage of a "small buffer" of (aro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width