Permalink
Browse files

Marked bytestrings with b prefix. Refs #18269

This is a preparation for unicode literals general usage in
Django (Python 3 compatibility).
  • Loading branch information...
1 parent 822d6d6 commit 38408f8007eae21b9f1cbbcc7f86d4b2042ff86a @claudep claudep committed May 19, 2012
Showing with 297 additions and 296 deletions.
  1. +1 −1 django/contrib/humanize/tests.py
  2. +1 −1 django/core/context_processors.py
  3. +1 −1 django/core/files/base.py
  4. +2 −2 django/core/files/uploadedfile.py
  5. +5 −5 django/core/handlers/wsgi.py
  6. +9 −9 django/db/backends/sqlite3/base.py
  7. +2 −2 django/db/models/base.py
  8. +2 −2 django/forms/models.py
  9. +3 −3 django/http/__init__.py
  10. +19 −19 django/http/multipartparser.py
  11. +4 −4 django/utils/crypto.py
  12. +2 −2 django/utils/encoding.py
  13. +1 −1 django/utils/html.py
  14. +1 −1 django/views/debug.py
  15. +1 −1 docs/ref/files/file.txt
  16. +2 −2 docs/ref/unicode.txt
  17. +7 −7 tests/modeltests/model_forms/tests.py
  18. +9 −9 tests/modeltests/serializers/tests.py
  19. +4 −4 tests/modeltests/str/tests.py
  20. +1 −1 tests/regressiontests/admin_util/tests.py
  21. +1 −1 tests/regressiontests/admin_widgets/tests.py
  22. +1 −1 tests/regressiontests/cache/tests.py
  23. +1 −1 tests/regressiontests/csrf_tests/tests.py
  24. +27 −27 tests/regressiontests/file_storage/tests.py
  25. +16 −16 tests/regressiontests/file_uploads/tests.py
  26. +9 −9 tests/regressiontests/forms/tests/fields.py
  27. +3 −3 tests/regressiontests/forms/tests/forms.py
  28. +3 −3 tests/regressiontests/forms/tests/models.py
  29. +3 −2 tests/regressiontests/forms/tests/regressions.py
  30. +1 −1 tests/regressiontests/forms/tests/widgets.py
  31. +1 −1 tests/regressiontests/handlers/tests.py
  32. +5 −5 tests/regressiontests/httpwrappers/tests.py
  33. +1 −1 tests/regressiontests/i18n/tests.py
  34. +1 −1 tests/regressiontests/localflavor/mk/tests.py
  35. +18 −18 tests/regressiontests/mail/tests.py
  36. +3 −3 tests/regressiontests/model_forms_regress/tests.py
  37. +44 −44 tests/regressiontests/requests/tests.py
  38. +3 −3 tests/regressiontests/signing/tests.py
  39. +26 −26 tests/regressiontests/staticfiles_tests/tests.py
  40. +1 −1 tests/regressiontests/string_lookup/tests.py
  41. +7 −7 tests/regressiontests/templates/tests.py
  42. +6 −6 tests/regressiontests/templates/unicode.py
  43. +36 −36 tests/regressiontests/test_client_regress/models.py
  44. +3 −3 tests/regressiontests/views/__init__.py
@@ -190,7 +190,7 @@ def now(self, tz=None):
orig_humanize_datetime = humanize.datetime
orig_timesince_datetime = timesince.datetime
humanize.datetime = MockDateTime
- timesince.datetime = new.module("mock_datetime")
+ timesince.datetime = new.module(b"mock_datetime")
timesince.datetime.datetime = MockDateTime
try:
@@ -22,7 +22,7 @@ def _get_val():
# In order to be able to provide debugging info in the
# case of misconfiguration, we use a sentinel value
# instead of returning an empty dict.
- return 'NOTPROVIDED'
+ return b'NOTPROVIDED'
@uruz
uruz May 19, 2012

NOTPROVIDED have b prefix there, but not at django/template/defaulttags.py line 45. Why?

@claudep
claudep May 19, 2012 Django member

Granted, this is inconsitent. At some point, I think that the csrf processor will simply return a Unicode string, but I'm not there yet. Feel free to create a ticket if you want to track this issue.

