Skip to main content

Enable Unicode input and display when running Python from Windows console.

Project description

A Python package to enable Unicode input and display when running Python from Windows console.

General information

When running Python in the standard console on Windows, there are several problems when one tries to enter or display Unicode characters. Part of this problem lies in the fact that Python doesn’t use the right functions (i.e. ReadConsoleW and WriteConsoleW) when interacting with Windows console (see http://bugs.python.org/issue1602). The module streams of our win_unicode_concole package provides alternative raw stream objects which are then intercorporated to the standard IO hierarchy (text –> buffer –> raw). Resulting objects streams.stdin, streams.stdout, and streams.stderr can be used instead of sys.std* objects. The function streams.enable replaces the standard sys.std* objects with our ones and streams.disable reinstalls the original ones.

Replacing the stream objects solves the problem for standard IO. That is, everything which uses the standard objects for IO (e.g. input(), print(), output in Python interactive loop) works. But there is another problem – Python interactive loop doesn’t use sys.stdin for input (see http://bugs.python.org/issue17620) so an alternative implementation of interactive loop is provided in console module and its functions enable and disable maintains (de)activation of our loop.

Since there is no hook to run our interactive loop instead of the standard one, we have to wrap the execution of any Python script so our loop is run at the right place. The logic for this is contained in runner module and a helper script run.py which is located outside of out package for practical reasons.

Installation

Install the package from PyPI via pip install win-unicode-console (recommended) or download the archive and install it from the archive (e.g. pip install win_unicode_console-0.1.zip) or install the package manually by placing directory win_unicode_console and module run.py from the archive to site-packages directory of your Python installation.

Usage

Since standard Python REPL doesn’t use sys.stdin for input, custom REPL has to be installed in order to enter Unicode interactivelly. To do this run Python scripts by py -m run script.py instead of py script.py, and py -i -m run script.py instead of py -i script.py. You can put "C:\Windows\py.exe" -i -m run "%1" %* to the registry in order to run .py files interactivelly and using custom REPL. To run the custom REPL when plain interactive console is run (just ‘py’) add environment variable PYTHONSTARTUP pointing to site-packages\run.py.

You can also add win_unicode_console.streams.enable() to your sitecustomize.py in order to activate custom stream objects in any case, so scripts can be run standardly in non-interactive mode with Unicode in console support. Note that if http://bugs.python.org/issue17620 was fixed, this would be the only thing needed and our custom console would be redundant. But when it is not fixed, running a script in interactive mode strandardly won’t work with enabled custom streams.

Acknowledgements

The code of streams module is based the code submited to http://bugs.python.org/issue1602.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

win_unicode_console-0.1.2.zip (11.4 kB view details)

Uploaded Source

File details

Details for the file win_unicode_console-0.1.2.zip.

File metadata

File hashes

Hashes for win_unicode_console-0.1.2.zip
Algorithm Hash digest
SHA256 c3cda408eb2d90e583f49741accfc06c10a9260839d19d5464dcb60a5bdc77db
MD5 49056570b85fb8863bfce47acbc0b878
BLAKE2b-256 ca515e5c34fecc54fceab3317cbb09f369b8fe0545ab85f1e50e94b995e696dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page