Marked bytestrings with b prefix. Refs #18269

This is a preparation for unicode literals general usage in
Django (Python 3 compatibility).
This commit is contained in:
Claude Paroz 2012-05-19 17:43:34 +02:00
parent 822d6d6dab
commit 38408f8007
44 changed files with 296 additions and 295 deletions

View File

@ -190,7 +190,7 @@ class HumanizeTests(TestCase):
orig_humanize_datetime = humanize.datetime orig_humanize_datetime = humanize.datetime
orig_timesince_datetime = timesince.datetime orig_timesince_datetime = timesince.datetime
humanize.datetime = MockDateTime humanize.datetime = MockDateTime
timesince.datetime = new.module("mock_datetime") timesince.datetime = new.module(b"mock_datetime")
timesince.datetime.datetime = MockDateTime timesince.datetime.datetime = MockDateTime
try: try:

View File

@ -22,7 +22,7 @@ def csrf(request):
# In order to be able to provide debugging info in the # In order to be able to provide debugging info in the
# case of misconfiguration, we use a sentinel value # case of misconfiguration, we use a sentinel value
# instead of returning an empty dict. # instead of returning an empty dict.
return 'NOTPROVIDED' return b'NOTPROVIDED'
else: else:
return token return token
_get_val = lazy(_get_val, str) _get_val = lazy(_get_val, str)

View File

@ -125,7 +125,7 @@ class ContentFile(File):
A File-like object that takes just raw content, rather than an actual file. A File-like object that takes just raw content, rather than an actual file.
""" """
def __init__(self, content, name=None): def __init__(self, content, name=None):
content = content or '' content = content or b''
super(ContentFile, self).__init__(BytesIO(content), name=name) super(ContentFile, self).__init__(BytesIO(content), name=name)
self.size = len(content) self.size = len(content)

View File

@ -30,8 +30,8 @@ class UploadedFile(File):
self.charset = charset self.charset = charset
def __repr__(self): def __repr__(self):
return "<%s: %s (%s)>" % ( return smart_str("<%s: %s (%s)>" % (
self.__class__.__name__, smart_str(self.name), self.content_type) self.__class__.__name__, self.name, self.content_type))
def _get_name(self): def _get_name(self):
return self._name return self._name

View File

@ -78,14 +78,14 @@ class LimitedStream(object):
def __init__(self, stream, limit, buf_size=64 * 1024 * 1024): def __init__(self, stream, limit, buf_size=64 * 1024 * 1024):
self.stream = stream self.stream = stream
self.remaining = limit self.remaining = limit
self.buffer = '' self.buffer = b''
self.buf_size = buf_size self.buf_size = buf_size
def _read_limited(self, size=None): def _read_limited(self, size=None):
if size is None or size > self.remaining: if size is None or size > self.remaining:
size = self.remaining size = self.remaining
if size == 0: if size == 0:
return '' return b''
result = self.stream.read(size) result = self.stream.read(size)
self.remaining -= len(result) self.remaining -= len(result)
return result return result
@ -93,17 +93,17 @@ class LimitedStream(object):
def read(self, size=None): def read(self, size=None):
if size is None: if size is None:
result = self.buffer + self._read_limited() result = self.buffer + self._read_limited()
self.buffer = '' self.buffer = b''
elif size < len(self.buffer): elif size < len(self.buffer):
result = self.buffer[:size] result = self.buffer[:size]
self.buffer = self.buffer[size:] self.buffer = self.buffer[size:]
else: # size >= len(self.buffer) else: # size >= len(self.buffer)
result = self.buffer + self._read_limited(size - len(self.buffer)) result = self.buffer + self._read_limited(size - len(self.buffer))
self.buffer = '' self.buffer = b''
return result return result
def readline(self, size=None): def readline(self, size=None):
while '\n' not in self.buffer and \ while b'\n' not in self.buffer and \
(size is None or len(self.buffer) < size): (size is None or len(self.buffer) < size):
if size: if size:
# since size is not None here, len(self.buffer) < size # since size is not None here, len(self.buffer) < size

View File

@ -51,15 +51,15 @@ def adapt_datetime_with_timezone_support(value):
default_timezone = timezone.get_default_timezone() default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone) value = timezone.make_aware(value, default_timezone)
value = value.astimezone(timezone.utc).replace(tzinfo=None) value = value.astimezone(timezone.utc).replace(tzinfo=None)
return value.isoformat(" ") return value.isoformat(b" ")
Database.register_converter("bool", lambda s: str(s) == '1') Database.register_converter(b"bool", lambda s: str(s) == '1')
Database.register_converter("time", parse_time) Database.register_converter(b"time", parse_time)
Database.register_converter("date", parse_date) Database.register_converter(b"date", parse_date)
Database.register_converter("datetime", parse_datetime_with_timezone_support) Database.register_converter(b"datetime", parse_datetime_with_timezone_support)
Database.register_converter("timestamp", parse_datetime_with_timezone_support) Database.register_converter(b"timestamp", parse_datetime_with_timezone_support)
Database.register_converter("TIMESTAMP", parse_datetime_with_timezone_support) Database.register_converter(b"TIMESTAMP", parse_datetime_with_timezone_support)
Database.register_converter("decimal", util.typecast_decimal) Database.register_converter(b"decimal", util.typecast_decimal)
Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
if Database.version_info >= (2, 4, 1): if Database.version_info >= (2, 4, 1):

View File

@ -57,11 +57,11 @@ class ModelBase(type):
new_class.add_to_class('_meta', Options(meta, **kwargs)) new_class.add_to_class('_meta', Options(meta, **kwargs))
if not abstract: if not abstract:
new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist', new_class.add_to_class('DoesNotExist', subclass_exception(b'DoesNotExist',
tuple(x.DoesNotExist tuple(x.DoesNotExist
for x in parents if hasattr(x, '_meta') and not x._meta.abstract) for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
or (ObjectDoesNotExist,), module)) or (ObjectDoesNotExist,), module))
new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned', new_class.add_to_class('MultipleObjectsReturned', subclass_exception(b'MultipleObjectsReturned',
tuple(x.MultipleObjectsReturned tuple(x.MultipleObjectsReturned
for x in parents if hasattr(x, '_meta') and not x._meta.abstract) for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
or (MultipleObjectsReturned,), module)) or (MultipleObjectsReturned,), module))

View File

