Title: _CustomTextWrap produces lines wider than `width` after the #399 fix (032dc0d) · Issue #478 · astanin/python-tabulate · GitHub
Open Graph Title: _CustomTextWrap produces lines wider than `width` after the #399 fix (032dc0d) · Issue #478 · astanin/python-tabulate
X Title: _CustomTextWrap produces lines wider than `width` after the #399 fix (032dc0d) · Issue #478 · astanin/python-tabulate
Description: The fix for #399 (032dc0d, on master, not in a release yet) stops the infinite loop, but it also makes some lines wider than the requested width. Before that commit, the same inputs wrapped correctly. Minimal repro >>> import tabulate, w...
Open Graph Description: The fix for #399 (032dc0d, on master, not in a release yet) stops the infinite loop, but it also makes some lines wider than the requested width. Before that commit, the same inputs wrapped correct...
X Description: The fix for #399 (032dc0d, on master, not in a release yet) stops the infinite loop, but it also makes some lines wider than the requested width. Before that commit, the same inputs wrapped correct...
Opengraph URL: https://github.com/astanin/python-tabulate/issues/478
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_CustomTextWrap produces lines wider than `width` after the #399 fix (032dc0d)","articleBody":"The fix for #399 (032dc0d, on `master`, not in a release yet) stops the infinite loop, but it also makes some lines wider than the requested width. Before that commit, the same inputs wrapped correctly.\n\n### Minimal repro\n\n```python\n>>> import tabulate, wcwidth\n>>> out = tabulate._CustomTextWrap(width=3).wrap(\"a 글글\")\n>>> out, [wcwidth.wcswidth(x) for x in out]\n(['a 글', '글'], [4, 2]) # master @ 268615a: first line is 4 columns in a 3-column width\n```\n\nBefore 032dc0d (and on PyPI 0.10.0): `['a ', '글', '글']`, which fits.\n\nIn a table:\n\n```python\n>>> print(tabulate.tabulate([[\"a 글글\"]], maxcolwidths=[3], tablefmt=\"grid\"))\n+------+\n| a 글 |\n| 글 |\n+------+\n```\n\n### Cause\n\nIn `_handle_long_word`:\n\n```python\ni = max(i, 2)\n```\n\nThis forces at least one character onto the line even when `cur_line` already has content, so a wide character that doesn't fit in the remaining space gets squeezed onto a non-empty line. Forcing progress is only needed when the line is empty. Taking the character in that case still breaks the loop, and otherwise the chunk can move to the next line as before:\n\n```python\nif i == 1 and not cur_line and stripped_chunk:\n i = 2\n```\n\n### How I found it\n\nI compared the pre-fix code and master on 6,910 random strings (a mix of ASCII, CJK, emoji, combining marks and ANSI codes, at widths 1/2/3/4/7). The pre-fix code hangs on 1,291 of them. Master fixes all of those hangs, but changes 201 outputs that previously terminated, and all 201 now have a line wider than `width`. With the guard above, every hang is fixed and none of the previously-terminating outputs change. The full `test/` suite passes, including the #399 test.\n\nHappy to open a PR with this change and a regression test if that's useful.\n","author":{"url":"https://github.com/RootCauseWeekly","@type":"Person","name":"RootCauseWeekly"},"datePublished":"2026-10-23T21:08:47Z","url":"https://github.com/astanin/python-tabulate/issues/478"}
| 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:90db446a-6974-4440-5b1d-1141bde3109a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E650:222894:9D053E:D5D045:6AFF3C8E |
| html-safe-nonce | e39ec008a1080a22d111fdced4c913e1fb1822d2d30e20c6070791b3e89cfa30 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjUwOjIyMjg5NDo5RDA1M0U6RDVEMDQ1OjZBRkYzQzhFIiwidmlzaXRvcl9pZCI6IjUwODcxNTcwNjc2MDEzNjIwNjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 730ca7e5f810a9aa87c8a150e1a55d0d8f3707fb0376a5a21a025744c9ffac7f |
| hovercard-subject-tag | issue:6005479461 |
| 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/astanin/python-tabulate/478/issue_layout |
| twitter:image | https://opengraph.githubassets.com/84a8d15fb565866ea2bf85dd4a7b92625be168474f02c14dc25f645fc91f6d14/astanin/python-tabulate/issues/478 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/84a8d15fb565866ea2bf85dd4a7b92625be168474f02c14dc25f645fc91f6d14/astanin/python-tabulate/issues/478 |
| og:image:alt | The fix for #399 (032dc0d, on master, not in a release yet) stops the infinite loop, but it also makes some lines wider than the requested width. Before that commit, the same inputs wrapped correct... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RootCauseWeekly |
| hostname | github.com |
| expected-hostname | github.com |
| None | 07f40d184b74ca701a2005d4a164bf7038be26fc145c2623336a91fe477cc584 |
| turbo-cache-control | no-preview |
| go-import | github.com/astanin/python-tabulate git https://github.com/astanin/python-tabulate.git |
| octolytics-dimension-user_id | 71802 |
| octolytics-dimension-user_login | astanin |
| octolytics-dimension-repository_id | 205875548 |
| octolytics-dimension-repository_nwo | astanin/python-tabulate |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 205875548 |
| octolytics-dimension-repository_network_root_nwo | astanin/python-tabulate |
| 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 | 8e428a801c2dccf4d1f411040a0e0b7fab7d4061 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width