cStringIO.StringIO has no write method?

L

Laszlo Nagy

This program:

import sys
import traceback
import cStringIO

a = 1.0
b = 0.0
try:
c=a/b
except:
f = cStringIO.StringIO('')
ei = sys.exc_info()
traceback.print_exception(ei[0],ei[1],ei[2],file=f)

raises this exception:

Traceback (most recent call last):
File "C:/Documents and Settings/gandalf/test.py", line 12, in ?
traceback.print_exception(ei[0],ei[1],ei[2],file=f)
File "C:\Python24\Lib\traceback.py", line 124, in print_exception
_print(file, 'Traceback (most recent call last):')
File "C:\Python24\Lib\traceback.py", line 13, in _print
file.write(str+terminator)
AttributeError: 'cStringIO.StringI' object has no attribute 'write'

What is 'cStringIO.StringI' anyway? Shouldn't it be 'cStringIO.StringIO'?

Laszlo
 
P

Peter Otten

Laszlo said:
This program:

import sys
import traceback
import cStringIO

a = 1.0
b = 0.0
try:
c=a/b
except:
f = cStringIO.StringIO('')
ei = sys.exc_info()
traceback.print_exception(ei[0],ei[1],ei[2],file=f)

raises this exception:

Traceback (most recent call last):
File "C:/Documents and Settings/gandalf/test.py", line 12, in ?
traceback.print_exception(ei[0],ei[1],ei[2],file=f)
File "C:\Python24\Lib\traceback.py", line 124, in print_exception
_print(file, 'Traceback (most recent call last):')
File "C:\Python24\Lib\traceback.py", line 13, in _print
file.write(str+terminator)
AttributeError: 'cStringIO.StringI' object has no attribute 'write'

What is 'cStringIO.StringI' anyway? Shouldn't it be 'cStringIO.StringIO'?

See http://docs.python.org/lib/module-cStringIO.html:

"""
Another difference from the StringIO module is that calling StringIO() with
a string parameter creates a read-only object. Unlike an object created
without a string parameter, it does not have write methods. These objects
are not generally visible. They turn up in tracebacks as StringI and
StringO.
"""

Peter
 
A

Alexis Roda

En/na Laszlo Nagy ha escrit:
This program:

import sys
import traceback
import cStringIO

a = 1.0
b = 0.0
try:
c=a/b
except: f = cStringIO.StringIO('')
ei = sys.exc_info()
traceback.print_exception(ei[0],ei[1],ei[2],file=f)

from cStringIO documentation:


Another difference from the StringIO module is that calling
StringIO() with a string parameter creates a read-only object.
Unlike an object created without a string parameter, it does
not have write methods.



HTH
 
L

Laszlo Nagy

from cStringIO documentation:


Another difference from the StringIO module is that calling
StringIO() with a string parameter creates a read-only object.
Unlike an object created without a string parameter, it does
not have write methods.
Looks like I did not read the manual carefully, overlooked/misunderstood
this and posted too early.
I'm sorry!


Laszlo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top