@ -388,10 +388,10 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
parent = (object,) parent = (object,)
if hasattr(form, 'Meta'): if hasattr(form, 'Meta'):
parent = (form.Meta, object) parent = (form.Meta, object)
Meta = type('Meta', parent, attrs) Meta = type(b'Meta', parent, attrs)
# Give this new form class a reasonable name. # Give this new form class a reasonable name.
class_name = model.__name__ + 'Form' class_name = model.__name__ + b'Form'
# Class attributes for the new form class. # Class attributes for the new form class.
form_class_attrs = { form_class_attrs = {

View File

@ -18,7 +18,7 @@ _cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\
# See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256 # See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256
_tc = Cookie.SimpleCookie() _tc = Cookie.SimpleCookie()
try: try:
_tc.load('foo:bar=1') _tc.load(b'foo:bar=1')
_cookie_allows_colon_in_names = True _cookie_allows_colon_in_names = True
except Cookie.CookieError: except Cookie.CookieError:
_cookie_allows_colon_in_names = False _cookie_allows_colon_in_names = False
@ -650,8 +650,8 @@ class HttpResponse(object):
def _get_content(self): def _get_content(self):
if self.has_header('Content-Encoding'): if self.has_header('Content-Encoding'):
return ''.join([str(e) for e in self._container]) return b''.join([str(e) for e in self._container])
return ''.join([smart_str(e, self._charset) for e in self._container]) return b''.join([smart_str(e, self._charset) for e in self._container])
def _set_content(self, value): def _set_content(self, value):
if hasattr(value, '__iter__'): if hasattr(value, '__iter__'):

View File

@ -268,7 +268,7 @@ class LazyStream(object):
""" """
self._producer = producer self._producer = producer
self._empty = False self._empty = False
self._leftover = '' self._leftover = b''
self.length = length self.length = length
self.position = 0 self.position = 0
self._remaining = length self._remaining = length
@ -282,7 +282,7 @@ class LazyStream(object):
remaining = (size is not None and [size] or [self._remaining])[0] remaining = (size is not None and [size] or [self._remaining])[0]
# do the whole thing in one shot if no limit was provided. # do the whole thing in one shot if no limit was provided.
if remaining is None: if remaining is None:
yield ''.join(self) yield b''.join(self)
return return
# otherwise do some bookkeeping to return exactly enough # otherwise do some bookkeeping to return exactly enough
@ -298,7 +298,7 @@ class LazyStream(object):
remaining -= len(emitting) remaining -= len(emitting)
yield emitting yield emitting
out = ''.join(parts()) out = b''.join(parts())
return out return out
def next(self): def next(self):
@ -311,7 +311,7 @@ class LazyStream(object):
""" """
if self._leftover: if self._leftover:
output = self._leftover output = self._leftover
self._leftover = '' self._leftover = b''
else: else:
output = next(self._producer) output = next(self._producer)
self._unget_history = [] self._unget_history = []
@ -341,7 +341,7 @@ class LazyStream(object):
return return
self._update_unget_history(len(bytes)) self._update_unget_history(len(bytes))
self.position -= len(bytes) self.position -= len(bytes)
self._leftover = ''.join([bytes, self._leftover]) self._leftover = b''.join([bytes, self._leftover])
def _update_unget_history(self, num_bytes): def _update_unget_history(self, num_bytes):
""" """
@ -459,7 +459,7 @@ class BoundaryIter(object):
if not chunks: if not chunks:
raise StopIteration() raise StopIteration()
chunk = ''.join(chunks) chunk = b''.join(chunks)
boundary = self._find_boundary(chunk, len(chunk) < self._rollback) boundary = self._find_boundary(chunk, len(chunk) < self._rollback)
if boundary: if boundary:
@ -495,9 +495,9 @@ class BoundaryIter(object):
end = index end = index
next = index + len(self._boundary) next = index + len(self._boundary)
# backup over CRLF # backup over CRLF
if data[max(0,end-1)] == '\n': if data[max(0,end-1)] == b'\n':
end -= 1 end -= 1
if data[max(0,end-1)] == '\r': if data[max(0,end-1)] == b'\r':
end -= 1 end -= 1
return end, next return end, next
@ -531,7 +531,7 @@ def parse_boundary_stream(stream, max_header_size):
# 'find' returns the top of these four bytes, so we'll # 'find' returns the top of these four bytes, so we'll
# need to munch them later to prevent them from polluting # need to munch them later to prevent them from polluting
# the payload. # the payload.
header_end = chunk.find('\r\n\r\n') header_end = chunk.find(b'\r\n\r\n')
def _parse_header(line): def _parse_header(line):
main_value_pair, params = parse_header(line) main_value_pair, params = parse_header(line)
@ -557,7 +557,7 @@ def parse_boundary_stream(stream, max_header_size):
outdict = {} outdict = {}
# Eliminate blank lines # Eliminate blank lines
for line in header.split('\r\n'): for line in header.split(b'\r\n'):
# This terminology ("main value" and "dictionary of # This terminology ("main value" and "dictionary of
# parameters") is from the Python docs. # parameters") is from the Python docs.
try: try:
@ -580,7 +580,7 @@ def parse_boundary_stream(stream, max_header_size):
class Parser(object): class Parser(object):
def __init__(self, stream, boundary): def __init__(self, stream, boundary):
self._stream = stream self._stream = stream
self._separator = '--' + boundary self._separator = b'--' + boundary
def __iter__(self): def __iter__(self):
boundarystream = InterBoundaryIter(self._stream, self._separator) boundarystream = InterBoundaryIter(self._stream, self._separator)
@ -590,27 +590,27 @@ class Parser(object):
def parse_header(line): def parse_header(line):
""" Parse the header into a key-value. """ """ Parse the header into a key-value. """
plist = _parse_header_params(';' + line) plist = _parse_header_params(b';' + line)
key = plist.pop(0).lower() key = plist.pop(0).lower()
pdict = {} pdict = {}
for p in plist: for p in plist:
i = p.find('=') i = p.find(b'=')
if i >= 0: if i >= 0:
name = p[:i].strip().lower() name = p[:i].strip().lower()
value = p[i+1:].strip() value = p[i+1:].strip()
if len(value) >= 2 and value[0] == value[-1] == '"': if len(value) >= 2 and value[0] == value[-1] == b'"':
value = value[1:-1] value = value[1:-1]
value = value.replace('\\\\', '\\').replace('\\"', '"') value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
pdict[name] = value pdict[name] = value
return key, pdict return key, pdict
def _parse_header_params(s): def _parse_header_params(s):
plist = [] plist = []
while s[:1] == ';': while s[:1] == b';':
s = s[1:] s = s[1:]
end = s.find(';') end = s.find(b';')
while end > 0 and s.count('"', 0, end) % 2: while end > 0 and s.count(b'"', 0, end) % 2:
end = s.find(';', end + 1) end = s.find(b';', end + 1)
if end < 0: if end < 0:
end = len(s) end = len(s)
f = s[:end] f = s[:end]

View File

@ -23,8 +23,8 @@ except NotImplementedError:
from django.conf import settings from django.conf import settings
_trans_5c = "".join([chr(x ^ 0x5C) for x in xrange(256)]) _trans_5c = b"".join([chr(x ^ 0x5C) for x in xrange(256)])
_trans_36 = "".join([chr(x ^ 0x36) for x in xrange(256)]) _trans_36 = b"".join([chr(x ^ 0x36) for x in xrange(256)])
def salted_hmac(key_salt, value, secret=None): def salted_hmac(key_salt, value, secret=None):
@ -148,11 +148,11 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None):
def F(i): def F(i):
def U(): def U():
u = salt + struct.pack('>I', i) u = salt + struct.pack(b'>I', i)
for j in xrange(int(iterations)): for j in xrange(int(iterations)):
u = _fast_hmac(password, u, digest).digest() u = _fast_hmac(password, u, digest).digest()
yield _bin_to_long(u) yield _bin_to_long(u)
return _long_to_bin(reduce(operator.xor, U()), hex_format_string) return _long_to_bin(reduce(operator.xor, U()), hex_format_string)
T = [F(x) for x in range(1, l + 1)] T = [F(x) for x in range(1, l + 1)]
return ''.join(T[:-1]) + T[-1][:r] return b''.join(T[:-1]) + T[-1][:r]

View File

@ -154,7 +154,7 @@ def iri_to_uri(iri):
# converted. # converted.
if iri is None: if iri is None:
return iri return iri
return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~") return urllib.quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def filepath_to_uri(path): def filepath_to_uri(path):
"""Convert an file system path to a URI portion that is suitable for """Convert an file system path to a URI portion that is suitable for
@ -173,7 +173,7 @@ def filepath_to_uri(path):
return path return path
# I know about `os.sep` and `os.altsep` but I want to leave # I know about `os.sep` and `os.altsep` but I want to leave
# some flexibility for hardcoding separators. # some flexibility for hardcoding separators.
return urllib.quote(smart_str(path).replace("\\", "/"), safe="/~!*()'") return urllib.quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'")
# The encoding of the default system locale but falls back to the # The encoding of the default system locale but falls back to the
# given fallback encoding if the encoding is unsupported by python or could # given fallback encoding if the encoding is unsupported by python or could

View File

@ -116,7 +116,7 @@ def smart_urlquote(url):
# contains a % not followed by two hexadecimal digits. See #9655. # contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url): if '%' not in url or unquoted_percents_re.search(url):
# See http://bugs.python.org/issue2637 # See http://bugs.python.org/issue2637
url = urllib.quote(smart_str(url), safe='!*\'();:@&=+$,/?#[]~') url = urllib.quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~')
return force_unicode(url) return force_unicode(url)

View File

@ -333,7 +333,7 @@ class ExceptionReporter(object):
source = source.splitlines() source = source.splitlines()
if source is None: if source is None:
try: try:
with open(filename) as fp: with open(filename, 'rb') as fp:
source = fp.readlines() source = fp.readlines()
except (OSError, IOError): except (OSError, IOError):
pass pass

View File

@ -96,7 +96,7 @@ The ``ContentFile`` Class
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
f1 = ContentFile("my string content") f1 = ContentFile(b"my string content")
f2 = ContentFile(u"my unicode content encoded as UTF-8".encode('UTF-8')) f2 = ContentFile(u"my unicode content encoded as UTF-8".encode('UTF-8'))
.. currentmodule:: django.core.files.images .. currentmodule:: django.core.files.images

View File

@ -269,7 +269,7 @@ You can pass either Unicode strings or UTF-8 bytestrings as arguments to
querysets are identical:: querysets are identical::
qs = People.objects.filter(name__contains=u'Å') qs = People.objects.filter(name__contains=u'Å')
qs = People.objects.filter(name__contains='\xc3\x85') # UTF-8 encoding of Å qs = People.objects.filter(name__contains=b'\xc3\x85') # UTF-8 encoding of Å
Templates Templates
========= =========
@ -277,7 +277,7 @@ Templates
You can use either Unicode or bytestrings when creating templates manually:: You can use either Unicode or bytestrings when creating templates manually::
from django.template import Template from django.template import Template
t1 = Template('This is a bytestring template.') t1 = Template(b'This is a bytestring template.')
t2 = Template(u'This is a Unicode template.') t2 = Template(u'This is a Unicode template.')
But the common case is to read templates from the filesystem, and this creates But the common case is to read templates from the filesystem, and this creates

View File

@ -1126,7 +1126,7 @@ class OldFormForXTests(TestCase):
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test1.txt', 'hello world')}) files={'file': SimpleUploadedFile('test1.txt', b'hello world')})
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
self.assertEqual(type(f.cleaned_data['file']), SimpleUploadedFile) self.assertEqual(type(f.cleaned_data['file']), SimpleUploadedFile)
instance = f.save() instance = f.save()
@ -1135,7 +1135,7 @@ class OldFormForXTests(TestCase):
instance.file.delete() instance.file.delete()
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test1.txt', 'hello world')}) files={'file': SimpleUploadedFile('test1.txt', b'hello world')})
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
self.assertEqual(type(f.cleaned_data['file']), SimpleUploadedFile) self.assertEqual(type(f.cleaned_data['file']), SimpleUploadedFile)
instance = f.save() instance = f.save()
@ -1144,7 +1144,7 @@ class OldFormForXTests(TestCase):
# Check if the max_length attribute has been inherited from the model. # Check if the max_length attribute has been inherited from the model.
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test-maxlength.txt', 'hello world')}) files={'file': SimpleUploadedFile('test-maxlength.txt', b'hello world')})
self.assertEqual(f.is_valid(), False) self.assertEqual(f.is_valid(), False)
# Edit an instance that already has the file defined in the model. This will not # Edit an instance that already has the file defined in the model. This will not
@ -1165,7 +1165,7 @@ class OldFormForXTests(TestCase):
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test2.txt', 'hello world')}, instance=instance) files={'file': SimpleUploadedFile('test2.txt', b'hello world')}, instance=instance)
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
instance = f.save() instance = f.save()
self.assertEqual(instance.file.name, 'tests/test2.txt') self.assertEqual(instance.file.name, 'tests/test2.txt')
@ -1174,7 +1174,7 @@ class OldFormForXTests(TestCase):
instance.file.delete() instance.file.delete()
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test2.txt', 'hello world')}) files={'file': SimpleUploadedFile('test2.txt', b'hello world')})
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
instance = f.save() instance = f.save()
self.assertEqual(instance.file.name, 'tests/test2.txt') self.assertEqual(instance.file.name, 'tests/test2.txt')
@ -1193,7 +1193,7 @@ class OldFormForXTests(TestCase):
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test3.txt', 'hello world')}, instance=instance) files={'file': SimpleUploadedFile('test3.txt', b'hello world')}, instance=instance)
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
instance = f.save() instance = f.save()
self.assertEqual(instance.file.name, 'tests/test3.txt') self.assertEqual(instance.file.name, 'tests/test3.txt')
@ -1215,7 +1215,7 @@ class OldFormForXTests(TestCase):
f = TextFileForm( f = TextFileForm(
data={'description': u'Assistance'}, data={'description': u'Assistance'},
files={'file': SimpleUploadedFile('test3.txt', 'hello world')}) files={'file': SimpleUploadedFile('test3.txt', b'hello world')})
self.assertEqual(f.is_valid(), True) self.assertEqual(f.is_valid(), True)
instance = f.save() instance = f.save()
self.assertEqual(instance.file.name, 'tests/test3.txt') self.assertEqual(instance.file.name, 'tests/test3.txt')

View File

@ -113,8 +113,8 @@ class SerializersTestBase(object):
Tests the ability to create new objects by Tests the ability to create new objects by
modifying serialized content. modifying serialized content.
""" """
old_headline = "Poker has no place on ESPN" old_headline = b"Poker has no place on ESPN"
new_headline = "Poker has no place on television" new_headline = b"Poker has no place on television"
serial_str = serializers.serialize(self.serializer_name, serial_str = serializers.serialize(self.serializer_name,
Article.objects.all()) Article.objects.all())
serial_str = serial_str.replace(old_headline, new_headline) serial_str = serial_str.replace(old_headline, new_headline)
@ -284,7 +284,7 @@ class SerializersTransactionTestBase(object):
class XmlSerializerTestCase(SerializersTestBase, TestCase): class XmlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "xml" serializer_name = "xml"
pkless_str = """<?xml version="1.0" encoding="utf-8"?> pkless_str = b"""<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0"> <django-objects version="1.0">
<object model="serializers.category"> <object model="serializers.category">
<field type="CharField" name="name">Reference</field> <field type="CharField" name="name">Reference</field>
@ -330,7 +330,7 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase):
class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "xml" serializer_name = "xml"
fwd_ref_str = """<?xml version="1.0" encoding="utf-8"?> fwd_ref_str = b"""<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0"> <django-objects version="1.0">
<object pk="1" model="serializers.article"> <object pk="1" model="serializers.article">
<field to="serializers.author" name="author" rel="ManyToOneRel">1</field> <field to="serializers.author" name="author" rel="ManyToOneRel">1</field>
@ -350,7 +350,7 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti
class JsonSerializerTestCase(SerializersTestBase, TestCase): class JsonSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "json" serializer_name = "json"
pkless_str = """[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]""" pkless_str = b"""[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]"""
@staticmethod @staticmethod
def _validate_output(serial_str): def _validate_output(serial_str):
@ -380,7 +380,7 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "json" serializer_name = "json"
fwd_ref_str = """[ fwd_ref_str = b"""[
{ {
"pk": 1, "pk": 1,
"model": "serializers.article", "model": "serializers.article",
@ -413,7 +413,7 @@ except ImportError:
else: else:
class YamlSerializerTestCase(SerializersTestBase, TestCase): class YamlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "yaml" serializer_name = "yaml"
fwd_ref_str = """- fields: fwd_ref_str = b"""- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]
@ -429,7 +429,7 @@ else:
pk: 1 pk: 1
model: serializers.author""" model: serializers.author"""
pkless_str = """- fields: pkless_str = b"""- fields:
name: Reference name: Reference
pk: null pk: null
model: serializers.category""" model: serializers.category"""
@ -469,7 +469,7 @@ else:
class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "yaml" serializer_name = "yaml"
fwd_ref_str = """- fields: fwd_ref_str = b"""- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]

View File

@ -11,11 +11,11 @@ from .models import Article, InternationalArticle
class SimpleTests(TestCase): class SimpleTests(TestCase):
def test_basic(self): def test_basic(self):
a = Article.objects.create( a = Article.objects.create(
headline='Area man programs in Python', headline=b'Area man programs in Python',
pub_date=datetime.datetime(2005, 7, 28) pub_date=datetime.datetime(2005, 7, 28)
) )
self.assertEqual(str(a), 'Area man programs in Python') self.assertEqual(str(a), b'Area man programs in Python')
self.assertEqual(repr(a), '<Article: Area man programs in Python>') self.assertEqual(repr(a), b'<Article: Area man programs in Python>')
def test_international(self): def test_international(self):
a = InternationalArticle.objects.create( a = InternationalArticle.objects.create(
@ -23,4 +23,4 @@ class SimpleTests(TestCase):
pub_date=datetime.datetime(2005, 7, 28) pub_date=datetime.datetime(2005, 7, 28)
) )
# The default str() output will be the UTF-8 encoded output of __unicode__(). # The default str() output will be the UTF-8 encoded output of __unicode__().
self.assertEqual(str(a), 'Girl wins \xe2\x82\xac12.500 in lottery') self.assertEqual(str(a), b'Girl wins \xe2\x82\xac12.500 in lottery')

View File

@ -170,7 +170,7 @@ class UtilTests(unittest.TestCase):
) )
self.assertEqual( self.assertEqual(
label_for_field("__str__", Article), label_for_field("__str__", Article),
"article" b"article"
) )
self.assertRaises( self.assertRaises(

View File

@ -280,7 +280,7 @@ class AdminFileWidgetTest(DjangoTestCase):
) )
self.assertHTMLEqual( self.assertHTMLEqual(
conditional_escape(w.render('test', SimpleUploadedFile('test', 'content'))), conditional_escape(w.render('test', SimpleUploadedFile('test', b'content'))),
'<input type="file" name="test" />', '<input type="file" name="test" />',
) )

