annotate Modules/arraymodule.c @ 41788:00413f70ce5c

Remove unused variables.
author Walter Dörwald <walter@livinglogic.de>
date Sun, 20 May 2007 12:49:47 +0000
parents bee8d01ef42c
children e4e8b12770e4
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
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
6 #define PY_SSIZE_T_CLEAN
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
7 #include "Python.h"
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
8 #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
9
1797
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
10 #ifdef STDC_HEADERS
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
11 #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
12 #else /* !STDC_HEADERS */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
13 #ifdef HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
14 #include <sys/types.h> /* For size_t */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
15 #endif /* HAVE_SYS_TYPES_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
16 #endif /* !STDC_HEADERS */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 struct arrayobject; /* Forward */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
20 /* 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
21 * 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
22 * 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
23 */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 struct arraydescr {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 int typecode;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 int itemsize;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
1235
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
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 typedef struct arrayobject {
36544
23e23e15e7e7 Bug #1432350: arrayobject should use PyObject_VAR_HEAD
Georg Brandl <georg@python.org>
parents: 36529
diff changeset
32 PyObject_VAR_HEAD
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;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
34 Py_ssize_t 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
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
45 array_resize(arrayobject *self, Py_ssize_t newsize)
31659
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 *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
105 c_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
41685
bab8a9c7f49e Fix the array tests. Only a minor change to the C code was required.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
107 Py_UNICODE buf[1];
bab8a9c7f49e Fix the array tests. Only a minor change to the C code was required.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
108 buf[0] = ((unsigned char *)ap->ob_item)[i];
bab8a9c7f49e Fix the array tests. Only a minor change to the C code was required.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
109 return PyUnicode_FromUnicode(buf, 1);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
113 c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 char x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
116 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
117 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
119 ((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
120 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
123 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
124 b_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
127 if (x >= 128)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 x -= 256;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
129 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
130 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
133 b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
135 short x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
136 /* 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
137 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
138 the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
139 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
140 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
141 else if (x < -128) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
142 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
143 "signed char is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
144 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
145 }
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
146 else if (x > 127) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
147 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
148 "signed char is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
149 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
150 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
152 ((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
153 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
156 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
157 BB_getitem(arrayobject *ap, Py_ssize_t 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
158 {
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 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
160 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
161 }
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
162
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
163 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
164 BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
165 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
166 unsigned char x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
167 /* '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
168 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
169 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
170 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
171 ((char *)ap->ob_item)[i] = x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
172 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
173 }
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
174
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
175 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
176 u_getitem(arrayobject *ap, Py_ssize_t i)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
177 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
178 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
179 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
180
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
181 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
182 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
183 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
184 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
185 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
186
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
187 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
188 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
189 if (len != 1) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
190 PyErr_SetString(PyExc_TypeError,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
191 "array item must be unicode character");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
192 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
193 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
194 if (i >= 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
195 ((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
196 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
197 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
198
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
199 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
200 h_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
202 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
203 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
206 h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 short x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
209 /* '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
210 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
211 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 ((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
214 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
217 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
218 HH_getitem(arrayobject *ap, Py_ssize_t 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
219 {
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 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
221 }
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
222
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
223 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
224 HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
225 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
226 int x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
227 /* 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
228 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
229 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
230 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
231 else if (x < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
232 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
233 "unsigned short is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
234 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
235 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
236 else if (x > USHRT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
237 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
238 "unsigned short is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
239 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
240 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
241 if (i >= 0)
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
242 ((short *)ap->ob_item)[i] = (short)x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
243 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
244 }
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
245
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
246 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
247 i_getitem(arrayobject *ap, Py_ssize_t 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
248 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
249 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
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
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
252 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
253 i_setitem(arrayobject *ap, Py_ssize_t 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
254 {
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
255 int x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
256 /* '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
257 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
258 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
259 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
260 ((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
261 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
262 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
263
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
264 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
265 II_getitem(arrayobject *ap, Py_ssize_t 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
266 {
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 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
268 (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
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
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
271 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
272 II_setitem(arrayobject *ap, Py_ssize_t 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
273 {
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 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
275 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
276 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
277 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
278 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
279 }
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
280 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
281 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
282 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
283 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
284 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
285 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
286 "unsigned int is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
287 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
288 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
289 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
290
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
291 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
292 if (x > UINT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
293 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
294 "unsigned int is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
295 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
296 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
297
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
298 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
299 ((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
300 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
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
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
303 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
304 l_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
306 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
307 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
310 l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 long x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
313 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
314 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 ((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
317 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
320 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
321 LL_getitem(arrayobject *ap, Py_ssize_t 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
322 {
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 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
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
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
326 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
327 LL_setitem(arrayobject *ap, Py_ssize_t 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
328 {
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 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
330 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
331 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
332 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
333 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
334 }
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
335 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
336 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
337 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
338 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
339 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
340 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
341 "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
342 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
343 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
344 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
345
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
346 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
347 if (x > ULONG_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
348 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
349 "unsigned long is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
350 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
351 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
352
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
353 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
354 ((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
355 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
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
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
358 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
359 f_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
361 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
362 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
365 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 float x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
368 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
369 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 ((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
372 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
375 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
376 d_getitem(arrayobject *ap, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
378 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
379 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
382 d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 double x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
385 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
386 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 ((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
389 return 0;
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
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 /* 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
393 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
394 {'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
395 {'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
396 {'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
397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 {'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
399 {'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
400 {'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
401 {'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
402 {'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
403 {'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
404 {'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
405 {'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
406 {'\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
407 };
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
408
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 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
411 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
413 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
414 newarrayobject(PyTypeObject *type, Py_ssize_t 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
415 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 arrayobject *op;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
417 size_t nbytes;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
418
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 if (size < 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
420 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
423
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 nbytes = size * descr->itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 /* Check for overflow */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4875
diff changeset
426 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
427 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
429 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
430 if (op == NULL) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
431 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
433 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
434 if (size <= 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 op->ob_item = NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 else {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
438 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
439 if (op->ob_item == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11711
diff changeset
440 PyObject_Del(op);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
441 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
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 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
445 op->allocated = size;
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
446 op->weakreflist = NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
447 return (PyObject *) op;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
450 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
451 getarrayitem(PyObject *op, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 register arrayobject *ap;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
454 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
455 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
456 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
457 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
458 }
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 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
461 ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 char *items;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
464 Py_ssize_t 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
465 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
466 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 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
470 return -1;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
471
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
472 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
473 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 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
475 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
476 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
477 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
478 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 }
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
480 if (where > n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
481 where = n;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
482 /* 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
483 if (where != n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
484 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
485 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
486 (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
487 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
488 }
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 /* Methods */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 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
493 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
494 {
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
495 if (op->weakreflist != NULL)
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
496 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
497 if (op->ob_item != NULL)
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
498 PyMem_DEL(op->ob_item);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
499 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
500 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
502 static PyObject *
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
503 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
504 {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
505 arrayobject *va, *wa;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
506 PyObject *vi = NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
507 PyObject *wi = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
508 Py_ssize_t i, k;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
509 PyObject *res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
510
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
511 if (!array_Check(v) || !array_Check(w)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
512 Py_INCREF(Py_NotImplemented);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
513 return Py_NotImplemented;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
514 }
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 va = (arrayobject *)v;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
517 wa = (arrayobject *)w;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
518
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
519 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
520 /* Shortcut: if the lengths differ, the arrays differ */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
521 if (op == Py_EQ)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
522 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
523 else
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
524 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
525 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
526 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
527 }
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 /* 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
530 k = 1;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
531 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
532 vi = getarrayitem(v, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
533 wi = getarrayitem(w, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
534 if (vi == NULL || wi == NULL) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
535 Py_XDECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
536 Py_XDECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
537 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
538 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
539 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
540 if (k == 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
541 break; /* Keeping vi and wi alive! */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
542 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
543 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
544 if (k < 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
545 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
546 }
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 if (k) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
549 /* No more items to compare -- compare sizes */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
550 Py_ssize_t vs = va->ob_size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
551 Py_ssize_t 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
552 int cmp;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
553 switch (op) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
554 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
555 case Py_LE: cmp = vs <= ws; break;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
556 case Py_EQ: cmp = vs == ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
557 case Py_NE: cmp = vs != ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
558 case Py_GT: cmp = vs > ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
559 case Py_GE: cmp = vs >= ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
560 default: return NULL; /* cannot happen */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
561 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
562 if (cmp)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
563 res = Py_True;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 else
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
565 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
566 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
567 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
568 }
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 /* 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
571 if (op == Py_EQ) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
572 Py_INCREF(Py_False);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
573 res = Py_False;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
575 else if (op == Py_NE) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
576 Py_INCREF(Py_True);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
577 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
578 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
579 else {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
580 /* 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
581 res = PyObject_RichCompare(vi, wi, op);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
582 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
583 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
584 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
585 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
588 static Py_ssize_t
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
589 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
590 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 return a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
594 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
595 array_item(arrayobject *a, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 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
598 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
599 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
601 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
602 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
604 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
605 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 ilow = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 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
611 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 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 ihigh = ilow;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 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
617 ihigh = a->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
618 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
619 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 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
622 (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
623 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
626 static PyObject *
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
627 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
628 {
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
629 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
630 }
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 PyDoc_STRVAR(copy_doc,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
633 "copy(array)\n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
634 \n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
635 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
636
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
637 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
638 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
639 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
640 Py_ssize_t size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 arrayobject *np;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
642 if (!array_Check(bb)) {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
643 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
644 "can only append array (not \"%.200s\") to array",
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
645 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
646 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 #define b ((arrayobject *)bb)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 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
650 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 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
654 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
655 if (np == NULL) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 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
659 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
660 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
661 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
664
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
665 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
666 array_repeat(arrayobject *a, Py_ssize_t n)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
668 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
669 Py_ssize_t size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
672 Py_ssize_t nbytes;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 if (n < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 n = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 size = a->ob_size * n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
676 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
677 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 p = np->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 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
681 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
682 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
683 p += nbytes;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
685 return (PyObject *) np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 }
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 static int
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
689 array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 char *item;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
692 Py_ssize_t n; /* Size of replacement array */
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
693 Py_ssize_t d; /* Change in size */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 #define b ((arrayobject *)v)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 n = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
697 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
698 n = b->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 if (a == b) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 /* 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
701 int ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 v = array_slice(b, 0, n);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
703 if (!v)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
704 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 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
706 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 return ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 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
710 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 return -1;
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 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 else {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
715 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
716 "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
717 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
718 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 ilow = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 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
723 ilow = a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 ihigh = ilow;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 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
729 ihigh = a->ob_size;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 item = a->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 d = n - (ihigh-ilow);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 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
733 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
734 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
735 (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
736 a->ob_size += d;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
737 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
738 /* Can't fail */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 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
740 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
741 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 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
743 PyMem_RESIZE(item, char,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
744 (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
745 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
746 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 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
750 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
751 (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
752 a->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 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
754 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
755 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 if (n > 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 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
758 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
759 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 #undef b
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
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
764 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 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
767 PyErr_SetString(PyExc_IndexError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
768 "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
769 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 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
773 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
774 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
777 setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
779 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
780 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
781 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
782
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
783 static int
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
784 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
785 {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
786 PyObject *it, *v;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
787
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
788 it = PyObject_GetIter(bb);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
789 if (it == NULL)
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
790 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
791
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
792 while ((v = PyIter_Next(it)) != NULL) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
793 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
794 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
795 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
796 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
797 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
798 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
799 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
800 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
801 if (PyErr_Occurred())
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
802 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
803 return 0;
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
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
806 static int
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
807 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
808 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
809 Py_ssize_t size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
810
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
811 if (!array_Check(bb))
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
812 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
813 #define b ((arrayobject *)bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
814 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
815 PyErr_SetString(PyExc_TypeError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
816 "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
817 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
818 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
819 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
820 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
821 if (self->ob_item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
822 PyObject_Del(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
823 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
824 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
825 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
826 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
827 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
828 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
829 self->allocated = size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
830
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
831 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
832 #undef b
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
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
835 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
836 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
837 {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
838 if (!array_Check(bb)) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
839 PyErr_Format(PyExc_TypeError,
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
840 "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
841 bb->ob_type->tp_name);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
842 return NULL;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
843 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
844 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
845 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
846 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
847 return (PyObject *)self;
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
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
850 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
851 array_inplace_repeat(arrayobject *self, Py_ssize_t n)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
852 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
853 char *items, *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
854 Py_ssize_t size, i;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
855
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
856 if (self->ob_size > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
857 if (n < 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
858 n = 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
859 items = self->ob_item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
860 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
861 if (n == 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
862 PyMem_FREE(items);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
863 self->ob_item = NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
864 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
865 self->allocated = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
866 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
867 else {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
868 PyMem_Resize(items, char, n * size);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
869 if (items == NULL)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
870 return PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
871 p = items;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
872 for (i = 1; i < n; i++) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
873 p += size;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
874 memcpy(p, items, size);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
875 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
876 self->ob_item = items;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
877 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
878 self->allocated = self->ob_size;
22098
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 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
881 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
882 return (PyObject *)self;
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
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
885
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
886 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
887 ins(arrayobject *self, Py_ssize_t where, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 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
890 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
891 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
892 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
893 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
894
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
895 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
896 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
897 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
898 Py_ssize_t count = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
899 Py_ssize_t i;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
900
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
901 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
902 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
903 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
904 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
905 if (cmp > 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
906 count++;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
907 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
908 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
909 }
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
910 return PyInt_FromSsize_t(count);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
911 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
912
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
913 PyDoc_STRVAR(count_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
914 "count(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
915 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
916 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
917
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
918 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
919 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
920 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
921 Py_ssize_t i;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
922
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
923 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
924 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
925 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
926 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
927 if (cmp > 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
928 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
929 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
930 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
931 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
932 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
933 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
934 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
935 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
936
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
937 PyDoc_STRVAR(index_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
938 "index(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
939 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
940 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
941
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
942 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
943 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
944 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
945 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
946 int cmp;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
947
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
948 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
949 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
950 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
951 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
952 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
953 return cmp;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
954 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
955
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
956 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
957 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
958 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
959 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
960
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
961 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
962 PyObject *selfi = getarrayitem((PyObject *)self,i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
963 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
964 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
965 if (cmp > 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
966 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
967 (PyObject *)NULL) != 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
968 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
969 Py_INCREF(Py_None);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
970 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
971 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
972 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
973 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
974 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
975 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
976 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
977 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
978
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
979 PyDoc_STRVAR(remove_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
980 "remove(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
981 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
982 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
983
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
984 static PyObject *
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
985 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
986 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
987 Py_ssize_t i = -1;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
988 PyObject *v;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
989 if (!PyArg_ParseTuple(args, "|n:pop", &i))
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
990 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
991 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
992 /* 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
993 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
994 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
995 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
996 if (i < 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
997 i += self->ob_size;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
998 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
999 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
1000 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1001 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1002 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
1003 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
1004 Py_DECREF(v);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1005 return NULL;
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 return v;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1008 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1009
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1010 PyDoc_STRVAR(pop_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1011 "pop([i])\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1012 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1013 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
1014
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1015 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1016 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
1017 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1018 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
1019 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1020 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
1021 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1022 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1023
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1024 PyDoc_STRVAR(extend_doc,
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1025 "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
1026 \n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1027 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
1028
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1029 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
1030 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
1031 {
36689
11484a72d966 Convert array.array.insert to use Py_ssize_t (like the rest already does.)
Thomas Wouters <thomas@python.org>
parents: 36544
diff changeset
1032 Py_ssize_t i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1033 PyObject *v;
36689
11484a72d966 Convert array.array.insert to use Py_ssize_t (like the rest already does.)
Thomas Wouters <thomas@python.org>
parents: 36544
diff changeset
1034 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 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
1037 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1039 PyDoc_STRVAR(insert_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1040 "insert(i,x)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1041 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1042 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
1043
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1044
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1045 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1046 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
1047 {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1048 PyObject* retval = NULL;
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1049 retval = PyTuple_New(2);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1050 if (!retval)
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1051 return NULL;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1052
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1053 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
1054 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
1055
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1056 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
1057 }
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1058
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1059 PyDoc_STRVAR(buffer_info_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1060 "buffer_info() -> (address, length)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1061 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1062 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
1063 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
1064 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
1065 the buffer length in bytes.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1066
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1067
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1068 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1069 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
1070 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 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
1072 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1074 PyDoc_STRVAR(append_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1075 "append(x)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1076 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1077 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
1078
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1079
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1080 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1081 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
1082 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1084 Py_ssize_t i;
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1085
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 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
1087 case 1:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 case 2:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 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
1091 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 p[0] = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 p[1] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 case 4:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 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
1098 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 char p1 = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 p[0] = p[3];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 p[1] = p[2];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 p[2] = p1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 p[3] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 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
1106 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
1107 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
1108 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
1109 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
1110 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
1111 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
1112 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
1113 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
1114 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
1115 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
1116 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
1117 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
1118 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
1119 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
1120 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1121 break;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 default:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1123 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
1124 "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
1125 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1127 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1128 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1131 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
1132 "byteswap()\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1133 \n\
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1134 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
1135 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
1136
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1137 static PyObject *
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1138 array_reduce(arrayobject *array)
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1139 {
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1140 PyObject *dict, *result;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1141
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1142 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1143 if (dict == NULL) {
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1144 PyErr_Clear();
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1145 dict = Py_None;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1146 Py_INCREF(dict);
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1147 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1148 if (array->ob_size > 0) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1149 result = Py_BuildValue("O(cs#)O",
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1150 array->ob_type,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1151 array->ob_descr->typecode,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1152 array->ob_item,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1153 array->ob_size * array->ob_descr->itemsize,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1154 dict);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1155 } else {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1156 result = Py_BuildValue("O(c)O",
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1157 array->ob_type,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1158 array->ob_descr->typecode,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1159 dict);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1160 }
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1161 Py_DECREF(dict);
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1162 return result;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1163 }
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1164
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1165 PyDoc_STRVAR(array_doc, "Return state information for pickling.");
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1166
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1167 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1168 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
1169 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1170 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1171 register char *p, *q;
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1172 /* little buffer to hold items while swapping */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1173 char tmp[256]; /* 8 is probably enough -- but why skimp */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1174 assert((size_t)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
1175
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 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
1177 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
1178 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
1179 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
1180 p += itemsize, q -= itemsize) {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1181 /* memory areas guaranteed disjoint, so memcpy
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1182 * is safe (& memmove may be slower).
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1183 */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1184 memcpy(tmp, p, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1185 memcpy(p, q, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1186 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
1187 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1189
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1190 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1191 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 }
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1193
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1194 PyDoc_STRVAR(reverse_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1195 "reverse()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1196 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1197 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
1198
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1199 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
1200 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
1201 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1202 PyObject *f;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1203 Py_ssize_t n;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 FILE *fp;
36529
442c0fb3c4ed Use correct format specifier for Py_ssize_t variable to PyArg_ParseTuple().
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1205 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1207 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
1208 if (fp == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1209 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
1210 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 char *item = self->ob_item;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1214 Py_ssize_t 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
1215 size_t nread;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1216 Py_ssize_t newlength;
10283
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1217 size_t newbytes;
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1218 /* 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
1219 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
1220 (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
1221 (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
1222 goto nomem;
d3bece46486e Carefully check for overflow when allocating the memory for fromfile
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1223 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
1224 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
1225 nomem:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1226 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1230 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
1231 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
1232 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
1233 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
1234 if (nread < (size_t)n) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1235 self->ob_size -= (n - nread);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1236 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
1237 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
1238 self->allocated = self->ob_size;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1239 PyErr_SetString(PyExc_EOFError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1240 "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
1241 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1244 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1245 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1246 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1248 PyDoc_STRVAR(fromfile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1249 "fromfile(f, n)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1250 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1251 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
1252 array. Also called as read.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1253
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1254
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1255 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1256 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
1257 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 FILE *fp;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1259
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1260 if (self->ob_size == 0)
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1261 goto done;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1262
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1263 fp = PyFile_AsFile(f);
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1264 if (fp != NULL) {
13942
31b25ac1f019 Use safer comparisons (only matters when sizeof(int) != sizeof(size_t)). fread
Trent Mick <trentm@activestate.com>
parents: 13792
diff changeset
1265 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
1266 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
1267 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
1268 clearerr(fp);
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 }
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1272 else {
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1273 Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1274 /* Write 64K blocks at a time */
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1275 /* XXX Make the block size settable */
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1276 int BLOCKSIZE = 64*1024;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1277 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1278 Py_ssize_t i;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1279 for (i = 0; i < nblocks; i++) {
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1280 char* ptr = self->ob_item + i*BLOCKSIZE;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1281 Py_ssize_t size = BLOCKSIZE;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1282 PyObject *bytes, *res;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1283 if (i*BLOCKSIZE + size > nbytes)
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1284 size = nbytes - i*BLOCKSIZE;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1285 bytes = PyBytes_FromStringAndSize(ptr, size);
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1286 if (bytes == NULL)
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1287 return NULL;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1288 res = PyObject_CallMethod(f, "write", "O",
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1289 bytes);
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1290 Py_DECREF(bytes);
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1291 if (res == NULL)
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1292 return NULL;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1293 }
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1294 }
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1295
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1296 done:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1297 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1298 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1301 PyDoc_STRVAR(tofile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1302 "tofile(f)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1303 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1304 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
1305 write.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1306
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1307
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1308 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1309 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
1310 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1311 Py_ssize_t n;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1312 Py_ssize_t 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
1313
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1314 if (!PyList_Check(list)) {
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1315 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
1316 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1318 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
1319 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 char *item = self->ob_item;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1321 Py_ssize_t i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1322 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
1323 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1324 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 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
1329 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
1330 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
1331 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
1332 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
1333 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
1334 self->ob_size -= n;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1335 PyMem_RESIZE(item, char,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1336 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
1337 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
1338 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
1339 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1343 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1344 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1347 PyDoc_STRVAR(fromlist_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1348 "fromlist(list)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1349 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1350 Append items to array from list.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1351
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1352
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1353 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1354 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
1355 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1356 PyObject *list = PyList_New(self->ob_size);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1357 Py_ssize_t i;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1358
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 if (list == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 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
1362 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
1363 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1364 Py_DECREF(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1367 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
1368 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 return list;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1372 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
1373 "tolist() -> list\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1374 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1375 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
1376
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1377
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1378 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
1379 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
1380 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381 char *str;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1382 Py_ssize_t n;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1383 int itemsize = self->ob_descr->itemsize;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1384 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
1385 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 if (n % itemsize != 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1387 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
1388 "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
1389 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1390 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1391 n = n / itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1392 if (n > 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1393 char *item = self->ob_item;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1394 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
1395 if (item == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1396 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1397 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1398 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399 self->ob_item = item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 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
1401 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
1402 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
1403 str, itemsize*n);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1405 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1406 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1409 PyDoc_STRVAR(fromstring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1410 "fromstring(string)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1411 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1412 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
1413 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
1414
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1415
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1416 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1417 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
1418 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1419 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
1420 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
1421 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1423 PyDoc_STRVAR(tostring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1424 "tostring() -> string\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1425 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1426 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
1427 representation.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1428
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1429
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1430
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1431 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1432 array_fromunicode(arrayobject *self, PyObject *args)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1433 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1434 Py_UNICODE *ustr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1435 Py_ssize_t n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1436
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1437 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
1438 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1439 if (self->ob_descr->typecode != 'u') {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1440 PyErr_SetString(PyExc_ValueError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1441 "fromunicode() may only be called on "
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1442 "type 'u' arrays");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1443 return NULL;
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 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1446 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
1447 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
1448 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1449 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1450 return NULL;
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 self->ob_item = (char *) item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1453 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
1454 self->allocated = self->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1455 memcpy(item + self->ob_size - n,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1456 ustr, n * sizeof(Py_UNICODE));
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1457 }
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 Py_INCREF(Py_None);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1460 return Py_None;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1461 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1462
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1463 PyDoc_STRVAR(fromunicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1464 "fromunicode(ustr)\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1465 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1466 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
1467 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
1468 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
1469 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
1470
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1471
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1472 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1473 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
1474 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1475 if (self->ob_descr->typecode != 'u') {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1476 PyErr_SetString(PyExc_ValueError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1477 "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
1478 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1479 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1480 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
1481 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1482
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1483 PyDoc_STRVAR(tounicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1484 "tounicode() -> unicode\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1485 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1486 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
1487 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
1488 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
1489 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
1490
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1491
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1492
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1493 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1494 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
1495 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1496 char tc = a->ob_descr->typecode;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1497 return PyString_FromStringAndSize(&tc, 1);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1498 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1499
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1500 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1501 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
1502 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1503 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
1504 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1505
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1506 static PyGetSetDef array_getsets [] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1507 {"typecode", (getter) array_get_typecode, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1508 "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
1509 {"itemsize", (getter) array_get_itemsize, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1510 "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
1511 {NULL}
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1512 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1513
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1514 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
1515 {"append", (PyCFunction)array_append, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1516 append_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1517 {"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
1518 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
1519 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1520 byteswap_doc},
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1521 {"__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
1522 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1523 {"count", (PyCFunction)array_count, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1524 count_doc},
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
1525 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1526 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1527 {"extend", (PyCFunction)array_extend, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1528 extend_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1529 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1530 fromfile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1531 {"fromlist", (PyCFunction)array_fromlist, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1532 fromlist_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1533 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1534 fromstring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1535 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1536 fromunicode_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1537 {"index", (PyCFunction)array_index, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1538 index_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1539 {"insert", (PyCFunction)array_insert, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1540 insert_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1541 {"pop", (PyCFunction)array_pop, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1542 pop_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1543 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1544 fromfile_doc},
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1545 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1546 array_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1547 {"remove", (PyCFunction)array_remove, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1548 remove_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1549 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1550 reverse_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1551 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1552 sort_doc},*/
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1553 {"tofile", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1554 tofile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1555 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1556 tolist_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1557 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1558 tostring_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1559 {"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
1560 tounicode_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1561 {"write", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1562 tofile_doc},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1563 {NULL, NULL} /* sentinel */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1564 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1565
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1566 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
1567 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
1568 {
41788
00413f70ce5c Remove unused variables.
Walter Dörwald <walter@livinglogic.de>
parents: 41782
diff changeset
1569 char typecode;
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1570 PyObject *s, *v = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1571 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1572
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1573 len = a->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1574 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
1575 if (len == 0) {
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1576 return PyUnicode_FromFormat("array('%c')", typecode);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577 }
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1578 if (typecode == 'c')
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1579 v = array_tostring(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1580 else if (typecode == 'u')
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1581 v = array_tounicode(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1582 else
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1583 v = array_tolist(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1584
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1585 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1586 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587 return s;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1589
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1590 static PyObject*
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1591 array_subscr(arrayobject* self, PyObject* item)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1592 {
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1593 if (PyIndex_Check(item)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1594 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
1595 if (i==-1 && PyErr_Occurred()) {
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1596 return NULL;
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
1597 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1598 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1599 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1600 return array_item(self, i);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1601 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1602 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1603 Py_ssize_t start, stop, step, slicelength, cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1604 PyObject* result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1605 arrayobject* ar;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1606 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1607
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1608 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1609 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1610 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1611 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1612
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1613 if (slicelength <= 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1614 return newarrayobject(&Arraytype, 0, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1615 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1616 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1617 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1618 if (!result) return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1619
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1620 ar = (arrayobject*)result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1621
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1622 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1623 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1624 memcpy(ar->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1625 self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1626 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1627 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1628
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1629 return result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1630 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1631 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1632 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1633 PyErr_SetString(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1634 "list indices must be integers");
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1635 return NULL;
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 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1638
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1639 static int
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1640 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
1641 {
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1642 if (PyIndex_Check(item)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1643 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
1644 if (i==-1 && PyErr_Occurred())
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1645 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1646 if (i < 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1647 i += self->ob_size;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1648 return array_ass_item(self, i, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1649 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1650 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1651 Py_ssize_t start, stop, step, slicelength;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1652 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1653
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1654 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1655 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1656 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1657 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1658
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1659 /* 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
1660 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1661 return array_ass_slice(self, start, stop, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1662
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1663 if (value == NULL) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1664 /* delete slice */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1665 Py_ssize_t cur, i, extra;
23929
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 (slicelength <= 0)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1668 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1669
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1670 if (step < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1671 stop = start + 1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1672 start = stop + step*(slicelength - 1) - 1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1673 step = -step;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1674 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1675
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1676 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
1677 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1678 memmove(self->ob_item + (cur - i)*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1679 self->ob_item + (cur + 1)*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1680 (step - 1) * itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1681 }
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1682 extra = self->ob_size - (cur + 1);
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1683 if (extra > 0) {
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1684 memmove(self->ob_item + (cur - i)*itemsize,
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1685 self->ob_item + (cur + 1)*itemsize,
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24283
diff changeset
1686 extra*itemsize);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1687 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1688
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1689 self->ob_size -= slicelength;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1690 self->ob_item = (char *)PyMem_REALLOC(self->ob_item,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1691 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
1692 self->allocated = self->ob_size;
23929
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 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1695 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1696 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1697 /* assign slice */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1698 Py_ssize_t cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1699 arrayobject* av;
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 if (!array_Check(value)) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1702 PyErr_Format(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1703 "must assign array (not \"%.200s\") to slice",
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1704 value->ob_type->tp_name);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1705 return -1;
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
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1708 av = (arrayobject*)value;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1709
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1710 if (av->ob_size != slicelength) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1711 PyErr_Format(PyExc_ValueError,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1712 "attempt to assign array of size %ld to extended slice of size %ld",
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1713 /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1714 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1715 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1716
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1717 if (!slicelength)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1718 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1719
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1720 /* protect against a[::-1] = a */
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1721 if (self == av) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1722 value = array_slice(av, 0, av->ob_size);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1723 av = (arrayobject*)value;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1724 if (!av)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1725 return -1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1726 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1727 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1728 Py_INCREF(value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1729 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1730
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1731 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1732 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1733 memcpy(self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1734 av->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1735 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1736 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1737
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1738 Py_DECREF(value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1739
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1740 return 0;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1741 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1742 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1743 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1744 PyErr_SetString(PyExc_TypeError,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1745 "list indices must be integers");
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1746 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1747 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1748 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1749
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1750 static PyMappingMethods array_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1751 (lenfunc)array_length,
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1752 (binaryfunc)array_subscr,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1753 (objobjargproc)array_ass_subscr
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1754 };
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1755
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1756 static const void *emptybuf = "";
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1757
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1758 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1759 array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1760 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1761 if ( index != 0 ) {
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1762 PyErr_SetString(PyExc_SystemError,
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1763 "Accessing non-existent array segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1764 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1765 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1766 *ptr = (void *)self->ob_item;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1767 if (*ptr == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1768 *ptr = emptybuf;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1769 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
1770 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1771
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1772 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1773 array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1774 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1775 if ( index != 0 ) {
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1776 PyErr_SetString(PyExc_SystemError,
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1777 "Accessing non-existent array segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1778 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1779 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1780 *ptr = (void *)self->ob_item;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1781 if (*ptr == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1782 *ptr = emptybuf;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1783 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
1784 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1785
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1786 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1787 array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1788 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1789 if ( lenp )
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1790 *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
1791 return 1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1792 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1793
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1794 static PySequenceMethods array_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1795 (lenfunc)array_length, /*sq_length*/
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
1796 (binaryfunc)array_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1797 (ssizeargfunc)array_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1798 (ssizeargfunc)array_item, /*sq_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1799 (ssizessizeargfunc)array_slice, /*sq_slice*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1800 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1801 (ssizessizeobjargproc)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
1802 (objobjproc)array_contains, /*sq_contains*/
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1803 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1804 (ssizeargfunc)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
1805 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1806
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1807 static PyBufferProcs array_as_buffer = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1808 (readbufferproc)array_buffer_getreadbuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1809 (writebufferproc)array_buffer_getwritebuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1810 (segcountproc)array_buffer_getsegcount,
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
1811 NULL,
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1812 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1813
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1814 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1815 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
1816 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1817 char c;
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1818 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
1819 struct arraydescr *descr;
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34174
diff changeset
1820
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1821 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34174
diff changeset
1822 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1823
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1824 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
1825 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1826
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1827 if (!(initial == NULL || PyList_Check(initial)
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1828 || 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
1829 || (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
1830 it = PyObject_GetIter(initial);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1831 if (it == NULL)
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1832 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1833 /* 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
1834 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
1835 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
1836 the array.
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1837 */
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1838 initial = NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1839 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1840 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
1841 if (descr->typecode == c) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1842 PyObject *a;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1843 Py_ssize_t len;
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1844
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1845 if (initial == NULL || !(PyList_Check(initial)
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1846 || PyTuple_Check(initial)))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1847 len = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1848 else
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1849 len = PySequence_Size(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1850
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1851 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
1852 if (a == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1853 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1854
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1855 if (len > 0) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1856 Py_ssize_t i;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1857 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
1858 PyObject *v =
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1859 PySequence_GetItem(initial, i);
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1860 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1861 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1862 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1863 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1864 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
1865 Py_DECREF(v);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1866 Py_DECREF(a);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1867 return NULL;
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1868 }
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1869 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1870 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1871 } else if (initial != NULL && PyString_Check(initial)) {
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1872 PyObject *t_initial, *v;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1873 t_initial = PyTuple_Pack(1, initial);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1874 if (t_initial == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1875 Py_DECREF(a);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1876 return NULL;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1877 }
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1878 v = array_fromstring((arrayobject *)a,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1879 t_initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1880 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
1881 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1882 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1884 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1885 Py_DECREF(v);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1886 } else if (initial != NULL && PyUnicode_Check(initial)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1887 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1888 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1889 arrayobject *self = (arrayobject *)a;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1890 char *item = self->ob_item;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1891 item = (char *)PyMem_Realloc(item, n);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1892 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1893 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1894 Py_DECREF(a);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1895 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1896 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1897 self->ob_item = item;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1898 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
1899 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
1900 self->allocated = self->ob_size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1901 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1902 }
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1903 if (it != NULL) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1904 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
1905 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1906 Py_DECREF(a);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1907 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1908 }
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1909 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1910 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1911 return a;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1912 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1913 }
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
1914 PyErr_SetString(PyExc_ValueError,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1915 "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
1916 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1917 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1918
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1919
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1920 PyDoc_STRVAR(module_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1921 "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
1922 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
1923 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
1924 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
1925 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
1926 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
1927 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1928 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
1929 'c' character 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1930 'b' signed integer 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1931 'B' unsigned integer 1 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1932 'u' Unicode character 2 \n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1933 'h' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1934 'H' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1935 'i' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1936 'I' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1937 'l' signed integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1938 'L' unsigned integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1939 'f' floating point 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1940 'd' floating point 8 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1941 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1942 The constructor is:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1943 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1944 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
1945 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1946
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1947 PyDoc_STRVAR(arraytype_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1948 "array(typecode [, initializer]) -> array\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1949 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1950 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
1951 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
1952 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
1953 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1954 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
1955 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
1956 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1957 Methods:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1958 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1959 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
1960 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
1961 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
1962 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
1963 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
1964 fromfile() -- read items from a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1965 fromlist() -- append items from the list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1966 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
1967 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
1968 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
1969 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
1970 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
1971 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
1972 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
1973 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
1974 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
1975 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
1976 write() -- DEPRECATED, use tofile()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1977 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1978 Attributes:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1979 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1980 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
1981 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
1982 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1983
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1984 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
1985
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1986 static PyTypeObject Arraytype = {
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
1987 PyObject_HEAD_INIT(NULL)
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1988 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
1989 "array.array",
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1990 sizeof(arrayobject),
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1991 0,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1992 (destructor)array_dealloc, /* tp_dealloc */
22124
edf05c69bbf3 Remove tp_print.
Martin v. Löwis <martin@v.loewis.de>
parents: 22098
diff changeset
1993 0, /* tp_print */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1994 0, /* tp_getattr */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1995 0, /* tp_setattr */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1996 0, /* tp_compare */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1997 (reprfunc)array_repr, /* tp_repr */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1998 0, /* tp_as_number*/
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1999 &array_as_sequence, /* tp_as_sequence*/
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2000 &array_as_mapping, /* tp_as_mapping*/
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2001 0, /* tp_hash */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2002 0, /* tp_call */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2003 0, /* tp_str */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2004 PyObject_GenericGetAttr, /* tp_getattro */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2005 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
2006 &array_as_buffer, /* tp_as_buffer*/
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
2007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2008 arraytype_doc, /* tp_doc */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2009 0, /* tp_traverse */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2010 0, /* tp_clear */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2011 array_richcompare, /* tp_richcompare */
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
2012 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
2013 (getiterfunc)array_iter, /* tp_iter */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2014 0, /* tp_iternext */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2015 array_methods, /* tp_methods */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2016 0, /* tp_members */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2017 array_getsets, /* tp_getset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2018 0, /* tp_base */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2019 0, /* tp_dict */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2020 0, /* tp_descr_get */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2021 0, /* tp_descr_set */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2022 0, /* tp_dictoffset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2023 0, /* tp_init */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2024 PyType_GenericAlloc, /* tp_alloc */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2025 array_new, /* tp_new */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2026 PyObject_Del, /* tp_free */
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2027 };
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2028
27128
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 /*********************** Array Iterator **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2031
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2032 typedef struct {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2033 PyObject_HEAD
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2034 Py_ssize_t index;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2035 arrayobject *ao;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2036 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2037 } arrayiterobject;
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 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
2040
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2041 #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
2042
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2043 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2044 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
2045 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2046 arrayiterobject *it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2047
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2048 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
2049 PyErr_BadInternalCall();
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2050 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2051 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2052
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2053 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
2054 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
2055 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2056
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2057 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
2058 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
2059 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
2060 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
2061 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
2062 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
2063 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2064
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2065 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2066 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
2067 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2068 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
2069 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
2070 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
2071 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2072 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2073
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2074 static void
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2075 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
2076 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2077 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
2078 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
2079 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
2080 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2081
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2082 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2083 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
2084 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
2085 Py_VISIT(it->ao);
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2086 return 0;
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 static PyTypeObject PyArrayIter_Type = {
27804
8d1b272c9e71 Patch #676837: Cygwin array module patch
Jason Tishler <jason@tishler.net>
parents: 27128
diff changeset
2090 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
2091 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
2092 "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
2093 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
2094 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
2095 /* methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2096 (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
2097 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
2098 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
2099 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
2100 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
2101 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
2102 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
2103 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
2104 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
2105 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
2106 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
2107 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
2108 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
2109 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
2110 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
2111 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
2112 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
2113 (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
2114 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
2115 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
2116 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28309
diff changeset
2117 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
2118 (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
2119 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
2120 };
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2121
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2122
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2123 /*********************** Install Module **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2124
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2125 /* No functions in array module. */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2126 static PyMethodDef a_methods[] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2127 {NULL, NULL, 0, NULL} /* Sentinel */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2128 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2129
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2130
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24424
diff changeset
2131 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13590
diff changeset
2132 initarray(void)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2133 {
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2134 PyObject *m;
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
2135
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
2136 if (PyType_Ready(&Arraytype) < 0)
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
2137 return;
27804
8d1b272c9e71 Patch #676837: Cygwin array module patch
Jason Tishler <jason@tishler.net>
parents: 27128
diff changeset
2138 PyArrayIter_Type.ob_type = &PyType_Type;
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2139 m = Py_InitModule3("array", a_methods, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35449
diff changeset
2140 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35449
diff changeset
2141 return;
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2142
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2143 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2144 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
2145 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2146 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
2147 /* 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
2148 }