seisen/urllib2_file — Issue #5: Can't use BytesIO
Author: lkraider
Created: 2011-11-01 18:40:30+00:00
State: open

Python >= 2.6 includes io.BytesIO (among others), that define fileno() but raise io.UnsupportedOperation if the implementation does not use a file descriptor, which is the case for BytesIO.

Also, the only way to get the length of the data seems to be through the getvalue() method, so a possible approach is:

```diff
diff --git a/urllib2_file.py b/urllib2_file.py
index f6fdf87..4684db7 100644
--- a/urllib2_file.py
+++ b/urllib2_file.py
@@ -71,6 +71,7 @@ eg:
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 = k
+ file_size = len(fd.getvalue())
fd.seek(0)
elif hasattr(fd, 'len'):
# StringIO
```