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

Proposed by Martin Packman
Status: Merged
Merged at revision: 131
Proposed branch: lp:~gz/testtools/bytes_to_join_bytes_675331
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 73 lines (+18/-8)
2 files modified
testtools/content.py (+5/-2)
testtools/tests/test_content.py (+13/-6)
To merge this branch: bzr merge lp:~gz/testtools/bytes_to_join_bytes_675331
Reviewer Review Type Date Requested Status
testtools developers Pending
Review via email: mp+40922@code.launchpad.net

Description of the change

Make some ugly code uglier so it has the right semantics on Python 3. As well as the problem raised in the bug, the __repr__ method had a similar issue.

Noting the obvious:
* Content objects want (but don't really validate that they get) byte strings, which means str for Python 2 and bytes for Python 3.
* A __repr__ method must return the native str type, which is a byte string on Python 2 and a unicode string on Python 3.

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

Just merged this. Thanks heaps!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/content.py'
2--- testtools/content.py 2010-10-28 22:06:41 +0000
3+++ testtools/content.py 2010-11-16 00:28:16 +0000
4@@ -9,6 +9,9 @@
5 from testtools.testresult import TestResult
6
7
8+_join_b = _b("").join
9+
10+
11 class Content(object):
12 """A MIME-like Content object.
13
14@@ -31,7 +34,7 @@
15
16 def __eq__(self, other):
17 return (self.content_type == other.content_type and
18- ''.join(self.iter_bytes()) == ''.join(other.iter_bytes()))
19+ _join_b(self.iter_bytes()) == _join_b(other.iter_bytes()))
20
21 def iter_bytes(self):
22 """Iterate over bytestrings of the serialised content."""
23@@ -68,7 +71,7 @@
24
25 def __repr__(self):
26 return "<Content type=%r, value=%r>" % (
27- self.content_type, ''.join(self.iter_bytes()))
28+ self.content_type, _join_b(self.iter_bytes()))
29
30
31 class TracebackContent(Content):
32
33=== modified file 'testtools/tests/test_content.py'
34--- testtools/tests/test_content.py 2010-11-11 10:59:48 +0000
35+++ testtools/tests/test_content.py 2010-11-16 00:28:16 +0000
36@@ -2,7 +2,7 @@
37
38 import unittest
39 from testtools import TestCase
40-from testtools.compat import _u
41+from testtools.compat import _b, _u
42 from testtools.content import Content, TracebackContent, text_content
43 from testtools.content_type import ContentType, UTF8_TEXT
44 from testtools.matchers import MatchesException, Raises
45@@ -29,16 +29,23 @@
46
47 def test___eq__(self):
48 content_type = ContentType("foo", "bar")
49- content1 = Content(content_type, lambda: ["bytes"])
50- content2 = Content(content_type, lambda: ["bytes"])
51- content3 = Content(content_type, lambda: ["by", "tes"])
52- content4 = Content(content_type, lambda: ["by", "te"])
53- content5 = Content(ContentType("f", "b"), lambda: ["by", "tes"])
54+ one_chunk = lambda: [_b("bytes")]
55+ two_chunk = lambda: [_b("by"), _b("tes")]
56+ content1 = Content(content_type, one_chunk)
57+ content2 = Content(content_type, one_chunk)
58+ content3 = Content(content_type, two_chunk)
59+ content4 = Content(content_type, lambda: [_b("by"), _b("te")])
60+ content5 = Content(ContentType("f", "b"), two_chunk)
61 self.assertEqual(content1, content2)
62 self.assertEqual(content1, content3)
63 self.assertNotEqual(content1, content4)
64 self.assertNotEqual(content1, content5)
65
66+ def test___repr__(self):
67+ content = Content(ContentType("application", "octet-stream"),
68+ lambda: [_b("\x00bin"), _b("ary\xff")])
69+ self.assertIn("\\x00binary\\xff", repr(content))
70+
71 def test_iter_text_not_text_errors(self):
72 content_type = ContentType("foo", "bar")
73 content = Content(content_type, lambda: ["bytes"])

Subscribers

People subscribed via source and target branches