[Patches] array.{index,count,remove}

Peter Schneider-Kamp peter@schneider-kamp.de
Sun, 14 May 2000 13:23:40 +0200


This is a multi-part message in MIME format.
--------------1F8F8C92A9D72DEE76E9CB8C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Problem:
4.17. Implement array.array.{index,remove,count}
[tim_one@email.msn.com] array.array objects currently
don't support list's index, remove or count methods,
but could <wink -- but once index is written, the other
two could build on it>. 
Later: array.array doesn't support the new(er) list.pop()
either, although that one is still (1.5.2a1) experimental. 
Edit this entry / Log info / Last changed on Wed Oct 7
18:43:34 1998 by Tim Peters 

Solution:
adapt listindex,listremove and listcount to arrays
I chose rather to adapt all three than to build on index

patch attached as plaintext context diff
--
I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter@schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de
--------------1F8F8C92A9D72DEE76E9CB8C
Content-Type: text/plain; charset=us-ascii;
 name="array.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="array.patch"

diff -c --recursive python/dist/src/Lib/test/test_array.py python-mod/dist/src/Lib/test/test_array.py
*** python/dist/src/Lib/test/test_array.py	Thu Jul 16 17:31:43 1998
--- python-mod/dist/src/Lib/test/test_array.py	Sun May 14 12:37:07 2000
***************
*** 67,72 ****
--- 67,79 ----
              a[1:-1] = a
              if a != array.array(type, "aabcdee"):
                  raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
+             if a.index("e") != 5:
+             	raise TestFailed, "array(%s) index-test" % `type`
+             if a.count("a") != 2:
+             	raise TestFailed, "array(%s) count-test" % `type`
+             a.remove("e")
+             if a != array.array(type, "aabcde"):
+             	raise TestFailed, "array(%s) remove-test" % `type`
          else:
              a = array.array(type, [1, 2, 3, 4, 5])
              a[:-1] = a
***************
*** 80,85 ****
--- 87,99 ----
              a[1:-1] = a
              if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
                  raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
+             if a.index(5) != 5:
+             	raise TestFailed, "array(%s) index-test" % `type`
+             if a.count(1) != 2:
+             	raise TestFailed, "array(%s) count-test" % `type`
+             a.remove(5)
+             if a != array.array(type, [1, 1, 2, 3, 4, 5]):
+             	raise TestFailed, "array(%s) remove-test" % `type`
  
  
  main()
diff -c --recursive python/dist/src/Modules/arraymodule.c python-mod/dist/src/Modules/arraymodule.c
*** python/dist/src/Modules/arraymodule.c	Thu May  4 01:44:31 2000
--- python-mod/dist/src/Modules/arraymodule.c	Sun May 14 12:23:36 2000
***************
*** 699,704 ****
--- 699,784 ----
  }
  
  static PyObject *
+ array_count(self, args)
+ 	arrayobject *self;
+ 	PyObject *args;
+ {
+ 	int count = 0;
+ 	int i;
+ 	PyObject *v;
+ 
+ 	if (!PyArg_Parse(args, "O", &v))
+ 		return NULL;
+ 	for (i = 0; i < self->ob_size; i++) {
+ 		if (PyObject_Compare(getarrayitem((PyObject *)self,i), v) == 0)
+ 			count++;
+ 		if (PyErr_Occurred())
+ 			return NULL;
+ 	}
+ 	return PyInt_FromLong((long)count);
+ }
+ 
+ static char count_doc [] =
+ "count (x)\n\
+ \n\
+ Return number of occurences of x in the array.";
+ 
+ static PyObject *
+ array_index(self, args)
+ 	arrayobject *self;
+         PyObject *args;
+ {
+ 	int i;
+ 	PyObject *v;
+ 
+ 	if (!PyArg_Parse(args, "O", &v))
+ 		return NULL;
+ 	for (i = 0; i < self->ob_size; i++) {
+ 		if (PyObject_Compare(getarrayitem((PyObject *)self,i), v) == 0)
+ 			return PyInt_FromLong((long)i);
+ 		if (PyErr_Occurred())
+ 			return NULL;
+ 	}
+ 	PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
+ 	return NULL;
+ }
+ 
+ static char index_doc [] =
+ "index (x)\n\
+ \n\
+ Return index of first occurence of x in the array.";
+ 
+ static PyObject *
+ array_remove(self, args)
+ 	arrayobject *self;
+ 	PyObject *args;
+ {
+ 	int i;
+ 	PyObject *v;
+ 
+ 	if (!PyArg_Parse(args, "O", &v))
+ 		return NULL;
+ 	for (i = 0; i < self->ob_size; i++) {
+ 		if (PyObject_Compare(getarrayitem((PyObject *)self,i), v) == 0) {
+ 			if (array_ass_slice(self, i, i+1,
+ 					   (PyObject *)NULL) != 0)
+ 				return NULL;
+ 			Py_INCREF(Py_None);
+ 			return Py_None;
+ 		}
+ 		if (PyErr_Occurred())
+ 			return NULL;
+ 	}
+ 	PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
+ 	return NULL;
+ }
+ 
+ static char remove_doc [] =
+ "remove (x)\n\
+ \n\
+ Remove the first occurence of x in the array.";
+ 
+ static PyObject *
  array_insert(self, args)
  	arrayobject *self;
  	PyObject *args;
***************
*** 845,924 ****
  \n\
  Reverse the order of the items in the array.";
  