View File

@ -822,7 +822,7 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase):
def test_second_call_doesnt_crash(self): def test_second_call_doesnt_crash(self):
err = StringIO.StringIO() err = StringIO.StringIO()
management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False, stderr=err) management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False, stderr=err)
self.assertTrue("Cache table 'test cache table' could not be created" in err.getvalue()) self.assertTrue(b"Cache table 'test cache table' could not be created" in err.getvalue())
@override_settings(USE_TZ=True) @override_settings(USE_TZ=True)

View File

@ -47,7 +47,7 @@ class TestingHttpRequest(HttpRequest):
class CsrfViewMiddlewareTest(TestCase): class CsrfViewMiddlewareTest(TestCase):
# The csrf token is potentially from an untrusted source, so could have # The csrf token is potentially from an untrusted source, so could have
# characters that need dealing with. # characters that need dealing with.
_csrf_id_cookie = "<1>\xc2\xa1" _csrf_id_cookie = b"<1>\xc2\xa1"
_csrf_id = "1" _csrf_id = "1"
def _get_GET_no_csrf_cookie_request(self): def _get_GET_no_csrf_cookie_request(self):

View File

@ -127,7 +127,7 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('test.file')) self.assertFalse(self.storage.exists('test.file'))
f = ContentFile('custom contents') f = ContentFile(b'custom contents')
f_name = self.storage.save('test.file', f) f_name = self.storage.save('test.file', f)
atime = self.storage.accessed_time(f_name) atime = self.storage.accessed_time(f_name)
@ -143,7 +143,7 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('test.file')) self.assertFalse(self.storage.exists('test.file'))
f = ContentFile('custom contents') f = ContentFile(b'custom contents')
f_name = self.storage.save('test.file', f) f_name = self.storage.save('test.file', f)
ctime = self.storage.created_time(f_name) ctime = self.storage.created_time(f_name)
@ -160,7 +160,7 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('test.file')) self.assertFalse(self.storage.exists('test.file'))
f = ContentFile('custom contents') f = ContentFile(b'custom contents')
f_name = self.storage.save('test.file', f) f_name = self.storage.save('test.file', f)
mtime = self.storage.modified_time(f_name) mtime = self.storage.modified_time(f_name)
@ -177,7 +177,7 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('test.file')) self.assertFalse(self.storage.exists('test.file'))
f = ContentFile('custom contents') f = ContentFile(b'custom contents')
f.name = 'test.file' f.name = 'test.file'
storage_f_name = self.storage.save(None, f) storage_f_name = self.storage.save(None, f)
@ -194,11 +194,11 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('path/to')) self.assertFalse(self.storage.exists('path/to'))
self.storage.save('path/to/test.file', self.storage.save('path/to/test.file',
ContentFile('file saved with path')) ContentFile(b'file saved with path'))
self.assertTrue(self.storage.exists('path/to')) self.assertTrue(self.storage.exists('path/to'))
self.assertEqual(self.storage.open('path/to/test.file').read(), self.assertEqual(self.storage.open('path/to/test.file').read(),
'file saved with path') b'file saved with path')
self.assertTrue(os.path.exists( self.assertTrue(os.path.exists(
os.path.join(self.temp_dir, 'path', 'to', 'test.file'))) os.path.join(self.temp_dir, 'path', 'to', 'test.file')))
@ -211,7 +211,7 @@ class FileStorageTests(unittest.TestCase):
""" """
self.assertFalse(self.storage.exists('test.file')) self.assertFalse(self.storage.exists('test.file'))
f = ContentFile('custom contents') f = ContentFile(b'custom contents')
f_name = self.storage.save('test.file', f) f_name = self.storage.save('test.file', f)
self.assertEqual(self.storage.path(f_name), self.assertEqual(self.storage.path(f_name),
@ -246,8 +246,8 @@ class FileStorageTests(unittest.TestCase):
self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_test_2'))
self.assertFalse(self.storage.exists('storage_dir_1')) self.assertFalse(self.storage.exists('storage_dir_1'))
f = self.storage.save('storage_test_1', ContentFile('custom content')) f = self.storage.save('storage_test_1', ContentFile(b'custom content'))
f = self.storage.save('storage_test_2', ContentFile('custom content')) f = self.storage.save('storage_test_2', ContentFile(b'custom content'))
os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1')) os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
dirs, files = self.storage.listdir('') dirs, files = self.storage.listdir('')
@ -304,18 +304,18 @@ class FileStorageTests(unittest.TestCase):
os.makedirs = fake_makedirs os.makedirs = fake_makedirs
self.storage.save('normal/test.file', self.storage.save('normal/test.file',
ContentFile('saved normally')) ContentFile(b'saved normally'))
self.assertEqual(self.storage.open('normal/test.file').read(), self.assertEqual(self.storage.open('normal/test.file').read(),
'saved normally') b'saved normally')
self.storage.save('raced/test.file', self.storage.save('raced/test.file',
ContentFile('saved with race')) ContentFile(b'saved with race'))
self.assertEqual(self.storage.open('raced/test.file').read(), self.assertEqual(self.storage.open('raced/test.file').read(),
'saved with race') b'saved with race')
# Check that OSErrors aside from EEXIST are still raised. # Check that OSErrors aside from EEXIST are still raised.
self.assertRaises(OSError, self.assertRaises(OSError,
self.storage.save, 'error/test.file', ContentFile('not saved')) self.storage.save, 'error/test.file', ContentFile(b'not saved'))
finally: finally:
os.makedirs = real_makedirs os.makedirs = real_makedirs
@ -341,16 +341,16 @@ class FileStorageTests(unittest.TestCase):
try: try:
os.remove = fake_remove os.remove = fake_remove
self.storage.save('normal.file', ContentFile('delete normally')) self.storage.save('normal.file', ContentFile(b'delete normally'))
self.storage.delete('normal.file') self.storage.delete('normal.file')
self.assertFalse(self.storage.exists('normal.file')) self.assertFalse(self.storage.exists('normal.file'))
self.storage.save('raced.file', ContentFile('delete with race')) self.storage.save('raced.file', ContentFile(b'delete with race'))
self.storage.delete('raced.file') self.storage.delete('raced.file')
self.assertFalse(self.storage.exists('normal.file')) self.assertFalse(self.storage.exists('normal.file'))
# Check that OSErrors aside from ENOENT are still raised. # Check that OSErrors aside from ENOENT are still raised.
self.storage.save('error.file', ContentFile('delete with error')) self.storage.save('error.file', ContentFile(b'delete with error'))
self.assertRaises(OSError, self.storage.delete, 'error.file') self.assertRaises(OSError, self.storage.delete, 'error.file')
finally: finally:
os.remove = real_remove os.remove = real_remove
@ -374,9 +374,9 @@ class CustomStorageTests(FileStorageTests):
storage_class = CustomStorage storage_class = CustomStorage
def test_custom_get_available_name(self): def test_custom_get_available_name(self):
first = self.storage.save('custom_storage', ContentFile('custom contents')) first = self.storage.save('custom_storage', ContentFile(b'custom contents'))
self.assertEqual(first, 'custom_storage') self.assertEqual(first, 'custom_storage')
second = self.storage.save('custom_storage', ContentFile('more contents')) second = self.storage.save('custom_storage', ContentFile(b'more contents'))
self.assertEqual(second, 'custom_storage.2') self.assertEqual(second, 'custom_storage.2')
self.storage.delete(first) self.storage.delete(first)
self.storage.delete(second) self.storage.delete(second)
@ -410,7 +410,7 @@ class FileSaveRaceConditionTest(unittest.TestCase):
shutil.rmtree(self.storage_dir) shutil.rmtree(self.storage_dir)
def save_file(self, name): def save_file(self, name):
name = self.storage.save(name, SlowFile("Data")) name = self.storage.save(name, SlowFile(b"Data"))
def test_race_condition(self): def test_race_condition(self):
self.thread.start() self.thread.start()
@ -433,7 +433,7 @@ class FileStoragePermissions(unittest.TestCase):
shutil.rmtree(self.storage_dir) shutil.rmtree(self.storage_dir)
def test_file_upload_permissions(self): def test_file_upload_permissions(self):
name = self.storage.save("the_file", ContentFile("data")) name = self.storage.save("the_file", ContentFile(b"data"))
actual_mode = os.stat(self.storage.path(name))[0] & 0777 actual_mode = os.stat(self.storage.path(name))[0] & 0777
self.assertEqual(actual_mode, 0666) self.assertEqual(actual_mode, 0666)
@ -453,8 +453,8 @@ class FileStoragePathParsing(unittest.TestCase):
sure we still mangle the file name instead of the directory name. sure we still mangle the file name instead of the directory name.
""" """
self.storage.save('dotted.path/test', ContentFile("1")) self.storage.save('dotted.path/test', ContentFile(b"1"))
self.storage.save('dotted.path/test', ContentFile("2")) self.storage.save('dotted.path/test', ContentFile(b"2"))
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
@ -465,8 +465,8 @@ class FileStoragePathParsing(unittest.TestCase):
File names with a dot as their first character don't have an extension, File names with a dot as their first character don't have an extension,
and the underscore should get added to the end. and the underscore should get added to the end.
""" """
self.storage.save('dotted.path/.test', ContentFile("1")) self.storage.save('dotted.path/.test', ContentFile(b"1"))
self.storage.save('dotted.path/.test', ContentFile("2")) self.storage.save('dotted.path/.test', ContentFile(b"2"))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1'))) self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
@ -542,11 +542,11 @@ class ContentFileTestCase(unittest.TestCase):
Test that the constructor of ContentFile accepts 'name' (#16590). Test that the constructor of ContentFile accepts 'name' (#16590).
""" """
def test_content_file_default_name(self): def test_content_file_default_name(self):
self.assertEqual(ContentFile("content").name, None) self.assertEqual(ContentFile(b"content").name, None)
def test_content_file_custom_name(self): def test_content_file_custom_name(self):
name = "I can have a name too!" name = "I can have a name too!"
self.assertEqual(ContentFile("content", name=name).name, name) self.assertEqual(ContentFile(b"content", name=name).name, name)
class NoNameFileTestCase(unittest.TestCase): class NoNameFileTestCase(unittest.TestCase):
""" """

View File

@ -24,7 +24,7 @@ UNICODE_FILENAME = u'test-0123456789_中文_Orléans.jpg'
class FileUploadTests(TestCase): class FileUploadTests(TestCase):
def test_simple_upload(self): def test_simple_upload(self):
with open(__file__) as fp: with open(__file__, 'rb') as fp:
post_data = { post_data = {
'name': 'Ringo', 'name': 'Ringo',
'file_field': fp, 'file_field': fp,
@ -36,11 +36,11 @@ class FileUploadTests(TestCase):
tdir = tempfile.gettempdir() tdir = tempfile.gettempdir()
file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir)
file1.write('a' * (2 ** 21)) file1.write(b'a' * (2 ** 21))
file1.seek(0) file1.seek(0)
file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir) file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir)
file2.write('a' * (10 * 2 ** 20)) file2.write(b'a' * (10 * 2 ** 20))
file2.seek(0) file2.seek(0)
post_data = { post_data = {
@ -89,7 +89,7 @@ class FileUploadTests(TestCase):
# This file contains chinese symbols and an accented char in the name. # This file contains chinese symbols and an accented char in the name.
with open(os.path.join(tdir, UNICODE_FILENAME.encode('utf-8')), 'w+b') as file1: with open(os.path.join(tdir, UNICODE_FILENAME.encode('utf-8')), 'w+b') as file1:
file1.write('b' * (2 ** 10)) file1.write(b'b' * (2 ** 10))
file1.seek(0) file1.seek(0)
post_data = { post_data = {
@ -214,7 +214,7 @@ class FileUploadTests(TestCase):
'CONTENT_TYPE': client.MULTIPART_CONTENT, 'CONTENT_TYPE': client.MULTIPART_CONTENT,
'PATH_INFO': '/file_uploads/echo/', 'PATH_INFO': '/file_uploads/echo/',
'REQUEST_METHOD': 'POST', 'REQUEST_METHOD': 'POST',
'wsgi.input': client.FakePayload(''), 'wsgi.input': client.FakePayload(b''),
} }
got = json.loads(self.client.request(**r).content) got = json.loads(self.client.request(**r).content)
self.assertEqual(got, {}) self.assertEqual(got, {})
@ -222,12 +222,12 @@ class FileUploadTests(TestCase):
def test_custom_upload_handler(self): def test_custom_upload_handler(self):
# A small file (under the 5M quota) # A small file (under the 5M quota)
smallfile = tempfile.NamedTemporaryFile() smallfile = tempfile.NamedTemporaryFile()
smallfile.write('a' * (2 ** 21)) smallfile.write(b'a' * (2 ** 21))
smallfile.seek(0) smallfile.seek(0)
# A big file (over the quota) # A big file (over the quota)
bigfile = tempfile.NamedTemporaryFile() bigfile = tempfile.NamedTemporaryFile()
bigfile.write('a' * (10 * 2 ** 20)) bigfile.write(b'a' * (10 * 2 ** 20))
bigfile.seek(0) bigfile.seek(0)
# Small file posting should work. # Small file posting should work.
@ -242,7 +242,7 @@ class FileUploadTests(TestCase):
def test_broken_custom_upload_handler(self): def test_broken_custom_upload_handler(self):
f = tempfile.NamedTemporaryFile() f = tempfile.NamedTemporaryFile()
f.write('a' * (2 ** 21)) f.write(b'a' * (2 ** 21))
f.seek(0) f.seek(0)
# AttributeError: You cannot alter upload handlers after the upload has been processed. # AttributeError: You cannot alter upload handlers after the upload has been processed.
@ -255,15 +255,15 @@ class FileUploadTests(TestCase):
def test_fileupload_getlist(self): def test_fileupload_getlist(self):
file1 = tempfile.NamedTemporaryFile() file1 = tempfile.NamedTemporaryFile()
file1.write('a' * (2 ** 23)) file1.write(b'a' * (2 ** 23))
file1.seek(0) file1.seek(0)
file2 = tempfile.NamedTemporaryFile() file2 = tempfile.NamedTemporaryFile()
file2.write('a' * (2 * 2 ** 18)) file2.write(b'a' * (2 * 2 ** 18))
file2.seek(0) file2.seek(0)
file2a = tempfile.NamedTemporaryFile() file2a = tempfile.NamedTemporaryFile()
file2a.write('a' * (5 * 2 ** 20)) file2a.write(b'a' * (5 * 2 ** 20))
file2a.seek(0) file2a.seek(0)
response = self.client.post('/file_uploads/getlist_count/', { response = self.client.post('/file_uploads/getlist_count/', {
@ -299,14 +299,14 @@ class FileUploadTests(TestCase):
# this test would fail. So we need to know exactly what kind of error # this test would fail. So we need to know exactly what kind of error
# it raises when there is an attempt to read more than the available bytes: # it raises when there is an attempt to read more than the available bytes:
try: try:
client.FakePayload('a').read(2) client.FakePayload(b'a').read(2)
except Exception as reference_error: except Exception as reference_error:
pass pass
# install the custom handler that tries to access request.POST # install the custom handler that tries to access request.POST
self.client.handler = POSTAccessingHandler() self.client.handler = POSTAccessingHandler()
with open(__file__) as fp: with open(__file__, 'rb') as fp:
post_data = { post_data = {
'name': 'Ringo', 'name': 'Ringo',
'file_field': fp, 'file_field': fp,
@ -374,7 +374,7 @@ class DirectoryCreationTests(unittest.TestCase):
"""Permission errors are not swallowed""" """Permission errors are not swallowed"""
os.chmod(temp_storage.location, 0500) os.chmod(temp_storage.location, 0500)
try: try:
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', 'x')) self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'))
except OSError as err: except OSError as err:
self.assertEqual(err.errno, errno.EACCES) self.assertEqual(err.errno, errno.EACCES)
except Exception: except Exception:
@ -383,9 +383,9 @@ class DirectoryCreationTests(unittest.TestCase):
def test_not_a_directory(self): def test_not_a_directory(self):
"""The correct IOError is raised when the upload directory name exists but isn't a directory""" """The correct IOError is raised when the upload directory name exists but isn't a directory"""
# Create a file with the upload directory name # Create a file with the upload directory name
open(UPLOAD_TO, 'w').close() open(UPLOAD_TO, 'wb').close()
try: try:
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', 'x')) self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'))
except IOError as err: except IOError as err:
# The test needs to be done on a specific string as IOError # The test needs to be done on a specific string as IOError
# is raised even without the patch (just not early enough) # is raised even without the patch (just not early enough)

View File

@ -540,27 +540,27 @@ class FieldsTests(SimpleTestCase):
self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None)
self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None, '') self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None, '')
self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', '')) self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', b''))
self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '') self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', b''), '')
self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf')) self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf'))
self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file') self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file')
self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None)) self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None))
self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', '')) self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', b''))
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', b'Some File Content'))))
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))) self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', u'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8')))))
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))) self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', b'Some File Content'), 'files/test4.pdf')))
def test_filefield_2(self): def test_filefield_2(self):
f = FileField(max_length = 5) f = FileField(max_length = 5)
self.assertRaisesMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world')) self.assertRaisesMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', b'hello world'))
self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf'))
self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', b'Some File Content'))))
def test_filefield_3(self): def test_filefield_3(self):
f = FileField(allow_empty_file=True) f = FileField(allow_empty_file=True)
self.assertEqual(SimpleUploadedFile, self.assertEqual(SimpleUploadedFile,
type(f.clean(SimpleUploadedFile('name', '')))) type(f.clean(SimpleUploadedFile('name', b''))))
# URLField ################################################################## # URLField ##################################################################

