1
0
mirror of https://github.com/django/django.git synced 2025-10-31 01:25:32 +00:00

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

@@ -268,7 +268,7 @@ class LazyStream(object):
"""
self._producer = producer
self._empty = False
self._leftover = ''
self._leftover = b''
self.length = length
self.position = 0
self._remaining = length
@@ -282,7 +282,7 @@ class LazyStream(object):
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 @@ class LazyStream(object):
remaining -= len(emitting)
yield emitting
out = ''.join(parts())
out = b''.join(parts())
return out
def next(self):
@@ -311,7 +311,7 @@ class LazyStream(object):
"""
if self._leftover:
output = self._leftover
self._leftover = ''
self._leftover = b''
else:
output = next(self._producer)
self._unget_history = []
@@ -341,7 +341,7 @@ class LazyStream(object):
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 @@ class BoundaryIter(object):
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 @@ class BoundaryIter(object):
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_boundary_stream(stream, max_header_size):
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_boundary_stream(stream, max_header_size):
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 @@ class Parser(object):
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]