Title: GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1 · Issue #108111 · python/cpython · GitHub
Open Graph Title: GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1 · Issue #108111 · python/cpython
X Title: GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1 · Issue #108111 · python/cpython
Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CPython versions tested on: 3.12 Operating...
Open Graph Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP...
X Description: Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP...
Opengraph URL: https://github.com/python/cpython/issues/108111
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1","articleBody":"# Bug report\r\n\r\n### Checklist\r\n\r\n- [X] I am confident this is a bug in CPython, not a bug in a third-party project\r\n- [X] I have searched the [CPython issue tracker](https://github.com/python/cpython/issues?q=is%3Aissue+sort%3Acreated-desc),\r\nand am confident this bug has not been reported before\r\n\r\n\r\n### CPython versions tested on:\r\n\r\n3.12\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\r\n\r\n### Output from running 'python -VV' on the command line:\r\n\r\nPython 3.12.0rc1 (main, Aug 16 2023, 05:03:59) [GCC 12.2.0]\r\n\r\n### A clear and concise description of the bug:\r\n\r\nI have code that writes out sections of a data file in chunks, and uses seeks to ensure that the position is correct before writing.\r\n\r\nIn the following example, I write 5 bytes, seek to position 5 and write five more bytes. If I flush the buffer, the result is as expected. If I do not, 5 null bytes are written between the two groups of intended bytes.\r\n\r\n```python\r\n#!/usr/bin/env python\r\n\r\nimport io\r\nimport gzip\r\n\r\n\r\nfor flush in (True, False):\r\n data = io.BytesIO()\r\n gzip_writer = gzip.GzipFile(fileobj=data, mode='wb')\r\n gzip_writer.write(b'abcde')\r\n\r\n # If the buffer isn't flushed, seek works from unchanged offset\r\n if flush and hasattr(gzip_writer, '_buffer'):\r\n gzip_writer._buffer.flush()\r\n\r\n gzip_writer.seek(5)\r\n gzip_writer.write(b'fghij')\r\n gzip_writer.close()\r\n \r\n # Recover result\r\n data.seek(0)\r\n gzip_reader = gzip.GzipFile(fileobj=data, mode='rb')\r\n result = gzip_reader.read()\r\n\r\n print(f'{flush=}: {result}')\r\n\r\n```\r\n\r\nIn the case where I seek but don't tell, I get spurious `\\x00` bytes:\r\n\r\n```\r\nflush=True: b'abcdefghij'\r\nflush=False: b'abcde\\x00\\x00\\x00\\x00\\x00fghij'\r\n```\r\n\r\nHere is the output in Python 3.10.10:\r\n\r\n```\r\nflush=True: b'abcdefghij'\r\nflush=False: b'abcdefghij'\r\n```\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-108341\n* gh-108402\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/effigies","@type":"Person","name":"effigies"},"datePublished":"2023-08-18T14:14:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/108111/cpython/issues/108111"}
| 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:0865bc67-df07-4797-ecf3-f2bb14562e32 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A808:2AC946:9620FE:C99270:6969BF67 |
| html-safe-nonce | 8b52083eccffa7be76b3855e7004dbdfdcb2046ab4f49aa7708769be6f3972d5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODA4OjJBQzk0Njo5NjIwRkU6Qzk5MjcwOjY5NjlCRjY3IiwidmlzaXRvcl9pZCI6IjgxNTg2Njk1OTYwNjgwMDM2ODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ed342212056e8cef7a8c582094329ab124796c7df49c040240a375face47db4d |
| hovercard-subject-tag | issue:1856797852 |
| 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/108111/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1b0ebcc0d0074a8d2617d586bb617af0248d8e4f4e60caf4246b7f1800553f37/python/cpython/issues/108111 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1b0ebcc0d0074a8d2617d586bb617af0248d8e4f4e60caf4246b7f1800553f37/python/cpython/issues/108111 |
| og:image:alt | Bug report Checklist I am confident this is a bug in CPython, not a bug in a third-party project I have searched the CPython issue tracker, and am confident this bug has not been reported before CP... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | effigies |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| 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 | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width