diff --git a/django/test/testcases.py b/django/test/testcases.py index 260b060c45a4c..1d52fed69f8b7 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -867,7 +867,9 @@ def log_message(*args): pass -if sys.version_info >= (2, 7, 0): +if sys.version_info >= (3, 3, 0): + _ImprovedEvent = threading.Event +elif sys.version_info >= (2, 7, 0): _ImprovedEvent = threading._Event else: class _ImprovedEvent(threading._Event): diff --git a/django/utils/functional.py b/django/utils/functional.py index 085a8fce59122..505931e158b18 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -293,6 +293,16 @@ def __getstate__(self): self._setup() return self._wrapped.__dict__ + # Python 3.3 will call __reduce__ when pickling; these methods are needed + # to serialize and deserialize correctly. They are not called in earlier + # versions of Python. + @classmethod + def __newobj__(cls, *args): + return cls.__new__(cls, *args) + + def __reduce__(self): + return (self.__newobj__, (self.__class__,), self.__getstate__()) + # Need to pretend to be the wrapped class, for the sake of objects that care # about this (especially in equality tests) __class__ = property(new_method_proxy(operator.attrgetter("__class__"))) diff --git a/django/views/debug.py b/django/views/debug.py index c59fe31fc833d..aaa7e40efe58e 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -354,15 +354,19 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod if source is None: return None, [], None, [] - encoding = 'ascii' - for line in source[:2]: - # File coding may be specified. Match pattern from PEP-263 - # (http://www.python.org/dev/peps/pep-0263/) - match = re.search(br'coding[:=]\s*([-\w.]+)', line) - if match: - encoding = match.group(1).decode('ascii') - break - source = [six.text_type(sline, encoding, 'replace') for sline in source] + # If we just read the source from a file, or if the loader did not + # apply tokenize.detect_encoding to decode the source into a Unicode + # string, then we should do that ourselves. + if isinstance(source[0], six.binary_type): + encoding = 'ascii' + for line in source[:2]: + # File coding may be specified. Match pattern from PEP-263 + # (http://www.python.org/dev/peps/pep-0263/) + match = re.search(br'coding[:=]\s*([-\w.]+)', line) + if match: + encoding = match.group(1).decode('ascii') + break + source = [six.text_type(sline, encoding, 'replace') for sline in source] lower_bound = max(0, lineno - context_lines) upper_bound = lineno + context_lines diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index 6ec9ca03afd17..ebd70d14d9a71 100644 --- a/tests/modeltests/basic/tests.py +++ b/tests/modeltests/basic/tests.py @@ -259,9 +259,8 @@ def test_object_creation(self): "datetime.datetime(2005, 7, 28, 0, 0)"]) # dates() requires valid arguments. - six.assertRaisesRegex(self, + self.assertRaises( TypeError, - "dates\(\) takes at least 3 arguments \(1 given\)", Article.objects.dates, ) diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 6f524bea29e16..48c36362aa552 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -1010,7 +1010,8 @@ def test_builtin_command(self): args = ['sqlall', 'admin_scripts'] out, err = self.run_manage(args) self.assertNoOutput(out) - self.assertOutput(err, "No module named foo42bar") + self.assertOutput(err, "No module named") + self.assertOutput(err, "foo42bar") def test_builtin_command_with_attribute_error(self): """ @@ -1033,7 +1034,8 @@ def test_nonexistent_app(self): args = ['validate'] out, err = self.run_manage(args) self.assertNoOutput(out) - self.assertOutput(err, 'No module named admin_scriptz') + self.assertOutput(err, 'No module named') + self.assertOutput(err, 'admin_scriptz') def test_broken_app(self): "manage.py validate reports an ImportError if an app's models.py raises one on import" diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py index 6b57ad6160ee0..595b65d9f1da0 100644 --- a/tests/regressiontests/file_storage/tests.py +++ b/tests/regressiontests/file_storage/tests.py @@ -78,7 +78,7 @@ def test_get_nonexisting_storage_module(self): six.assertRaisesRegex(self, ImproperlyConfigured, ('Error importing storage module django.core.files.non_existing_' - 'storage: "No module named .*non_existing_storage"'), + 'storage: "No module named .*non_existing_storage'), get_storage_class, 'django.core.files.non_existing_storage.NonExistingStorage' ) diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py index 678db4a9cc2d4..55363bc5b76c0 100644 --- a/tests/regressiontests/fixtures_regress/tests.py +++ b/tests/regressiontests/fixtures_regress/tests.py @@ -159,7 +159,7 @@ def test_unimportable_serializer(self): Test that failing serializer import raises the proper error """ with six.assertRaisesRegex(self, ImportError, - "No module named unexistent.path"): + r"No module named.*unexistent"): management.call_command( 'loaddata', 'bad_fixture1.unkn', diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py index f9e1112b2e985..e5c5bb5f8d88a 100644 --- a/tests/regressiontests/requests/tests.py +++ b/tests/regressiontests/requests/tests.py @@ -56,7 +56,7 @@ def test_wsgirequest_repr(self): str_prefix("")) def test_parse_cookie(self): - self.assertEqual(parse_cookie('invalid:key=true'), {}) + self.assertEqual(parse_cookie('invalid@key=true'), {}) def test_httprequest_location(self): request = HttpRequest()