[Python-bugs-list] [Bug #113960] Problems with reverse() in the array module

noreply@sourceforge.net noreply@sourceforge.net
Sat, 16 Sep 2000 15:32:34 -0700


Bug #113960, was updated on 2000-Sep-09 13:20
Here is a current snapshot of the bug.

Project: Python
Category: Modules
Status: Closed
Resolution: Fixed
Bug Group: None
Priority: 8
Summary: Problems with reverse() in the array module

Details: When I was using Python-2.0b1, reverse() for arrays was acting funny for me:

>>> import array
>>> bob = array.array('c', 'a string')
>>> bob.reverse()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: <array>.reverse requires exactly 0 arguments

But, I didn't pass it any arguments :-).  When I looked at the code I came up with the following fix:

*** arraymodule.c.orig	Fri Sep  1 19:29:26 2000
--- arraymodule.c	Sat Sep  9 16:04:16 2000
***************
*** 935,945 ****
  	register char *p, *q;
  	char tmp[sizeof(double)]; /* Assume that's the max item size */
  
! 	if (args != NULL) {
! 		PyErr_SetString(PyExc_TypeError,
! 		     "<array>.reverse requires exactly 0 arguments");
! 		return NULL;
! 	}
  
  	if (self->ob_size > 1) {
  		for (p = self->ob_item,
--- 935,942 ----
  	register char *p, *q;
  	char tmp[sizeof(double)]; /* Assume that's the max item size */
  
! 	if (!PyArg_ParseTuple(args, ":reverse"))
! 	    return NULL;
  
  	if (self->ob_size > 1) {
  		for (p = self->ob_item,

Reverse seems to work properly with this:

>>> import array
>>> bob = array.array('c', 'a string')
>>> bob.reverse()
>>> print bob
array('c', 'gnirts a')

and the error message for passing argument seems reasonable:

>>> bob.reverse('spam')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: reverse requires exactly 0 arguments; 1 given

I'm really not an expert on any of this, but I hope the report is useful. Thanks for listening.

Brad

Follow-Ups:

Date: 2000-Sep-09 18:08
By: tim_one

Comment:
Thanks!  Boosted the priority and assigned to me:  this is plain embarrassing -- the code couldn't possibly work as intended.  I'll fix it and add a regression test to the std test suite so it never happens again.
-------------------------------------------------------

Date: 2000-Sep-16 15:32
By: tim_one

Comment:
Fixed and closed; checkin comment:

arraymodule:  Fix SF bug 113960.
    reverse() didn't work at all due to bad arg check.
    Fixed that.
    Added Brad Chapman to ACKS file, as the proud new owner of two implicitly copyrighted lines of Python source code <wink>.
    Repaired buffer_info's total lack of arg-checking.
    Replaced memmove by memcpy in reverse() guts, as memmove is often slower and the memory areas are guaranteed disjoint.
    Replaced poke-and-hope unchecked decl of tmp buffer size by assert-checked larger tmp buffer.
    Got rid of inconsistent spaces before open paren in docstrings.
    Added reverse() sanity tests to test_array.py.

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

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=113960&group_id=5470