Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when assigning tempfile.TemporaryFile to a variable and executing

Tags:

python

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?

like image 888
Sammitch Avatar asked Feb 15 '26 07:02

Sammitch


1 Answers

temp_type become instance method. self.temp_type() call become TemporaryFile(self).

Try following:

temp_type = staticmethod(tempfile.TemporaryFile)
like image 102
falsetru Avatar answered Feb 16 '26 21:02

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!