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
=== modified file 'testtools/content.py'
--- testtools/content.py 2010-10-28 22:06:41 +0000
+++ testtools/content.py 2010-11-16 00:28:16 +0000
@@ -9,6 +9,9 @@
9from testtools.testresult import TestResult9from testtools.testresult import TestResult
1010
1111
12_join_b = _b("").join
13
14
12class Content(object):15class Content(object):
13 """A MIME-like Content object.16 """A MIME-like Content object.
1417
@@ -31,7 +34,7 @@
3134
32 def __eq__(self, other):35 def __eq__(self, other):
33 return (self.content_type == other.content_type and36 return (self.content_type == other.content_type and
34 ''.join(self.iter_bytes()) == ''.join(other.iter_bytes()))37 _join_b(self.iter_bytes()) == _join_b(other.iter_bytes()))
3538
36 def iter_bytes(self):39 def iter_bytes(self):
37 """Iterate over bytestrings of the serialised content."""40 """Iterate over bytestrings of the serialised content."""
@@ -68,7 +71,7 @@
6871
69 def __repr__(self):72 def __repr__(self):
70 return "<Content type=%r, value=%r>" % (73 return "<Content type=%r, value=%r>" % (
71 self.content_type, ''.join(self.iter_bytes()))74 self.content_type, _join_b(self.iter_bytes()))
7275
7376
74class TracebackContent(Content):77class TracebackContent(Content):
7578
=== modified file 'testtools/tests/test_content.py'
--- testtools/tests/test_content.py 2010-11-11 10:59:48 +0000
+++ testtools/tests/test_content.py 2010-11-16 00:28:16 +0000
@@ -2,7 +2,7 @@
22
3import unittest3import unittest
4from testtools import TestCase4from testtools import TestCase
5from testtools.compat import _u5from testtools.compat import _b, _u
6from testtools.content import Content, TracebackContent, text_content6from testtools.content import Content, TracebackContent, text_content
7from testtools.content_type import ContentType, UTF8_TEXT7from testtools.content_type import ContentType, UTF8_TEXT
8from testtools.matchers import MatchesException, Raises8from testtools.matchers import MatchesException, Raises
@@ -29,16 +29,23 @@
2929
30 def test___eq__(self):30 def test___eq__(self):
31 content_type = ContentType("foo", "bar")31 content_type = ContentType("foo", "bar")
32 content1 = Content(content_type, lambda: ["bytes"])32 one_chunk = lambda: [_b("bytes")]
33 content2 = Content(content_type, lambda: ["bytes"])33 two_chunk = lambda: [_b("by"), _b("tes")]
34 content3 = Content(content_type, lambda: ["by", "tes"])34 content1 = Content(content_type, one_chunk)
35 content4 = Content(content_type, lambda: ["by", "te"])35 content2 = Content(content_type, one_chunk)
36 content5 = Content(ContentType("f", "b"), lambda: ["by", "tes"])36 content3 = Content(content_type, two_chunk)
37 content4 = Content(content_type, lambda: [_b("by"), _b("te")])
38 content5 = Content(ContentType("f", "b"), two_chunk)
37 self.assertEqual(content1, content2)39 self.assertEqual(content1, content2)
38 self.assertEqual(content1, content3)40 self.assertEqual(content1, content3)
39 self.assertNotEqual(content1, content4)41 self.assertNotEqual(content1, content4)
40 self.assertNotEqual(content1, content5)42 self.assertNotEqual(content1, content5)
4143
44 def test___repr__(self):
45 content = Content(ContentType("application", "octet-stream"),
46 lambda: [_b("\x00bin"), _b("ary\xff")])
47 self.assertIn("\\x00binary\\xff", repr(content))
48
42 def test_iter_text_not_text_errors(self):49 def test_iter_text_not_text_errors(self):
43 content_type = ContentType("foo", "bar")50 content_type = ContentType("foo", "bar")
44 content = Content(content_type, lambda: ["bytes"])51 content = Content(content_type, lambda: ["bytes"])

Subscribers

People subscribed via source and target branches