View File

@ -1463,17 +1463,17 @@ class FormsTestCase(TestCase):
f = FileForm(data={}, files={}, auto_id=False) f = FileForm(data={}, files={}, auto_id=False)
self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="file" name="file1" /></td></tr>') self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="file" name="file1" /></td></tr>')
f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', '')}, auto_id=False) f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', b'')}, auto_id=False)
self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>The submitted file is empty.</li></ul><input type="file" name="file1" /></td></tr>') self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>The submitted file is empty.</li></ul><input type="file" name="file1" /></td></tr>')
f = FileForm(data={}, files={'file1': 'something that is not a file'}, auto_id=False) f = FileForm(data={}, files={'file1': 'something that is not a file'}, auto_id=False)
self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>No file was submitted. Check the encoding type on the form.</li></ul><input type="file" name="file1" /></td></tr>') self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><ul class="errorlist"><li>No file was submitted. Check the encoding type on the form.</li></ul><input type="file" name="file1" /></td></tr>')
f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', 'some content')}, auto_id=False) f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', b'some content')}, auto_id=False)
self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><input type="file" name="file1" /></td></tr>') self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><input type="file" name="file1" /></td></tr>')
self.assertTrue(f.is_valid()) self.assertTrue(f.is_valid())
f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')}, auto_id=False) f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', u'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><input type="file" name="file1" /></td></tr>') self.assertHTMLEqual(f.as_table(), '<tr><th>File1:</th><td><input type="file" name="file1" /></td></tr>')
def test_basic_processing_in_view(self): def test_basic_processing_in_view(self):

View File

@ -106,7 +106,7 @@ class ModelFormCallableModelDefault(TestCase):
class FormsModelTestCase(TestCase): class FormsModelTestCase(TestCase):
def test_unicode_filename(self): def test_unicode_filename(self):
# FileModel with unicode filename and data ######################### # FileModel with unicode filename and data #########################
f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')}, auto_id=False) f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', u'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
self.assertTrue(f.is_valid()) self.assertTrue(f.is_valid())
self.assertTrue('file1' in f.cleaned_data) self.assertTrue('file1' in f.cleaned_data)
m = FileModel.objects.create(file=f.cleaned_data['file1']) m = FileModel.objects.create(file=f.cleaned_data['file1'])
@ -177,7 +177,7 @@ class RelatedModelFormTests(TestCase):
class Meta: class Meta:
model=A model=A
self.assertRaises(ValueError, ModelFormMetaclass, 'Form', (ModelForm,), {'Meta': Meta}) self.assertRaises(ValueError, ModelFormMetaclass, b'Form', (ModelForm,), {'Meta': Meta})
class B(models.Model): class B(models.Model):
pass pass
@ -195,4 +195,4 @@ class RelatedModelFormTests(TestCase):
class Meta: class Meta:
model=A model=A
self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), {'Meta': Meta}), ModelForm)) self.assertTrue(issubclass(ModelFormMetaclass(b'Form', (ModelForm,), {'Meta': Meta}), ModelForm))