else:
return token
_get_val = lazy(_get_val, str)
@@ -125,7 +125,7 @@ class ContentFile(File):
A File-like object that takes just raw content, rather than an actual file.
"""
def __init__(self, content, name=None):
- content = content or ''
+ content = content or b''
super(ContentFile, self).__init__(BytesIO(content), name=name)
self.size = len(content)
@@ -30,8 +30,8 @@ def __init__(self, file=None, name=None, content_type=None, size=None, charset=N
self.charset = charset
def __repr__(self):
- return "<%s: %s (%s)>" % (
- self.__class__.__name__, smart_str(self.name), self.content_type)
+ return smart_str("<%s: %s (%s)>" % (
+ self.__class__.__name__, self.name, self.content_type))
def _get_name(self):
return self._name
@@ -78,32 +78,32 @@ class LimitedStream(object):
def __init__(self, stream, limit, buf_size=64 * 1024 * 1024):
self.stream = stream
self.remaining = limit
- self.buffer = ''
+ self.buffer = b''
self.buf_size = buf_size
def _read_limited(self, size=None):
if size is None or size > self.remaining:
size = self.remaining
if size == 0:
- return ''
+ return b''
result = self.stream.read(size)
self.remaining -= len(result)
return result
def read(self, size=None):
if size is None:
result = self.buffer + self._read_limited()
- self.buffer = ''
+ self.buffer = b''
elif size < len(self.buffer):
result = self.buffer[:size]
self.buffer = self.buffer[size:]
else: # size >= len(self.buffer)
result = self.buffer + self._read_limited(size - len(self.buffer))
- self.buffer = ''
+ self.buffer = b''
return result
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):
if size:
# since size is not None here, len(self.buffer) < size
@@ -51,15 +51,15 @@ def adapt_datetime_with_timezone_support(value):
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
value = value.astimezone(timezone.utc).replace(tzinfo=None)
- return value.isoformat(" ")
-
-Database.register_converter("bool", lambda s: str(s) == '1')
-Database.register_converter("time", parse_time)
-Database.register_converter("date", parse_date)
-Database.register_converter("datetime", parse_datetime_with_timezone_support)
-Database.register_converter("timestamp", parse_datetime_with_timezone_support)
-Database.register_converter("TIMESTAMP", parse_datetime_with_timezone_support)
-Database.register_converter("decimal", util.typecast_decimal)
+ return value.isoformat(b" ")
+
+Database.register_converter(b"bool", lambda s: str(s) == '1')
+Database.register_converter(b"time", parse_time)
+Database.register_converter(b"date", parse_date)
+Database.register_converter(b"datetime", parse_datetime_with_timezone_support)
+Database.register_converter(b"timestamp", parse_datetime_with_timezone_support)
+Database.register_converter(b"TIMESTAMP", parse_datetime_with_timezone_support)
+Database.register_converter(b"decimal", util.typecast_decimal)
Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
if Database.version_info >= (2, 4, 1):
@@ -57,11 +57,11 @@ def __new__(cls, name, bases, attrs):
new_class.add_to_class('_meta', Options(meta, **kwargs))
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
for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
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
for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
or (MultipleObjectsReturned,), module))
@@ -388,10 +388,10 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
parent = (object,)
if hasattr(form, 'Meta'):
parent = (form.Meta, object)
- Meta = type('Meta', parent, attrs)
+ Meta = type(b'Meta', parent, attrs)
# 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.
form_class_attrs = {
@@ -18,7 +18,7 @@
# See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256
_tc = Cookie.SimpleCookie()
try:
- _tc.load('foo:bar=1')
+ _tc.load(b'foo:bar=1')
_cookie_allows_colon_in_names = True
except Cookie.CookieError:
_cookie_allows_colon_in_names = False
@@ -650,8 +650,8 @@ def delete_cookie(self, key, path='/', domain=None):
def _get_content(self):
if self.has_header('Content-Encoding'):
- return ''.join([str(e) for e in self._container])
- return ''.join([smart_str(e, self._charset) for e in self._container])
+ return b''.join([str(e) for e in self._container])
+ return b''.join([smart_str(e, self._charset) for e in self._container])
def _set_content(self, value):
if hasattr(value, '__iter__'):
@@ -268,7 +268,7 @@ def __init__(self, producer, length=None):
"""
self._producer = producer
self._empty = False
- self._leftover = ''
+ self._leftover = b''
self.length = length
self.position = 0
self._remaining = length
@@ -282,7 +282,7 @@ def parts():
remaining = (size is not None and [size] or [self._remaining])[0]
# do the whole thing in one shot if no limit was provided.
if remaining is None:
- yield ''.join(self)
+ yield b''.join(self)
return
# otherwise do some bookkeeping to return exactly enough
@@ -298,7 +298,7 @@ def parts():
remaining -= len(emitting)
yield emitting
- out = ''.join(parts())
+ out = b''.join(parts())
return out
def next(self):
@@ -311,7 +311,7 @@ def next(self):
"""
if self._leftover:
output = self._leftover
- self._leftover = ''
+ self._leftover = b''
else:
output = next(self._producer)
self._unget_history = []
@@ -341,7 +341,7 @@ def unget(self, bytes):
return
self._update_unget_history(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):
"""
@@ -459,7 +459,7 @@ def next(self):
if not chunks:
raise StopIteration()
- chunk = ''.join(chunks)
+ chunk = b''.join(chunks)
boundary = self._find_boundary(chunk, len(chunk) < self._rollback)
if boundary:
@@ -495,9 +495,9 @@ def _find_boundary(self, data, eof = False):
end = index
next = index + len(self._boundary)
# backup over CRLF
- if data[max(0,end-1)] == '\n':
+ if data[max(0,end-1)] == b'\n':
end -= 1
- if data[max(0,end-1)] == '\r':
+ if data[max(0,end-1)] == b'\r':
end -= 1
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
# need to munch them later to prevent them from polluting
# the payload.
- header_end = chunk.find('\r\n\r\n')
+ header_end = chunk.find(b'\r\n\r\n')
def _parse_header(line):
main_value_pair, params = parse_header(line)
@@ -557,7 +557,7 @@ def _parse_header(line):
outdict = {}
# 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
# parameters") is from the Python docs.
try:
@@ -580,7 +580,7 @@ def _parse_header(line):
class Parser(object):
def __init__(self, stream, boundary):
self._stream = stream
- self._separator = '--' + boundary
+ self._separator = b'--' + boundary
def __iter__(self):
boundarystream = InterBoundaryIter(self._stream, self._separator)
@@ -590,27 +590,27 @@ def __iter__(self):
def parse_header(line):
""" Parse the header into a key-value. """
- plist = _parse_header_params(';' + line)
+ plist = _parse_header_params(b';' + line)
key = plist.pop(0).lower()
pdict = {}
for p in plist:
- i = p.find('=')
+ i = p.find(b'=')
if i >= 0:
name = p[:i].strip().lower()
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.replace('\\\\', '\\').replace('\\"', '"')
+ value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
pdict[name] = value
return key, pdict
def _parse_header_params(s):
plist = []
- while s[:1] == ';':
+ while s[:1] == b';':
s = s[1:]
- end = s.find(';')
- while end > 0 and s.count('"', 0, end) % 2:
- end = s.find(';', end + 1)
+ end = s.find(b';')
+ while end > 0 and s.count(b'"', 0, end) % 2:
+ end = s.find(b';', end + 1)
if end < 0:
end = len(s)
f = s[:end]
@@ -23,8 +23,8 @@
from django.conf import settings
-_trans_5c = "".join([chr(x ^ 0x5C) for x in xrange(256)])
-_trans_36 = "".join([chr(x ^ 0x36) for x in xrange(256)])
+_trans_5c = b"".join([chr(x ^ 0x5C) 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):
@@ -148,11 +148,11 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None):
def F(i):
def U():
- u = salt + struct.pack('>I', i)
+ u = salt + struct.pack(b'>I', i)
for j in xrange(int(iterations)):
u = _fast_hmac(password, u, digest).digest()
yield _bin_to_long(u)
return _long_to_bin(reduce(operator.xor, U()), hex_format_string)
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]
@@ -154,7 +154,7 @@ def iri_to_uri(iri):
# converted.
if iri is None:
return iri
- return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
+ return urllib.quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def filepath_to_uri(path):
"""Convert an file system path to a URI portion that is suitable for
@@ -173,7 +173,7 @@ def filepath_to_uri(path):
return path
# I know about `os.sep` and `os.altsep` but I want to leave
# 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
# given fallback encoding if the encoding is unsupported by python or could
@@ -116,7 +116,7 @@ def smart_urlquote(url):
# contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url):
# 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)
@@ -333,7 +333,7 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod
source = source.splitlines()
if source is None:
try:
- with open(filename) as fp:
+ with open(filename, 'rb') as fp:
source = fp.readlines()
except (OSError, IOError):
pass
@@ -96,7 +96,7 @@ The ``ContentFile`` Class
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'))
.. currentmodule:: django.core.files.images
@@ -269,15 +269,15 @@ You can pass either Unicode strings or UTF-8 bytestrings as arguments to
querysets are identical::
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
=========
You can use either Unicode or bytestrings when creating templates manually::
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.')
But the common case is to read templates from the filesystem, and this creates
Oops, something went wrong.

0 comments on commit 38408f8

Please sign in to comment.