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$