From 4a8e920e25c5c7832e50c440730ecb6558f9c18c Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Thu, 30 Jun 2011 16:51:58 +0100 Subject: [PATCH] Work with Python 3 --- testtools/content_type.py | 2 +- testtools/helpers.py | 8 +++-- testtools/testresult/real.py | 2 +- testtools/tests/test_helpers.py | 24 +++++++-------- testtools/tests/test_testresult.py | 48 +++++++++++++++--------------- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/testtools/content_type.py b/testtools/content_type.py index 3a9de1c..82c301b 100644 --- a/testtools/content_type.py +++ b/testtools/content_type.py @@ -30,7 +30,7 @@ class ContentType(object): if self.parameters: params = '; ' 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: params = '' return "%s/%s%s" % (self.type, self.subtype, params) diff --git a/testtools/helpers.py b/testtools/helpers.py index 3ce8e97..57d16f9 100644 --- a/testtools/helpers.py +++ b/testtools/helpers.py @@ -1,10 +1,12 @@ -# Copyright (c) 2010 testtools developers. See LICENSE for details. +# Copyright (c) 2010-2011 testtools developers. See LICENSE for details. __all__ = [ 'try_import', 'try_imports', ] +import sys + def try_import(name, alternative=None, error_callback=None): """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) try: module = __import__(module_name) - except ImportError, e: - last_error = e + except ImportError: + last_error = sys.exc_info()[1] module_segments.pop() continue else: diff --git a/testtools/testresult/real.py b/testtools/testresult/real.py index a885950..8be0cd5 100644 --- a/testtools/testresult/real.py +++ b/testtools/testresult/real.py @@ -628,7 +628,7 @@ def _details_to_str(details, special=None): if content.content_type.type != 'text': binary_attachments.append((key, content.content_type)) continue - text = _b('').join(content.iter_text()).strip() + text = _u('').join(content.iter_text()).strip() if not text: empty_attachments.append(key) continue diff --git a/testtools/tests/test_helpers.py b/testtools/tests/test_helpers.py index 83ae367..c19fa89 100644 --- a/testtools/tests/test_helpers.py +++ b/testtools/tests/test_helpers.py @@ -10,12 +10,12 @@ from testtools.matchers import ( Is, Not, ) - - -def check_error_callback(test, function, arg, expected_error_count, + + +def check_error_callback(test, function, arg, expected_error_count, expect_result): """General test template for error_callback argument. - + :param test: Test case instance. :param function: Either try_import or try_imports. :param arg: Name or names to import. @@ -29,7 +29,7 @@ def check_error_callback(test, function, arg, expected_error_count, cb_calls.append(e) try: result = function(arg, error_callback=cb) - except ImportError, e: + except ImportError: test.assertFalse(expect_result) else: if expect_result: @@ -79,20 +79,20 @@ class TestTryImport(TestCase): result = try_import('os.path.join') import os self.assertThat(result, Is(os.path.join)) - + def test_error_callback(self): # the error callback is called on failures. check_error_callback(self, try_import, 'doesntexist', 1, False) def test_error_callback_missing_module_member(self): - # the error callback is called on failures to find an object + # the error callback is called on failures to find an object # inside an existing module. check_error_callback(self, try_import, 'os.nonexistent', 1, False) def test_error_callback_not_on_success(self): # the error callback is not called on success. check_error_callback(self, try_import, 'os.path', 0, True) - + class TestTryImports(TestCase): @@ -140,16 +140,16 @@ class TestTryImports(TestCase): result = try_imports(['os.doesntexist', 'os.path']) import os self.assertThat(result, Is(os.path)) - + def test_error_callback(self): # One error for every class that doesn't exist. - check_error_callback(self, try_imports, + check_error_callback(self, try_imports, ['os.doesntexist', 'os.notthiseither'], 2, False) - check_error_callback(self, try_imports, + check_error_callback(self, try_imports, ['os.doesntexist', 'os.notthiseither', 'os'], 2, True) - check_error_callback(self, try_imports, + check_error_callback(self, try_imports, ['os.path'], 0, True) diff --git a/testtools/tests/test_testresult.py b/testtools/tests/test_testresult.py index f119981..7482ceb 100644 --- a/testtools/tests/test_testresult.py +++ b/testtools/tests/test_testresult.py @@ -376,14 +376,14 @@ class TestTestResult(TestCase): self.assertThat( result.errors[0][1], DocTestMatches( - u'Traceback (most recent call last):\n' - u' File "testtools/runtest.py", line ..., in _run_user\n' - u' return fn(*args, **kwargs)\n' - u' File "testtools/testcase.py", line ..., in _run_test_method\n' - u' return self._get_test_method()()\n' - u' File "testtools/tests/test_testresult.py", line ..., in error\n' - u' 1/0\n' - u'ZeroDivisionError: integer division or modulo by zero\n', + 'Traceback (most recent call last):\n' + ' File "testtools/runtest.py", line ..., in _run_user\n' + ' return fn(*args, **kwargs)\n' + ' File "testtools/testcase.py", line ..., in _run_test_method\n' + ' return self._get_test_method()()\n' + ' File "testtools/tests/test_testresult.py", line ..., in error\n' + ' 1/0\n' + 'ZeroDivisionError: ...\n', doctest.ELLIPSIS)) @@ -1401,14 +1401,14 @@ class TestDetailsToStr(TestCase): def test_no_details(self): string = _details_to_str({}) - self.assertThat(string, Equals(u'')) + self.assertThat(string, Equals('')) def test_binary_content(self): content = content_from_stream( StringIO('foo'), content_type=ContentType('image', 'jpeg')) string = _details_to_str({'attachment': content}) self.assertThat( - string, Equals(u"""\ + string, Equals("""\ Binary content: attachment (image/jpeg) """)) @@ -1416,37 +1416,37 @@ Binary content: def test_single_line_content(self): content = text_content('foo') 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): content = text_content('foo\nbar\nbaz') 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): content = text_content('foo') 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): string = _details_to_str( {'attachment': text_content('foo\nfoo'), 'attachment-1': text_content('bar\nbar')}) self.assertThat( - string, Equals(u'attachment: {{{\n' - u'foo\n' - u'foo\n' - u'}}}\n' - u'\n' - u'attachment-1: {{{\n' - u'bar\n' - u'bar\n' - u'}}}\n')) + string, Equals('attachment: {{{\n' + 'foo\n' + 'foo\n' + '}}}\n' + '\n' + 'attachment-1: {{{\n' + 'bar\n' + 'bar\n' + '}}}\n')) def test_empty_attachment(self): string = _details_to_str({'attachment': text_content('')}) self.assertThat( - string, Equals(u"""\ + string, Equals("""\ Empty attachments: attachment """)) @@ -1464,7 +1464,7 @@ Empty attachments: } string = _details_to_str(attachments, special='attachment-1') self.assertThat( - string, Equals(u"""\ + string, Equals("""\ Binary content: attachment-2 (image/jpeg) attachment-5 (image/jpeg)