[Patches] array.pop
Peter Schneider-Kamp
peter@schneider-kamp.de
Sun, 14 May 2000 14:22:12 +0200
This is a multi-part message in MIME format.
--------------CF1A8F54BE8A656EF3130EA1
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Problem:
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 listpop to arrays
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
--------------CF1A8F54BE8A656EF3130EA1
Content-Type: text/plain; charset=us-ascii;
name="array.pop.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="array.pop.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 Sun May 14 12:45:40 2000
--- python-mod/dist/src/Lib/test/test_array.py Sun May 14 14:12:28 2000
***************
*** 74,79 ****
--- 74,89 ----
a.remove("e")
if a != array.array(type, "aabcde"):
raise TestFailed, "array(%s) remove-test" % `type`
+ if a.pop(0) != "a":
+ raise TestFailed, "array(%s) pop-test" % `type`
+ if a.pop(1) != "b":
+ raise TestFailed, "array(%s) pop-test" % `type`
+ a.pop()
+ a.pop()
+ a.pop()
+ a.pop()
+ if a != array.array(type):
+ raise TestFailed, "array(%s) pop-test" % `type`
else:
a = array.array(type, [1, 2, 3, 4, 5])
a[:-1] = a
***************
*** 94,99 ****
--- 104,119 ----
a.remove(5)
if a != array.array(type, [1, 1, 2, 3, 4, 5]):
raise TestFailed, "array(%s) remove-test" % `type`
+ if a.pop(0) != 1:
+ raise TestFailed, "array(%s) pop-test" % `type`
+ if a.pop(1) != 2:
+ raise TestFailed, "array(%s) pop-test" % `type`
+ a.pop()
+ a.pop()
+ a.pop()
+ a.pop()
+ if a != array.array(type):
+ raise TestFailed, "array(%s) pop-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 Sun May 14 12:45:40 2000
--- python-mod/dist/src/Modules/arraymodule.c Sun May 14 14:09:16 2000
***************
*** 779,784 ****
--- 779,818 ----
Remove the first occurence of x in the array.";
static PyObject *
+ array_pop(self, args)
+ arrayobject *self;
+ PyObject *args;
+ {
+ int i = -1;
+ PyObject *v;
+ if (!PyArg_ParseTuple(args, "|i:pop", &i))
+ return NULL;
+ if (self->ob_size == 0) {
+ /* Special-case most common failure cause */
+ PyErr_SetString(PyExc_IndexError, "pop from empty array");
+ return NULL;
+ }
+ if (i < 0)
+ i += self->ob_size;
+ if (i < 0 || i >= self->ob_size) {
+ PyErr_SetString(PyExc_IndexError, "pop index out of range");
+ return NULL;
+ }
+ v = getarrayitem((PyObject *)self,i);
+ Py_INCREF(v);
+ if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ return v;
+ }
+
+ static char pop_doc [] =
+ "pop ([i])\n\
+ \n\
+ Return the i-th element and delete it from the array. i defaults to -1.";
+
+ static PyObject *
array_insert(self, args)
arrayobject *self;
PyObject *args;
***************
*** 1154,1159 ****
--- 1188,1194 ----
{"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
{"index", (PyCFunction)array_index, 0, index_doc},
{"insert", (PyCFunction)array_insert, 0, insert_doc},
+ {"pop", (PyCFunction)array_pop, 1, pop_doc},
{"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
{"remove", (PyCFunction)array_remove, 0, remove_doc},
{"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
--------------CF1A8F54BE8A656EF3130EA1--