View File

@ -54,10 +54,11 @@ class FormsRegressionsTestCase(TestCase):
# Testing choice validation with UTF-8 bytestrings as input (these are the # Testing choice validation with UTF-8 bytestrings as input (these are the
# Russian abbreviations "мес." and "шт.". # Russian abbreviations "мес." and "шт.".
UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.')) UNITS = ((b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'),
(b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'))
f = ChoiceField(choices=UNITS) f = ChoiceField(choices=UNITS)
self.assertEqual(f.clean(u'\u0448\u0442.'), u'\u0448\u0442.') self.assertEqual(f.clean(u'\u0448\u0442.'), u'\u0448\u0442.')
self.assertEqual(f.clean('\xd1\x88\xd1\x82.'), u'\u0448\u0442.') self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), u'\u0448\u0442.')
# Translated error messages used to be buggy. # Translated error messages used to be buggy.
with override('ru'): with override('ru'):

View File

@ -1182,7 +1182,7 @@ class ClearableFileInputTests(TestCase):
""" """
widget = ClearableFileInput() widget = ClearableFileInput()
widget.is_required = True widget.is_required = True
f = SimpleUploadedFile('something.txt', 'content') f = SimpleUploadedFile('something.txt', b'content')
self.assertEqual(widget.value_from_datadict( self.assertEqual(widget.value_from_datadict(
data={'myfile-clear': True}, data={'myfile-clear': True},
files={'myfile': f}, files={'myfile': f},

View File

@ -28,7 +28,7 @@ class HandlerTests(unittest.TestCase):
def test_bad_path_info(self): def test_bad_path_info(self):
"""Tests for bug #15672 ('request' referenced before assignment)""" """Tests for bug #15672 ('request' referenced before assignment)"""
environ = RequestFactory().get('/').environ environ = RequestFactory().get('/').environ
environ['PATH_INFO'] = '\xed' environ['PATH_INFO'] = b'\xed'
handler = WSGIHandler() handler = WSGIHandler()
response = handler(environ, lambda *a, **k: None) response = handler(environ, lambda *a, **k: None)
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)

View File

@ -174,7 +174,7 @@ class QueryDictTests(unittest.TestCase):
QueryDicts must be able to handle invalid input encoding (in this QueryDicts must be able to handle invalid input encoding (in this
case, bad UTF-8 encoding). case, bad UTF-8 encoding).
""" """
q = QueryDict('foo=bar&foo=\xff') q = QueryDict(b'foo=bar&foo=\xff')
self.assertEqual(q['foo'], u'\ufffd') self.assertEqual(q['foo'], u'\ufffd')
self.assertEqual(q.getlist('foo'), [u'bar', u'\ufffd']) self.assertEqual(q.getlist('foo'), [u'bar', u'\ufffd'])
@ -198,7 +198,7 @@ class QueryDictTests(unittest.TestCase):
def test_non_default_encoding(self): def test_non_default_encoding(self):
"""#13572 - QueryDict with a non-default encoding""" """#13572 - QueryDict with a non-default encoding"""
q = QueryDict('sbb=one', encoding='rot_13') q = QueryDict(b'sbb=one', encoding='rot_13')
self.assertEqual(q.encoding , 'rot_13' ) self.assertEqual(q.encoding , 'rot_13' )
self.assertEqual(q.items() , [(u'foo', u'bar')] ) self.assertEqual(q.items() , [(u'foo', u'bar')] )
self.assertEqual(q.urlencode() , 'sbb=one' ) self.assertEqual(q.urlencode() , 'sbb=one' )
@ -285,8 +285,8 @@ class HttpResponseTests(unittest.TestCase):
except StopIteration: except StopIteration:
break break
#'\xde\x9e' == unichr(1950).encode('utf-8') #'\xde\x9e' == unichr(1950).encode('utf-8')
self.assertEqual(result, ['1', '2', '3', '\xde\x9e']) self.assertEqual(result, ['1', '2', '3', b'\xde\x9e'])
self.assertEqual(r.content, '123\xde\x9e') self.assertEqual(r.content, b'123\xde\x9e')
#with Content-Encoding header #with Content-Encoding header
r = HttpResponse([1,1,2,4,8]) r = HttpResponse([1,1,2,4,8])
@ -321,7 +321,7 @@ class CookieTests(unittest.TestCase):
Test that we haven't broken normal encoding Test that we haven't broken normal encoding
""" """
c = SimpleCookie() c = SimpleCookie()
c['test'] = "\xf0" c['test'] = b"\xf0"
c2 = SimpleCookie() c2 = SimpleCookie()
c2.load(c.output()) c2.load(c.output())
self.assertEqual(c['test'].value, c2['test'].value) self.assertEqual(c['test'].value, c2['test'].value)

View File

@ -230,7 +230,7 @@ class TranslationTests(TestCase):
""" """
Translating a string requiring no auto-escaping shouldn't change the "safe" status. Translating a string requiring no auto-escaping shouldn't change the "safe" status.
""" """
s = mark_safe('Password') s = mark_safe(b'Password')
self.assertEqual(SafeString, type(s)) self.assertEqual(SafeString, type(s))
with translation.override('de', deactivate=True): with translation.override('de', deactivate=True):
self.assertEqual(SafeUnicode, type(ugettext(s))) self.assertEqual(SafeUnicode, type(ugettext(s)))

View File

@ -92,7 +92,7 @@ class MKLocalFlavorTests(SimpleTestCase):
""" """
Test that the empty option is there. Test that the empty option is there.
""" """
municipality_select_html = """\ municipality_select_html = b"""\
<select name="municipality" id="id_municipality"> <select name="municipality" id="id_municipality">
<option value="">---------</option> <option value="">---------</option>
<option value="AD">Aerodrom</option> <option value="AD">Aerodrom</option>

View File

@ -86,7 +86,7 @@ class MailTests(TestCase):
""" """
headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers) email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers)
self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent') self.assertEqual(email.message().as_string(), b'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
def test_from_header(self): def test_from_header(self):
""" """
@ -168,7 +168,7 @@ class MailTests(TestCase):
email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com']) email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com'])
email.encoding = 'iso-8859-1' email.encoding = 'iso-8859-1'
message = email.message() message = email.message()
self.assertTrue(message.as_string().startswith('Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com')) self.assertTrue(message.as_string().startswith(b'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com'))
self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.') self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.')
# Make sure MIME attachments also works correctly with other encodings than utf-8 # Make sure MIME attachments also works correctly with other encodings than utf-8
@ -177,8 +177,8 @@ class MailTests(TestCase):
msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com']) msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com'])
msg.encoding = 'iso-8859-1' msg.encoding = 'iso-8859-1'
msg.attach_alternative(html_content, "text/html") msg.attach_alternative(html_content, "text/html")
self.assertEqual(msg.message().get_payload(0).as_string(), 'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.') self.assertEqual(msg.message().get_payload(0).as_string(), b'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.')
self.assertEqual(msg.message().get_payload(1).as_string(), 'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>') self.assertEqual(msg.message().get_payload(1).as_string(), b'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>')
def test_attachments(self): def test_attachments(self):
"""Regression test for #9367""" """Regression test for #9367"""
@ -188,7 +188,7 @@ class MailTests(TestCase):
html_content = '<p>This is an <strong>important</strong> message.</p>' html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], headers=headers) msg = EmailMultiAlternatives(subject, text_content, from_email, [to], headers=headers)
msg.attach_alternative(html_content, "text/html") msg.attach_alternative(html_content, "text/html")
msg.attach("an attachment.pdf", "%PDF-1.4.%...", mimetype="application/pdf") msg.attach("an attachment.pdf", b"%PDF-1.4.%...", mimetype="application/pdf")
msg_str = msg.message().as_string() msg_str = msg.message().as_string()
message = email.message_from_string(msg_str) message = email.message_from_string(msg_str)
self.assertTrue(message.is_multipart()) self.assertTrue(message.is_multipart())
@ -205,7 +205,7 @@ class MailTests(TestCase):
content = 'This is the message.' content = 'This is the message.'
msg = EmailMessage(subject, content, from_email, [to], headers=headers) msg = EmailMessage(subject, content, from_email, [to], headers=headers)
# Unicode in file name # Unicode in file name
msg.attach(u"une pièce jointe.pdf", "%PDF-1.4.%...", mimetype="application/pdf") msg.attach(u"une pièce jointe.pdf", b"%PDF-1.4.%...", mimetype="application/pdf")
msg_str = msg.message().as_string() msg_str = msg.message().as_string()
message = email.message_from_string(msg_str) message = email.message_from_string(msg_str)
payload = message.get_payload() payload = message.get_payload()
@ -289,31 +289,31 @@ class MailTests(TestCase):
# Regression for #13433 - Make sure that EmailMessage doesn't mangle # Regression for #13433 - Make sure that EmailMessage doesn't mangle
# 'From ' in message body. # 'From ' in message body.
email = EmailMessage('Subject', 'From the future', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) email = EmailMessage('Subject', 'From the future', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
self.assertFalse('>From the future' in email.message().as_string()) self.assertFalse(b'>From the future' in email.message().as_string())
def test_dont_base64_encode(self): def test_dont_base64_encode(self):
# Ticket #3472 # Ticket #3472
# Shouldn't use Base64 encoding at all # Shouldn't use Base64 encoding at all
msg = EmailMessage('Subject', 'UTF-8 encoded body', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) msg = EmailMessage('Subject', 'UTF-8 encoded body', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
self.assertFalse('Content-Transfer-Encoding: base64' in msg.message().as_string()) self.assertFalse(b'Content-Transfer-Encoding: base64' in msg.message().as_string())
# Ticket #11212 # Ticket #11212
# Shouldn't use quoted printable, should detect it can represent content with 7 bit data # Shouldn't use quoted printable, should detect it can represent content with 7 bit data
msg = EmailMessage('Subject', 'Body with only ASCII characters.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) msg = EmailMessage('Subject', 'Body with only ASCII characters.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
s = msg.message().as_string() s = msg.message().as_string()
self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s) self.assertFalse(b'Content-Transfer-Encoding: quoted-printable' in s)
self.assertTrue('Content-Transfer-Encoding: 7bit' in s) self.assertTrue(b'Content-Transfer-Encoding: 7bit' in s)
# Shouldn't use quoted printable, should detect it can represent content with 8 bit data # Shouldn't use quoted printable, should detect it can represent content with 8 bit data
msg = EmailMessage('Subject', 'Body with latin characters: àáä.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) msg = EmailMessage('Subject', 'Body with latin characters: àáä.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
s = msg.message().as_string() s = msg.message().as_string()
self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s) self.assertFalse(b'Content-Transfer-Encoding: quoted-printable' in s)
self.assertTrue('Content-Transfer-Encoding: 8bit' in s) self.assertTrue(b'Content-Transfer-Encoding: 8bit' in s)
msg = EmailMessage('Subject', u'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) msg = EmailMessage('Subject', u'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
s = msg.message().as_string() s = msg.message().as_string()
self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s) self.assertFalse(b'Content-Transfer-Encoding: quoted-printable' in s)
self.assertTrue('Content-Transfer-Encoding: 8bit' in s) self.assertTrue(b'Content-Transfer-Encoding: 8bit' in s)
class BaseEmailBackendTests(object): class BaseEmailBackendTests(object):
@ -438,7 +438,7 @@ class BaseEmailBackendTests(object):
email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com']) email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com'])
mail.get_connection().send_messages([email]) mail.get_connection().send_messages([email])
message = self.get_the_message() message = self.get_the_message()
self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ') self.assertStartsWith(message.as_string(), b'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ')
def test_idn_send(self): def test_idn_send(self):
""" """
@ -517,8 +517,8 @@ class FileBackendTests(BaseEmailBackendTests, TestCase):
def get_mailbox_content(self): def get_mailbox_content(self):
messages = [] messages = []
for filename in os.listdir(self.tmp_dir): for filename in os.listdir(self.tmp_dir):
with open(os.path.join(self.tmp_dir, filename)) as fp: with open(os.path.join(self.tmp_dir, filename), 'rb') as fp:
session = fp.read().split('\n' + ('-' * 79) + '\n') session = fp.read().split(b'\n' + (b'-' * 79) + b'\n')
messages.extend(email.message_from_string(m) for m in session if m) messages.extend(email.message_from_string(m) for m in session if m)
return messages return messages
@ -569,7 +569,7 @@ class ConsoleBackendTests(BaseEmailBackendTests, TestCase):
self.stream = sys.stdout = StringIO() self.stream = sys.stdout = StringIO()
def get_mailbox_content(self): def get_mailbox_content(self):
messages = self.stream.getvalue().split('\n' + ('-' * 79) + '\n') messages = self.stream.getvalue().split(b'\n' + (b'-' * 79) + b'\n')
return [email.message_from_string(m) for m in messages if m] return [email.message_from_string(m) for m in messages if m]
def test_console_stream_kwarg(self): def test_console_stream_kwarg(self):

