Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Fixed printing box drawing chars on py26 Windows.
Browse files Browse the repository at this point in the history
On Python 2.6 on Windows, trying to write box drawing characters using
sys.stdout.write (or err) raised UnicodeEncodeError. Apparently using
print() instead works fine.
  • Loading branch information
Robpol86 committed May 11, 2016
1 parent 67e8e74 commit d89d0ac
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ Changelog

This project adheres to `Semantic Versioning <http://semver.org/>`_.

2.1.1 - 2016-05-10
------------------

Fixed
* Printing box drawing characters on Windows from Python 2.6.

2.1.0 - 2016-05-07
------------------

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ test_script: tox -e lint,py35,py34,py33,py27,py26,py35x64,py34x64,py33x64,py27x6
on_finish:
- appveyor PushArtifact test_example_test_windows_screenshot.png
- appveyor PushArtifact test_windows_test_enable_disable.png
- appveyor PushArtifact test_windows_test_box_characters.png
2 changes: 1 addition & 1 deletion colorclass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@

__author__ = '@Robpol86'
__license__ = 'MIT'
__version__ = '2.1.0'
__version__ = '2.1.1'
4 changes: 3 additions & 1 deletion colorclass/windows.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Windows console screen buffer handlers."""

from __future__ import print_function

import atexit
import ctypes
import re
Expand Down Expand Up @@ -270,7 +272,7 @@ def write(self, p_str):
continue
if not RE_SPLIT.match(segment):
# No color codes, print regular text.
self._original_stream.write(segment)
print(segment, file=self._original_stream, end='')
self._original_stream.flush()
continue
for color_code in (int(c) for c in RE_NUMBER_SEARCH.findall(segment)[0].split(';')):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def readme():
name='colorclass',
packages=['colorclass'],
url='https://github.com/Robpol86/colorclass',
version='2.1.0',
version='2.1.1',
zip_safe=True,
)
Binary file added tests/sub_box_green_win10.bmp
Binary file not shown.
Binary file added tests/sub_box_green_winxp.bmp
Binary file not shown.
Binary file added tests/sub_box_sans_win10.bmp
Binary file not shown.
Binary file added tests/sub_box_sans_winxp.bmp
Binary file not shown.
68 changes: 68 additions & 0 deletions tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,71 @@ def test_enable_disable(tmpdir):
with RunNewConsole(command) as gen:
screenshot_until_match(str(screenshot), 15, with_colors, 2, gen)
screenshot_until_match(str(screenshot), 15, sans_colors, 2, gen)


@pytest.mark.skipif(str(not windows.IS_WINDOWS))
def test_box_characters(tmpdir):
"""Test for unicode errors with special characters.
:param tmpdir: pytest fixture.
"""
screenshot = PROJECT_ROOT.join('test_windows_test_box_characters.png')
if screenshot.check():
screenshot.remove()
script = tmpdir.join('script.py')
command = [sys.executable, str(script)]

script.write(dedent("""\
from __future__ import print_function
import os, time
from colorclass import Color, Windows
Windows.enable(auto_colors=True)
chars = [
'+', '-', '|',
b'\\xb3'.decode('ibm437'),
b'\\xb4'.decode('ibm437'),
b'\\xb9'.decode('ibm437'),
b'\\xba'.decode('ibm437'),
b'\\xbb'.decode('ibm437'),
b'\\xbc'.decode('ibm437'),
b'\\xbf'.decode('ibm437'),
b'\\xc0'.decode('ibm437'),
b'\\xc1'.decode('ibm437'),
b'\\xc2'.decode('ibm437'),
b'\\xc3'.decode('ibm437'),
b'\\xc4'.decode('ibm437'),
b'\\xc5'.decode('ibm437'),
b'\\xc8'.decode('ibm437'),
b'\\xc9'.decode('ibm437'),
b'\\xca'.decode('ibm437'),
b'\\xcb'.decode('ibm437'),
b'\\xcc'.decode('ibm437'),
b'\\xcd'.decode('ibm437'),
b'\\xce'.decode('ibm437'),
b'\\xd9'.decode('ibm437'),
b'\\xda'.decode('ibm437'),
]
for c in chars:
print(c, end='')
print()
for c in chars:
print(Color.green(c, auto=True), end='')
print()
stop_after = time.time() + 20
while not os.path.exists(r'%s') and time.time() < stop_after:
time.sleep(0.5)
""") % str(screenshot))

# Setup expected.
with_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_green_*.bmp')]
sans_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_sans_*.bmp')]
assert with_colors
assert sans_colors

# Run.
with RunNewConsole(command) as gen:
screenshot_until_match(str(screenshot), 15, with_colors, 1, gen)
screenshot_until_match(str(screenshot), 15, sans_colors, 1, gen)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
author = @Robpol86
license = MIT
name = colorclass
version = 2.1.0
version = 2.1.1

[tox]
envlist = lint,py{34,27,26}
Expand Down

0 comments on commit d89d0ac

Please sign in to comment.