Mercurial > p > roundup > code
comparison roundup/cgi/client.py @ 5775:17e110426ad7
issue2551046: Attempts to attach file or create large message fail
under python2
This patch fixes a bug introduced for working around an issue in
python3. There were a few ways to fix this, but I think this fix:
1) follows the spirit of the original patch
2) follows python programming expectations
I am still having an issue trying to get both code paths executed from
the test suite. The super code path is not tested, but I did validate
that path from the browser and it works producing valid uploaded files
in both python 2 and 3.
Thanks to Ezio Melotti and Joseph Myers for their help in guiding me
to come up with this solution.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 08 Jun 2019 21:10:39 -0400 |
| parents | b67636bc87d0 |
| children | 0e6d45413e88 |
comparison
equal
deleted
inserted
replaced
| 5774:765f8c0e99ef | 5775:17e110426ad7 |
|---|---|
| 228 self.session_db.commit() | 228 self.session_db.commit() |
| 229 | 229 |
| 230 if set_cookie: | 230 if set_cookie: |
| 231 self.client.add_cookie(self.cookie_name, self._sid, expire=expire) | 231 self.client.add_cookie(self.cookie_name, self._sid, expire=expire) |
| 232 | 232 |
| 233 class BinaryFieldStorage(cgi.FieldStorage): | 233 # import from object as well so it's a new style object and I can use super() |
| 234 class BinaryFieldStorage(cgi.FieldStorage, object): | |
| 234 '''This class works around the bug https://bugs.python.org/issue27777. | 235 '''This class works around the bug https://bugs.python.org/issue27777. |
| 235 | 236 |
| 236 cgi.FieldStorage must save all data as binary/bytes. This is | 237 cgi.FieldStorage must save all data as binary/bytes. This is |
| 237 needed for handling json and xml data blobs under python | 238 needed for handling json and xml data blobs under python |
| 238 3. Under python 2, str and binary are interchangable, not so | 239 3. Under python 2, str and binary are interchangable, not so |
| 241 def make_file(self, mode=None): | 242 def make_file(self, mode=None): |
| 242 ''' work around https://bugs.python.org/issue27777 ''' | 243 ''' work around https://bugs.python.org/issue27777 ''' |
| 243 import tempfile | 244 import tempfile |
| 244 if self.length >= 0: | 245 if self.length >= 0: |
| 245 return tempfile.TemporaryFile("wb+") | 246 return tempfile.TemporaryFile("wb+") |
| 246 return super().make_file() | 247 return super(BinaryFieldStorage, self).make_file() |
| 247 | 248 |
| 248 class Client: | 249 class Client: |
| 249 """Instantiate to handle one CGI request. | 250 """Instantiate to handle one CGI request. |
| 250 | 251 |
| 251 See inner_main for request processing. | 252 See inner_main for request processing. |
