Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Specified when open should use binary mode.
Browse files Browse the repository at this point in the history
Thanks Vinaj Sajip for the help of his django3 branch.
  • Loading branch information
claudep committed May 25, 2012
1 parent e73838b commit edfa95c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
14 changes: 8 additions & 6 deletions django/contrib/formtools/tests/wizard/namedwizardtests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_form_finish(self):
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}),
Expand All @@ -147,7 +147,7 @@ def test_form_finish(self):
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},
Expand All @@ -168,7 +168,7 @@ def test_cleaned_data(self):
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}),
Expand All @@ -180,7 +180,9 @@ def test_cleaned_data(self):
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,
Expand All @@ -197,7 +199,7 @@ def test_cleaned_data(self):
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,
Expand All @@ -221,7 +223,7 @@ def test_manipulated_data(self):
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}),
Expand Down
12 changes: 6 additions & 6 deletions django/contrib/formtools/tests/wizard/wizardtests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_form_finish(self):
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')
Expand All @@ -93,7 +93,7 @@ def test_form_finish(self):
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},
Expand All @@ -110,7 +110,7 @@ def test_cleaned_data(self):
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)

Expand All @@ -121,7 +121,7 @@ def test_cleaned_data(self):
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,
Expand All @@ -138,7 +138,7 @@ def test_manipulated_data(self):
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)

Expand All @@ -165,7 +165,7 @@ def test_form_refresh(self):
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')
Expand Down
2 changes: 1 addition & 1 deletion django/core/management/commands/compilemessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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 \
Expand Down
2 changes: 1 addition & 1 deletion django/template/loaders/app_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_template_sources(self, template_name, template_dirs=None):
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
Expand Down
2 changes: 1 addition & 1 deletion django/template/loaders/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def load_template_source(self, template_name, template_dirs=None):
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)
Expand Down
10 changes: 5 additions & 5 deletions tests/regressiontests/views/tests/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_serve(self):
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))
Expand All @@ -41,14 +41,14 @@ def test_unknown_mime_type(self):
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):
Expand All @@ -71,7 +71,7 @@ def test_invalid_if_modified_since(self):
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']))
Expand All @@ -86,7 +86,7 @@ def test_invalid_if_modified_since2(self):
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']))
Expand Down

0 comments on commit edfa95c

Please sign in to comment.