Some code:
import cStringIO def f(): buffer = cStringIO.StringIO() buffer.write('something') return buffer.getvalue()
The documentation says:
StringIO.close()
: Free the memory buffer. Attempting to do further operations with a closed StringIO object will raise a ValueError.
Do I have to do buffer.close()
, or it will happen automatically when buffer goes out of scope and is garbage collected?
UPDATE:
I did a test:
import StringIO, weakref def handler(ref): print 'Buffer died!' def f(): buffer = StringIO.StringIO() ref = weakref.ref(buffer, handler) buffer.write('something') return buffer.getvalue() print 'before f()' f() print 'after f()'
Result:
vic@wic:~/projects$ python test.py before f() Buffer died! after f() vic@wic:~/projects$
While the buffer should eventually be destroyed/closed by the gc (the timing of this is implementation-dependent), it is safer practice to explicitly close immediately after upload.
This function prints output to stdout and doesn't return any value. If you want to know whether the command ran successfully or not, you have to look into output and decide. Using StringIO, you can capture output and check if it is desired output or not. Save this answer.
StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.
The io module provides Python's main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These are generic categories, and various backing stores can be used for each of them.
Generally it's still better to call close()
or use the with
statement, because there may be some unexpected behaviour in special circumstances. For example, the expat-IncrementalParser
seems to expect a file to be closed, or it won't return the last tidbit of parsed xml until a timeout occurs in some rare circumstances.
But for the with
-statement, which handles the closing for you, you have to use the StringIO
class from the io
-Modules, as stated in the comment of Ivc.
This was a major headache in some legacy sax-parser script we solved by closing the StringIO manually.
The "out-of-scope" close didn't work. It just waited for the timeout-limit.
From the source:
class StringIO: ... def close(self): """Free the memory buffer. """ if not self.closed: self.closed = True del self.buf, self.pos
So StringIO.close
just frees the memory buffer deleting references to StringIO.buf
and StringIO.pos
. But if self
is garbage collected, its attributes will also be garbage collected, having the same effect as StringIO.close
.
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