Work with Python 3

This commit is contained in:
Jonathan Lange
2011-06-30 16:51:58 +01:00
parent 4dcd3c33dd
commit 4a8e920e25
5 changed files with 43 additions and 41 deletions

View File

@@ -30,7 +30,7 @@ class ContentType(object):
if self.parameters: if self.parameters:
params = '; ' params = '; '
params += ', '.join( params += ', '.join(
'%s="%s"' % (k, v) for k, v in self.parameters.iteritems()) '%s="%s"' % (k, v) for k, v in self.parameters.items())
else: else:
params = '' params = ''
return "%s/%s%s" % (self.type, self.subtype, params) return "%s/%s%s" % (self.type, self.subtype, params)

View File

@@ -1,10 +1,12 @@
# Copyright (c) 2010 testtools developers. See LICENSE for details. # Copyright (c) 2010-2011 testtools developers. See LICENSE for details.
__all__ = [ __all__ = [
'try_import', 'try_import',
'try_imports', 'try_imports',
] ]
import sys
def try_import(name, alternative=None, error_callback=None): def try_import(name, alternative=None, error_callback=None):
"""Attempt to import ``name``. If it fails, return ``alternative``. """Attempt to import ``name``. If it fails, return ``alternative``.
@@ -25,8 +27,8 @@ def try_import(name, alternative=None, error_callback=None):
module_name = '.'.join(module_segments) module_name = '.'.join(module_segments)
try: try:
module = __import__(module_name) module = __import__(module_name)
except ImportError, e: except ImportError:
last_error = e last_error = sys.exc_info()[1]
module_segments.pop() module_segments.pop()
continue continue
else: else:

View File

@@ -628,7 +628,7 @@ def _details_to_str(details, special=None):
if content.content_type.type != 'text': if content.content_type.type != 'text':
binary_attachments.append((key, content.content_type)) binary_attachments.append((key, content.content_type))
continue continue
text = _b('').join(content.iter_text()).strip() text = _u('').join(content.iter_text()).strip()
if not text: if not text:
empty_attachments.append(key) empty_attachments.append(key)
continue continue

View File

@@ -29,7 +29,7 @@ def check_error_callback(test, function, arg, expected_error_count,
cb_calls.append(e) cb_calls.append(e)
try: try:
result = function(arg, error_callback=cb) result = function(arg, error_callback=cb)
except ImportError, e: except ImportError:
test.assertFalse(expect_result) test.assertFalse(expect_result)
else: else:
if expect_result: if expect_result:

View File

@@ -376,14 +376,14 @@ class TestTestResult(TestCase):
self.assertThat( self.assertThat(
result.errors[0][1], result.errors[0][1],
DocTestMatches( DocTestMatches(
u'Traceback (most recent call last):\n' 'Traceback (most recent call last):\n'
u' File "testtools/runtest.py", line ..., in _run_user\n' ' File "testtools/runtest.py", line ..., in _run_user\n'
u' return fn(*args, **kwargs)\n' ' return fn(*args, **kwargs)\n'
u' File "testtools/testcase.py", line ..., in _run_test_method\n' ' File "testtools/testcase.py", line ..., in _run_test_method\n'
u' return self._get_test_method()()\n' ' return self._get_test_method()()\n'
u' File "testtools/tests/test_testresult.py", line ..., in error\n' ' File "testtools/tests/test_testresult.py", line ..., in error\n'
u' 1/0\n' ' 1/0\n'
u'ZeroDivisionError: integer division or modulo by zero\n', 'ZeroDivisionError: ...\n',
doctest.ELLIPSIS)) doctest.ELLIPSIS))
@@ -1401,14 +1401,14 @@ class TestDetailsToStr(TestCase):
def test_no_details(self): def test_no_details(self):
string = _details_to_str({}) string = _details_to_str({})
self.assertThat(string, Equals(u'')) self.assertThat(string, Equals(''))
def test_binary_content(self): def test_binary_content(self):
content = content_from_stream( content = content_from_stream(
StringIO('foo'), content_type=ContentType('image', 'jpeg')) StringIO('foo'), content_type=ContentType('image', 'jpeg'))
string = _details_to_str({'attachment': content}) string = _details_to_str({'attachment': content})
self.assertThat( self.assertThat(
string, Equals(u"""\ string, Equals("""\
Binary content: Binary content:
attachment (image/jpeg) attachment (image/jpeg)
""")) """))
@@ -1416,37 +1416,37 @@ Binary content:
def test_single_line_content(self): def test_single_line_content(self):
content = text_content('foo') content = text_content('foo')
string = _details_to_str({'attachment': content}) string = _details_to_str({'attachment': content})
self.assertThat(string, Equals(u'attachment: {{{foo}}}\n')) self.assertThat(string, Equals('attachment: {{{foo}}}\n'))
def test_multi_line_text_content(self): def test_multi_line_text_content(self):
content = text_content('foo\nbar\nbaz') content = text_content('foo\nbar\nbaz')
string = _details_to_str({'attachment': content}) string = _details_to_str({'attachment': content})
self.assertThat(string, Equals(u'attachment: {{{\nfoo\nbar\nbaz\n}}}\n')) self.assertThat(string, Equals('attachment: {{{\nfoo\nbar\nbaz\n}}}\n'))
def test_special_text_content(self): def test_special_text_content(self):
content = text_content('foo') content = text_content('foo')
string = _details_to_str({'attachment': content}, special='attachment') string = _details_to_str({'attachment': content}, special='attachment')
self.assertThat(string, Equals(u'foo\n')) self.assertThat(string, Equals('foo\n'))
def test_multiple_text_content(self): def test_multiple_text_content(self):
string = _details_to_str( string = _details_to_str(
{'attachment': text_content('foo\nfoo'), {'attachment': text_content('foo\nfoo'),
'attachment-1': text_content('bar\nbar')}) 'attachment-1': text_content('bar\nbar')})
self.assertThat( self.assertThat(
string, Equals(u'attachment: {{{\n' string, Equals('attachment: {{{\n'
u'foo\n' 'foo\n'
u'foo\n' 'foo\n'
u'}}}\n' '}}}\n'
u'\n' '\n'
u'attachment-1: {{{\n' 'attachment-1: {{{\n'
u'bar\n' 'bar\n'
u'bar\n' 'bar\n'
u'}}}\n')) '}}}\n'))
def test_empty_attachment(self): def test_empty_attachment(self):
string = _details_to_str({'attachment': text_content('')}) string = _details_to_str({'attachment': text_content('')})
self.assertThat( self.assertThat(
string, Equals(u"""\ string, Equals("""\
Empty attachments: Empty attachments:
attachment attachment
""")) """))
@@ -1464,7 +1464,7 @@ Empty attachments:
} }
string = _details_to_str(attachments, special='attachment-1') string = _details_to_str(attachments, special='attachment-1')
self.assertThat( self.assertThat(
string, Equals(u"""\ string, Equals("""\
Binary content: Binary content:
attachment-2 (image/jpeg) attachment-2 (image/jpeg)
attachment-5 (image/jpeg) attachment-5 (image/jpeg)