Skip to content

fix(generator): fall back to the login link on cp1252 consoles - #195

Open
MohammedAlkindi wants to merge 1 commit into
chigwell:mainfrom
MohammedAlkindi:fix/qr-unicode-fallback
Open

fix(generator): fall back to the login link on cp1252 consoles#195
MohammedAlkindi wants to merge 1 commit into
chigwell:mainfrom
MohammedAlkindi:fix/qr-unicode-fallback

Conversation

@MohammedAlkindi

Copy link
Copy Markdown

Fixes #150.

On Windows, redirected stdout falls back to the ANSI code page (cp1252). The half-block characters from print_ascii cannot be encoded, so _render_qr dies with UnicodeEncodeError before any login path is shown. Reproduced on Windows 11, Python 3.13, piped stdout.

Two changes in session_string_generator.py:

  • _render_qr now checks whether stdout can encode the rendered art and prints the tg:// login link instead when it cannot. Same fallback idea qsardor proposed in the issue body; I test encodability up front instead of catching, because with hardened stdio (below) the print no longer raises, it would just draw an unscannable grid of ?.
  • main() reconfigures stdout/stderr with errors="replace". As tonydzi noted, the QR art is only the first member of this failure class: the account label and error text also reach stdout and can carry non-latin characters. This stops any of them from aborting a login, and it helps users who cannot set PYTHONUTF8=1 in their launcher.

Three regression tests added. The fallback test fails on main with the exact error from the issue. black, flake8 and the whitespace hooks pass on the changed files; the docker compose hook needs a local .env and passes once one exists.

Verification on Windows 11, Python 3.13

Before, piped stdout under cp1252:

File "session_string_generator.py", line 80, in _render_qr
    print(f.getvalue())
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-26

After, same environment:

This console (cp1252) cannot draw the QR code.
Open this link on a device where you're logged in instead:
  tg://login?token=...
Expires at: 11:29:03
Waiting for you to scan...

pytest tests/test_session_string_generator.py: 15 passed.
Full suite: 276 passed, 8 failed identically on main and on this branch
(POSIX permission and pool-lock tests that do not hold on Windows).

Redirected stdout on Windows uses the ANSI code page, which cannot
encode the half-block characters print_ascii emits, so the generator
crashed with UnicodeEncodeError before showing any login path (chigwell#150).
Render the QR art only when stdout can encode it, otherwise print the
tg:// login link, and reconfigure stdio with errors=replace so no other
print site (account labels, error text) can abort a login mid-flight.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix 'charmap' codec can't encode error on Windows / agent logs (UnicodeEncodeError fallback in QR rendering)

1 participant