Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In python 3.4 multipart file upload breaks due to change in cgi.FieldStorage #567

Closed
acv opened this issue Jan 11, 2014 · 2 comments
Closed

Comments

@acv
Copy link

acv commented Jan 11, 2014

In Python 3.4, the cgi.FileStorage class now has a __del__ method that close the file objects.

BaseRequest.POST() uses that class to parse multipart requests and then grabs the files directly into FileUpload instances. When the function exits, the variable data is garbage collected and the __del__ method is called, closing the file handle held in the returned post dictionary.

Unit test test_environ.test_multipart() catches this because it tries to access the file object and read()s it which fails with this exception:

ValueError: I/O operation on closed file.

This doesn't happen in python 3.3.3.

I'm not sure the best way to go around fixing this. Perhaps stashing the cgi.FileStorage instance in the return post dictionary? That would fix the dereference but it's ugly:

diff --git a/bottle.py b/bottle.py
index 9ab7b3d..edae3b5 100644
--- a/bottle.py
+++ b/bottle.py
@@ -1221,6 +1221,7 @@ class BaseRequest(object):
             if item.filename:
                 post[item.name] = FileUpload(item.file, item.name,
                                              item.filename, item.headers)
+                post.__FieldStorage = data
             else:
                 post[item.name] = item.value
         return post
@defnull
Copy link
Member

defnull commented Jan 12, 2014

http://bugs.python.org/issue18394 If they do not fix this, we have to. Your workaround looks ok.

@defnull
Copy link
Member

defnull commented Feb 4, 2014

Looks like a wont-fix, so we have to.

defnull added a commit that referenced this issue Apr 29, 2014
jklp added a commit to bluecottagesoftware/watson-http that referenced this issue Jul 22, 2015
Due to a bug in python 3.4 and the FieldStorage object (bottlepy/bottle#567 / http://bugs.python.org/issue18394), looks like watson.http.messages.Request is unwrapping FieldStorage and returning it into a “files” tuple.

The unwrapping is causing the del to be called and closing the file - which means uploaded files can't be read.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants