Commit 87fa27fc authored by Leo Pound Singer's avatar Leo Pound Singer

Fix Python 3 unicode exception in file uploads

This fixes the crash when attempting to create a new file in Python 3:

    $ gracedb Test gstlal HighMass coinc.xml
    Traceback (most recent call last):
      File "/Users/lpsinger/local/bin/gracedb", line 564, in <module>
        code = main()
      File "/Users/lpsinger/local/bin/gracedb", line 501, in main
        offline=options.offline, labels=options.labels)
      File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 619, in createEvent
        return self.post(uri, fields, files=files)
      File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 381, in post
        return self.post_or_put("POST", *args, **kwargs)
      File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 399, in post_or_put
        content_type, body = encode_multipart_formdata(body, files)
      File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 1285, in encode_multipart_formdata
        body = CRLF.join(L)
    TypeError: sequence item 20: expected str instance, bytes found
parent 8cd13224
......@@ -1262,28 +1262,36 @@ def encode_multipart_formdata(fields, files):
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = b'\r\n'
L = []
for (key, value) in fields:
if value is None: continue
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
# str(value) in case it is unicode
L.append(str(value))
L.append(b'--' + BOUNDARY)
L.append(b'Content-Disposition: form-data; name="%s"' % key.encode())
L.append(b'')
# encode in case it is unicode
if isinstance(value, six.binary_type):
L.append(value)
elif isinstance(value, six.text_type):
L.append(value.encode())
else:
L.append(str(value).encode())
for (key, filename, value) in files:
if value is None: continue
L.append('--' + BOUNDARY)
L.append(b'--' + BOUNDARY)
# str(filename) in case it is unicode
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, str(filename)))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
L.append(b'Content-Disposition: form-data; name="%s"; filename="%s"' % (key.encode(), filename.encode()))
L.append(b'Content-Type: %s' % get_content_type(filename).encode())
L.append(b'')
if isinstance(value, six.text_type):
L.append(value.encode())
else:
L.append(value)
L.append(b'--' + BOUNDARY + b'--')
L.append(b'')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
content_type = b'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def get_content_type(filename):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment