Title: `readline.set_history_length` corrupts history files when used in a libedit build · Issue #121160 · python/cpython · GitHub
Open Graph Title: `readline.set_history_length` corrupts history files when used in a libedit build · Issue #121160 · python/cpython
X Title: `readline.set_history_length` corrupts history files when used in a libedit build · Issue #121160 · python/cpython
Description: Bug report Bug description: Since the libedit transition (correction: If an end user switches from a GNU readline build of Python to an editline build of Python), several important functions related to history files are broken. It is inc...
Open Graph Description: Bug report Bug description: Since the libedit transition (correction: If an end user switches from a GNU readline build of Python to an editline build of Python), several important functions relate...
X Description: Bug report Bug description: Since the libedit transition (correction: If an end user switches from a GNU readline build of Python to an editline build of Python), several important functions relate...
Opengraph URL: https://github.com/python/cpython/issues/121160
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`readline.set_history_length` corrupts history files when used in a libedit build","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\n~~Since the libedit transition~~ ([correction](https://github.com/python/cpython/issues/121160#issuecomment-2200501386): If an end user switches from a GNU readline build of Python to an editline build of Python), several important functions related to history files are broken. \r\n\r\n1. It is inconvenient enough that history files written by GNU readline based `readline` module are unreadable and result in a rather confusing `OSError` with `exc.errno == errno.EINVAL` (without a corresponding operating system routine that returns such an error), but a workaround intercepting this `OSError` can be written that converts the old format to the new one. This has been covered before, e.g. in https://github.com/python/cpython/issues/120766.\r\n2. More importantly, history files written by libedit based `readline` module are unreadable by itself when `set_history_length` is used, as demonstrated by the code below.\r\n3. While a workaround for that is also possible (also below), as far as I can tell, until 3.13, so until a year from now (?) (whenever https://github.com/python/cpython/issues/112510 becomes available in a release), your only option is `\"libedit\" in readline.__doc__`, which I expect will be what many people will leave in the codebase even once `readline.backend` becomes available.\r\n\r\nReproducer:\r\n\r\n```python\r\nimport readline\r\n\r\nreadline.add_history(\"foo bar\")\r\nreadline.add_history(\"nope nope\")\r\nreadline.write_history_file(\"history-works\")\r\n\r\n# this works:\r\nreadline.read_history_file(\"history-works\")\r\n\r\nreadline.set_history_length(2)\r\nreadline.write_history_file(\"history-breaks\")\r\n\r\n# this breaks:\r\nreadline.read_history_file(\"history-breaks\")\r\n```\r\n\r\nWorkaround:\r\n\r\n```python\r\ndef _is_using_libedit():\r\n if hasattr(readline, \"backend\"):\r\n return readline.backend == \"editline\"\r\n else:\r\n return \"libedit\" in readline.__doc__\r\n\r\nreadline.set_history_length(1000)\r\nif _is_using_libedit():\r\n readline.replace_history_item(\r\n max(0, readline.get_current_history_length() - readline.get_history_length()),\r\n \"_HiStOrY_V2_\")\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\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-121326\n* gh-121327\n* gh-121856\n* gh-121857\n* gh-122030\n* gh-122031\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/whitequark","@type":"Person","name":"whitequark"},"datePublished":"2024-06-29T15:58:02Z","url":"https://github.com/python/cpython/issues/121160"}
| 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:49f528b5-4622-f836-003a-e94e5374dc2b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 840C:4BB79:38298B0:47BEE24:6B01FE94 |
| html-safe-nonce | 196fd65844f5b20bf445c6a3d5aff4d9bf9206df7a06c8f75262d2f466cea327 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDBDOjRCQjc5OjM4Mjk4QjA6NDdCRUUyNDo2QjAxRkU5NCIsInZpc2l0b3JfaWQiOiI4MzYzOTkzODE4MDc3NTg5OTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3ce241522ec618f5ec3dc14207a810a077d1e4d882905a192daa06f7fa9569bc |
| hovercard-subject-tag | issue:2381826981 |
| 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/121160/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7dccefa0a960625870af3846670a1020532e049506553e781a6216ceeae7d2fc/python/cpython/issues/121160 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7dccefa0a960625870af3846670a1020532e049506553e781a6216ceeae7d2fc/python/cpython/issues/121160 |
| og:image:alt | Bug report Bug description: Since the libedit transition (correction: If an end user switches from a GNU readline build of Python to an editline build of Python), several important functions relate... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | whitequark |
| 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