Merge lp:~gz/testtools/wrap_utf8_terminals_804122 into lp:~testtools-committers/testtools/trunk

Proposed by Martin Packman
Status: Merged
Merged at revision: 199
Proposed branch: lp:~gz/testtools/wrap_utf8_terminals_804122
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 51 lines (+14/-7)
2 files modified
testtools/compat.py (+3/-1)
testtools/tests/test_compat.py (+11/-6)
To merge this branch: bzr merge lp:~gz/testtools/wrap_utf8_terminals_804122
Reviewer Review Type Date Requested Status
testtools developers Pending
Review via email: mp+66635@code.launchpad.net

Description of the change

Fixes a misunderstanding I had about how smart Python 2 was when writing unicode to streams with a unicode encoding. Only print does the right thing, the write method is still dumb and some parts of testtools use `stream.write` rather than `print >>stream`.

There's a slight risk here in that a utf-8 stream will now reject non-ascii bytestrings, but that's what my terminal has been doing since I introduced this code.

To post a comment you must log in.
199. By Martin Packman

Fix test on Python 3

Revision history for this message
Jonathan Lange (jml) wrote :

Thanks for the fix. I'm going to try splitting the test into two to see if it's more clear that way.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/compat.py'
2--- testtools/compat.py 2011-04-24 21:48:56 +0000
3+++ testtools/compat.py 2011-07-01 18:06:39 +0000
4@@ -119,7 +119,9 @@
5 return codecs.getwriter("ascii")(stream, "replace")
6 if writer.__module__.rsplit(".", 1)[1].startswith("utf"):
7 # The current stream has a unicode encoding so no error handler is needed
8- return stream
9+ if sys.version_info > (3, 0):
10+ return stream
11+ return writer(stream)
12 if sys.version_info > (3, 0):
13 # Python 3 doesn't seem to make this easy, handle a common case
14 try:
15
16=== modified file 'testtools/tests/test_compat.py'
17--- testtools/tests/test_compat.py 2010-11-29 00:21:59 +0000
18+++ testtools/tests/test_compat.py 2011-07-01 18:06:39 +0000
19@@ -15,6 +15,7 @@
20 _detect_encoding,
21 _get_source_encoding,
22 _u,
23+ str_is_unicode,
24 unicode_output_stream,
25 )
26 from testtools.matchers import (
27@@ -225,14 +226,18 @@
28 unicode_output_stream(sout).write(self.uni)
29 self.assertEqual([_b("pa?\xe8?n")], sout.writelog)
30
31- def test_unicode_encodings_not_wrapped(self):
32- """A unicode encoding is left unwrapped as needs no error handler"""
33+ def test_unicode_encodings_wrapped(self):
34+ """A unicode encoding is wrapped but needs no error handler"""
35 sout = _FakeOutputStream()
36 sout.encoding = "utf-8"
37- self.assertIs(unicode_output_stream(sout), sout)
38- sout = _FakeOutputStream()
39- sout.encoding = "utf-16-be"
40- self.assertIs(unicode_output_stream(sout), sout)
41+ uout = unicode_output_stream(sout)
42+ if str_is_unicode:
43+ # No wrapping needed if native str type is unicode
44+ self.assertIs(uout, sout)
45+ return
46+ self.assertEqual(uout.errors, "strict")
47+ uout.write(self.uni)
48+ self.assertEqual([_b("pa\xc9\xaa\xce\xb8\xc9\x99n")], sout.writelog)
49
50 def test_stringio(self):
51 """A StringIO object should maybe get an ascii native str type"""

Subscribers

People subscribed via source and target branches