Title: bz2.BZ2File / gzip.GZipFile / lzma.LZMAFile expose misleading `fileno` method. · Issue #100066 · python/cpython · GitHub
Open Graph Title: bz2.BZ2File / gzip.GZipFile / lzma.LZMAFile expose misleading `fileno` method. · Issue #100066 · python/cpython
X Title: bz2.BZ2File / gzip.GZipFile / lzma.LZMAFile expose misleading `fileno` method. · Issue #100066 · python/cpython
Description: The various compressing/decompressing file wrappers (bz2.BZ2File, gzip.GZipFile, lzma.LZMAFile) currently have fileno methods that return the underlying file descriptor: cpython/Lib/bz2.py Lines 126 to 129 in 0a4c82d def fileno(self): ""...
Open Graph Description: The various compressing/decompressing file wrappers (bz2.BZ2File, gzip.GZipFile, lzma.LZMAFile) currently have fileno methods that return the underlying file descriptor: cpython/Lib/bz2.py Lines 12...
X Description: The various compressing/decompressing file wrappers (bz2.BZ2File, gzip.GZipFile, lzma.LZMAFile) currently have fileno methods that return the underlying file descriptor: cpython/Lib/bz2.py Lines 12...
Opengraph URL: https://github.com/python/cpython/issues/100066
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bz2.BZ2File / gzip.GZipFile / lzma.LZMAFile expose misleading `fileno` method.","articleBody":"The various compressing/decompressing file wrappers (`bz2.BZ2File`, `gzip.GZipFile`, `lzma.LZMAFile`) currently have `fileno` methods that return the _underlying_ file descriptor: https://github.com/python/cpython/blob/0a4c82ddd34a3578684b45b76f49cd289a08740b/Lib/bz2.py#L126-L129\r\n\r\nI imagine this was done because it seemed useful, but I'm not sure what use it is. You can't safely use things like `select` since the compression/decompression might buffer, and passing it to things that use the file descriptor directly will produce garbage (when reading) or corrupt the file (when writing).\r\n\r\nAn example how misleading this can be, courtesy of @ericfrederich:\r\n```\r\n\u003e\u003e\u003e import bz2\r\n\u003e\u003e\u003e import subprocess\r\n\u003e\u003e\u003e with bz2.open('/tmp/out.bz2', 'w') as f:\r\n... subprocess.check_call(['echo', '-n', \"Why doesn't this work?\"], stdout=f)\r\n...\r\n0\r\n\u003e\u003e\u003e bz2.open('/tmp/out.bz2', 'r').read()\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/usr/lib/python3.7/bz2.py\", line 178, in read\r\n return self._buffer.read(size)\r\n File \"/usr/lib/python3.7/_compression.py\", line 103, in read\r\n data = self._decompressor.decompress(rawblock, size)\r\nOSError: Invalid data stream\r\n\u003e\u003e\u003e open('/tmp/out.bz2', 'rb').read()\r\nb\"Why doesn't this work?BZh9\\x17rE8P\\x90\\x00\\x00\\x00\\x00\"\r\n```\r\n\r\nNote the (empty) bz2 data after the data written by the subprocess.\r\n\r\nAm I missing a situation where this is actually useful? If there isn't one, can we consider adding a warning for the confusing behaviour?","author":{"url":"https://github.com/Yhg1s","@type":"Person","name":"Yhg1s"},"datePublished":"2022-12-06T22:25:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/100066/cpython/issues/100066"}
| 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:df946545-9429-e87f-878c-0d3b36c6ff81 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED40:3609E5:2815F7:372E51:6A6028A2 |
| html-safe-nonce | 2f23da70584dc41708bdfedf9757c8ae7fe29d2d34b3ea0d09137914315fc2df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDQwOjM2MDlFNToyODE1Rjc6MzcyRTUxOjZBNjAyOEEyIiwidmlzaXRvcl9pZCI6IjY5MTg1MTcwOTk1MzcxMTUyOTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9f1dfefb26ea5ad01c01813f1ab23f851b8377b84f88f1866040bed11d86ef6f |
| hovercard-subject-tag | issue:1480337648 |
| 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/100066/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ac652265d516ab24bcd56f97e79cf184aa15372389f62540003d94ad334ca7e4/python/cpython/issues/100066 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ac652265d516ab24bcd56f97e79cf184aa15372389f62540003d94ad334ca7e4/python/cpython/issues/100066 |
| og:image:alt | The various compressing/decompressing file wrappers (bz2.BZ2File, gzip.GZipFile, lzma.LZMAFile) currently have fileno methods that return the underlying file descriptor: cpython/Lib/bz2.py Lines 12... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Yhg1s |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60da8c2a42fa2bbf5f7567474990ec467836a84444262a58e200fa91b7f3d2d0 |
| 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 | 9824515e740d83d5eb82168a089b806ab0fe04a1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width