annotate Modules/arraymodule.c @ 33316:0b181bf230c9 legacy-trunk

SF feature request #992967: array.array objects should support sequences. Made the constructor accept general iterables.
author Raymond Hettinger <python@rcn.com>
date Sun, 29 Aug 2004 07:50:43 +0000
parents 2c169ca5fa69
children 97c97194e64b 30b97c1bb12d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Array object implementation */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* An array is a uniform list -- all items have the same type.
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 The item type is restricted to simple C types like int or float */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
6 #include "Python.h"
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
7 #include "structmember.h"
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
8
1797
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
9 #ifdef STDC_HEADERS
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
10 #include <stddef.h>
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
11 #else /* !STDC_HEADERS */
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
12 #ifndef DONT_HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
13 #include <sys/types.h> /* For size_t */
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
14 #endif /* DONT_HAVE_SYS_TYPES_H */
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
15 #endif /* !STDC_HEADERS */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 struct arrayobject; /* Forward */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
19 /* All possible arraydescr values are defined in the vector "descriptors"
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
20 * below. That's defined later because the appropriate get and set
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
21 * functions aren't visible yet.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
22 */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 struct arraydescr {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 int typecode;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 int itemsize;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13268
diff changeset
26 PyObject * (*getitem)(struct arrayobject *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13268
diff changeset
27 int (*setitem)(struct arrayobject *, int, PyObject *);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 typedef struct arrayobject {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
31 PyObject_HEAD
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
32 int ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 char *ob_item;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
34 int allocated;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 struct arraydescr *ob_descr;
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
36 PyObject *weakreflist; /* List of weak references */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 } arrayobject;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23929
diff changeset
39 static PyTypeObject Arraytype;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
42 #define array_CheckExact(op) ((op)->ob_type == &Arraytype)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
44 static int
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
45 array_resize(arrayobject *self, int newsize)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
46 {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
47 char *items;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
48 size_t _new_size;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
49
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
50 /* Bypass realloc() when a previous overallocation is large enough
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
51 to accommodate the newsize. If the newsize is 16 smaller than the
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
52 current size, then proceed with the realloc() to shrink the list.
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
53 */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
54
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
55 if (self->allocated >= newsize &&
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
56 self->ob_size < newsize + 16 &&
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
57 self->ob_item != NULL) {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
58 self->ob_size = newsize;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
59 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
60 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
61
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
62 /* This over-allocates proportional to the array size, making room
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
63 * for additional growth. The over-allocation is mild, but is
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
64 * enough to give linear-time amortized behavior over a long
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
65 * sequence of appends() in the presence of a poorly-performing
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
66 * system realloc().
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
67 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
68 * Note, the pattern starts out the same as for lists but then
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
69 * grows at a smaller rate so that larger arrays only overallocate
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
70 * by about 1/16th -- this is done because arrays are presumed to be more
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
71 * memory critical.
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
72 */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
73
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
74 _new_size = (newsize >> 4) + (self->ob_size < 8 ? 3 : 7) + newsize;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
75 items = self->ob_item;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
76 /* XXX The following multiplication and division does not optimize away
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
77 like it does for lists since the size is not known at compile time */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
80 else
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
81 items = NULL;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
82 if (items == NULL) {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
83 PyErr_NoMemory();
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
84 return -1;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
85 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
86 self->ob_item = items;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
87 self->ob_size = newsize;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
88 self->allocated = _new_size;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
89 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
90 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
91
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
92 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
93 Get and Set functions for each type.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
94 A Get function takes an arrayobject* and an integer index, returning the
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
95 array value at that index wrapped in an appropriate PyObject*.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
96 A Set function takes an arrayobject, integer index, and PyObject*; sets
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
97 the array value at that index to the raw C data extracted from the PyObject*,
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
98 and returns 0 if successful, else nonzero on failure (PyObject* not of an
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
99 appropriate type or value).
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
100 Note that the basic Get and Set functions do NOT check that the index is
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
101 in bounds; that's the responsibility of the caller.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
102 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
104 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
105 c_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
111 c_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 char x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
114 if (!PyArg_Parse(v, "c;array item must be char", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
117 ((char *)ap->ob_item)[i] = x;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
121 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
122 b_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 long x = ((char *)ap->ob_item)[i];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if (x >= 128)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 x -= 256;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
127 return PyInt_FromLong(x);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
131 b_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
133 short x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
134 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
135 must use the next size up that is signed ('h') and manually do
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
136 the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
137 if (!PyArg_Parse(v, "h;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 return -1;
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
139 else if (x < -128) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
140 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
141 "signed char is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
142 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
143 }
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
144 else if (x > 127) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
145 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
146 "signed char is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
147 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
148 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
150 ((char *)ap->ob_item)[i] = (char)x;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
154 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
155 BB_getitem(arrayobject *ap, int i)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
156 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
157 long x = ((unsigned char *)ap->ob_item)[i];
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
158 return PyInt_FromLong(x);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
159 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
160
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
161 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
162 BB_setitem(arrayobject *ap, int i, PyObject *v)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
163 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
164 unsigned char x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
166 if (!PyArg_Parse(v, "b;array item must be integer", &x))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
167 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
168 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
169 ((char *)ap->ob_item)[i] = x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
170 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
171 }
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
172
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
173 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
174 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
175 u_getitem(arrayobject *ap, int i)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
176 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
178 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
179
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
180 static int
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
181 u_setitem(arrayobject *ap, int i, PyObject *v)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
182 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
183 Py_UNICODE *p;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
184 int len;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
185
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
187 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
188 if (len != 1) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
189 PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
190 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
191 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
192 if (i >= 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
193 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
194 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
195 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
196 #endif
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
197
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
198 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
199 h_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
201 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
205 h_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 short x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
209 if (!PyArg_Parse(v, "h;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 ((short *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
216 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
217 HH_getitem(arrayobject *ap, int i)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
218 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
219 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
220 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
221
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
222 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
223 HH_setitem(arrayobject *ap, int i, PyObject *v)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
224 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
225 int x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
227 must use the next size up and manually do the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
228 if (!PyArg_Parse(v, "i;array item must be integer", &x))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
229 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
230 else if (x < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
231 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
232 "unsigned short is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
233 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
234 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
235 else if (x > USHRT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
236 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
237 "unsigned short is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
238 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
239 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
240 if (i >= 0)
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
241 ((short *)ap->ob_item)[i] = (short)x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
242 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
243 }
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
244
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
245 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
246 i_getitem(arrayobject *ap, int i)
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
247 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
248 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
249 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
250
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
251 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
252 i_setitem(arrayobject *ap, int i, PyObject *v)
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
253 {
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
254 int x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
256 if (!PyArg_Parse(v, "i;array item must be integer", &x))
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
257 return -1;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
258 if (i >= 0)
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
259 ((int *)ap->ob_item)[i] = x;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
260 return 0;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
261 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
262
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
263 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
264 II_getitem(arrayobject *ap, int i)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
265 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
266 return PyLong_FromUnsignedLong(
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
267 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
268 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
269
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
270 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
271 II_setitem(arrayobject *ap, int i, PyObject *v)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
272 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
273 unsigned long x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
274 if (PyLong_Check(v)) {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
275 x = PyLong_AsUnsignedLong(v);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
276 if (x == (unsigned long) -1 && PyErr_Occurred())
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
277 return -1;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
278 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
279 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
280 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
281 if (!PyArg_Parse(v, "l;array item must be integer", &y))
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
282 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
283 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
284 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
285 "unsigned int is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
286 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
287 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
288 x = (unsigned long)y;
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
289
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
290 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
291 if (x > UINT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
292 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
293 "unsigned int is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
294 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
295 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
296
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
297 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
299 return 0;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
300 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
301
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
302 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
303 l_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
305 return PyInt_FromLong(((long *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
309 l_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 long x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
312 if (!PyArg_Parse(v, "l;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 ((long *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
319 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
320 LL_getitem(arrayobject *ap, int i)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
321 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
323 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
324
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
325 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
326 LL_setitem(arrayobject *ap, int i, PyObject *v)
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
327 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
328 unsigned long x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
329 if (PyLong_Check(v)) {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
330 x = PyLong_AsUnsignedLong(v);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
331 if (x == (unsigned long) -1 && PyErr_Occurred())
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
332 return -1;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
333 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
334 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
335 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
336 if (!PyArg_Parse(v, "l;array item must be integer", &y))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
337 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
338 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
339 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
340 "unsigned long is less than minimum");
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
341 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
342 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
343 x = (unsigned long)y;
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
344
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
345 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
346 if (x > ULONG_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
347 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
348 "unsigned long is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
349 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
350 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
351
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
352 if (i >= 0)
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
353 ((unsigned long *)ap->ob_item)[i] = x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
354 return 0;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
355 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
356
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
357 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
358 f_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
364 f_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 float x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
367 if (!PyArg_Parse(v, "f;array item must be float", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 ((float *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
374 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
375 d_getitem(arrayobject *ap, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
381 d_setitem(arrayobject *ap, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 double x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
384 if (!PyArg_Parse(v, "d;array item must be float", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 ((double *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 /* Description of types */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
392 static struct arraydescr descriptors[] = {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 {'c', sizeof(char), c_getitem, c_setitem},
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 {'b', sizeof(char), b_getitem, b_setitem},
5542
cfc13f3f3876 Address the following problem on DOS and Win 3.1, reported by Jim
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
395 {'B', sizeof(char), BB_getitem, BB_setitem},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
396 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
398 #endif
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 {'h', sizeof(short), h_getitem, h_setitem},
5542
cfc13f3f3876 Address the following problem on DOS and Win 3.1, reported by Jim
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
400 {'H', sizeof(short), HH_getitem, HH_setitem},
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
401 {'i', sizeof(int), i_getitem, i_setitem},
5542
cfc13f3f3876 Address the following problem on DOS and Win 3.1, reported by Jim
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
402 {'I', sizeof(int), II_getitem, II_setitem},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 {'l', sizeof(long), l_getitem, l_setitem},
5542
cfc13f3f3876 Address the following problem on DOS and Win 3.1, reported by Jim
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
404 {'L', sizeof(long), LL_getitem, LL_setitem},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 {'f', sizeof(float), f_getitem, f_setitem},
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 {'d', sizeof(double), d_getitem, d_setitem},
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 {'\0', 0, 0, 0} /* Sentinel */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 };
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
409
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
410 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
411 Implementations of array object methods.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
412 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
414 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
415 newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 arrayobject *op;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
418 size_t nbytes;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
419
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 if (size < 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
421 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
424
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 nbytes = size * descr->itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 /* Check for overflow */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4875
diff changeset
427 if (nbytes / descr->itemsize != (size_t)size) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
428 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
430 op = (arrayobject *) type->tp_alloc(type, 0);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 if (op == NULL) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
432 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
434 op->ob_size = size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 if (size <= 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 op->ob_item = NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 else {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
439 op->ob_item = PyMem_NEW(char, nbytes);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 if (op->ob_item == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11711
diff changeset
441 PyObject_Del(op);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
442 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 op->ob_descr = descr;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
446 op->allocated = size;
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
447 op->weakreflist = NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
448 return (PyObject *) op;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
451 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
452 getarrayitem(PyObject *op, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 register arrayobject *ap;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
455 assert(array_Check(op));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 ap = (arrayobject *)op;
29244
23ad8b5d7b4a All calls to getarrayitem() (which is static) are done either in loops
Walter Dörwald <walter@livinglogic.de>
parents: 29163
diff changeset
457 assert(i>=0 && i<ap->ob_size);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 return (*ap->ob_descr->getitem)(ap, i);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
462 ins1(arrayobject *self, int where, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 char *items;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
465 int n = self->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
467 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 return -1;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
472
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
473 if (array_resize(self, n+1) == -1)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
474 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 items = self->ob_item;
29163
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
476 if (where < 0) {
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
477 where += n;
29163
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
478 if (where < 0)
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
479 where = 0;
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
480 }
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
481 if (where > n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
482 where = n;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
483 /* appends don't need to call memmove() */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
484 if (where != n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
485 memmove(items + (where+1)*self->ob_descr->itemsize,
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
486 items + where*self->ob_descr->itemsize,
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
487 (n-where)*self->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 return (*self->ob_descr->setitem)(self, where, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 /* Methods */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 static void
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
494 array_dealloc(arrayobject *op)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 {
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
496 if (op->weakreflist != NULL)
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
497 PyObject_ClearWeakRefs((PyObject *) op);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 if (op->ob_item != NULL)
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
499 PyMem_DEL(op->ob_item);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
500 op->ob_type->tp_free((PyObject *)op);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
503 static PyObject *
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
504 array_richcompare(PyObject *v, PyObject *w, int op)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
506 arrayobject *va, *wa;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
507 PyObject *vi = NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
508 PyObject *wi = NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
509 int i, k;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
510 PyObject *res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
511
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
512 if (!array_Check(v) || !array_Check(w)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
513 Py_INCREF(Py_NotImplemented);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
514 return Py_NotImplemented;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
515 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
516
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
517 va = (arrayobject *)v;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
518 wa = (arrayobject *)w;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
519
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
520 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
521 /* Shortcut: if the lengths differ, the arrays differ */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
522 if (op == Py_EQ)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
523 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
524 else
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
525 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
526 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
527 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
528 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
529
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
530 /* Search for the first index where items are different */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
531 k = 1;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
532 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
533 vi = getarrayitem(v, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
534 wi = getarrayitem(w, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
535 if (vi == NULL || wi == NULL) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
536 Py_XDECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
537 Py_XDECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
538 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
539 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
540 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
541 if (k == 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
542 break; /* Keeping vi and wi alive! */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
543 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
544 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
545 if (k < 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
546 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
547 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
548
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
549 if (k) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
550 /* No more items to compare -- compare sizes */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
551 int vs = va->ob_size;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
552 int ws = wa->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 int cmp;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
554 switch (op) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
555 case Py_LT: cmp = vs < ws; break;
16268
c2ce9b2e5bb4 Correct one-line typo, reported by yole @ SF, bug 130077.
Guido van Rossum <guido@python.org>
parents: 16232
diff changeset
556 case Py_LE: cmp = vs <= ws; break;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
557 case Py_EQ: cmp = vs == ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
558 case Py_NE: cmp = vs != ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
559 case Py_GT: cmp = vs > ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
560 case Py_GE: cmp = vs >= ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
561 default: return NULL; /* cannot happen */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
562 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
563 if (cmp)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
564 res = Py_True;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 else
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
566 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
567 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
568 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
569 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
570
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
571 /* We have an item that differs. First, shortcuts for EQ/NE */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
572 if (op == Py_EQ) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
573 Py_INCREF(Py_False);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
574 res = Py_False;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
576 else if (op == Py_NE) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
577 Py_INCREF(Py_True);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
578 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
579 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
580 else {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
581 /* Compare the final item again using the proper operator */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
582 res = PyObject_RichCompare(vi, wi, op);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
583 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
584 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
585 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
586 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
590 array_length(arrayobject *a)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 return a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
595 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
596 array_item(arrayobject *a, int i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 if (i < 0 || i >= a->ob_size) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
599 PyErr_SetString(PyExc_IndexError, "array index out of range");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
602 return getarrayitem((PyObject *)a, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
605 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
606 array_slice(arrayobject *a, int ilow, int ihigh)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 ilow = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 else if (ilow > a->ob_size)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 ilow = a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 ihigh = ilow;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 else if (ihigh > a->ob_size)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 ihigh = a->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
619 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 (ihigh-ilow) * a->ob_descr->itemsize);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
624 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
627 static PyObject *
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
628 array_copy(arrayobject *a, PyObject *unused)
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
629 {
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
630 return array_slice(a, 0, a->ob_size);
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
631 }
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
632
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
633 PyDoc_STRVAR(copy_doc,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
634 "copy(array)\n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
635 \n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
636 Return a copy of the array.");
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
637
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
638 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
639 array_concat(arrayobject *a, PyObject *bb)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 int size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 arrayobject *np;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
643 if (!array_Check(bb)) {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
644 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
645 "can only append array (not \"%.200s\") to array",
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
646 bb->ob_type->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 #define b ((arrayobject *)bb)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 if (a->ob_descr != b->ob_descr) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
651 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 size = a->ob_size + b->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
655 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 if (np == NULL) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
1397
1ce2c0f565ef Fix stupib bug in concatenation
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
661 b->ob_item, b->ob_size*b->ob_descr->itemsize);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
662 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
666 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
667 array_repeat(arrayobject *a, int n)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 {
2055
cf31eab6ab5a Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents: 1797
diff changeset
669 int i;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 int size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 char *p;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 int nbytes;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 if (n < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 n = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 size = a->ob_size * n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
677 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 p = np->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 nbytes = a->ob_size * a->ob_descr->itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 for (i = 0; i < n; i++) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 memcpy(p, a->ob_item, nbytes);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 p += nbytes;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
686 return (PyObject *) np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
690 array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 char *item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 int n; /* Size of replacement array */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 int d; /* Change in size */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 #define b ((arrayobject *)v)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 n = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
698 else if (array_Check(v)) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 n = b->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 if (a == b) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 /* Special case "a[i:j] = a" -- copy b first */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 int ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 v = array_slice(b, 0, n);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 ret = array_ass_slice(a, ilow, ihigh, v);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
705 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 return ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 if (b->ob_descr != a->ob_descr) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
709 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 else {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
714 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
715 "can only assign array (not \"%.200s\") to array slice",
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
716 v->ob_type->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 ilow = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 else if (ilow > a->ob_size)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 ilow = a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 ihigh = ilow;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 else if (ihigh > a->ob_size)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 ihigh = a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 item = a->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 d = n - (ihigh-ilow);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 if (d < 0) { /* Delete -d items */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 item + ihigh*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 (a->ob_size-ihigh)*a->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 a->ob_size += d;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
736 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 /* Can't fail */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 a->ob_item = item;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
739 a->allocated = a->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 else if (d > 0) { /* Insert d items */
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
742 PyMem_RESIZE(item, char,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
743 (a->ob_size + d)*a->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
745 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 item + ihigh*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 (a->ob_size-ihigh)*a->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 a->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 a->ob_size += d;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
753 a->allocated = a->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 if (n > 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 n*b->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
761
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
763 array_ass_item(arrayobject *a, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 if (i < 0 || i >= a->ob_size) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
766 PyErr_SetString(PyExc_IndexError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
767 "array assignment index out of range");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 return array_ass_slice(a, i, i+1, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 return (*a->ob_descr->setitem)(a, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
774
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
776 setarrayitem(PyObject *a, int i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
778 assert(array_Check(a));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 return array_ass_item((arrayobject *)a, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
781
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
782 static int
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
783 array_iter_extend(arrayobject *self, PyObject *bb)
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
784 {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
785 PyObject *it, *v;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
786
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
787 it = PyObject_GetIter(bb);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
788 if (it == NULL)
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
789 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
790
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
791 while ((v = PyIter_Next(it)) != NULL) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
792 if (ins1(self, (int) self->ob_size, v) != 0) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
793 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
794 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
795 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
796 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
797 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
798 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
799 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
800 if (PyErr_Occurred())
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
801 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
802 return 0;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
803 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
804
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
805 static int
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
806 array_do_extend(arrayobject *self, PyObject *bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
807 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
808 int size;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
809
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
810 if (!array_Check(bb))
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
811 return array_iter_extend(self, bb);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
812 #define b ((arrayobject *)bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
813 if (self->ob_descr != b->ob_descr) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
814 PyErr_SetString(PyExc_TypeError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
815 "can only extend with array of same kind");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
816 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
817 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
818 size = self->ob_size + b->ob_size;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
819 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
820 if (self->ob_item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
821 PyObject_Del(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
822 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
823 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
824 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
825 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
826 b->ob_item, b->ob_size*b->ob_descr->itemsize);
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
827 self->ob_size = size;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
828 self->allocated = size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
829
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
830 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
831 #undef b
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
832 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
833
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
834 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
835 array_inplace_concat(arrayobject *self, PyObject *bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
836 {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
837 if (!array_Check(bb)) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
838 PyErr_Format(PyExc_TypeError,
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
839 "can only extend array with array (not \"%.200s\")",
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
840 bb->ob_type->tp_name);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
841 return NULL;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
842 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
843 if (array_do_extend(self, bb) == -1)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
844 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
845 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
846 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
847 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
848
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
849 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
850 array_inplace_repeat(arrayobject *self, int n)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
851 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
852 char *items, *p;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
853 int size, i;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
854
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
855 if (self->ob_size > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
856 if (n < 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
857 n = 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
858 items = self->ob_item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
859 size = self->ob_size * self->ob_descr->itemsize;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
860 if (n == 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
861 PyMem_FREE(items);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
862 self->ob_item = NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
863 self->ob_size = 0;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
864 self->allocated = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
865 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
866 else {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
867 PyMem_Resize(items, char, n * size);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
868 if (items == NULL)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
869 return PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
870 p = items;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
871 for (i = 1; i < n; i++) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
872 p += size;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
873 memcpy(p, items, size);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
874 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
875 self->ob_item = items;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
876 self->ob_size *= n;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
877 self->allocated = self->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
878 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
879 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
880 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
881 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
882 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
883
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
884
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
885 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
886 ins(arrayobject *self, int where, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 if (ins1(self, where, v) != 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
890 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
891 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
892 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
893
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
894 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
895 array_count(arrayobject *self, PyObject *v)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
896 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
897 int count = 0;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
898 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
899
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
900 for (i = 0; i < self->ob_size; i++) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
901 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
902 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
903 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
904 if (cmp > 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
905 count++;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
906 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
907 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
908 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
909 return PyInt_FromLong((long)count);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
910 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
911
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
912 PyDoc_STRVAR(count_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
913 "count(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
914 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
915 Return number of occurences of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
916
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
917 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
918 array_index(arrayobject *self, PyObject *v)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
919 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
920 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
921
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
922 for (i = 0; i < self->ob_size; i++) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
923 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
924 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
925 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
926 if (cmp > 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
927 return PyInt_FromLong((long)i);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
928 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
929 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
930 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
931 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
932 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
933 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
934 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
935
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
936 PyDoc_STRVAR(index_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
937 "index(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
938 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
939 Return index of first occurence of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
940
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
941 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
942 array_contains(arrayobject *self, PyObject *v)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
943 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
944 int i, cmp;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
945
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
946 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
947 PyObject *selfi = getarrayitem((PyObject *)self, i);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
948 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
949 Py_DECREF(selfi);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
950 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
951 return cmp;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
952 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
953
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
954 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
955 array_remove(arrayobject *self, PyObject *v)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
956 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
957 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
958
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
959 for (i = 0; i < self->ob_size; i++) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
960 PyObject *selfi = getarrayitem((PyObject *)self,i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
961 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
962 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
963 if (cmp > 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
964 if (array_ass_slice(self, i, i+1,
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
965 (PyObject *)NULL) != 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
966 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
967 Py_INCREF(Py_None);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
968 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
969 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
970 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
971 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
972 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
973 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
974 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
975 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
976
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
977 PyDoc_STRVAR(remove_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
978 "remove(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
979 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
980 Remove the first occurence of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
981
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
982 static PyObject *
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
983 array_pop(arrayobject *self, PyObject *args)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
984 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
985 int i = -1;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
986 PyObject *v;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
987 if (!PyArg_ParseTuple(args, "|i:pop", &i))
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
988 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
989 if (self->ob_size == 0) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
990 /* Special-case most common failure cause */
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
991 PyErr_SetString(PyExc_IndexError, "pop from empty array");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
992 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
993 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
994 if (i < 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
995 i += self->ob_size;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
996 if (i < 0 || i >= self->ob_size) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
997 PyErr_SetString(PyExc_IndexError, "pop index out of range");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
998 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
999 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1000 v = getarrayitem((PyObject *)self,i);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1001 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1002 Py_DECREF(v);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1003 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1004 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1005 return v;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1006 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1007
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1008 PyDoc_STRVAR(pop_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1009 "pop([i])\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1010 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1011 Return the i-th element and delete it from the array. i defaults to -1.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1012
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1013 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1014 array_extend(arrayobject *self, PyObject *bb)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1015 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1016 if (array_do_extend(self, bb) == -1)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1017 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1018 Py_INCREF(Py_None);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1019 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1020 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1021
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1022 PyDoc_STRVAR(extend_doc,
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1023 "extend(array or iterable)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1024 \n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1025 Append items to the end of the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1026
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1027 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1028 array_insert(arrayobject *self, PyObject *args)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 int i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1031 PyObject *v;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1032 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 return ins(self, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1037 PyDoc_STRVAR(insert_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1038 "insert(i,x)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1039 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1040 Insert a new item x into the array before position i.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1041
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1042
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1043 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1044 array_buffer_info(arrayobject *self, PyObject *unused)
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1045 {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1046 PyObject* retval = NULL;
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1047 retval = PyTuple_New(2);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1048 if (!retval)
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1049 return NULL;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1050
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1051 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1052 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1053
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1054 return retval;
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1055 }
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1056
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1057 PyDoc_STRVAR(buffer_info_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1058 "buffer_info() -> (address, length)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1059 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1060 Return a tuple (address, length) giving the current memory address and\n\
18851
ef2031c54875 Fix buffer_info() docstring to match reality. See SF bug #444842.
Guido van Rossum <guido@python.org>
parents: 18036
diff changeset
1061 the length in items of the buffer used to hold array's contents\n\
ef2031c54875 Fix buffer_info() docstring to match reality. See SF bug #444842.
Guido van Rossum <guido@python.org>
parents: 18036
diff changeset
1062 The length should be multiplied by the itemsize attribute to calculate\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1063 the buffer length in bytes.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1064
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1065
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1066 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1067 array_append(arrayobject *self, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 return ins(self, (int) self->ob_size, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1072 PyDoc_STRVAR(append_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1073 "append(x)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1074 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1075 Append new value x to the end of the array.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1076
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1077
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1078 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1079 array_byteswap(arrayobject *self, PyObject *unused)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1081 char *p;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1082 int i;
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1083
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 switch (self->ob_descr->itemsize) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1085 case 1:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 case 2:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 p[0] = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1091 p[1] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 case 4:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 char p1 = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098 p[0] = p[3];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 p[1] = p[2];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 p[2] = p1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 p[3] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 break;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1104 case 8:
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1105 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1106 char p0 = p[0];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1107 char p1 = p[1];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1108 char p2 = p[2];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1109 char p3 = p[3];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1110 p[0] = p[7];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1111 p[1] = p[6];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1112 p[2] = p[5];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1113 p[3] = p[4];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1114 p[4] = p3;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1115 p[5] = p2;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1116 p[6] = p1;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1117 p[7] = p0;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1118 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1119 break;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 default:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1121 PyErr_SetString(PyExc_RuntimeError,
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 "don't know how to byteswap this array type");
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1124 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1125 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1126 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1129 PyDoc_STRVAR(byteswap_doc,
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1130 "byteswap()\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1131 \n\
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1132 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1133 4, or 8 bytes in size, RuntimeError is raised.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1134
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1135 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1136 array_reverse(arrayobject *self, PyObject *unused)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1138 register int itemsize = self->ob_descr->itemsize;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1139 register char *p, *q;
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1140 /* little buffer to hold items while swapping */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1141 char tmp[256]; /* 8 is probably enough -- but why skimp */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1142 assert(itemsize <= sizeof(tmp));
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1143
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 if (self->ob_size > 1) {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1145 for (p = self->ob_item,
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1146 q = self->ob_item + (self->ob_size - 1)*itemsize;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1147 p < q;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1148 p += itemsize, q -= itemsize) {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1149 /* memory areas guaranteed disjoint, so memcpy
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1150 * is safe (& memmove may be slower).
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1151 */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1152 memcpy(tmp, p, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1153 memcpy(p, q, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1154 memcpy(q, tmp, itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1157
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1158 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1159 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1160 }
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1161
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1162 PyDoc_STRVAR(reverse_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1163 "reverse()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1164 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1165 Reverse the order of the items in the array.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1166
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1167 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1168 array_fromfile(arrayobject *self, PyObject *args)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1170 PyObject *f;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 int n;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 FILE *fp;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1173 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1175 fp = PyFile_AsFile(f);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 if (fp == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1177 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1181 char *item = self->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182 int itemsize = self->ob_descr->itemsize;
13081
0acec211a209 Trent Mick: use size_t instead of int where appropriate (in
Guido van Rossum <guido@python.org>
parents: 13068
diff changeset
1183 size_t nread;
10283
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1184 int newlength;
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1185 size_t newbytes;
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1186 /* Be careful here about overflow */
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1187 if ((newlength = self->ob_size + n) <= 0 ||
10434
a4768315948d Use an unsigned cast to avoid a warning in VC++.
Guido van Rossum <guido@python.org>
parents: 10283
diff changeset
1188 (newbytes = newlength * itemsize) / itemsize !=
a4768315948d Use an unsigned cast to avoid a warning in VC++.
Guido van Rossum <guido@python.org>
parents: 10283
diff changeset
1189 (size_t)newlength)
10283
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1190 goto nomem;
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1191 PyMem_RESIZE(item, char, newbytes);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 if (item == NULL) {
10283
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1193 nomem:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1194 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1198 self->ob_size += n;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1199 self->allocated = self->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 nread = fread(item + (self->ob_size - n) * itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 itemsize, n, fp);
13081
0acec211a209 Trent Mick: use size_t instead of int where appropriate (in
Guido van Rossum <guido@python.org>
parents: 13068
diff changeset
1202 if (nread < (size_t)n) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 self->ob_size -= (n - nread);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1204 PyMem_RESIZE(item, char, self->ob_size*itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 self->ob_item = item;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1206 self->allocated = self->ob_size;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1207 PyErr_SetString(PyExc_EOFError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1208 "not enough items in file");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1212 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1213 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1215
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1216 PyDoc_STRVAR(fromfile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1217 "fromfile(f, n)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1218 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1219 Read n objects from the file object f and append them to the end of the\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1220 array. Also called as read.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1221
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1222
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1223 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1224 array_tofile(arrayobject *self, PyObject *f)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 FILE *fp;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1227
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1228 fp = PyFile_AsFile(f);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 if (fp == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1230 PyErr_SetString(PyExc_TypeError, "arg must be open file");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 if (self->ob_size > 0) {
13942
31b25ac1f019 Use safer comparisons (only matters when sizeof(int) != sizeof(size_t)). fread
Trent Mick <trentm@activestate.com>
parents: 13792
diff changeset
1234 if (fwrite(self->ob_item, self->ob_descr->itemsize,
31b25ac1f019 Use safer comparisons (only matters when sizeof(int) != sizeof(size_t)). fread
Trent Mick <trentm@activestate.com>
parents: 13792
diff changeset
1235 self->ob_size, fp) != (size_t)self->ob_size) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1236 PyErr_SetFromErrno(PyExc_IOError);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 clearerr(fp);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1241 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1242 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1245 PyDoc_STRVAR(tofile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1246 "tofile(f)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1247 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1248 Write all items (as machine values) to the file object f. Also called as\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1249 write.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1250
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1251
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1252 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1253 array_fromlist(arrayobject *self, PyObject *list)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 int n;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 int itemsize = self->ob_descr->itemsize;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1257
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1258 if (!PyList_Check(list)) {
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1259 PyErr_SetString(PyExc_TypeError, "arg must be list");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1262 n = PyList_Size(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264 char *item = self->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265 int i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1266 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1268 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272 self->ob_size += n;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1273 self->allocated = self->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 for (i = 0; i < n; i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1275 PyObject *v = PyList_GetItem(list, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 if ((*self->ob_descr->setitem)(self,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 self->ob_size - n + i, v) != 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 self->ob_size -= n;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1279 PyMem_RESIZE(item, char,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1280 self->ob_size * itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 self->ob_item = item;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1282 self->allocated = self->ob_size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1287 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1288 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1291 PyDoc_STRVAR(fromlist_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1292 "fromlist(list)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1293 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1294 Append items to array from list.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1295
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1296
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1297 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1298 array_tolist(arrayobject *self, PyObject *unused)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1300 PyObject *list = PyList_New(self->ob_size);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 int i;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1302
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 if (list == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305 for (i = 0; i < self->ob_size; i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1306 PyObject *v = getarrayitem((PyObject *)self, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1308 Py_DECREF(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1311 PyList_SetItem(list, i, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 return list;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1316 PyDoc_STRVAR(tolist_doc,
9360
4c23ae6c6e82 ACK! There was still an unescaped newline in a docstring.
Guido van Rossum <guido@python.org>
parents: 9343
diff changeset
1317 "tolist() -> list\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1318 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1319 Convert array to an ordinary list with the same items.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1320
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1321
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1322 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1323 array_fromstring(arrayobject *self, PyObject *args)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 char *str;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 int n;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 int itemsize = self->ob_descr->itemsize;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1328 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330 if (n % itemsize != 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1331 PyErr_SetString(PyExc_ValueError,
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 "string length not a multiple of item size");
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 n = n / itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 char *item = self->ob_item;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1338 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1340 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 self->ob_size += n;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1345 self->allocated = self->ob_size;
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1346 memcpy(item + (self->ob_size - n) * itemsize,
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1347 str, itemsize*n);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1349 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1350 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1353 PyDoc_STRVAR(fromstring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1354 "fromstring(string)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1355 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1356 Appends items from the string, interpreting it as an array of machine\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1357 values,as if it had been read from a file using the fromfile() method).");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1358
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1359
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1360 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1361 array_tostring(arrayobject *self, PyObject *unused)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1363 return PyString_FromStringAndSize(self->ob_item,
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364 self->ob_size * self->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1367 PyDoc_STRVAR(tostring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1368 "tostring() -> string\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1369 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1370 Convert the array to an array of machine values and return the string\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1371 representation.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1372
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1373
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1374
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1375 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1376 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1377 array_fromunicode(arrayobject *self, PyObject *args)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1378 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1379 Py_UNICODE *ustr;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1380 int n;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1381
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1382 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1383 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1384 if (self->ob_descr->typecode != 'u') {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1385 PyErr_SetString(PyExc_ValueError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1386 "fromunicode() may only be called on "
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1387 "type 'u' arrays");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1388 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1389 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1390 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1391 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1392 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1393 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1394 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1395 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1396 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1397 self->ob_item = (char *) item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1398 self->ob_size += n;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1399 self->allocated = self->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1400 memcpy(item + self->ob_size - n,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1401 ustr, n * sizeof(Py_UNICODE));
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1402 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1403
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1404 Py_INCREF(Py_None);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1405 return Py_None;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1406 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1407
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1408 PyDoc_STRVAR(fromunicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1409 "fromunicode(ustr)\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1410 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1411 Extends this array with data from the unicode string ustr.\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1412 The array must be a type 'u' array; otherwise a ValueError\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1413 is raised. Use array.fromstring(ustr.decode(...)) to\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1414 append Unicode data to an array of some other type.");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1415
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1416
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1417 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1418 array_tounicode(arrayobject *self, PyObject *unused)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1419 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1420 if (self->ob_descr->typecode != 'u') {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1421 PyErr_SetString(PyExc_ValueError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1422 "tounicode() may only be called on type 'u' arrays");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1423 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1424 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1425 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1426 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1427
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1428 PyDoc_STRVAR(tounicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1429 "tounicode() -> unicode\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1430 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1431 Convert the array to a unicode string. The array must be\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1432 a type 'u' array; otherwise a ValueError is raised. Use\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1433 array.tostring().decode() to obtain a unicode string from\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1434 an array of some other type.");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1435
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1436 #endif /* Py_USING_UNICODE */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1437
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1438
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1439 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1440 array_get_typecode(arrayobject *a, void *closure)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1441 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1442 char tc = a->ob_descr->typecode;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1443 return PyString_FromStringAndSize(&tc, 1);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1444 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1445
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1446 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1447 array_get_itemsize(arrayobject *a, void *closure)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1448 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1449 return PyInt_FromLong((long)a->ob_descr->itemsize);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1450 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1451
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1452 static PyGetSetDef array_getsets [] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1453 {"typecode", (getter) array_get_typecode, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1454 "the typecode character used to create the array"},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1455 {"itemsize", (getter) array_get_itemsize, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1456 "the size, in bytes, of one array item"},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1457 {NULL}
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1458 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1459
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1460 PyMethodDef array_methods[] = {
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1461 {"append", (PyCFunction)array_append, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1462 append_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1463 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1464 buffer_info_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1465 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1466 byteswap_doc},
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1467 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1468 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1469 {"count", (PyCFunction)array_count, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1470 count_doc},
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1471 {"__deepcopy__",(PyCFunction)array_copy, METH_NOARGS,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1472 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1473 {"extend", (PyCFunction)array_extend, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1474 extend_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1475 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1476 fromfile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1477 {"fromlist", (PyCFunction)array_fromlist, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1478 fromlist_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1479 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1480 fromstring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1481 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1482 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1483 fromunicode_doc},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1484 #endif
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1485 {"index", (PyCFunction)array_index, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1486 index_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1487 {"insert", (PyCFunction)array_insert, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1488 insert_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1489 {"pop", (PyCFunction)array_pop, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1490 pop_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1491 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1492 fromfile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1493 {"remove", (PyCFunction)array_remove, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1494 remove_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1495 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1496 reverse_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1497 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1498 sort_doc},*/
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1499 {"tofile", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1500 tofile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1501 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1502 tolist_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1503 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1504 tostring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1505 #ifdef Py_USING_UNICODE
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1506 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1507 tounicode_doc},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1508 #endif
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1509 {"write", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1510 tofile_doc},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1511 {NULL, NULL} /* sentinel */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1512 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1514 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1515 array_repr(arrayobject *a)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1516 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1517 char buf[256], typecode;
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1518 PyObject *s, *t, *v = NULL;
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1519 int len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1520
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 len = a->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1522 typecode = a->ob_descr->typecode;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 if (len == 0) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1524 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1525 return PyString_FromString(buf);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1526 }
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1527
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1528 if (typecode == 'c')
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1529 v = array_tostring(a, NULL);
23433
fb64f21176af array_tounicode isn't defined in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents: 23365
diff changeset
1530 #ifdef Py_USING_UNICODE
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1531 else if (typecode == 'u')
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1532 v = array_tounicode(a, NULL);
23433
fb64f21176af array_tounicode isn't defined in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents: 23365
diff changeset
1533 #endif
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1534 else
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1535 v = array_tolist(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1536 t = PyObject_Repr(v);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1537 Py_XDECREF(v);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1538
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1539 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1540 s = PyString_FromString(buf);
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1541 PyString_ConcatAndDel(&s, t);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1542 PyString_ConcatAndDel(&s, PyString_FromString(")"));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543 return s;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1546 static PyObject*
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1547 array_subscr(arrayobject* self, PyObject* item)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1548 {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1549 if (PyInt_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1550 long i = PyInt_AS_LONG(item);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1551 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1552 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1553 return array_item(self, i);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1554 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1555 else if (PyLong_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1556 long i = PyLong_AsLong(item);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1557 if (i == -1 && PyErr_Occurred())
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1558 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1559 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1560 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1561 return array_item(self, i);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1562 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1563 else if (PySlice_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1564 int start, stop, step, slicelength, cur, i;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1565 PyObject* result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1566 arrayobject* ar;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1567 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1568
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1569 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1570 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1571 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1572 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1573
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1574 if (slicelength <= 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1575 return newarrayobject(&Arraytype, 0, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1576 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1577 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1578 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1579 if (!result) return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1580
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1581 ar = (arrayobject*)result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1582
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1583 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1584 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1585 memcpy(ar->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1586 self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1587 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1588 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1589
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1590 return result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1591 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1592 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1593 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1594 PyErr_SetString(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1595 "list indices must be integers");
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1596 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1597 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1598 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1599
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1600 static int
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1601 array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1602 {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1603 if (PyInt_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1604 long i = PyInt_AS_LONG(item);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1605 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1606 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1607 return array_ass_item(self, i, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1608 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1609 else if (PyLong_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1610 long i = PyLong_AsLong(item);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1611 if (i == -1 && PyErr_Occurred())
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1612 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1613 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1614 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1615 return array_ass_item(self, i, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1616 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1617 else if (PySlice_Check(item)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1618 int start, stop, step, slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1619 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1620
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1621 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1622 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1623 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1624 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1625
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1626 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1627 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1628 return array_ass_slice(self, start, stop, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1629
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1630 if (value == NULL) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1631 /* delete slice */
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1632 int cur, i, extra;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1633
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1634 if (slicelength <= 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1635 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1636
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1637 if (step < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1638 stop = start + 1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1639 start = stop + step*(slicelength - 1) - 1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1640 step = -step;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1641 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1642
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1643 for (cur = start, i = 0; i < slicelength - 1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1644 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1645 memmove(self->ob_item + (cur - i)*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1646 self->ob_item + (cur + 1)*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1647 (step - 1) * itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1648 }
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1649 extra = self->ob_size - (cur + 1);
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1650 if (extra > 0) {
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1651 memmove(self->ob_item + (cur - i)*itemsize,
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1652 self->ob_item + (cur + 1)*itemsize,
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1653 extra*itemsize);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1654 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1655
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1656 self->ob_size -= slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1657 self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1658 self->allocated = self->ob_size;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1659
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1660 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1661 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1662 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1663 /* assign slice */
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1664 int cur, i;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1665 arrayobject* av;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1666
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1667 if (!array_Check(value)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1668 PyErr_Format(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1669 "must assign array (not \"%.200s\") to slice",
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1670 value->ob_type->tp_name);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1671 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1672 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1673
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1674 av = (arrayobject*)value;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1675
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1676 if (av->ob_size != slicelength) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1677 PyErr_Format(PyExc_ValueError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1678 "attempt to assign array of size %d to extended slice of size %d",
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1679 av->ob_size, slicelength);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1680 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1681 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1682
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1683 if (!slicelength)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1684 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1685
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1686 /* protect against a[::-1] = a */
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1687 if (self == av) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1688 value = array_slice(av, 0, av->ob_size);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1689 av = (arrayobject*)value;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1690 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1691 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1692 Py_INCREF(value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1693 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1694
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1695 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1696 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1697 memcpy(self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1698 av->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1699 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1700 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1701
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1702 Py_DECREF(value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1703
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1704 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1705 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1706 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1707 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1708 PyErr_SetString(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1709 "list indices must be integers");
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1710 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1711 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1712 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1713
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1714 static PyMappingMethods array_as_mapping = {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1715 (inquiry)array_length,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1716 (binaryfunc)array_subscr,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1717 (objobjargproc)array_ass_subscr
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1718 };
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1719
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1720 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1721 array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1722 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1723 if ( index != 0 ) {
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1724 PyErr_SetString(PyExc_SystemError,
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1725 "Accessing non-existent array segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1726 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1727 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1728 *ptr = (void *)self->ob_item;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1729 return self->ob_size*self->ob_descr->itemsize;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1730 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1731
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1732 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1733 array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1734 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1735 if ( index != 0 ) {
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1736 PyErr_SetString(PyExc_SystemError,
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1737 "Accessing non-existent array segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1738 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1739 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1740 *ptr = (void *)self->ob_item;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1741 return self->ob_size*self->ob_descr->itemsize;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1742 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1743
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1744 static int
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1745 array_buffer_getsegcount(arrayobject *self, int *lenp)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1746 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1747 if ( lenp )
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1748 *lenp = self->ob_size*self->ob_descr->itemsize;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1749 return 1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1750 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1751
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1752 static PySequenceMethods array_as_sequence = {
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1753 (inquiry)array_length, /*sq_length*/
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1754 (binaryfunc)array_concat, /*sq_concat*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1755 (intargfunc)array_repeat, /*sq_repeat*/
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1756 (intargfunc)array_item, /*sq_item*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1757 (intintargfunc)array_slice, /*sq_slice*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1758 (intobjargproc)array_ass_item, /*sq_ass_item*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1759 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1760 (objobjproc)array_contains, /*sq_contains*/
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1761 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1762 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1763 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1764
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1765 static PyBufferProcs array_as_buffer = {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1766 (getreadbufferproc)array_buffer_getreadbuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1767 (getwritebufferproc)array_buffer_getwritebuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1768 (getsegcountproc)array_buffer_getsegcount,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1769 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1770
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1771 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1772 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1773 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1774 char c;
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1775 PyObject *initial = NULL, *it = NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1776 struct arraydescr *descr;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1777
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1778 if (kwds != NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1779 int i = PyObject_Length(kwds);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1780 if (i < 0)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1781 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1782 else if (i > 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1783 PyErr_SetString(PyExc_TypeError,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1784 "array.array constructor takes "
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1785 "no keyword arguments");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1786 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1787 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1788 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1789
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1790 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1791 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1792
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1793 if (!(initial == NULL || PyList_Check(initial)
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1794 || PyString_Check(initial) || PyTuple_Check(initial)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1795 || (c == 'u' && PyUnicode_Check(initial)))) {
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1796 it = PyObject_GetIter(initial);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1797 if (it == NULL)
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1798 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1799 /* We set initial to NULL so that the subsequent code
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1800 will create an empty array of the appropriate type
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1801 and afterwards we can use array_iter_extend to populate
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1802 the array.
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1803 */
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1804 initial = NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1805 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1806 for (descr = descriptors; descr->typecode != '\0'; descr++) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1807 if (descr->typecode == c) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1808 PyObject *a;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1809 int len;
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1810
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1811 if (initial == NULL || !(PyList_Check(initial)
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1812 || PyTuple_Check(initial)))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1813 len = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1814 else
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1815 len = PySequence_Size(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1816
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1817 a = newarrayobject(type, len, descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1818 if (a == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1819 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1820
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1821 if (len > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1822 int i;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1823 for (i = 0; i < len; i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1824 PyObject *v =
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1825 PySequence_GetItem(initial, i);
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1826 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1827 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1828 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1829 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1830 if (setarrayitem(a, i, v) != 0) {
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1831 Py_DECREF(v);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1832 Py_DECREF(a);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1833 return NULL;
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1834 }
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1835 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1836 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1837 } else if (initial != NULL && PyString_Check(initial)) {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30066
diff changeset
1838 PyObject *t_initial = PyTuple_Pack(1,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1839 initial);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1840 PyObject *v =
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1841 array_fromstring((arrayobject *)a,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1842 t_initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1843 Py_DECREF(t_initial);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1844 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1845 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1846 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1847 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1848 Py_DECREF(v);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1849 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1850 } else if (initial != NULL && PyUnicode_Check(initial)) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1851 int n = PyUnicode_GET_DATA_SIZE(initial);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1852 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1853 arrayobject *self = (arrayobject *)a;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1854 char *item = self->ob_item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1855 item = PyMem_Realloc(item, n);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1856 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1857 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1858 Py_DECREF(a);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1859 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1860 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1861 self->ob_item = item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1862 self->ob_size = n / sizeof(Py_UNICODE);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1863 memcpy(item, PyUnicode_AS_DATA(initial), n);
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
1864 self->allocated = self->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1865 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1866 #endif
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1867 }
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1868 if (it != NULL) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1869 if (array_iter_extend((arrayobject *)a, it) == -1) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1870 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1871 Py_DECREF(a);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1872 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1873 }
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1874 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1875 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1876 return a;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1877 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1878 }
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1879 PyErr_SetString(PyExc_ValueError,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1880 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1881 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1882 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1884
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1885 PyDoc_STRVAR(module_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1886 "This module defines an object type which can efficiently represent\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1887 an array of basic values: characters, integers, floating point\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1888 numbers. Arrays are sequence types and behave very much like lists,\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1889 except that the type of objects stored in them is constrained. The\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1890 type is specified at object creation time by using a type code, which\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1891 is a single character. The following type codes are defined:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1892 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1893 Type code C Type Minimum size in bytes \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1894 'c' character 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1895 'b' signed integer 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1896 'B' unsigned integer 1 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1897 'u' Unicode character 2 \n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1898 'h' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1899 'H' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1900 'i' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1901 'I' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1902 'l' signed integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1903 'L' unsigned integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1904 'f' floating point 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1905 'd' floating point 8 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1906 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1907 The constructor is:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1908 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1909 array(typecode [, initializer]) -- create a new array\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1910 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1911
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1912 PyDoc_STRVAR(arraytype_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1913 "array(typecode [, initializer]) -> array\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1914 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1915 Return a new array whose items are restricted by typecode, and\n\
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1916 initialized from the optional initializer value, which must be a list,\n\
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1917 string. or iterable over elements of the appropriate type.\n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1918 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1919 Arrays represent basic values and behave very much like lists, except\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1920 the type of objects stored in them is constrained.\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1921 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1922 Methods:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1923 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1924 append() -- append a new item to the end of the array\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1925 buffer_info() -- return information giving the current memory info\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1926 byteswap() -- byteswap all the items of the array\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1927 count() -- return number of occurences of an object\n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1928 extend() -- extend array by appending multiple elements from an iterable\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1929 fromfile() -- read items from a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1930 fromlist() -- append items from the list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1931 fromstring() -- append items from the string\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1932 index() -- return index of first occurence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1933 insert() -- insert a new item into the array at a provided position\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1934 pop() -- remove and return item (default last)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1935 read() -- DEPRECATED, use fromfile()\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1936 remove() -- remove first occurence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1937 reverse() -- reverse the order of the items in the array\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1938 tofile() -- write all items to a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1939 tolist() -- return the array converted to an ordinary list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1940 tostring() -- return the array converted to a string\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1941 write() -- DEPRECATED, use tofile()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1942 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1943 Attributes:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1944 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1945 typecode -- the typecode character used to create the array\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1946 itemsize -- the length in bytes of one array item\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1947 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1948
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1949 static PyObject *array_iter(arrayobject *ao);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1950
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1951 static PyTypeObject Arraytype = {
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
1952 PyObject_HEAD_INIT(NULL)
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1953 0,
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 21073
diff changeset
1954 "array.array",
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1955 sizeof(arrayobject),
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1956 0,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1957 (destructor)array_dealloc, /* tp_dealloc */
22124
edf05c69bbf3 Remove tp_print.
Martin v. Löwis <martin@v.loewis.de>
parents: 22098
diff changeset
1958 0, /* tp_print */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1959 0, /* tp_getattr */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1960 0, /* tp_setattr */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1961 0, /* tp_compare */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1962 (reprfunc)array_repr, /* tp_repr */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1963 0, /* tp_as _number*/
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1964 &array_as_sequence, /* tp_as _sequence*/
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1965 &array_as_mapping, /* tp_as _mapping*/
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1966 0, /* tp_hash */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1967 0, /* tp_call */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1968 0, /* tp_str */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
1969 PyObject_GenericGetAttr, /* tp_getattro */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1970 0, /* tp_setattro */
18036
a02085871c3f Make it possible to find the use of tp_as_buffer here with a global search.
Tim Peters <tim.peters@gmail.com>
parents: 16268
diff changeset
1971 &array_as_buffer, /* tp_as_buffer*/
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
1972 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1973 arraytype_doc, /* tp_doc */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1974 0, /* tp_traverse */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1975 0, /* tp_clear */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1976 array_richcompare, /* tp_richcompare */
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
1977 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1978 (getiterfunc)array_iter, /* tp_iter */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1979 0, /* tp_iternext */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1980 array_methods, /* tp_methods */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1981 0, /* tp_members */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1982 array_getsets, /* tp_getset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1983 0, /* tp_base */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1984 0, /* tp_dict */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1985 0, /* tp_descr_get */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1986 0, /* tp_descr_set */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1987 0, /* tp_dictoffset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1988 0, /* tp_init */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
1989 PyType_GenericAlloc, /* tp_alloc */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1990 array_new, /* tp_new */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
1991 PyObject_Del, /* tp_free */
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1992 };
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1993
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1994
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1995 /*********************** Array Iterator **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1996
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1997 typedef struct {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1998 PyObject_HEAD
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1999 long index;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2000 arrayobject *ao;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2001 PyObject * (*getitem)(struct arrayobject *, int);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2002 } arrayiterobject;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2003
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2004 static PyTypeObject PyArrayIter_Type;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2005
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2006 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2007
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2008 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2009 array_iter(arrayobject *ao)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2010 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2011 arrayiterobject *it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2012
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2013 if (!array_Check(ao)) {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2014 PyErr_BadInternalCall();
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2015 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2016 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2017
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2018 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2019 if (it == NULL)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2020 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2021
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2022 Py_INCREF(ao);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2023 it->ao = ao;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2024 it->index = 0;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2025 it->getitem = ao->ob_descr->getitem;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2026 PyObject_GC_Track(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2027 return (PyObject *)it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2028 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2029
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2030 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2031 arrayiter_next(arrayiterobject *it)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2032 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2033 assert(PyArrayIter_Check(it));
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2034 if (it->index < it->ao->ob_size)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2035 return (*it->getitem)(it->ao, it->index++);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2036 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2037 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2038
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2039 static void
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2040 arrayiter_dealloc(arrayiterobject *it)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2041 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2042 PyObject_GC_UnTrack(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2043 Py_XDECREF(it->ao);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2044 PyObject_GC_Del(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2045 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2046
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2047 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2048 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2049 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2050 if (it->ao != NULL)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2051 return visit((PyObject *)(it->ao), arg);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2052 return 0;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2053 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2054
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2055 static PyTypeObject PyArrayIter_Type = {
27804
8d1b272c9e71 Patch #676837: Cygwin array module patch
Jason Tishler <jason@tishler.net>
parents: 27128
diff changeset
2056 PyObject_HEAD_INIT(NULL)
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2057 0, /* ob_size */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2058 "arrayiterator", /* tp_name */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2059 sizeof(arrayiterobject), /* tp_basicsize */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2060 0, /* tp_itemsize */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2061 /* methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2062 (destructor)arrayiter_dealloc, /* tp_dealloc */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2063 0, /* tp_print */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2064 0, /* tp_getattr */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2065 0, /* tp_setattr */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2066 0, /* tp_compare */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2067 0, /* tp_repr */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2068 0, /* tp_as_number */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2069 0, /* tp_as_sequence */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2070 0, /* tp_as_mapping */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2071 0, /* tp_hash */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2072 0, /* tp_call */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2073 0, /* tp_str */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2074 PyObject_GenericGetAttr, /* tp_getattro */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2075 0, /* tp_setattro */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2076 0, /* tp_as_buffer */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2077 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2078 0, /* tp_doc */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2079 (traverseproc)arrayiter_traverse, /* tp_traverse */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2080 0, /* tp_clear */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2081 0, /* tp_richcompare */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2082 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28309
diff changeset
2083 PyObject_SelfIter, /* tp_iter */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2084 (iternextfunc)arrayiter_next, /* tp_iternext */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2085 0, /* tp_methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2086 };
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2087
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2088
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2089 /*********************** Install Module **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2090
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2091 /* No functions in array module. */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2092 static PyMethodDef a_methods[] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2093 {NULL, NULL, 0, NULL} /* Sentinel */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2094 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2095
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2096
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24424
diff changeset
2097 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13590
diff changeset
2098 initarray(void)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2099 {
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2100 PyObject *m;
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
2101
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2102 Arraytype.ob_type = &PyType_Type;
27804
8d1b272c9e71 Patch #676837: Cygwin array module patch
Jason Tishler <jason@tishler.net>
parents: 27128
diff changeset
2103 PyArrayIter_Type.ob_type = &PyType_Type;
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2104 m = Py_InitModule3("array", a_methods, module_doc);
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2105
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2106 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2107 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2108 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2109 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
9362
50e0631cf117 No need to issue a fatal error if the PyDict_SetItemString fails; the
Guido van Rossum <guido@python.org>
parents: 9360
diff changeset
2110 /* No need to check the error here, the caller will do that */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2111 }