So let's say that my class looks like the below, with all relevant imports already done:
class LargeRequest(server.Request):
memory_limit = 1024*1024*25
temp_type = tempfile.TemporaryFile
def parse_multipart(self, fp, pdict):
while loop_condition:
if self.temp_type.__name__ == 'SpooledTemporaryFile':
data = self.temp_type(max_size=self.memory_limit)
else:
data = self.temp_type()
...
data.write('stuff')
When I run this I get an error:
File "/home/user/workspace/twisted/server.py", line 226, in parse_multipart
data = self.temp_type()
File "/usr/lib/python2.7/tempfile.py", line 484, in TemporaryFile
if 'b' in mode:
exceptions.TypeError: argument of type 'instance' is not iterable
Which refers to this line in tempfile.py.
Also, this error does not occur when I simply do:
data = tempfile.TemporaryFile()
But I would like a bit more flexibility. What am I doing wrong?
temp_type become instance method. self.temp_type() call become TemporaryFile(self).
Try following:
temp_type = staticmethod(tempfile.TemporaryFile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With