From edfa95c22ffe8f58060bb985ad1b6fbca9506f5e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 25 May 2012 20:37:38 +0200 Subject: [PATCH] Specified when open should use binary mode. Thanks Vinaj Sajip for the help of his django3 branch. --- .../tests/wizard/namedwizardtests/tests.py | 14 ++++++++------ .../formtools/tests/wizard/wizardtests/tests.py | 12 ++++++------ django/core/management/commands/compilemessages.py | 2 +- django/template/loaders/app_directories.py | 2 +- django/template/loaders/filesystem.py | 2 +- tests/regressiontests/views/tests/static.py | 10 +++++----- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py index 550622180f..37913fa078 100644 --- a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py @@ -120,7 +120,7 @@ class NamedWizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post( reverse(self.wizard_urlname, kwargs={'step': response.context['wizard']['steps'].current}), @@ -147,7 +147,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['form_list'] - self.assertEqual(all_data[1]['file1'].read(), open(__file__).read()) + self.assertEqual(all_data[1]['file1'].read(), open(__file__, 'rb').read()) del all_data[1]['file1'] self.assertEqual(all_data, [ {'name': u'Pony', 'thirsty': True, 'user': self.testuser}, @@ -168,7 +168,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post( reverse(self.wizard_urlname, kwargs={'step': response.context['wizard']['steps'].current}), @@ -180,7 +180,9 @@ class NamedWizardTests(object): response = self.client.get(step2_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form2') - self.assertEqual(response.context['wizard']['form'].files['form2-file1'].read(), open(__file__).read()) + self.assertEqual( + response.context['wizard']['form'].files['form2-file1'].read(), + open(__file__, 'rb').read()) response = self.client.post( reverse(self.wizard_urlname, @@ -197,7 +199,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['all_cleaned_data'] - self.assertEqual(all_data['file1'].read(), open(__file__).read()) + self.assertEqual(all_data['file1'].read(), open(__file__, 'rb').read()) del all_data['file1'] self.assertEqual( all_data, @@ -221,7 +223,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post( reverse(self.wizard_urlname, kwargs={'step': response.context['wizard']['steps'].current}), diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py index 7afaf51f13..a9a81ba70f 100644 --- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py @@ -80,7 +80,7 @@ class WizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form3') @@ -93,7 +93,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['form_list'] - self.assertEqual(all_data[1]['file1'].read(), open(__file__).read()) + self.assertEqual(all_data[1]['file1'].read(), open(__file__, 'rb').read()) del all_data[1]['file1'] self.assertEqual(all_data, [ {'name': u'Pony', 'thirsty': True, 'user': self.testuser}, @@ -110,7 +110,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -121,7 +121,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) all_data = response.context['all_cleaned_data'] - self.assertEqual(all_data['file1'].read(), open(__file__).read()) + self.assertEqual(all_data['file1'].read(), open(__file__, 'rb').read()) del all_data['file1'] self.assertEqual(all_data, { 'name': u'Pony', 'thirsty': True, 'user': self.testuser, @@ -138,7 +138,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -165,7 +165,7 @@ class WizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - post_data['form2-file1'] = open(__file__) + post_data['form2-file1'] = open(__file__, 'rb') response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form3') diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py index 354787eb91..fdc3535cf6 100644 --- a/django/core/management/commands/compilemessages.py +++ b/django/core/management/commands/compilemessages.py @@ -5,7 +5,7 @@ from optparse import make_option from django.core.management.base import BaseCommand, CommandError def has_bom(fn): - with open(fn, 'r') as f: + with open(fn, 'rb') as f: sample = f.read(4) return sample[:3] == '\xef\xbb\xbf' or \ sample.startswith(codecs.BOM_UTF16_LE) or \ diff --git a/django/template/loaders/app_directories.py b/django/template/loaders/app_directories.py index d6b3a17c8c..6e0f079de7 100644 --- a/django/template/loaders/app_directories.py +++ b/django/template/loaders/app_directories.py @@ -52,7 +52,7 @@ class Loader(BaseLoader): def load_template_source(self, template_name, template_dirs=None): for filepath in self.get_template_sources(template_name, template_dirs): try: - with open(filepath) as fp: + with open(filepath, 'rb') as fp: return (fp.read().decode(settings.FILE_CHARSET), filepath) except IOError: pass diff --git a/django/template/loaders/filesystem.py b/django/template/loaders/filesystem.py index 8352406424..e87cfef987 100644 --- a/django/template/loaders/filesystem.py +++ b/django/template/loaders/filesystem.py @@ -34,7 +34,7 @@ class Loader(BaseLoader): tried = [] for filepath in self.get_template_sources(template_name, template_dirs): try: - with open(filepath) as fp: + with open(filepath, 'rb') as fp: return (fp.read().decode(settings.FILE_CHARSET), filepath) except IOError: tried.append(filepath) diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py index 6cabf6453c..9d87ade137 100644 --- a/tests/regressiontests/views/tests/static.py +++ b/tests/regressiontests/views/tests/static.py @@ -29,7 +29,7 @@ class StaticTests(TestCase): for filename in media_files: response = self.client.get('/views/%s/%s' % (self.prefix, filename)) file_path = path.join(media_dir, filename) - with open(file_path) as fp: + with open(file_path, 'rb') as fp: self.assertEqual(fp.read(), response.content) self.assertEqual(len(response.content), int(response['Content-Length'])) self.assertEqual(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None)) @@ -41,14 +41,14 @@ class StaticTests(TestCase): def test_copes_with_empty_path_component(self): file_name = 'file.txt' response = self.client.get('/views/%s//%s' % (self.prefix, file_name)) - with open(path.join(media_dir, file_name)) as fp: + with open(path.join(media_dir, file_name), 'rb') as fp: self.assertEqual(fp.read(), response.content) def test_is_modified_since(self): file_name = 'file.txt' response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT') - with open(path.join(media_dir, file_name)) as fp: + with open(path.join(media_dir, file_name), 'rb') as fp: self.assertEqual(fp.read(), response.content) def test_not_modified_since(self): @@ -71,7 +71,7 @@ class StaticTests(TestCase): invalid_date = 'Mon, 28 May 999999999999 28:25:26 GMT' response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date) - with open(path.join(media_dir, file_name)) as fp: + with open(path.join(media_dir, file_name), 'rb') as fp: self.assertEqual(fp.read(), response.content) self.assertEqual(len(response.content), int(response['Content-Length'])) @@ -86,7 +86,7 @@ class StaticTests(TestCase): invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT' response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date) - with open(path.join(media_dir, file_name)) as fp: + with open(path.join(media_dir, file_name), 'rb') as fp: self.assertEqual(fp.read(), response.content) self.assertEqual(len(response.content), int(response['Content-Length']))