Issue1020517

classification
Title: cStringIO mode attribute not set.
Type: Severity: normal
Components: Library Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: fwierzbicki, sgala
Priority: normal Keywords:

Created on 2004-09-01.15:59:49 by anonymous, last changed 2006-05-15.02:51:13 by fwierzbicki.

Messages
msg927 (view) Author: Nobody/Anonymous (nobody) Date: 2004-09-01.15:59:49
I'm trying to get the Universal Feed Parser to run
under Jython, and I'm running into a problem where
creating a cStringIO object from a string doesn't
change the mode to "rb", and so the code doesn't know
that it can read from the string.

Specifically, the code I'm trying to execute is:
data = gzip.GzipFile(fileobj=_StringIO(data)).read()

and GzipFile contains the code:

if mode is None:
    if hasattr(fileobj, 'mode'): mode = fileobj.mode
    else: mode = 'rb'

which will set the mode of the GzipFile to "w", because
of the line:
transient public String mode = "w";
in the file:
jython/jython/org/python/modules/cStringIO.java

An off-the-top-of-my-head fix would be to change the
constructor taking a string from:
StringIO(String buf) {
    this.buf = new char[buf.length() + 16];
    write(buf);
    seek(0);
}
to:
StringIO(String buf) {
    this.buf = new char[buf.length() + 16];
    this.mode ="r";
    write(buf);
    seek(0);
}


Please feel free to email me at bwinton@latte.ca if you
need any more details, or explanation.
msg928 (view) Author: Santiago Gala (sgala) Date: 2005-08-23.20:20:24
Logged In: YES 
user_id=178886

I'm about to send a patch that fixes this and another error
in the same class.

With this patch cStringIO works with feedparser.py

basically, I commented the mode line, as StringIO does not
have a mode attribute, and also I needed to insert an else
(obvious bug) at the end of seek, as it was always resetting
pos to 0.
msg929 (view) Author: Santiago Gala (sgala) Date: 2005-08-26.09:49:16
Logged In: YES 
user_id=178886

I found and solved this bug. Patch is request number 
1267425. I hope this will make into next release

There are two separate problems:
- jython's cStringIO sets (through reflection) name and mode
attributes for
the created stream. Mode is wrong, always 'w', and name is
used by gzip as a hint that the stream is coming from a
file. Furthermore, cStringIO in cPython does not set these
attributes.
- There is a bug in seek, where the second parameter is
completely ignored (an else missing)


Output from jython:
Jython 2.2a1 on java1.4.2 (JIT: jitc)
Type "copyright", "credits" or "license" for more information.
>>>  import cStringIO
>>> a=cStringIO.StringIO("Hello, world!")
>>> dir(a)
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__repr__',
'__setattr__', '__str__', 'atty', 'close', 'closed',
'flush', 'getvalue', 'isatty', 'mode', 'name', 'read',
'readline', 'readlineNoNl', 'readlines', 'reset', 'seek',
'softspace', 'tell', 'truncate', 'value', 'write',
'writeChar', 'writelines']
>>> a.name
'<cStringIO>'
>>> a.mode
'w'
>>>

Output from python 2.2
>>> import cStringIO
>>> a=cStringIO.StringIO("Hello, world!")
>>> dir(a)
['close', 'flush', 'getvalue', 'isatty', 'read', 'readline',
'readlines', 'reset', 'seek', 'tell', 'truncate']
>>> a.name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: name
>>> a.mode
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: mode

Output from python 2.3
>>> import cStringIO
>>> a=cStringIO.StringIO("Hello, world!")
>>> dir(a)
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__',
'close', 'closed', 'flush', 'getvalue', 'isatty', 'next',
'read', 'readline', 'readlines', 'reset', 'seek', 'tell',
'truncate']
>>> a.name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'cStringIO.StringI' object has no attribute
'name'
>>> a.mode
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'cStringIO.StringI' object has no attribute
'mode'

Output from python 2.4
>>> import cStringIO
>>> a=cStringIO.StringIO("Hello, world!")
>>> dir(a)
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__',
'close', 'closed', 'flush', 'getvalue', 'isatty', 'next',
'read', 'readline', 'readlines', 'reset', 'seek', 'tell',
'truncate']
>>> a.name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'cStringIO.StringI' object has no attribute
'name'
>>> a.mode
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'cStringIO.StringI' object has no attribute
'mode'

msg930 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2006-05-15.02:51:13
Logged In: YES 
user_id=193969

I'm going to accept the patch mentioned here.  See the patch
comments for more.
History
Date User Action Args
2004-09-01 15:59:49anonymouscreate