Title: io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument · Issue #115059 · python/cpython · GitHub
Open Graph Title: io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument · Issue #115059 · python/cpython
X Title: io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument · Issue #115059 · python/cpython
Description: Bug report Bug description: As reported by the StackOverflow question: https://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter In the code below, when f.read is not given ...
Open Graph Description: Bug report Bug description: As reported by the StackOverflow question: https://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter In t...
X Description: Bug report Bug description: As reported by the StackOverflow question: https://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter In t...
Opengraph URL: https://github.com/python/cpython/issues/115059
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nAs reported by the StackOverflow question:\r\nhttps://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter\r\n\r\nIn the code below, when `f.read` is not given an argument, the pending content in the write buffer gets flushed so the output is `***s is a line` as expected.\r\n```\r\nwith open(\"new.txt\", 'w+') as f:\r\n f.write(\"this is a line\")\r\n\r\nwith open(\"new.txt\", 'r+') as f:\r\n f.write(\"***\")\r\n f.read() # \u003c\u003c note here\r\n f.seek(0)\r\n print(f.read()) # outputs: ***s is a line\r\n```\r\nBut when `f.read` is given a size argument, it is apparent that the underlying write buffer is not flushed immediately, causing the pending content to be written at the end of the file, rendering an output of `this is a line***` instead:\r\n```\r\nwith open(\"new.txt\", 'w+') as f:\r\n f.write(\"this is a line\")\r\n\r\nwith open(\"new.txt\", 'r+') as f:\r\n f.write(\"***\")\r\n f.read(1) # \u003c\u003c note here\r\n f.seek(0)\r\n print(f.read()) # outputs: this is a line***\r\n```\r\nThis issue occurs only with the C implementation of `io.TextIOWrapper` since the code above would work as expected if it is prepended with:\r\n```\r\nfrom _pyio import open\r\n```\r\n\r\n### CPython versions tested on:\r\n\r\n3.8, 3.11\r\n\r\n### Operating systems tested on:\r\n\r\nLinux, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-115163\n* gh-115206\n* gh-115205\n* gh-115240\n* gh-115244\n* gh-115245\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/blhsing","@type":"Person","name":"blhsing"},"datePublished":"2024-02-06T03:28:42Z","url":"https://github.com/python/cpython/issues/115059"}
| 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:1da7ddee-2395-0d27-9cda-f7c6849091f1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8EE:46358:2DB695:3BC36F:6B02335D |
| html-safe-nonce | ed6bc2b10824cb4fca6c31a4ad91a45e34650f59ad8ede367aac2e95b507aa50 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEVFOjQ2MzU4OjJEQjY5NTozQkMzNkY6NkIwMjMzNUQiLCJ2aXNpdG9yX2lkIjoiMTEyNjE3OTkwNjE0NTg5MTE2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 581758c0b177ea0151b5fdece3f5f2af4de9addedec1b43a0de63829e3772930 |
| hovercard-subject-tag | issue:2119875587 |
| 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/115059/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a5ec224e75020290540753ef0d467ed5126927f54573b7a6ff025565d6f3d4cf/python/cpython/issues/115059 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a5ec224e75020290540753ef0d467ed5126927f54573b7a6ff025565d6f3d4cf/python/cpython/issues/115059 |
| og:image:alt | Bug report Bug description: As reported by the StackOverflow question: https://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter In t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | blhsing |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width