Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:27:10+00:00 2026-05-31T07:27:10+00:00

Some code: import cStringIO def f(): buffer = cStringIO.StringIO() buffer.write(‘something’) return buffer.getvalue() The documentation

  • 0

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$
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:27:11+00:00Added an answer on May 31, 2026 at 7:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen some code such as: out.println(print something); I tried import java.lang.System; but it's
I have seen in some source code (by other developers) something like this: #import
The following sample code: import token, tokenize, StringIO def generate_tokens(src): rawstr = StringIO.StringIO(unicode(src)) tokens
here is some code: from Tkinter import * class Main(object): def __init__(self): self.console =
I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree
Given code like this in somemod.py : try: Import cStringIO as StringIO except ImportError:
I am running some import code asynchronously from a simple WinForms app using a
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
I have some code which I think should compile, but doesn't: import java.io.InputStream; import
For some reason, I can't get pyglet to draw sprites. Here's my code: import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.