Title: Add `.take_bytes([n])` a zero-copy path from `bytearray` to `bytes` · Issue #139871 · python/cpython · GitHub
Open Graph Title: Add `.take_bytes([n])` a zero-copy path from `bytearray` to `bytes` · Issue #139871 · python/cpython
X Title: Add `.take_bytes([n])` a zero-copy path from `bytearray` to `bytes` · Issue #139871 · python/cpython
Description: Feature or enhancement Proposal: Update bytearray to have its internal buffer always be a PyBytesObject and add a new method, .take_bytes([n]) which extracts that buffer. .take_bytes([n]) adds a way to go from a "mutable" buffer of bytes...
Open Graph Description: Feature or enhancement Proposal: Update bytearray to have its internal buffer always be a PyBytesObject and add a new method, .take_bytes([n]) which extracts that buffer. .take_bytes([n]) adds a wa...
X Description: Feature or enhancement Proposal: Update bytearray to have its internal buffer always be a PyBytesObject and add a new method, .take_bytes([n]) which extracts that buffer. .take_bytes([n]) adds a wa...
Opengraph URL: https://github.com/python/cpython/issues/139871
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `.take_bytes([n])` a zero-copy path from `bytearray` to `bytes`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nUpdate `bytearray` to have its internal buffer always be a `PyBytesObject` and add a new method, `.take_bytes([n])` which extracts that buffer.\n\n`.take_bytes([n])` adds a way to go from a \"mutable\" buffer of bytes to an immutable one without requiring a memcpy.\n\nThis creates a path to resolve gh-60107\n\n# When would I use this?\n\nAny code which makes a `ba = bytearray()`, modifies it, then calls `bytes(ba)`. Other common patterns are `bytes(ba); ba.clear()` or `bytes(ba[:n]); del ba[:n]`. Note that if you want to discard data past a point, the most efficient pattern becomes `ba.resize(n); ba.take_bytes()` so that `take_bytes([n])` doesn’t need to keep around the soon to be discarded extra bytes.\n\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\nhttps://discuss.python.org/t/add-take-bytes-n-to-bytearray-providing-a-zero-copy-path-to-bytes/103804/5\nhttps://discuss.python.org/t/add-zero-copy-conversion-of-bytearray-to-bytes-by-providing-bytes/79164 -- edit: added\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-140128\n* gh-140770\n* gh-141741\n* gh-141862\n* gh-142243\n* gh-149520\n* gh-149622\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/cmaloney","@type":"Person","name":"cmaloney"},"datePublished":"2025-10-09T18:18:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/139871/cpython/issues/139871"}
| 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:7abab47f-5b61-25f2-cc9b-e5b06dbcf911 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F76:1FD9C:336BE91:4493BD9:6A654708 |
| html-safe-nonce | dedc4f43880fe61750df02ab70b9e69a0049832b3c32b0b0dc19a837d71c150c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Rjc2OjFGRDlDOjMzNkJFOTE6NDQ5M0JEOTo2QTY1NDcwOCIsInZpc2l0b3JfaWQiOiI3NDk2OTE3NDcxOTU2NjQ5NzM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 02fbda936e0e598905a265066cf574f12a6559036e6fac62a406ce1d7933f208 |
| hovercard-subject-tag | issue:3500184578 |
| 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/139871/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f9a3aa96dd1b26a5e27761d1eeaab9d436de66f490e8e8b77290b61a16deb28/python/cpython/issues/139871 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f9a3aa96dd1b26a5e27761d1eeaab9d436de66f490e8e8b77290b61a16deb28/python/cpython/issues/139871 |
| og:image:alt | Feature or enhancement Proposal: Update bytearray to have its internal buffer always be a PyBytesObject and add a new method, .take_bytes([n]) which extracts that buffer. .take_bytes([n]) adds a wa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cmaloney |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b |
| 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 | 309153364422b3c499922d1a2a6404910a58ed8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width