Permalink
Browse files

Use BytesIO to prevent encoding issues

  • Loading branch information...
lkraider committed Nov 1, 2011
1 parent 787dafe commit 1429a81776ec3a8c769f726a989fc11c46424fce
Showing with 10 additions and 5 deletions.
  1. +3 −3 Pyrrot2.py
  2. +7 −2 urllib2_file.py
View
@@ -17,7 +17,7 @@
import logging
import cPickle
-import StringIO
+import io
import hashlib
import os
import random
@@ -73,8 +73,8 @@ def upload(hash, filename):
for ext in SUBS_EXTS:
file = os.path.splitext(filename)[0] + ext
if os.path.isfile(file):
- fd_file = open(file)
- fd = StringIO.StringIO()
+ fd_file = open(file, 'rb')
+ fd = io.BytesIO()
fd.name = hash + ".srt"
fd.write(fd_file.read())
data = { 'hash': hash, 'file': fd }
View
@@ -71,6 +71,7 @@
import httplib
import mimetools
import mimetypes
+import io
import os
import os.path
import socket
@@ -104,8 +105,12 @@ def send_data(v_vars, v_files, boundary, sock=None):
if hasattr(fd, 'fileno'):
# a File
- name = fd.name.split(os.path.sep)[-1]
- file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
+ try:
+ name = fd.name.split(os.path.sep)[-1]
+ file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
+ except io.UnsupportedOperation:
+ name = fd.name
+ file_size = len(fd.getvalue())
fd.seek(0)
elif hasattr(fd, 'len'):
# StringIO

0 comments on commit 1429a81

Please sign in to comment.