4Answers
  • 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Do I have to do StringIO.close()?

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$

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.

  • 16
Reply Report
      • 2
    • Except note that StringIO and cStringIO in Py2 don't implement the context manager protocol - so, to use it in a with statement you need to do with contextlib.closing(StringIO()) as buffer:. Py3's io.StringIO, on the other hand, can be used in a with statement directly.
      • 1
    • Ah, I hadn't realised that the io module existed that far back. Thanks for the pointer. However, it remains that the StringIO.StringIO and cStringIO.StringIO modules used in the OP don't. I'm actually slightly surprised they're not marked as deprecated in 2.6/2.7, and that there isn't even the usual note in the 2.7 docs saying "these don't exist anymore in 3.x".
      • 2
    • You're right and quite frankly i didn't really pay attention to the fact, the OP didn't use the io Module! Thanks for pointing that out! ;-)

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.

  • 13
Reply Report

StringIO.close() is merely a convenience for routines that take a file-like and eventually attempt to close them. There is no need to do so yourself.

  • 8
Reply Report

Trending Tags