Title: non-readline PyOS_StdioReadline when used with PyOS_InputHook fails is buggy with long input · Issue #103929 · python/cpython · GitHub
Open Graph Title: non-readline PyOS_StdioReadline when used with PyOS_InputHook fails is buggy with long input · Issue #103929 · python/cpython
X Title: non-readline PyOS_StdioReadline when used with PyOS_InputHook fails is buggy with long input · Issue #103929 · python/cpython
Description: Bug report To get this bug you must: not be using the readline based stdin code path must have a GUI toolkit with install as PyOS_InputHook imported try to enter stings into input that are 99 characters or longer. stdin is in line buffer...
Open Graph Description: Bug report To get this bug you must: not be using the readline based stdin code path must have a GUI toolkit with install as PyOS_InputHook imported try to enter stings into input that are 99 chara...
X Description: Bug report To get this bug you must: not be using the readline based stdin code path must have a GUI toolkit with install as PyOS_InputHook imported try to enter stings into input that are 99 chara...
Opengraph URL: https://github.com/python/cpython/issues/103929
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"non-readline PyOS_StdioReadline when used with PyOS_InputHook fails is buggy with long input","articleBody":"# Bug report\r\n\r\nTo get this bug you must:\r\n - not be using the `readline` based stdin code path\r\n - must have a GUI toolkit with install as `PyOS_InputHook` imported\r\n - try to enter stings into `input` that are 99 characters or longer.\r\n - stdin is in line buffered mode\r\n\r\nThe source of the problem is that in \r\n\r\nhttps://github.com/python/cpython/blob/385d8d2b6438c7825006df6106dab41a95332a6a/Parser/myreadline.c#L47-L58\r\n\r\nwhich is called from https://github.com/python/cpython/blob/385d8d2b6438c7825006df6106dab41a95332a6a/Parser/myreadline.c#L304-L322\r\n\r\nWe get the following sequence of events:\r\n\r\n - user calls `input`\r\n - `my_fgets` calls the input hook which blocks until stdin reports ready to read\r\n - `fgets` reads up to the first 99 characters and `my_fgets` returns\r\n - if the input is longer than 99 (including the new line), then the last read character will not be the newline and the calling loop will call `my_fgets` again\r\n - The input hook will be called again, but because there is no new user input (just remaining characters from the last pass) the inputhook blocks.\r\n - if the user hits enter a second time the inputhook will return and `fgets` will read to the original new line\r\n - the second new line will still be in the stdin buffer and will come out immediately the next time `input` is called.\r\n\r\n\r\nPossible flaws in my understanding:\r\n\r\n - I am not clear why the next `input` call immediately rather than getting stuck in the input hook\r\n - entries that are multiples of 100 do not require multiple extra enters\r\n\r\n\r\nThis script demonstrates the problem:\r\n\r\n```python\r\nimport string\r\nimport sys\r\nfrom tkinter import Tk\r\nfrom tkinter import ttk\r\n\r\n\r\ndef run():\r\n \"\"\"\r\n This sets up a minimal tk application that has enough functionality\r\n to verify it is \"live\" and the inputhook is running while waiting for\r\n user input.\r\n \"\"\"\r\n root = Tk()\r\n frm = ttk.Frame(root, padding=10)\r\n frm.grid()\r\n lbl = ttk.Label(frm, text=\"push count = 0\")\r\n lbl.grid(column=0, row=0)\r\n\r\n j = 0\r\n\r\n def set_label():\r\n nonlocal j\r\n j += 1\r\n lbl[\"text\"] = f\"push count = {j}\"\r\n\r\n ttk.Button(frm, text=\"Push me!\", command=set_label).grid(column=1, row=0)\r\n\r\n return root, frm\r\n\r\n\r\nrun()\r\n\r\nprint(\"This is a demo of a bug in the non-readline based stdio code\\n\\n\")\r\nprint(f\"The readline module is not loaded: {'readline' in sys.modules=}\")\r\n\r\ntest_string = (string.ascii_lowercase + string.ascii_uppercase) * 2\r\n\r\nprint(\r\n f\"\"\"\r\nWe are using the test string :\r\n\r\n\\t{test_string}\r\n\r\nas it is easy to eyeball the length (it is 104 characters long in 26\r\ncharacter blocks).\r\n\r\nYou should see a tk window with a button that says \"Push me!\" and a counter.\r\nPushing the button should increment the counter.\r\n\r\nFollow the instructions to demonstrate the bug.\r\n\r\n\r\n\"\"\"\r\n)\r\n\r\nprint(\r\n f\"\"\"\r\nTo see a case where it works paste\r\n\r\n{test_string[:10]}\r\n\r\ninto the prompt below. Before hitting return, try pushing\r\nthe button on the UI to verify that the inputhook is running.\r\n\"\"\"\r\n)\r\n\r\na = input(\"paste here >> \")\r\nprint(f\"You pasted {a}\")\r\n\r\n\r\nprint(\r\n f\"\"\"\r\nTo see it fail past the full string\r\n\r\n{test_string}\r\n\r\ninto the prompt below (you will have to hit enter twice)\r\n\"\"\"\r\n)\r\n\r\na = input(\"paste here >> \")\r\nprint(f\"You pasted {a}\")\r\n\r\nprint(\"There is still a newline in the buffer, this input will be 'skipped'\\n\")\r\na = input(\"you can not input here >> \")\r\nprint(f\"we got an empty string!: {a=!r} (also note no new line in stdout)\")\r\n\r\n\r\nprint(\r\n f\"\"\"\r\n\r\nyou can now play with it or ctrl-d to exit.\r\n\r\nThe longest string that works is (98 letters + new line):\r\n\r\n{test_string[:98]}\r\n\r\n\"\"\"\r\n)\r\n\r\nwhile True:\r\n print(\"\\n\")\r\n a = input(\"test input >> \")\r\n print(f\"what you entered: {a=}\")\r\n```\r\n\r\nThis needs to be run as `python demo.py` not pasted into a shell because the code paths that rely on `readline` work correctly. Running as `python -uu demo.py` also works correctly.\r\n\r\n# Your environment\r\n\r\n\u003c!-- Include as many relevant details as possible about the environment you experienced the bug in -->\r\n\r\n- CPython versions tested on: 3.10.10, 3.11.3, 3.9+\r\n- Operating system and architecture: (arch) linux x86, OSX\r\n\r\nThis was originally reported via https://github.com/matplotlib/matplotlib/issues/25756 where you can see my notes as I sorted this out.\r\n\r\nBased on the code paths I expect this to not be reproducible on Windows.\r\n\r\nI think this bug goes back to at least 717c6f95bebcd4693781e25bae3f7f9900cece07 so I expect all currently supported versions of Python to be affected.\r\n\r\nI will shortly open a PR with a proposed fix.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-103931\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tacaswell","@type":"Person","name":"tacaswell"},"datePublished":"2023-04-27T15:32:33Z","url":"https://github.com/python/cpython/issues/103929"}
| 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:6c6f267c-70bc-334d-9253-8e131bb636dd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB70:1DC474:18F286:20F6CB:6B024A63 |
| html-safe-nonce | a11dcf5cc6997b28d321f567e3f6a356a0718019f8da16b7d61a1dd4161b8f96 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjcwOjFEQzQ3NDoxOEYyODY6MjBGNkNCOjZCMDI0QTYzIiwidmlzaXRvcl9pZCI6IjU4MzczNTk5NzU4NTY0OTExMDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5bb3980b38b1ab0c9632fd25435c0403ba0aab0e061dbe9db3b1040ab11652ae |
| hovercard-subject-tag | issue:1687096734 |
| 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/103929/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e9a95e8173144ec03e6ac90caae2bd8b39ad741a7641c6dedfeda7a6eabaf6e4/python/cpython/issues/103929 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e9a95e8173144ec03e6ac90caae2bd8b39ad741a7641c6dedfeda7a6eabaf6e4/python/cpython/issues/103929 |
| og:image:alt | Bug report To get this bug you must: not be using the readline based stdin code path must have a GUI toolkit with install as PyOS_InputHook imported try to enter stings into input that are 99 chara... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tacaswell |
| 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