- /* The following routines were adapted from listobject.c but not converted.
-    To make them work you will have to work! */
- 
- #if 0
- static PyObject *
- array_index(self, args)
- 	arrayobject *self;
- 	PyObject *args;
- {
- 	int i;
- 	
- 	if (args == NULL) {
- 		PyErr_BadArgument();
- 		return NULL;
- 	}
- 	for (i = 0; i < self->ob_size; i++) {
- 		if (PyObject_Compare(self->ob_item[i], args) == 0)
- 			return PyInt_FromLong((long)i);
- 		/* XXX PyErr_Occurred */
- 	}
- 	PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
- 	return NULL;
- }
- #endif
- 
- #if 0
- static PyObject *
- array_count(self, args)
- 	arrayobject *self;
- 	PyObject *args;
- {
- 	int count = 0;
- 	int i;
- 	
- 	if (args == NULL) {
- 		PyErr_BadArgument();
- 		return NULL;
- 	}
- 	for (i = 0; i < self->ob_size; i++) {
- 		if (PyObject_Compare(self->ob_item[i], args) == 0)
- 			count++;
- 		/* XXX PyErr_Occurred */
- 	}
- 	return PyInt_FromLong((long)count);
- }
- #endif
- 
- #if 0
- static PyObject *
- array_remove(self, args)
- 	arrayobject *self;
- 	PyObject *args;
- {
- 	int i;
- 	
- 	if (args == NULL) {
- 		PyErr_BadArgument();
- 		return NULL;
- 	}
- 	for (i = 0; i < self->ob_size; i++) {
- 		if (PyObject_Compare(self->ob_item[i], args) == 0) {
- 			if (array_ass_slice(self, i, i+1,
- 					    (PyObject *)NULL) != 0)
- 				return NULL;
- 			Py_INCREF(Py_None);
- 			return Py_None;
- 		}
- 		/* XXX PyErr_Occurred */
- 	}
- 	PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
- 	return NULL;
- }
- #endif
- 
  static PyObject *
  array_fromfile(self, args)
  	arrayobject *self;
--- 925,930 ----
***************
*** 1142,1157 ****
  	{"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
  	{"byteswap",	(PyCFunction)array_byteswap, METH_VARARGS,
           byteswap_doc},
! /*	{"count",	(method)array_count},*/
  	{"fromfile",	(PyCFunction)array_fromfile, 0, fromfile_doc},
  	{"fromlist",	(PyCFunction)array_fromlist, 0, fromlist_doc},
  	{"fromstring",	(PyCFunction)array_fromstring, 0, fromstring_doc},
! /*	{"index",	(method)array_index},*/
  	{"insert",	(PyCFunction)array_insert, 0, insert_doc},
  	{"read",	(PyCFunction)array_fromfile, 0, fromfile_doc},
! /*	{"remove",	(method)array_remove},*/
  	{"reverse",	(PyCFunction)array_reverse, 0, reverse_doc},
! /*	{"sort",	(method)array_sort},*/
  	{"tofile",	(PyCFunction)array_tofile, 0, tofile_doc},
  	{"tolist",	(PyCFunction)array_tolist, 0, tolist_doc},
  	{"tostring",	(PyCFunction)array_tostring, 0, tostring_doc},
--- 1148,1163 ----
  	{"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
  	{"byteswap",	(PyCFunction)array_byteswap, METH_VARARGS,
           byteswap_doc},
! 	{"count",	(PyCFunction)array_count, 0, count_doc},
  	{"fromfile",	(PyCFunction)array_fromfile, 0, fromfile_doc},
  	{"fromlist",	(PyCFunction)array_fromlist, 0, fromlist_doc},
  	{"fromstring",	(PyCFunction)array_fromstring, 0, fromstring_doc},
! 	{"index",	(PyCFunction)array_index, 0, index_doc},
  	{"insert",	(PyCFunction)array_insert, 0, insert_doc},
  	{"read",	(PyCFunction)array_fromfile, 0, fromfile_doc},
! 	{"remove",	(PyCFunction)array_remove, 0, remove_doc},
  	{"reverse",	(PyCFunction)array_reverse, 0, reverse_doc},
! /*	{"sort",	(PyCFunction)array_sort, 0, sort_doc},*/
  	{"tofile",	(PyCFunction)array_tofile, 0, tofile_doc},
  	{"tolist",	(PyCFunction)array_tolist, 0, tolist_doc},
  	{"tostring",	(PyCFunction)array_tostring, 0, tostring_doc},
diff -c --recursive python/dist/src/Modules/timemodule.c python-mod/dist/src/Modules/timemodule.c
*** python/dist/src/Modules/timemodule.c	Tue May  9 21:52:40 2000
--- python-mod/dist/src/Modules/timemodule.c	Fri May 12 12:41:39 2000
***************
*** 439,445 ****
  	if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
  	        return NULL;
  	memset((ANY *) &tm, '\0', sizeof(tm));
! 	s = strptime(buf, fmt, &tm);
  	if (s == NULL) {
  		PyErr_SetString(PyExc_ValueError, "format mismatch");
  		return NULL;
--- 439,445 ----
  	if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
  	        return NULL;
  	memset((ANY *) &tm, '\0', sizeof(tm));
! 	s = (char*)strptime(buf, fmt, &tm);
  	if (s == NULL) {
  		PyErr_SetString(PyExc_ValueError, "format mismatch");
  		return NULL;


--------------1F8F8C92A9D72DEE76E9CB8C--