View File

@ -394,7 +394,7 @@ class FileFieldTests(unittest.TestCase):
form = DocumentForm() form = DocumentForm()
self.assertTrue('name="myfile"' in unicode(form)) self.assertTrue('name="myfile"' in unicode(form))
self.assertTrue('myfile-clear' not in unicode(form)) self.assertTrue('myfile-clear' not in unicode(form))
form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', 'content')}) form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', b'content')})
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
doc = form.save(commit=False) doc = form.save(commit=False)
self.assertEqual(doc.myfile.name, 'something.txt') self.assertEqual(doc.myfile.name, 'something.txt')
@ -411,11 +411,11 @@ class FileFieldTests(unittest.TestCase):
includes the current file and the clear checkbox. includes the current file and the clear checkbox.
""" """
form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', 'content')}) form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', b'content')})
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
doc = form.save(commit=False) doc = form.save(commit=False)
form = DocumentForm(instance=doc, form = DocumentForm(instance=doc,
files={'myfile': SimpleUploadedFile('something.txt', 'content')}, files={'myfile': SimpleUploadedFile('something.txt', b'content')},
data={'myfile-clear': 'true'}) data={'myfile-clear': 'true'})
self.assertTrue(not form.is_valid()) self.assertTrue(not form.is_valid())
self.assertEqual(form.errors['myfile'], self.assertEqual(form.errors['myfile'],

View File

@ -204,91 +204,91 @@ class RequestsTests(unittest.TestCase):
def test_limited_stream(self): def test_limited_stream(self):
# Read all of a limited stream # Read all of a limited stream
stream = LimitedStream(StringIO('test'), 2) stream = LimitedStream(StringIO(b'test'), 2)
self.assertEqual(stream.read(), 'te') self.assertEqual(stream.read(), b'te')
# Reading again returns nothing. # Reading again returns nothing.
self.assertEqual(stream.read(), '') self.assertEqual(stream.read(), b'')
# Read a number of characters greater than the stream has to offer # Read a number of characters greater than the stream has to offer
stream = LimitedStream(StringIO('test'), 2) stream = LimitedStream(StringIO(b'test'), 2)
self.assertEqual(stream.read(5), 'te') self.assertEqual(stream.read(5), b'te')
# Reading again returns nothing. # Reading again returns nothing.
self.assertEqual(stream.readline(5), '') self.assertEqual(stream.readline(5), b'')
# Read sequentially from a stream # Read sequentially from a stream
stream = LimitedStream(StringIO('12345678'), 8) stream = LimitedStream(StringIO(b'12345678'), 8)
self.assertEqual(stream.read(5), '12345') self.assertEqual(stream.read(5), b'12345')
self.assertEqual(stream.read(5), '678') self.assertEqual(stream.read(5), b'678')
# Reading again returns nothing. # Reading again returns nothing.
self.assertEqual(stream.readline(5), '') self.assertEqual(stream.readline(5), b'')
# Read lines from a stream # Read lines from a stream
stream = LimitedStream(StringIO('1234\n5678\nabcd\nefgh\nijkl'), 24) stream = LimitedStream(StringIO(b'1234\n5678\nabcd\nefgh\nijkl'), 24)
# Read a full line, unconditionally # Read a full line, unconditionally
self.assertEqual(stream.readline(), '1234\n') self.assertEqual(stream.readline(), b'1234\n')
# Read a number of characters less than a line # Read a number of characters less than a line
self.assertEqual(stream.readline(2), '56') self.assertEqual(stream.readline(2), b'56')
# Read the rest of the partial line # Read the rest of the partial line
self.assertEqual(stream.readline(), '78\n') self.assertEqual(stream.readline(), b'78\n')
# Read a full line, with a character limit greater than the line length # Read a full line, with a character limit greater than the line length
self.assertEqual(stream.readline(6), 'abcd\n') self.assertEqual(stream.readline(6), b'abcd\n')
# Read the next line, deliberately terminated at the line end # Read the next line, deliberately terminated at the line end
self.assertEqual(stream.readline(4), 'efgh') self.assertEqual(stream.readline(4), b'efgh')
# Read the next line... just the line end # Read the next line... just the line end
self.assertEqual(stream.readline(), '\n') self.assertEqual(stream.readline(), b'\n')
# Read everything else. # Read everything else.
self.assertEqual(stream.readline(), 'ijkl') self.assertEqual(stream.readline(), b'ijkl')
# Regression for #15018 # Regression for #15018
# If a stream contains a newline, but the provided length # If a stream contains a newline, but the provided length
# is less than the number of provided characters, the newline # is less than the number of provided characters, the newline
# doesn't reset the available character count # doesn't reset the available character count
stream = LimitedStream(StringIO('1234\nabcdef'), 9) stream = LimitedStream(StringIO(b'1234\nabcdef'), 9)
self.assertEqual(stream.readline(10), '1234\n') self.assertEqual(stream.readline(10), b'1234\n')
self.assertEqual(stream.readline(3), 'abc') self.assertEqual(stream.readline(3), b'abc')
# Now expire the available characters # Now expire the available characters
self.assertEqual(stream.readline(3), 'd') self.assertEqual(stream.readline(3), b'd')
# Reading again returns nothing. # Reading again returns nothing.
self.assertEqual(stream.readline(2), '') self.assertEqual(stream.readline(2), b'')
# Same test, but with read, not readline. # Same test, but with read, not readline.
stream = LimitedStream(StringIO('1234\nabcdef'), 9) stream = LimitedStream(StringIO(b'1234\nabcdef'), 9)
self.assertEqual(stream.read(6), '1234\na') self.assertEqual(stream.read(6), b'1234\na')
self.assertEqual(stream.read(2), 'bc') self.assertEqual(stream.read(2), b'bc')
self.assertEqual(stream.read(2), 'd') self.assertEqual(stream.read(2), b'd')
self.assertEqual(stream.read(2), '') self.assertEqual(stream.read(2), b'')
self.assertEqual(stream.read(), '') self.assertEqual(stream.read(), b'')
def test_stream(self): def test_stream(self):
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
self.assertEqual(request.read(), 'name=value') self.assertEqual(request.read(), b'name=value')
def test_read_after_value(self): def test_read_after_value(self):
""" """
Reading from request is allowed after accessing request contents as Reading from request is allowed after accessing request contents as
POST or body. POST or body.
""" """
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
self.assertEqual(request.POST, {u'name': [u'value']}) self.assertEqual(request.POST, {u'name': [u'value']})
self.assertEqual(request.body, 'name=value') self.assertEqual(request.body, b'name=value')
self.assertEqual(request.read(), 'name=value') self.assertEqual(request.read(), b'name=value')
def test_value_after_read(self): def test_value_after_read(self):
""" """
Construction of POST or body is not allowed after reading Construction of POST or body is not allowed after reading
from request. from request.
""" """
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
self.assertEqual(request.read(2), 'na') self.assertEqual(request.read(2), b'na')
self.assertRaises(Exception, lambda: request.body) self.assertRaises(Exception, lambda: request.body)
self.assertEqual(request.POST, {}) self.assertEqual(request.POST, {})
@ -335,17 +335,17 @@ class RequestsTests(unittest.TestCase):
self.assertEqual(request.POST, {}) self.assertEqual(request.POST, {})
def test_read_by_lines(self): def test_read_by_lines(self):
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
self.assertEqual(list(request), ['name=value']) self.assertEqual(list(request), [b'name=value'])
def test_POST_after_body_read(self): def test_POST_after_body_read(self):
""" """
POST should be populated even if body is read first POST should be populated even if body is read first
""" """
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
@ -357,12 +357,12 @@ class RequestsTests(unittest.TestCase):
POST should be populated even if body is read first, and then POST should be populated even if body is read first, and then
the stream is read second. the stream is read second.
""" """
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
raw_data = request.body raw_data = request.body
self.assertEqual(request.read(1), u'n') self.assertEqual(request.read(1), b'n')
self.assertEqual(request.POST, {u'name': [u'value']}) self.assertEqual(request.POST, {u'name': [u'value']})
def test_POST_after_body_read_and_stream_read_multipart(self): def test_POST_after_body_read_and_stream_read_multipart(self):
@ -383,14 +383,14 @@ class RequestsTests(unittest.TestCase):
'wsgi.input': StringIO(payload)}) 'wsgi.input': StringIO(payload)})
raw_data = request.body raw_data = request.body
# Consume enough data to mess up the parsing: # Consume enough data to mess up the parsing:
self.assertEqual(request.read(13), u'--boundary\r\nC') self.assertEqual(request.read(13), b'--boundary\r\nC')
self.assertEqual(request.POST, {u'name': [u'value']}) self.assertEqual(request.POST, {u'name': [u'value']})
def test_raw_post_data_returns_body(self): def test_raw_post_data_returns_body(self):
""" """
HttpRequest.raw_post_body should be the same as HttpRequest.body HttpRequest.raw_post_body should be the same as HttpRequest.body
""" """
payload = 'Hello There!' payload = b'Hello There!'
request = WSGIRequest({ request = WSGIRequest({
'REQUEST_METHOD': 'POST', 'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
@ -409,7 +409,7 @@ class RequestsTests(unittest.TestCase):
def read(self, len=0): def read(self, len=0):
raise IOError("kaboom!") raise IOError("kaboom!")
payload = 'name=value' payload = b'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST', request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload), 'CONTENT_LENGTH': len(payload),
'wsgi.input': ExplodingStringIO(payload)}) 'wsgi.input': ExplodingStringIO(payload)})

View File

