Python 3 fixes

This commit is contained in:
Andrew Godwin 2016-02-05 16:47:11 -08:00
parent 13766a3027
commit fa58375a51
3 changed files with 25 additions and 7 deletions

View File

@ -71,7 +71,7 @@ class DatabaseChannelLayer(object):
# Keep making channel names till one isn't present. # Keep making channel names till one isn't present.
while True: while True:
random_string = "".join(random.choice(string.ascii_letters) for i in range(8)) random_string = "".join(random.choice(string.ascii_letters) for i in range(8))
new_name = pattern.replace(b"?", random_string) new_name = pattern.replace("?", random_string)
if not self.channel_model.objects.filter(channel=new_name).exists(): if not self.channel_model.objects.filter(channel=new_name).exists():
return new_name return new_name

View File

@ -58,12 +58,14 @@ class AsgiRequest(http.HttpRequest):
corrected_name = "CONTENT_TYPE" corrected_name = "CONTENT_TYPE"
else: else:
corrected_name = 'HTTP_%s' % name.upper().replace("-", "_") corrected_name = 'HTTP_%s' % name.upper().replace("-", "_")
self.META[corrected_name] = value # TODO: Look at request encoding for unicode decode
self.META[corrected_name] = value.decode("latin1")
# Pull out content length info # Pull out content length info
if self.META.get('CONTENT_LENGTH', None): if self.META.get('CONTENT_LENGTH', None):
try: try:
self._content_length = int(self.META['CONTENT_LENGTH']) self._content_length = int(self.META['CONTENT_LENGTH'])
except (ValueError, TypeError): except (ValueError, TypeError) as e:
print (self.META)
pass pass
# Body handling # Body handling
self._body = message.get("body", b"") self._body = message.get("body", b"")
@ -164,9 +166,25 @@ class AsgiHandler(base.BaseHandler):
# Collect cookies into headers. # Collect cookies into headers.
# Note that we have to preserve header case as there are some non-RFC # Note that we have to preserve header case as there are some non-RFC
# compliant clients that want things like Content-Type correct. Ugh. # compliant clients that want things like Content-Type correct. Ugh.
response_headers = [(str(k), str(v)) for k, v in response.items()] response_headers = []
for header, value in response.items():
if isinstance(header, six.binary_type):
header = header.decode("latin1")
if isinstance(value, six.text_type):
value = value.encode("latin1")
response_headers.append(
(
six.text_type(header),
six.binary_type(value),
)
)
for c in response.cookies.values(): for c in response.cookies.values():
response_headers.append((str('Set-Cookie'), str(c.output(header='')))) response_headers.append(
(
'Set-Cookie',
six.binary_type(c.output(header='')),
)
)
# Make initial response message # Make initial response message
message = { message = {
"status": response.status_code, "status": response.status_code,

View File

@ -159,7 +159,7 @@ class RequestTests(SimpleTestCase):
"body_channel": "test-input", "body_channel": "test-input",
"headers": { "headers": {
"content-type": b"multipart/form-data; boundary=BOUNDARY", "content-type": b"multipart/form-data; boundary=BOUNDARY",
"content-length": six.binary_type(len(body)), "content-length": six.text_type(len(body)).encode("ascii"),
}, },
}, "test") }, "test")
self.channel_layer.send("test-input", { self.channel_layer.send("test-input", {
@ -175,4 +175,4 @@ class RequestTests(SimpleTestCase):
self.assertTrue(request.META["CONTENT_TYPE"].startswith("multipart/form-data")) self.assertTrue(request.META["CONTENT_TYPE"].startswith("multipart/form-data"))
self.assertFalse(request._post_parse_error) self.assertFalse(request._post_parse_error)
self.assertEqual(request.POST["title"], "My First Book") self.assertEqual(request.POST["title"], "My First Book")
self.assertEqual(request.FILES["pdf"].read(), "FAKEPDFBYTESGOHERE") self.assertEqual(request.FILES["pdf"].read(), b"FAKEPDFBYTESGOHERE")