[ python-Bugs-1730114 ] cStringIO no longer accepts array.array objects

SourceForge.net noreply at sourceforge.net
Sun Jun 3 07:49:22 CEST 2007


Bugs item #1730114, was opened at 2007-06-02 20:01
Message generated for change (Comment added) made by reedobrien
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1730114&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: reedobrien (reedobrien)
Assigned to: Nobody/Anonymous (nobody)
Summary: cStringIO no longer accepts array.array objects

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: reedobrien (reedobrien)
Date: 2007-06-03 00:49

Message:
Logged In: YES 
user_id=995094
Originator: YES

It appears this is the change that broke it.
http://svn.python.org/view/python/branches/release25-maint/Modules/cStringIO.c?rev=52302&r1=51333&r2=52302

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",
				s->ob_type->tp_name); 
			return NULL;
		}
	}
  else
	{
		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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1730114&group_id=5470


More information about the Python-bugs-list mailing list