@ -12,8 +12,8 @@ class TestSigner(TestCase):
signer = signing.Signer('predictable-secret') signer = signing.Signer('predictable-secret')
signer2 = signing.Signer('predictable-secret2') signer2 = signing.Signer('predictable-secret2')
for s in ( for s in (
'hello', b'hello',
'3098247:529:087:', b'3098247:529:087:',
u'\u2019'.encode('utf-8'), u'\u2019'.encode('utf-8'),
): ):
self.assertEqual( self.assertEqual(
@ -69,7 +69,7 @@ class TestSigner(TestCase):
"dumps and loads be reversible for any JSON serializable object" "dumps and loads be reversible for any JSON serializable object"
objects = ( objects = (
['a', 'list'], ['a', 'list'],
'a string', b'a string',
u'a unicode string \u2019', u'a unicode string \u2019',
{'a': 'dictionary'}, {'a': 'dictionary'},
) )

View File

@ -387,8 +387,8 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
self.assertEqual(relpath, "cached/styles.93b1147e8552.css") self.assertEqual(relpath, "cached/styles.93b1147e8552.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn(b"cached/other.css", content)
self.assertIn("other.d41d8cd98f00.css", content) self.assertIn(b"other.d41d8cd98f00.css", content)
def test_path_with_querystring(self): def test_path_with_querystring(self):
relpath = self.cached_file_path("cached/styles.css?spam=eggs") relpath = self.cached_file_path("cached/styles.css?spam=eggs")
@ -397,8 +397,8 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
with storage.staticfiles_storage.open( with storage.staticfiles_storage.open(
"cached/styles.93b1147e8552.css") as relfile: "cached/styles.93b1147e8552.css") as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn(b"cached/other.css", content)
self.assertIn("other.d41d8cd98f00.css", content) self.assertIn(b"other.d41d8cd98f00.css", content)
def test_path_with_fragment(self): def test_path_with_fragment(self):
relpath = self.cached_file_path("cached/styles.css#eggs") relpath = self.cached_file_path("cached/styles.css#eggs")
@ -406,62 +406,62 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
with storage.staticfiles_storage.open( with storage.staticfiles_storage.open(
"cached/styles.93b1147e8552.css") as relfile: "cached/styles.93b1147e8552.css") as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn(b"cached/other.css", content)
self.assertIn("other.d41d8cd98f00.css", content) self.assertIn(b"other.d41d8cd98f00.css", content)
def test_path_with_querystring_and_fragment(self): def test_path_with_querystring_and_fragment(self):
relpath = self.cached_file_path("cached/css/fragments.css") relpath = self.cached_file_path("cached/css/fragments.css")
self.assertEqual(relpath, "cached/css/fragments.75433540b096.css") self.assertEqual(relpath, "cached/css/fragments.75433540b096.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertIn('fonts/font.a4b0478549d0.eot?#iefix', content) self.assertIn(b'fonts/font.a4b0478549d0.eot?#iefix', content)
self.assertIn('fonts/font.b8d603e42714.svg#webfontIyfZbseF', content) self.assertIn(b'fonts/font.b8d603e42714.svg#webfontIyfZbseF', content)
self.assertIn('data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content) self.assertIn(b'data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content)
self.assertIn('#default#VML', content) self.assertIn(b'#default#VML', content)
def test_template_tag_absolute(self): def test_template_tag_absolute(self):
relpath = self.cached_file_path("cached/absolute.css") relpath = self.cached_file_path("cached/absolute.css")
self.assertEqual(relpath, "cached/absolute.23f087ad823a.css") self.assertEqual(relpath, "cached/absolute.23f087ad823a.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("/static/cached/styles.css", content) self.assertNotIn(b"/static/cached/styles.css", content)
self.assertIn("/static/cached/styles.93b1147e8552.css", content) self.assertIn(b"/static/cached/styles.93b1147e8552.css", content)
self.assertIn('/static/cached/img/relative.acae32e4532b.png', content) self.assertIn(b'/static/cached/img/relative.acae32e4532b.png', content)
def test_template_tag_denorm(self): def test_template_tag_denorm(self):
relpath = self.cached_file_path("cached/denorm.css") relpath = self.cached_file_path("cached/denorm.css")
self.assertEqual(relpath, "cached/denorm.c5bd139ad821.css") self.assertEqual(relpath, "cached/denorm.c5bd139ad821.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("..//cached///styles.css", content) self.assertNotIn(b"..//cached///styles.css", content)
self.assertIn("../cached/styles.93b1147e8552.css", content) self.assertIn(b"../cached/styles.93b1147e8552.css", content)
self.assertNotIn("url(img/relative.png )", content) self.assertNotIn(b"url(img/relative.png )", content)
self.assertIn('url("img/relative.acae32e4532b.png', content) self.assertIn(b'url("img/relative.acae32e4532b.png', content)
def test_template_tag_relative(self): def test_template_tag_relative(self):
relpath = self.cached_file_path("cached/relative.css") relpath = self.cached_file_path("cached/relative.css")
self.assertEqual(relpath, "cached/relative.2217ea7273c2.css") self.assertEqual(relpath, "cached/relative.2217ea7273c2.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("../cached/styles.css", content) self.assertNotIn(b"../cached/styles.css", content)
self.assertNotIn('@import "styles.css"', content) self.assertNotIn(b'@import "styles.css"', content)
self.assertNotIn('url(img/relative.png)', content) self.assertNotIn(b'url(img/relative.png)', content)
self.assertIn('url("img/relative.acae32e4532b.png")', content) self.assertIn(b'url("img/relative.acae32e4532b.png")', content)
self.assertIn("../cached/styles.93b1147e8552.css", content) self.assertIn(b"../cached/styles.93b1147e8552.css", content)
def test_template_tag_deep_relative(self): def test_template_tag_deep_relative(self):
relpath = self.cached_file_path("cached/css/window.css") relpath = self.cached_file_path("cached/css/window.css")
self.assertEqual(relpath, "cached/css/window.9db38d5169f3.css") self.assertEqual(relpath, "cached/css/window.9db38d5169f3.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn('url(img/window.png)', content) self.assertNotIn(b'url(img/window.png)', content)
self.assertIn('url("img/window.acae32e4532b.png")', content) self.assertIn(b'url("img/window.acae32e4532b.png")', content)
def test_template_tag_url(self): def test_template_tag_url(self):
relpath = self.cached_file_path("cached/url.css") relpath = self.cached_file_path("cached/url.css")
self.assertEqual(relpath, "cached/url.615e21601e4b.css") self.assertEqual(relpath, "cached/url.615e21601e4b.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
self.assertIn("https://", relfile.read()) self.assertIn(b"https://", relfile.read())
def test_cache_invalidation(self): def test_cache_invalidation(self):
name = "cached/styles.css" name = "cached/styles.css"
@ -508,7 +508,7 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
""" """
Handle cache key creation correctly, see #17861. Handle cache key creation correctly, see #17861.
""" """
name = "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/" + chr(22) + chr(180) name = b"/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/" + chr(22) + chr(180)
cache_key = storage.staticfiles_storage.cache_key(name) cache_key = storage.staticfiles_storage.cache_key(name)
cache_validator = BaseCache({}) cache_validator = BaseCache({})
cache_validator.validate_key(cache_key) cache_validator.validate_key(cache_key)

View File

@ -54,7 +54,7 @@ class StringLookupTests(TestCase):
self.assertEqual(Foo.objects.get(friend__contains=u'\xe7'), fx) self.assertEqual(Foo.objects.get(friend__contains=u'\xe7'), fx)
# We can also do the above query using UTF-8 strings. # We can also do the above query using UTF-8 strings.
self.assertEqual(Foo.objects.get(friend__contains='\xc3\xa7'), fx) self.assertEqual(Foo.objects.get(friend__contains=b'\xc3\xa7'), fx)
def test_queries_on_textfields(self): def test_queries_on_textfields(self):
""" """

View File

@ -192,17 +192,17 @@ class Templates(unittest.TestCase):
test_template_sources('../dir1blah', template_dirs, []) test_template_sources('../dir1blah', template_dirs, [])
# UTF-8 bytestrings are permitted. # UTF-8 bytestrings are permitted.
test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs, test_template_sources(b'\xc3\x85ngstr\xc3\xb6m', template_dirs,
[u'/dir1/Ångström', u'/dir2/Ångström']) [u'/dir1/Ångström', u'/dir2/Ångström'])
# Unicode strings are permitted. # Unicode strings are permitted.
test_template_sources(u'Ångström', template_dirs, test_template_sources(u'Ångström', template_dirs,
[u'/dir1/Ångström', u'/dir2/Ångström']) [u'/dir1/Ångström', u'/dir2/Ångström'])
test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström']) test_template_sources(u'Ångström', [b'/Stra\xc3\x9fe'], [u'/Straße/Ångström'])
test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'], test_template_sources(b'\xc3\x85ngstr\xc3\xb6m', [b'/Stra\xc3\x9fe'],
[u'/Straße/Ångström']) [u'/Straße/Ångström'])
# Invalid UTF-8 encoding in bytestrings is not. Should raise a # Invalid UTF-8 encoding in bytestrings is not. Should raise a
# semi-useful error message. # semi-useful error message.
test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError) test_template_sources(b'\xc3\xc3', template_dirs, UnicodeDecodeError)
# Case insensitive tests (for win32). Not run unless we're on # Case insensitive tests (for win32). Not run unless we're on
# a case insensitive operating system. # a case insensitive operating system.
@ -1239,11 +1239,11 @@ class Templates(unittest.TestCase):
'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"), 'i18n02': ('{% load i18n %}{% trans "xxxyyyxxx" %}', {}, "xxxyyyxxx"),
# simple translation of a variable # simple translation of a variable
'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u"Å"), 'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': b'\xc3\x85'}, u"Å"),
# simple translation of a variable and filter # simple translation of a variable and filter
'i18n04': ('{% load i18n %}{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u'å'), 'i18n04': ('{% load i18n %}{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}', {'anton': b'\xc3\x85'}, u'å'),
'legacyi18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u'å'), 'legacyi18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': b'\xc3\x85'}, u'å'),
# simple translation of a string with interpolation # simple translation of a string with interpolation
'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"), 'i18n05': ('{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}', {'anton': 'yyy'}, "xxxyyyxxx"),

View File

@ -10,16 +10,16 @@ class UnicodeTests(TestCase):
t1 = Template(u'ŠĐĆŽćžšđ {{ var }}') t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
# Templates can also be created from bytestrings. These are assumed to # Templates can also be created from bytestrings. These are assumed to
# be encoded using UTF-8. # be encoded using UTF-8.
s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}' s = b'\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
t2 = Template(s) t2 = Template(s)
s = '\x80\xc5\xc0' s = b'\x80\xc5\xc0'
self.assertRaises(TemplateEncodingError, Template, s) self.assertRaises(TemplateEncodingError, Template, s)
# Contexts can be constructed from unicode or UTF-8 bytestrings. # Contexts can be constructed from unicode or UTF-8 bytestrings.
c1 = Context({"var": "foo"}) c1 = Context({b"var": b"foo"})
c2 = Context({u"var": "foo"}) c2 = Context({u"var": b"foo"})
c3 = Context({"var": u"Đđ"}) c3 = Context({b"var": u"Đđ"})
c4 = Context({u"var": "\xc4\x90\xc4\x91"}) c4 = Context({u"var": b"\xc4\x90\xc4\x91"})
# Since both templates and all four contexts represent the same thing, # Since both templates and all four contexts represent the same thing,
# they all render the same (and are returned as unicode objects and # they all render the same (and are returned as unicode objects and

View File

@ -122,14 +122,14 @@ class AssertContainsTests(TestCase):
#Regression test for #10183 #Regression test for #10183
r = self.client.get('/test_client_regress/check_unicode/') r = self.client.get('/test_client_regress/check_unicode/')
self.assertContains(r, u'さかき') self.assertContains(r, u'さかき')
self.assertContains(r, '\xe5\xb3\xa0'.decode('utf-8')) self.assertContains(r, b'\xe5\xb3\xa0'.decode('utf-8'))
def test_unicode_not_contains(self): def test_unicode_not_contains(self):
"Unicode characters can be searched for, and not found in template context" "Unicode characters can be searched for, and not found in template context"
#Regression test for #10183 #Regression test for #10183
r = self.client.get('/test_client_regress/check_unicode/') r = self.client.get('/test_client_regress/check_unicode/')
self.assertNotContains(r, u'はたけ') self.assertNotContains(r, u'はたけ')
self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8')) self.assertNotContains(r, b'\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
def test_assert_contains_renders_template_response(self): def test_assert_contains_renders_template_response(self):
""" Test that we can pass in an unrendered SimpleTemplateReponse """ Test that we can pass in an unrendered SimpleTemplateReponse
@ -571,25 +571,25 @@ class URLEscapingTests(TestCase):
"Get a view that has a simple string argument" "Get a view that has a simple string argument"
response = self.client.get(reverse('arg_view', args=['Slartibartfast'])) response = self.client.get(reverse('arg_view', args=['Slartibartfast']))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Howdy, Slartibartfast') self.assertEqual(response.content, b'Howdy, Slartibartfast')
def test_argument_with_space_get(self): def test_argument_with_space_get(self):
"Get a view that has a string argument that requires escaping" "Get a view that has a string argument that requires escaping"
response = self.client.get(reverse('arg_view', args=['Arthur Dent'])) response = self.client.get(reverse('arg_view', args=['Arthur Dent']))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Hi, Arthur') self.assertEqual(response.content, b'Hi, Arthur')
def test_simple_argument_post(self): def test_simple_argument_post(self):
"Post for a view that has a simple string argument" "Post for a view that has a simple string argument"
response = self.client.post(reverse('arg_view', args=['Slartibartfast'])) response = self.client.post(reverse('arg_view', args=['Slartibartfast']))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Howdy, Slartibartfast') self.assertEqual(response.content, b'Howdy, Slartibartfast')
def test_argument_with_space_post(self): def test_argument_with_space_post(self):
"Post for a view that has a string argument that requires escaping" "Post for a view that has a string argument that requires escaping"
response = self.client.post(reverse('arg_view', args=['Arthur Dent'])) response = self.client.post(reverse('arg_view', args=['Arthur Dent']))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'Hi, Arthur') self.assertEqual(response.content, b'Hi, Arthur')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class ExceptionTests(TestCase): class ExceptionTests(TestCase):
@ -721,17 +721,17 @@ class SessionTests(TestCase):
# The session doesn't exist to start. # The session doesn't exist to start.
response = self.client.get('/test_client_regress/check_session/') response = self.client.get('/test_client_regress/check_session/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'NO') self.assertEqual(response.content, b'NO')
# This request sets a session variable. # This request sets a session variable.
response = self.client.get('/test_client_regress/set_session/') response = self.client.get('/test_client_regress/set_session/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'set_session') self.assertEqual(response.content, b'set_session')
# Check that the session has been modified # Check that the session has been modified
response = self.client.get('/test_client_regress/check_session/') response = self.client.get('/test_client_regress/check_session/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'YES') self.assertEqual(response.content, b'YES')
# Log in # Log in
login = self.client.login(username='testclient',password='password') login = self.client.login(username='testclient',password='password')
@ -740,7 +740,7 @@ class SessionTests(TestCase):
# Session should still contain the modified value # Session should still contain the modified value
response = self.client.get('/test_client_regress/check_session/') response = self.client.get('/test_client_regress/check_session/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'YES') self.assertEqual(response.content, b'YES')
def test_logout(self): def test_logout(self):
"""Logout should work whether the user is logged in or not (#9978).""" """Logout should work whether the user is logged in or not (#9978)."""
@ -755,39 +755,39 @@ class RequestMethodTests(TestCase):
"Request a view via request method GET" "Request a view via request method GET"
response = self.client.get('/test_client_regress/request_methods/') response = self.client.get('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: GET') self.assertEqual(response.content, b'request method: GET')
def test_post(self): def test_post(self):
"Request a view via request method POST" "Request a view via request method POST"
response = self.client.post('/test_client_regress/request_methods/') response = self.client.post('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: POST') self.assertEqual(response.content, b'request method: POST')
def test_head(self): def test_head(self):
"Request a view via request method HEAD" "Request a view via request method HEAD"
response = self.client.head('/test_client_regress/request_methods/') response = self.client.head('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# A HEAD request doesn't return any content. # A HEAD request doesn't return any content.
self.assertNotEqual(response.content, 'request method: HEAD') self.assertNotEqual(response.content, b'request method: HEAD')
self.assertEqual(response.content, '') self.assertEqual(response.content, b'')
def test_options(self): def test_options(self):
"Request a view via request method OPTIONS" "Request a view via request method OPTIONS"
response = self.client.options('/test_client_regress/request_methods/') response = self.client.options('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: OPTIONS') self.assertEqual(response.content, b'request method: OPTIONS')
def test_put(self): def test_put(self):
"Request a view via request method PUT" "Request a view via request method PUT"
response = self.client.put('/test_client_regress/request_methods/') response = self.client.put('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: PUT') self.assertEqual(response.content, b'request method: PUT')
def test_delete(self): def test_delete(self):
"Request a view via request method DELETE" "Request a view via request method DELETE"
response = self.client.delete('/test_client_regress/request_methods/') response = self.client.delete('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: DELETE') self.assertEqual(response.content, b'request method: DELETE')
class RequestMethodStringDataTests(TestCase): class RequestMethodStringDataTests(TestCase):
def test_post(self): def test_post(self):
@ -796,7 +796,7 @@ class RequestMethodStringDataTests(TestCase):
data = u'{"test": "json"}' data = u'{"test": "json"}'
response = self.client.post('/test_client_regress/request_methods/', data=data, content_type='application/json') response = self.client.post('/test_client_regress/request_methods/', data=data, content_type='application/json')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: POST') self.assertEqual(response.content, b'request method: POST')
def test_put(self): def test_put(self):
"Request a view with string data via request method PUT" "Request a view with string data via request method PUT"
@ -804,7 +804,7 @@ class RequestMethodStringDataTests(TestCase):
data = u'{"test": "json"}' data = u'{"test": "json"}'
response = self.client.put('/test_client_regress/request_methods/', data=data, content_type='application/json') response = self.client.put('/test_client_regress/request_methods/', data=data, content_type='application/json')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'request method: PUT') self.assertEqual(response.content, b'request method: PUT')
class QueryStringTests(TestCase): class QueryStringTests(TestCase):
def test_get_like_requests(self): def test_get_like_requests(self):
@ -913,34 +913,34 @@ class DummyFile(object):
class UploadedFileEncodingTest(TestCase): class UploadedFileEncodingTest(TestCase):
def test_file_encoding(self): def test_file_encoding(self):
encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin')) encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
self.assertEqual('--TEST_BOUNDARY', encoded_file[0]) self.assertEqual(b'--TEST_BOUNDARY', encoded_file[0])
self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1]) self.assertEqual(b'Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1]) self.assertEqual(b'TEST_FILE_CONTENT', encoded_file[-1])
def test_guesses_content_type_on_file_encoding(self): def test_guesses_content_type_on_file_encoding(self):
self.assertEqual('Content-Type: application/octet-stream', self.assertEqual(b'Content-Type: application/octet-stream',
encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2]) encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
self.assertEqual('Content-Type: text/plain', self.assertEqual(b'Content-Type: text/plain',
encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2]) encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
self.assertIn(encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2], ( self.assertIn(encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2], (
'Content-Type: application/x-compress', b'Content-Type: application/x-compress',
'Content-Type: application/x-zip', b'Content-Type: application/x-zip',
'Content-Type: application/x-zip-compressed', b'Content-Type: application/x-zip-compressed',
'Content-Type: application/zip',)) b'Content-Type: application/zip',))
self.assertEqual('Content-Type: application/octet-stream', self.assertEqual(b'Content-Type: application/octet-stream',
encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2]) encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
class RequestHeadersTest(TestCase): class RequestHeadersTest(TestCase):
def test_client_headers(self): def test_client_headers(self):
"A test client can receive custom headers" "A test client can receive custom headers"
response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123') response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
self.assertEqual(response.content, "HTTP_X_ARG_CHECK: Testing 123") self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_client_headers_redirect(self): def test_client_headers_redirect(self):
"Test client headers are preserved through redirects" "Test client headers are preserved through redirects"
response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123') response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
self.assertEqual(response.content, "HTTP_X_ARG_CHECK: Testing 123") self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
self.assertRedirects(response, '/test_client_regress/check_headers/', self.assertRedirects(response, '/test_client_regress/check_headers/',
status_code=301, target_status_code=200) status_code=301, target_status_code=200)
@ -956,22 +956,22 @@ class ReadLimitedStreamTest(TestCase):
def test_body_from_empty_request(self): def test_body_from_empty_request(self):
"""HttpRequest.body on a test client GET request should return """HttpRequest.body on a test client GET request should return
the empty string.""" the empty string."""
self.assertEqual(self.client.get("/test_client_regress/body/").content, '') self.assertEqual(self.client.get("/test_client_regress/body/").content, b'')
def test_read_from_empty_request(self): def test_read_from_empty_request(self):
"""HttpRequest.read() on a test client GET request should return the """HttpRequest.read() on a test client GET request should return the
empty string.""" empty string."""
self.assertEqual(self.client.get("/test_client_regress/read_all/").content, '') self.assertEqual(self.client.get("/test_client_regress/read_all/").content, b'')
def test_read_numbytes_from_empty_request(self): def test_read_numbytes_from_empty_request(self):
"""HttpRequest.read(LARGE_BUFFER) on a test client GET request should """HttpRequest.read(LARGE_BUFFER) on a test client GET request should
return the empty string.""" return the empty string."""
self.assertEqual(self.client.get("/test_client_regress/read_buffer/").content, '') self.assertEqual(self.client.get("/test_client_regress/read_buffer/").content, b'')
def test_read_from_nonempty_request(self): def test_read_from_nonempty_request(self):
"""HttpRequest.read() on a test client PUT request with some payload """HttpRequest.read() on a test client PUT request with some payload
should return that payload.""" should return that payload."""
payload = 'foobar' payload = b'foobar'
self.assertEqual(self.client.put("/test_client_regress/read_all/", self.assertEqual(self.client.put("/test_client_regress/read_all/",
data=payload, data=payload,
content_type='text/plain').content, payload) content_type='text/plain').content, payload)
@ -979,7 +979,7 @@ class ReadLimitedStreamTest(TestCase):
def test_read_numbytes_from_nonempty_request(self): def test_read_numbytes_from_nonempty_request(self):
"""HttpRequest.read(LARGE_BUFFER) on a test client PUT request with """HttpRequest.read(LARGE_BUFFER) on a test client PUT request with
some payload should return that payload.""" some payload should return that payload."""
payload = 'foobar' payload = b'foobar'
self.assertEqual(self.client.put("/test_client_regress/read_buffer/", self.assertEqual(self.client.put("/test_client_regress/read_buffer/",
data=payload, data=payload,
content_type='text/plain').content, payload) content_type='text/plain').content, payload)

View File

@ -3,8 +3,8 @@
class BrokenException(Exception): class BrokenException(Exception):
pass pass
except_args = ('Broken!', # plain exception with ASCII text except_args = (b'Broken!', # plain exception with ASCII text
u'¡Broken!', # non-ASCII unicode data u'¡Broken!', # non-ASCII unicode data
'¡Broken!', # non-ASCII, utf-8 encoded bytestring u'¡Broken!'.encode('utf-8'), # non-ASCII, utf-8 encoded bytestring
'\xa1Broken!', ) # non-ASCII, latin1 bytestring b'\xa1Broken!', ) # non-ASCII, latin1 bytestring