Merge "Port parse_mime_headers() to Python 3"
This commit is contained in:
@@ -3490,11 +3490,21 @@ def parse_mime_headers(doc_file):
|
|||||||
headers = []
|
headers = []
|
||||||
while True:
|
while True:
|
||||||
line = doc_file.readline()
|
line = doc_file.readline()
|
||||||
|
done = line in (b'\r\n', b'\n', b'')
|
||||||
|
if six.PY3:
|
||||||
|
try:
|
||||||
|
line = line.decode('utf-8')
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
line = line.decode('latin1')
|
||||||
headers.append(line)
|
headers.append(line)
|
||||||
if line in (b'\r\n', b'\n', b''):
|
if done:
|
||||||
break
|
break
|
||||||
header_string = b''.join(headers)
|
if six.PY3:
|
||||||
return HeaderKeyDict(email.parser.Parser().parsestr(header_string))
|
header_string = ''.join(headers)
|
||||||
|
else:
|
||||||
|
header_string = b''.join(headers)
|
||||||
|
headers = email.parser.Parser().parsestr(header_string)
|
||||||
|
return HeaderKeyDict(headers)
|
||||||
|
|
||||||
|
|
||||||
def mime_to_document_iters(input_file, boundary, read_chunk_size=4096):
|
def mime_to_document_iters(input_file, boundary, read_chunk_size=4096):
|
||||||
|
|||||||
@@ -5148,6 +5148,10 @@ Utf-8: \xd0\xba\xd0\xbe\xd0\xbd\xd1\x82\xd0\xb5\xd0\xb9\xd0\xbd\xd0\xb5\xd1\x80
|
|||||||
This is the body
|
This is the body
|
||||||
""")
|
""")
|
||||||
headers = utils.parse_mime_headers(doc_file)
|
headers = utils.parse_mime_headers(doc_file)
|
||||||
|
utf8 = u'\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440'
|
||||||
|
if six.PY2:
|
||||||
|
utf8 = utf8.encode('utf-8')
|
||||||
|
|
||||||
expected_headers = {
|
expected_headers = {
|
||||||
'Content-Disposition': 'form-data; name="file_size"',
|
'Content-Disposition': 'form-data; name="file_size"',
|
||||||
'Foo': "Bar",
|
'Foo': "Bar",
|
||||||
@@ -5157,8 +5161,7 @@ This is the body
|
|||||||
'Connexion': "=?iso8859-1?q?r=E9initialis=E9e_par_l=27homologue?=",
|
'Connexion': "=?iso8859-1?q?r=E9initialis=E9e_par_l=27homologue?=",
|
||||||
'Status': "=?utf-8?b?5byA5aeL6YCa6L+H5a+56LGh5aSN5Yi2?=",
|
'Status': "=?utf-8?b?5byA5aeL6YCa6L+H5a+56LGh5aSN5Yi2?=",
|
||||||
'Latin-1': "Resincronizaci\xf3n realizada con \xe9xito",
|
'Latin-1': "Resincronizaci\xf3n realizada con \xe9xito",
|
||||||
'Utf-8': ("\xd0\xba\xd0\xbe\xd0\xbd\xd1\x82\xd0\xb5\xd0\xb9\xd0"
|
'Utf-8': utf8,
|
||||||
"\xbd\xd0\xb5\xd1\x80")
|
|
||||||
}
|
}
|
||||||
self.assertEqual(expected_headers, headers)
|
self.assertEqual(expected_headers, headers)
|
||||||
self.assertEqual(b"This is the body\n", doc_file.read())
|
self.assertEqual(b"This is the body\n", doc_file.read())
|
||||||
|
|||||||
Reference in New Issue
Block a user