Title: cStringIO no longer accepts array.array objects
Type: Stage:
Components: Extension Modules Versions: Python 2.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, python-dev, reedobrien, rhettinger
Priority: normal Keywords:

Created on 2007-06-03 01:01 by reedobrien, last changed 2011-10-21 20:26 by python-dev. This issue is now closed.

File name Uploaded Description Edit
cStringIO.c reedobrien, 2007-06-03 05:49
Messages (6)
msg32188 - (view) Author: reedobrien (reedobrien) Date: 2007-06-03 01:01
It worked fine in 2.5 and 2.4.4

Python 2.5.1 (r251:54863, Jun  2 2007, 19:09:15) 
Type "copyright", "credits" or "license" for more information.

In [1]: from cStringIO import StringIO

In [2]: from array import array

In [3]: a = array('B', [0,1,2])

In [4]: StringIO(a)
<type 'exceptions.TypeError'>             Traceback (most recent call last)

/Users/reedobrien/<ipython console> in <module>()

<type 'exceptions.TypeError'>: expected a character buffer object

Recompiling with the 2.5 cStringIO.c it works.

Python 2.5.1 (r251:54863, Jun  2 2007, 20:06:12) 
Type "copyright", "credits" or "license" for more information.

In [1]: from array import array

In [2]: from cStringIO import StringIO

In [3]: a = array('B', [0,1,2])

In [4]: StringIO(a)
Out[4]: <cStringIO.StringI object at 0xe8c0>

I blame Visual Studio. 
I don't know enough c to fix it.
msg32189 - (view) Author: reedobrien (reedobrien) Date: 2007-06-03 05:49
It appears this is the change that broke it.

This is the log entry from that changeset:
Bug #1548891: The cStringIO.StringIO() constructor now encodes unicode
arguments with the system default encoding just like the write()
method does, instead of converting it to a raw buffer.
 (backport from rev. 52301)

Perhaps the cPickle module should be used instead...

BUT at first glance the following seems to make both work:
  if (PyUnicode_Check(s)) 
		if (PyObject_AsCharBuffer(s, (const char **)&buf, &size) != 0)
			PyErr_Format(PyExc_TypeError, "expected character buffer, %.200s found",
			return NULL;
		if (PyObject_AsReadBuffer(s, (const void **)&buf, &size) != 0)
		return NULL;

But the more I think about it the more I think cPickle is more appropriate for this purpose.  In that case I should make a blurb for the docs about not storing arbitrary data in cStringIO. 

Either way I am attaching the cStringIO.c file that worked for me...
File Added: cStringIO.c
msg32190 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-06-03 06:40
Georgbot, I believe this was your checkin.
msg32191 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-06-06 18:12
I'll look at it.
msg32192 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-08-08 13:04
Fixed by reverting the offending change. Added a note in the docs. Committed revisions 56830, 56831 (2.5).
msg146130 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-10-21 20:26
New changeset c91661e0d714 by Antoine Pitrou in branch '2.7':
Add test for fix of issue #1730114.
Date User Action Args
2011-10-21 20:26:10python-devsetnosy: + python-dev
messages: + msg146130
2007-06-03 01:01:32reedobriencreate