Skip to content Skip to sidebar Skip to footer

Python's Stringio Doesn't Do Well With `with` Statements

I need to stub tempfile and StringIO seemed perfect. Only that all this fails in an omission: In [1]: from StringIO import StringIO In [2]: with StringIO('foo') as f: f.read() --&

Solution 1:

The StringIO module predates the with statement. Since StringIO has been removed in Python 3 anyways, you can just use its replacement, io.BytesIO:

>>>import io>>>with io.BytesIO(b"foo") as f: f.read()
b'foo'

Solution 2:

this monkeypatch works for me in python2. call monkeypatch in your initialization routine.

import logging
from StringIO import StringIO
logging.basicConfig(level=logging.DEBUG if__debug__else logging.INFO)

defdebug(*args):
    logging.debug('args: %s', args)
    return args[0]

defmonkeypatch():
    '''
    allow StringIO to use `with` statement
    '''
    StringIO.__exit__ = debug
    StringIO.__enter__ = debug

if __name__ == '__main__':
    monkeypatch()
    with StringIO("this is a test") as infile:
        print infile.read()

test run:

jcomeau@aspire:~/stackoverflow/12028637$ python test.py 
DEBUG:root:args: (<StringIO.StringIO instance at 0xf73e76ec>,)
this is a test
DEBUG:root:args: (<StringIO.StringIO instance at 0xf73e76ec>, None, None, None)
jcomeau@aspire:~/stackoverflow/12028637$

Post a Comment for "Python's Stringio Doesn't Do Well With `with` Statements"