annotate Modules/arraymodule.c @ 63129:e5cdb12768cb 2.7

Merged revisions 83030 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83030 | antoine.pitrou | 2010-07-21 18:41:31 +0200 (mer., 21 juil. 2010) | 5 lines Issue #5395: check that array.fromfile() re-raises an IOError instead of replacing it with EOFError. (this is only an added test, but 2.x will get a fix too) ........
author Antoine Pitrou <solipsis@pitrou.net>
date Wed, 21 Jul 2010 16:47:28 +0000
parents fd25030223b0
children b906e9e5f2a7
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 */
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38676
diff changeset
13 #ifdef HAVE_SYS_TYPES_H
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
14 #include <sys/types.h> /* For size_t */
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38676
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
25 int typecode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
26 int itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
32 PyObject_VAR_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
33 char *ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
34 Py_ssize_t allocated;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
35 struct arraydescr *ob_descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
42 #define array_CheckExact(op) (Py_TYPE(op) == &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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
47 char *items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
48 size_t _new_size;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
49
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
50 /* Bypass realloc() when a previous overallocation is large enough
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
51 to accommodate the newsize. If the newsize is 16 smaller than the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
52 current size, then proceed with the realloc() to shrink the list.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
53 */
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
54
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
55 if (self->allocated >= newsize &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
56 Py_SIZE(self) < newsize + 16 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
57 self->ob_item != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
58 Py_SIZE(self) = newsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
59 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
60 }
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
61
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
62 /* This over-allocates proportional to the array size, making room
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
63 * for additional growth. The over-allocation is mild, but is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
64 * enough to give linear-time amortized behavior over a long
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
65 * sequence of appends() in the presence of a poorly-performing
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
66 * system realloc().
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
67 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
68 * Note, the pattern starts out the same as for lists but then
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
69 * grows at a smaller rate so that larger arrays only overallocate
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
70 * by about 1/16th -- this is done because arrays are presumed to be more
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
71 * memory critical.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
72 */
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
73
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
74 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
75 items = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
76 /* XXX The following multiplication and division does not optimize away
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
77 like it does for lists since the size is not known at compile time */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
80 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
81 items = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
82 if (items == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
83 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
84 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
85 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
86 self->ob_item = items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
87 Py_SIZE(self) = newsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
88 self->allocated = _new_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
89 return 0;
31659
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
111 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
112 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
113 char x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
114 if (!PyArg_Parse(v, "c;array item must be char", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
115 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
116 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
117 ((char *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
118 return 0;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
121 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
122 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
123 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
124 long x = ((char *)ap->ob_item)[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
125 if (x >= 128)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
126 x -= 256;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
127 return PyInt_FromLong(x);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
131 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
132 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
133 short x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
134 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
135 must use the next size up that is signed ('h') and manually do
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
136 the overflow checking */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
137 if (!PyArg_Parse(v, "h;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
138 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
139 else if (x < -128) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
140 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
141 "signed char is less than minimum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
142 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
143 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
144 else if (x > 127) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
145 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
146 "signed char is greater than maximum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
147 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
148 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
149 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
150 ((char *)ap->ob_item)[i] = (char)x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
151 return 0;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
154 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
155 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
156 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
157 long x = ((unsigned char *)ap->ob_item)[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
158 return PyInt_FromLong(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
159 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
160
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
161 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
162 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
163 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
164 unsigned char x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
166 if (!PyArg_Parse(v, "b;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
167 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
168 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
169 ((char *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
170 return 0;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
171 }
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
172
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
173 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
174 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
175 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
176 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
178 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
179
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
180 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
181 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
182 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
183 Py_UNICODE *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
184 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
185
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
187 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
188 if (len != 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
189 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
190 "array item must be unicode character");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
191 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
192 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
193 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
194 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
195 return 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
196 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
197 #endif
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
208 short x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
209 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
210 if (!PyArg_Parse(v, "h;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
211 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
212 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
213 ((short *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
214 return 0;
1235
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
220 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[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
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
226 int x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
227 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
228 must use the next size up and manually do the overflow checking */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
229 if (!PyArg_Parse(v, "i;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
230 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
231 else if (x < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
232 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
233 "unsigned short is less than minimum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
234 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
235 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
236 else if (x > USHRT_MAX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
237 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
238 "unsigned short is greater than maximum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
239 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
240 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
241 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
242 ((short *)ap->ob_item)[i] = (short)x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
243 return 0;
13068
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
255 int x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
256 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
257 if (!PyArg_Parse(v, "i;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
258 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
259 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
260 ((int *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
261 return 0;
1393
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
267 return PyLong_FromUnsignedLong(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
268 (unsigned long) ((unsigned int *)ap->ob_item)[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
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
274 unsigned long x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
275 if (PyLong_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
276 x = PyLong_AsUnsignedLong(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
277 if (x == (unsigned long) -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
278 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
279 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
280 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
281 long y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
282 if (!PyArg_Parse(v, "l;array item must be integer", &y))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
283 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
284 if (y < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
285 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
286 "unsigned int is less than minimum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
287 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
288 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
291 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
292 if (x > UINT_MAX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
293 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
294 "unsigned int is greater than maximum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
295 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
296 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
297
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
298 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
299 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
300 return 0;
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
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
312 long x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
313 if (!PyArg_Parse(v, "l;array item must be integer", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
314 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
315 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
316 ((long *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
317 return 0;
1235
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
323 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[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
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
329 unsigned long x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
330 if (PyLong_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
331 x = PyLong_AsUnsignedLong(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
332 if (x == (unsigned long) -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
333 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
334 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
335 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
336 long y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
337 if (!PyArg_Parse(v, "l;array item must be integer", &y))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
338 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
339 if (y < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
340 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
341 "unsigned long is less than minimum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
342 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
343 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
346 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
347 if (x > ULONG_MAX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
348 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
349 "unsigned long is greater than maximum");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
350 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
353 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
354 ((unsigned long *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
355 return 0;
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
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
367 float x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
368 if (!PyArg_Parse(v, "f;array item must be float", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
369 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
370 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
371 ((float *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
372 return 0;
1235
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
384 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
385 if (!PyArg_Parse(v, "d;array item must be float", &x))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
386 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
387 if (i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
388 ((double *)ap->ob_item)[i] = x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
389 return 0;
1235
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[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
394 {'c', sizeof(char), c_getitem, c_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
395 {'b', sizeof(char), b_getitem, b_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
398 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
399 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
400 {'h', sizeof(short), h_getitem, h_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
401 {'H', sizeof(short), HH_getitem, HH_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
402 {'i', sizeof(int), i_getitem, i_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
403 {'I', sizeof(int), II_getitem, II_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
404 {'l', sizeof(long), l_getitem, l_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
405 {'L', sizeof(long), LL_getitem, LL_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
406 {'f', sizeof(float), f_getitem, f_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
407 {'d', sizeof(double), d_getitem, d_setitem},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
408 {'\0', 0, 0, 0} /* Sentinel */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 };
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
410
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
411 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
412 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
413 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
415 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
416 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
417 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
418 arrayobject *op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
419 size_t nbytes;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
420
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
421 if (size < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
422 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
423 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
424 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
425
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
426 nbytes = size * descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
427 /* Check for overflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
428 if (nbytes / descr->itemsize != (size_t)size) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
429 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
430 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
431 op = (arrayobject *) type->tp_alloc(type, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
432 if (op == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
433 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
434 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
435 op->ob_descr = descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
436 op->allocated = size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
437 op->weakreflist = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
438 Py_SIZE(op) = size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
439 if (size <= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
440 op->ob_item = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
441 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
442 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
443 op->ob_item = PyMem_NEW(char, nbytes);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
444 if (op->ob_item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
445 Py_DECREF(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
446 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
447 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
448 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
449 return (PyObject *) op;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
452 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
453 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
454 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
455 register arrayobject *ap;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
456 assert(array_Check(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
457 ap = (arrayobject *)op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
458 assert(i>=0 && i<Py_SIZE(ap));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
459 return (*ap->ob_descr->getitem)(ap, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
463 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
464 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
465 char *items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
466 Py_ssize_t n = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
467 if (v == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
468 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
469 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
470 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
471 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
472 return -1;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
473
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
474 if (array_resize(self, n+1) == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
475 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
476 items = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
477 if (where < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
478 where += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
479 if (where < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
480 where = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
481 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
482 if (where > n)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
483 where = n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
484 /* appends don't need to call memmove() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
485 if (where != n)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
486 memmove(items + (where+1)*self->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
487 items + where*self->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
488 (n-where)*self->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
489 return (*self->ob_descr->setitem)(self, where, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 /* Methods */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 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
495 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
496 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
497 if (op->weakreflist != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
498 PyObject_ClearWeakRefs((PyObject *) op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
499 if (op->ob_item != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
500 PyMem_DEL(op->ob_item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
501 Py_TYPE(op)->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
502 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
504 static PyObject *
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
505 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
506 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
507 arrayobject *va, *wa;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
508 PyObject *vi = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
509 PyObject *wi = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
510 Py_ssize_t i, k;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
511 PyObject *res;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
512
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
513 if (!array_Check(v) || !array_Check(w)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
514 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
515 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
516 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
517
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
518 va = (arrayobject *)v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
519 wa = (arrayobject *)w;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
520
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
521 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
522 /* Shortcut: if the lengths differ, the arrays differ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
523 if (op == Py_EQ)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
524 res = Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
525 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
526 res = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
527 Py_INCREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
528 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
529 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
530
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
531 /* Search for the first index where items are different */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
532 k = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
533 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
534 vi = getarrayitem(v, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
535 wi = getarrayitem(w, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
536 if (vi == NULL || wi == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
537 Py_XDECREF(vi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
538 Py_XDECREF(wi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
539 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
540 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
541 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
542 if (k == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
543 break; /* Keeping vi and wi alive! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
544 Py_DECREF(vi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
545 Py_DECREF(wi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
546 if (k < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
547 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
548 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
549
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
550 if (k) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
551 /* No more items to compare -- compare sizes */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
552 Py_ssize_t vs = Py_SIZE(va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
553 Py_ssize_t ws = Py_SIZE(wa);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
554 int cmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
555 switch (op) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
556 case Py_LT: cmp = vs < ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
557 case Py_LE: cmp = vs <= ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
558 case Py_EQ: cmp = vs == ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
559 case Py_NE: cmp = vs != ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
560 case Py_GT: cmp = vs > ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
561 case Py_GE: cmp = vs >= ws; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
562 default: return NULL; /* cannot happen */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
563 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
564 if (cmp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
565 res = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
566 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
567 res = Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
568 Py_INCREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
569 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
570 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
571
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
572 /* We have an item that differs. First, shortcuts for EQ/NE */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
573 if (op == Py_EQ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
574 Py_INCREF(Py_False);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
575 res = Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
576 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
577 else if (op == Py_NE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
578 Py_INCREF(Py_True);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
579 res = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
580 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
581 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
582 /* Compare the final item again using the proper operator */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
583 res = PyObject_RichCompare(vi, wi, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
584 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
585 Py_DECREF(vi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
586 Py_DECREF(wi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
587 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
590 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
591 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
592 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
593 return Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
596 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
597 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
598 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
599 if (i < 0 || i >= Py_SIZE(a)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
600 PyErr_SetString(PyExc_IndexError, "array index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
601 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
602 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
603 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
604 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
606 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
607 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
608 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
609 arrayobject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
610 if (ilow < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
611 ilow = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
612 else if (ilow > Py_SIZE(a))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
613 ilow = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
614 if (ihigh < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
615 ihigh = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
616 if (ihigh < ilow)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
617 ihigh = ilow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
618 else if (ihigh > Py_SIZE(a))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
619 ihigh = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
620 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
621 if (np == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
622 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
623 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
624 (ihigh-ilow) * a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
625 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
628 static PyObject *
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
629 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
630 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
631 return array_slice(a, 0, Py_SIZE(a));
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
632 }
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
633
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
634 PyDoc_STRVAR(copy_doc,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
635 "copy(array)\n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
636 \n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
637 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
638
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
639 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
640 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
641 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
642 Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
643 arrayobject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
644 if (!array_Check(bb)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
645 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
646 "can only append array (not \"%.200s\") to array",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
647 Py_TYPE(bb)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
648 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
649 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 #define b ((arrayobject *)bb)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
651 if (a->ob_descr != b->ob_descr) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
652 PyErr_BadArgument();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
653 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
654 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
655 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
656 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
657 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
658 size = Py_SIZE(a) + Py_SIZE(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
659 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
660 if (np == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
661 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
662 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
663 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
664 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
665 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
666 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
669
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
670 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
671 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
672 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
673 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
674 Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
675 arrayobject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
676 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
677 Py_ssize_t nbytes;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
678 if (n < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
679 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
680 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
681 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
682 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
683 size = Py_SIZE(a) * n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
684 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
685 if (np == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
686 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
687 p = np->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
688 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
689 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
690 memcpy(p, a->ob_item, nbytes);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
691 p += nbytes;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
692 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
693 return (PyObject *) np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
695
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 static int
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
697 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
698 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
699 char *item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
700 Py_ssize_t n; /* Size of replacement array */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
701 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
702 #define b ((arrayobject *)v)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
703 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
704 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
705 else if (array_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
706 n = Py_SIZE(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
707 if (a == b) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
708 /* Special case "a[i:j] = a" -- copy b first */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
709 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
710 v = array_slice(b, 0, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
711 if (!v)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
712 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
713 ret = array_ass_slice(a, ilow, ihigh, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
714 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
715 return ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
716 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
717 if (b->ob_descr != a->ob_descr) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
718 PyErr_BadArgument();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
719 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
720 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
721 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
722 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
723 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
724 "can only assign array (not \"%.200s\") to array slice",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
725 Py_TYPE(v)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
726 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
727 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
728 if (ilow < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
729 ilow = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
730 else if (ilow > Py_SIZE(a))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
731 ilow = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
732 if (ihigh < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
733 ihigh = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
734 if (ihigh < ilow)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
735 ihigh = ilow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
736 else if (ihigh > Py_SIZE(a))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
737 ihigh = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
738 item = a->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
739 d = n - (ihigh-ilow);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
740 if (d < 0) { /* Delete -d items */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
741 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
742 item + ihigh*a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
743 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
744 Py_SIZE(a) += d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
745 PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
746 /* Can't fail */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
747 a->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
748 a->allocated = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
749 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
750 else if (d > 0) { /* Insert d items */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
751 PyMem_RESIZE(item, char,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
752 (Py_SIZE(a) + d)*a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
753 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
754 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
755 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
756 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
757 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
758 item + ihigh*a->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
759 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
760 a->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
761 Py_SIZE(a) += d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
762 a->allocated = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
763 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
764 if (n > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
765 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
766 n*b->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
767 return 0;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
772 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
773 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
774 if (i < 0 || i >= Py_SIZE(a)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
775 PyErr_SetString(PyExc_IndexError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
776 "array assignment index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
777 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
778 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
779 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
780 return array_ass_slice(a, i, i+1, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
781 return (*a->ob_descr->setitem)(a, i, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
783
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
785 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
786 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
787 assert(array_Check(a));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
788 return array_ass_item((arrayobject *)a, i, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
790
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
791 static int
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
792 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
793 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
794 PyObject *it, *v;
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
795
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
796 it = PyObject_GetIter(bb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
797 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
798 return -1;
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
799
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
800 while ((v = PyIter_Next(it)) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
801 if (ins1(self, (int) Py_SIZE(self), v) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
802 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
803 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
804 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
805 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
806 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
807 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
808 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
809 if (PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
810 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
811 return 0;
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
812 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
813
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
814 static int
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
815 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
816 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
817 Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
818 char *old_item;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
819
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
820 if (!array_Check(bb))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
821 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
822 #define b ((arrayobject *)bb)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
823 if (self->ob_descr != b->ob_descr) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
824 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
825 "can only extend with array of same kind");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
826 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
827 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
828 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
829 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
830 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
831 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
832 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
833 size = Py_SIZE(self) + Py_SIZE(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
834 old_item = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
835 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
836 if (self->ob_item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
837 self->ob_item = old_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
838 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
839 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
840 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
841 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
842 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
843 Py_SIZE(self) = size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
844 self->allocated = size;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
845
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
846 return 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
847 #undef b
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 *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
851 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
852 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
853 if (!array_Check(bb)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
854 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
855 "can only extend array with array (not \"%.200s\")",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
856 Py_TYPE(bb)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
857 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
858 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
859 if (array_do_extend(self, bb) == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
860 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
861 Py_INCREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
862 return (PyObject *)self;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
863 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
864
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
865 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
866 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
867 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
868 char *items, *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
869 Py_ssize_t size, i;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
870
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
871 if (Py_SIZE(self) > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
872 if (n < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
873 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
874 items = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
875 if ((self->ob_descr->itemsize != 0) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
876 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
877 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
878 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
879 size = Py_SIZE(self) * self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
880 if (n == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
881 PyMem_FREE(items);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
882 self->ob_item = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
883 Py_SIZE(self) = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
884 self->allocated = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
885 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
886 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
887 if (size > PY_SSIZE_T_MAX / n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
888 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
889 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
890 PyMem_RESIZE(items, char, n * size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
891 if (items == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
892 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
893 p = items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
894 for (i = 1; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
895 p += size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
896 memcpy(p, items, size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
897 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
898 self->ob_item = items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
899 Py_SIZE(self) *= n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
900 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
901 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
902 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
903 Py_INCREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
904 return (PyObject *)self;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
905 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
906
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
907
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
908 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
909 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
910 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
911 if (ins1(self, where, v) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
912 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
913 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
914 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
916
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
917 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
918 array_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
919 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
920 Py_ssize_t count = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
923 for (i = 0; i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
924 PyObject *selfi = getarrayitem((PyObject *)self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
925 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
926 Py_DECREF(selfi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
927 if (cmp > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
928 count++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
929 else if (cmp < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
930 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
931 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
932 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
933 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
934
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
935 PyDoc_STRVAR(count_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
936 "count(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
937 \n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
938 Return number of occurrences 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
939
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
940 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
941 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
942 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
943 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
944
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
945 for (i = 0; i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
946 PyObject *selfi = getarrayitem((PyObject *)self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
947 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
948 Py_DECREF(selfi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
949 if (cmp > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
950 return PyInt_FromLong((long)i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
951 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
952 else if (cmp < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
953 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
954 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
955 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
956 return NULL;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
957 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
958
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
959 PyDoc_STRVAR(index_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
960 "index(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
961 \n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
962 Return index of first occurrence 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
963
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
964 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
965 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
966 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
967 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
968 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
969
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
970 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
971 PyObject *selfi = getarrayitem((PyObject *)self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
972 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
973 Py_DECREF(selfi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
974 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
975 return cmp;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
976 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
977
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
978 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
979 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
980 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
981 int i;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
982
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
983 for (i = 0; i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
984 PyObject *selfi = getarrayitem((PyObject *)self,i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
985 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
986 Py_DECREF(selfi);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
987 if (cmp > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
988 if (array_ass_slice(self, i, i+1,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
989 (PyObject *)NULL) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
990 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
991 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
992 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
993 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
994 else if (cmp < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
995 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
996 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
997 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
998 return NULL;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
999 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1000
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1001 PyDoc_STRVAR(remove_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1002 "remove(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1003 \n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
1004 Remove the first occurrence 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
1005
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1006 static PyObject *
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1007 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
1008 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1009 Py_ssize_t i = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1010 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1011 if (!PyArg_ParseTuple(args, "|n:pop", &i))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1012 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1013 if (Py_SIZE(self) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1014 /* Special-case most common failure cause */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1015 PyErr_SetString(PyExc_IndexError, "pop from empty array");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1016 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1017 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1018 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1019 i += Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1020 if (i < 0 || i >= Py_SIZE(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1021 PyErr_SetString(PyExc_IndexError, "pop index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1022 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1023 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1024 v = getarrayitem((PyObject *)self,i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1025 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1026 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1027 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1028 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1029 return v;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1030 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1031
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1032 PyDoc_STRVAR(pop_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1033 "pop([i])\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1034 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1035 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
1036
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1037 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1038 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
1039 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1040 if (array_do_extend(self, bb) == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1041 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1042 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1043 return Py_None;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1044 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1045
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1046 PyDoc_STRVAR(extend_doc,
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1047 "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
1048 \n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1049 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
1050
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1051 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
1052 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
1053 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1054 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1055 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1056 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1057 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1058 return ins(self, i, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1061 PyDoc_STRVAR(insert_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1062 "insert(i,x)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1063 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1064 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
1065
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1066
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1067 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1068 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
1069 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1070 PyObject* retval = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1071 retval = PyTuple_New(2);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1072 if (!retval)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1073 return NULL;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1074
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1075 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1076 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_SIZE(self))));
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1077
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1078 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
1079 }
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1080
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1081 PyDoc_STRVAR(buffer_info_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1082 "buffer_info() -> (address, length)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1083 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1084 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
1085 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
1086 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
1087 the buffer length in bytes.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1088
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1089
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1090 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1091 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
1092 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1093 return ins(self, (int) Py_SIZE(self), v);
1235
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1096 PyDoc_STRVAR(append_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1097 "append(x)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1098 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1099 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
1100
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1101
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1102 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1103 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
1104 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1105 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1106 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
1107
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1108 switch (self->ob_descr->itemsize) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1109 case 1:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1110 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1111 case 2:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1112 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1113 char p0 = p[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1114 p[0] = p[1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1115 p[1] = p0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1116 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1117 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1118 case 4:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1119 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1120 char p0 = p[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1121 char p1 = p[1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1122 p[0] = p[3];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1123 p[1] = p[2];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1124 p[2] = p1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1125 p[3] = p0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1126 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1127 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1128 case 8:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1129 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1130 char p0 = p[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1131 char p1 = p[1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1132 char p2 = p[2];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1133 char p3 = p[3];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1134 p[0] = p[7];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1135 p[1] = p[6];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1136 p[2] = p[5];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1137 p[3] = p[4];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1138 p[4] = p3;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1139 p[5] = p2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1140 p[6] = p1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1141 p[7] = p0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1142 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1143 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1144 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1145 PyErr_SetString(PyExc_RuntimeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1146 "don't know how to byteswap this array type");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1147 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1148 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1149 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1150 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1152
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1153 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
1154 "byteswap()\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1155 \n\
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1156 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
1157 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
1158
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1159 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1160 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
1161 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1162 register Py_ssize_t itemsize = self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1163 register char *p, *q;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1164 /* little buffer to hold items while swapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1165 char tmp[256]; /* 8 is probably enough -- but why skimp */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1166 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
1167
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1168 if (Py_SIZE(self) > 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1169 for (p = self->ob_item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1170 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1171 p < q;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1172 p += itemsize, q -= itemsize) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1173 /* memory areas guaranteed disjoint, so memcpy
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1174 * is safe (& memmove may be slower).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1175 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1176 memcpy(tmp, p, itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1177 memcpy(p, q, itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1178 memcpy(q, tmp, itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1179 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1180 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1181
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1182 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1183 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 }
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1185
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1186 PyDoc_STRVAR(reverse_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1187 "reverse()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1188 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1189 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
1190
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1191 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
1192 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
1193 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1194 PyObject *f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1195 Py_ssize_t n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1196 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1197 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1198 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1199 fp = PyFile_AsFile(f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1200 if (fp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1201 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1202 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1203 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1204 if (n > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1205 char *item = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1206 Py_ssize_t itemsize = self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1207 size_t nread;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1208 Py_ssize_t newlength;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1209 size_t newbytes;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1210 /* Be careful here about overflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1211 if ((newlength = Py_SIZE(self) + n) <= 0 ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1212 (newbytes = newlength * itemsize) / itemsize !=
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1213 (size_t)newlength)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1214 goto nomem;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1215 PyMem_RESIZE(item, char, newbytes);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1216 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1217 nomem:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1218 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1219 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1220 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1221 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1222 Py_SIZE(self) += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1223 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1224 nread = fread(item + (Py_SIZE(self) - n) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1225 itemsize, n, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1226 if (nread < (size_t)n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1227 Py_SIZE(self) -= (n - nread);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1228 PyMem_RESIZE(item, char, Py_SIZE(self)*itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1229 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1230 self->allocated = Py_SIZE(self);
63129
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1231 if (ferror(fp)) {
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1232 PyErr_SetFromErrno(PyExc_IOError);
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1233 clearerr(fp);
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1234 }
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1235 else {
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1236 PyErr_SetString(PyExc_EOFError,
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1237 "not enough items in file");
e5cdb12768cb Merged revisions 83030 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
1238 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1239 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1240 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1241 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1242 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1243 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1246 PyDoc_STRVAR(fromfile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1247 "fromfile(f, n)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1248 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1249 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
1250 array. Also called as read.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1251
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1252
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1253 static PyObject *
46077
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1254 array_fromfile_as_read(arrayobject *self, PyObject *args)
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1255 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1256 if (PyErr_WarnPy3k("array.read() not supported in 3.x; "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1257 "use array.fromfile()", 1) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1258 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1259 return array_fromfile(self, args);
46077
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1260 }
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1261
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1262
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1263 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1264 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
1265 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1266 FILE *fp;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1267
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1268 fp = PyFile_AsFile(f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1269 if (fp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1270 PyErr_SetString(PyExc_TypeError, "arg must be open file");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1271 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1272 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1273 if (self->ob_size > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1274 if (fwrite(self->ob_item, self->ob_descr->itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1275 self->ob_size, fp) != (size_t)self->ob_size) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1276 PyErr_SetFromErrno(PyExc_IOError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1277 clearerr(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1278 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1279 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1280 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1281 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1282 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1285 PyDoc_STRVAR(tofile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1286 "tofile(f)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1287 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1288 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
1289 write.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1290
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1291
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1292 static PyObject *
46077
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1293 array_tofile_as_write(arrayobject *self, PyObject *f)
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1294 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1295 if (PyErr_WarnPy3k("array.write() not supported in 3.x; "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1296 "use array.tofile()", 1) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1297 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1298 return array_tofile(self, f);
46077
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1299 }
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1300
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1301
ae659ff0528b #2359: add Py3k warning for array.read/array.write.
Georg Brandl <georg@python.org>
parents: 44350
diff changeset
1302 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1303 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
1304 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1305 Py_ssize_t n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1306 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
1307
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1308 if (!PyList_Check(list)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1309 PyErr_SetString(PyExc_TypeError, "arg must be list");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1310 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1311 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1312 n = PyList_Size(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1313 if (n > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1314 char *item = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1315 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1316 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1317 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1318 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1319 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1320 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1321 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1322 Py_SIZE(self) += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1323 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1324 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1325 PyObject *v = PyList_GetItem(list, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1326 if ((*self->ob_descr->setitem)(self,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1327 Py_SIZE(self) - n + i, v) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1328 Py_SIZE(self) -= n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1329 if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1330 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1331 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1332 PyMem_RESIZE(item, char,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1333 Py_SIZE(self) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1334 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1335 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1336 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1337 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1338 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1339 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1340 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1341 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1344 PyDoc_STRVAR(fromlist_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1345 "fromlist(list)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1346 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1347 Append items to array from list.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1348
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1349
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1350 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1351 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
1352 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1353 PyObject *list = PyList_New(Py_SIZE(self));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1354 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
1355
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1356 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1357 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1358 for (i = 0; i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1359 PyObject *v = getarrayitem((PyObject *)self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1360 if (v == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1361 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1362 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1363 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1364 PyList_SetItem(list, i, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1365 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1366 return list;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1369 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
1370 "tolist() -> list\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1371 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1372 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
1373
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1374
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1375 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
1376 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
1377 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1378 char *str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1379 Py_ssize_t n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1380 int itemsize = self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1381 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1382 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1383 if (n % itemsize != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1384 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1385 "string length not a multiple of item size");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1386 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1387 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1388 n = n / itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1389 if (n > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1390 char *item = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1391 if ((n > PY_SSIZE_T_MAX - Py_SIZE(self)) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1392 ((Py_SIZE(self) + n) > PY_SSIZE_T_MAX / itemsize)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1393 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1394 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1395 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1396 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1397 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1398 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1399 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1400 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1401 Py_SIZE(self) += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1402 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1403 memcpy(item + (Py_SIZE(self) - n) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1404 str, itemsize*n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1405 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1406 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1407 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1410 PyDoc_STRVAR(fromstring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1411 "fromstring(string)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1412 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1413 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
1414 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
1415
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1416
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1417 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1418 array_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
1419 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1420 if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1421 return PyString_FromStringAndSize(self->ob_item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1422 Py_SIZE(self) * self->ob_descr->itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1423 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1424 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1425 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1427
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1428 PyDoc_STRVAR(tostring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1429 "tostring() -> string\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1430 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1431 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
1432 representation.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1433
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1434
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1435
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1436 #ifdef Py_USING_UNICODE
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1437 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1438 array_fromunicode(arrayobject *self, PyObject *args)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1439 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1440 Py_UNICODE *ustr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1441 Py_ssize_t n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1442
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1443 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1444 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1445 if (self->ob_descr->typecode != 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1446 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1447 "fromunicode() may only be called on "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1448 "type 'u' arrays");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1449 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1450 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1451 if (n > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1452 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1453 if (Py_SIZE(self) > PY_SSIZE_T_MAX - n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1454 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1455 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1456 PyMem_RESIZE(item, Py_UNICODE, Py_SIZE(self) + n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1457 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1458 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1459 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1460 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1461 self->ob_item = (char *) item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1462 Py_SIZE(self) += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1463 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1464 memcpy(item + Py_SIZE(self) - n,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1465 ustr, n * sizeof(Py_UNICODE));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1466 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1467
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1468 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1469 return Py_None;
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1472 PyDoc_STRVAR(fromunicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1473 "fromunicode(ustr)\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1474 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1475 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
1476 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
1477 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
1478 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
1479
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1480
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1481 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1482 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
1483 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1484 if (self->ob_descr->typecode != 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1485 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1486 "tounicode() may only be called on type 'u' arrays");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1487 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1488 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1489 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1492 PyDoc_STRVAR(tounicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1493 "tounicode() -> unicode\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1494 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1495 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
1496 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
1497 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
1498 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
1499
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1500 #endif /* Py_USING_UNICODE */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1501
55260
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1502 static PyObject *
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1503 array_reduce(arrayobject *array)
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1504 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1505 PyObject *dict, *result, *list;
55260
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1506
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1507 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1508 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1509 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1510 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1511 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1512 dict = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1513 Py_INCREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1514 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1515 /* Unlike in Python 3.x, we never use the more efficient memory
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1516 * representation of an array for pickling. This is unfortunately
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1517 * necessary to allow array objects to be unpickled by Python 3.x,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1518 * since str objects from 2.x are always decoded to unicode in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1519 * Python 3.x.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1520 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1521 list = array_tolist(array, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1522 if (list == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1523 Py_DECREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1524 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1525 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1526 result = Py_BuildValue(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1527 "O(cO)O", Py_TYPE(array), array->ob_descr->typecode, list, dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1528 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1529 Py_DECREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1530 return result;
55260
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1531 }
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1532
3a316243bd6c Issue #2389: Pickle array objects using a list representation for portability
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55049
diff changeset
1533 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1534
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1535 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1536 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
1537 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1538 char tc = a->ob_descr->typecode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1539 return PyString_FromStringAndSize(&tc, 1);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1540 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1541
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1542 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1543 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
1544 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1545 return PyInt_FromLong((long)a->ob_descr->itemsize);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1546 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1547
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1548 static PyGetSetDef array_getsets [] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1549 {"typecode", (getter) array_get_typecode, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1550 "the typecode character used to create the array"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1551 {"itemsize", (getter) array_get_itemsize, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1552 "the size, in bytes, of one array item"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1553 {NULL}
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1554 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1555
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47672
diff changeset
1556 static PyMethodDef array_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1557 {"append", (PyCFunction)array_append, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1558 append_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1559 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1560 buffer_info_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1561 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1562 byteswap_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1563 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1564 copy_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1565 {"count", (PyCFunction)array_count, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1566 count_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1567 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1568 copy_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1569 {"extend", (PyCFunction)array_extend, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1570 extend_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1571 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1572 fromfile_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1573 {"fromlist", (PyCFunction)array_fromlist, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1574 fromlist_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1575 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1576 fromstring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1577 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1578 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1579 fromunicode_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1580 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1581 {"index", (PyCFunction)array_index, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1582 index_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1583 {"insert", (PyCFunction)array_insert, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1584 insert_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1585 {"pop", (PyCFunction)array_pop, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1586 pop_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1587 {"read", (PyCFunction)array_fromfile_as_read, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1588 fromfile_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1589 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1590 reduce_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1591 {"remove", (PyCFunction)array_remove, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1592 remove_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1593 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1594 reverse_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1595 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1596 sort_doc},*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1597 {"tofile", (PyCFunction)array_tofile, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1598 tofile_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1599 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1600 tolist_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1601 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1602 tostring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1603 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1604 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1605 tounicode_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1606 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1607 {"write", (PyCFunction)array_tofile_as_write, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1608 tofile_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1609 {NULL, NULL} /* sentinel */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1610 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1611
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1612 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
1613 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
1614 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1615 char buf[256], typecode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1616 PyObject *s, *t, *v = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1617 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1618
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1619 len = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1620 typecode = a->ob_descr->typecode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1621 if (len == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1622 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1623 return PyString_FromString(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1624 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1625
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1626 if (typecode == 'c')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1627 v = array_tostring(a, NULL);
23433
fb64f21176af array_tounicode isn't defined in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents: 23365
diff changeset
1628 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1629 else if (typecode == 'u')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1630 v = array_tounicode(a, NULL);
23433
fb64f21176af array_tounicode isn't defined in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents: 23365
diff changeset
1631 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1632 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1633 v = array_tolist(a, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1634 t = PyObject_Repr(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1635 Py_XDECREF(v);
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1636
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1637 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1638 s = PyString_FromString(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1639 PyString_ConcatAndDel(&s, t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1640 PyString_ConcatAndDel(&s, PyString_FromString(")"));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1641 return s;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1642 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1643
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1644 static PyObject*
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1645 array_subscr(arrayobject* self, PyObject* item)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1646 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1647 if (PyIndex_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1648 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1649 if (i==-1 && PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1650 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1651 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1652 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1653 i += Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1654 return array_item(self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1655 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1656 else if (PySlice_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1657 Py_ssize_t start, stop, step, slicelength, cur, i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1658 PyObject* result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1659 arrayobject* ar;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1660 int itemsize = self->ob_descr->itemsize;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1661
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1662 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1663 &start, &stop, &step, &slicelength) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1664 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1665 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1666
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1667 if (slicelength <= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1668 return newarrayobject(&Arraytype, 0, self->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1669 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1670 else if (step == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1671 PyObject *result = newarrayobject(&Arraytype,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1672 slicelength, self->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1673 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1674 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1675 memcpy(((arrayobject *)result)->ob_item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1676 self->ob_item + start * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1677 slicelength * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1678 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1679 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1680 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1681 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1682 if (!result) return NULL;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1683
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1684 ar = (arrayobject*)result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1685
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1686 for (cur = start, i = 0; i < slicelength;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1687 cur += step, i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1688 memcpy(ar->ob_item + i*itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1689 self->ob_item + cur*itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1690 itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1691 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1692
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1693 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1694 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1695 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1696 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1697 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1698 "array indices must be integers");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1699 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1700 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1701 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1702
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1703 static int
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1704 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
1705 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1706 Py_ssize_t start, stop, step, slicelength, needed;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1707 arrayobject* other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1708 int itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1709
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1710 if (PyIndex_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1711 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
1712
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1713 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1714 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1715 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1716 i += Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1717 if (i < 0 || i >= Py_SIZE(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1718 PyErr_SetString(PyExc_IndexError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1719 "array assignment index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1720 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1721 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1722 if (value == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1723 /* Fall through to slice assignment */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1724 start = i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1725 stop = i + 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1726 step = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1727 slicelength = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1728 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1729 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1730 return (*self->ob_descr->setitem)(self, i, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1731 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1732 else if (PySlice_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1733 if (PySlice_GetIndicesEx((PySliceObject *)item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1734 Py_SIZE(self), &start, &stop,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1735 &step, &slicelength) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1736 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1737 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1738 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1739 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1740 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1741 "array indices must be integer");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1742 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1743 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1744 if (value == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1745 other = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1746 needed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1747 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1748 else if (array_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1749 other = (arrayobject *)value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1750 needed = Py_SIZE(other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1751 if (self == other) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1752 /* Special case "self[i:j] = self" -- copy self first */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1753 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1754 value = array_slice(other, 0, needed);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1755 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1756 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1757 ret = array_ass_subscr(self, item, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1758 Py_DECREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1759 return ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1760 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1761 if (other->ob_descr != self->ob_descr) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1762 PyErr_BadArgument();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1763 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1764 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1765 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1766 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1767 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1768 "can only assign array (not \"%.200s\") to array slice",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1769 Py_TYPE(value)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1770 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1771 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1772 itemsize = self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1773 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1774 if ((step > 0 && stop < start) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1775 (step < 0 && stop > start))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1776 stop = start;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1777 if (step == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1778 if (slicelength > needed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1779 memmove(self->ob_item + (start + needed) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1780 self->ob_item + stop * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1781 (Py_SIZE(self) - stop) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1782 if (array_resize(self, Py_SIZE(self) +
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1783 needed - slicelength) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1784 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1785 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1786 else if (slicelength < needed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1787 if (array_resize(self, Py_SIZE(self) +
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1788 needed - slicelength) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1789 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1790 memmove(self->ob_item + (start + needed) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1791 self->ob_item + stop * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1792 (Py_SIZE(self) - start - needed) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1793 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1794 if (needed > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1795 memcpy(self->ob_item + start * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1796 other->ob_item, needed * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1797 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1798 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1799 else if (needed == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1800 /* Delete slice */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1801 size_t cur;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1802 Py_ssize_t i;
58561
c57998e3be33 Issue #7788: Fix a crash produced by deleting a list slice with huge
Mark Dickinson <dickinsm@gmail.com>
parents: 55260
diff changeset
1803
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1804 if (step < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1805 stop = start + 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1806 start = stop + step * (slicelength - 1) - 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1807 step = -step;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1808 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1809 for (cur = start, i = 0; i < slicelength;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1810 cur += step, i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1811 Py_ssize_t lim = step - 1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1812
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1813 if (cur + step >= (size_t)Py_SIZE(self))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1814 lim = Py_SIZE(self) - cur - 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1815 memmove(self->ob_item + (cur - i) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1816 self->ob_item + (cur + 1) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1817 lim * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1818 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1819 cur = start + slicelength * step;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1820 if (cur < (size_t)Py_SIZE(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1821 memmove(self->ob_item + (cur-slicelength) * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1822 self->ob_item + cur * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1823 (Py_SIZE(self) - cur) * itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1824 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1825 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1826 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1827 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1828 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1829 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1830 Py_ssize_t cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1831
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1832 if (needed != slicelength) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1833 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1834 "attempt to assign array of size %zd "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1835 "to extended slice of size %zd",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1836 needed, slicelength);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1837 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1838 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1839 for (cur = start, i = 0; i < slicelength;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1840 cur += step, i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1841 memcpy(self->ob_item + cur * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1842 other->ob_item + i * itemsize,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1843 itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1844 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1845 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1846 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1847 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1848
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1849 static PyMappingMethods array_as_mapping = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1850 (lenfunc)array_length,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1851 (binaryfunc)array_subscr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1852 (objobjargproc)array_ass_subscr
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1853 };
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1854
41361
4a03860ae08b Array module's buffer interface can now handle empty arrays.
Raymond Hettinger <python@rcn.com>
parents: 41359
diff changeset
1855 static const void *emptybuf = "";
4a03860ae08b Array module's buffer interface can now handle empty arrays.
Raymond Hettinger <python@rcn.com>
parents: 41359
diff changeset
1856
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1857 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1858 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
1859 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1860 if ( index != 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1861 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1862 "Accessing non-existent array segment");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1863 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1864 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1865 *ptr = (void *)self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1866 if (*ptr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1867 *ptr = emptybuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1868 return Py_SIZE(self)*self->ob_descr->itemsize;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1869 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1870
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1871 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1872 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
1873 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1874 if ( index != 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1875 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1876 "Accessing non-existent array segment");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1877 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1878 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1879 *ptr = (void *)self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1880 if (*ptr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1881 *ptr = emptybuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1882 return Py_SIZE(self)*self->ob_descr->itemsize;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1883 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1884
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1885 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1886 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
1887 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1888 if ( lenp )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1889 *lenp = Py_SIZE(self)*self->ob_descr->itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1890 return 1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1891 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1892
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1893 static PySequenceMethods array_as_sequence = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1894 (lenfunc)array_length, /*sq_length*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1895 (binaryfunc)array_concat, /*sq_concat*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1896 (ssizeargfunc)array_repeat, /*sq_repeat*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1897 (ssizeargfunc)array_item, /*sq_item*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1898 (ssizessizeargfunc)array_slice, /*sq_slice*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1899 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1900 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1901 (objobjproc)array_contains, /*sq_contains*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1902 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1903 (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
1904 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1905
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1906 static PyBufferProcs array_as_buffer = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1907 (readbufferproc)array_buffer_getreadbuf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1908 (writebufferproc)array_buffer_getwritebuf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1909 (segcountproc)array_buffer_getsegcount,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1910 NULL,
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1911 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1912
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1913 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1914 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
1915 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1916 char c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1917 PyObject *initial = NULL, *it = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1918 struct arraydescr *descr;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1919
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1920 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1921 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1922
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1923 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1924 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1925
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1926 if (!(initial == NULL || PyList_Check(initial)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1927 || PyString_Check(initial) || PyTuple_Check(initial)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1928 || (c == 'u' && PyUnicode_Check(initial)))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1929 it = PyObject_GetIter(initial);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1930 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1931 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1932 /* We set initial to NULL so that the subsequent code
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1933 will create an empty array of the appropriate type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1934 and afterwards we can use array_iter_extend to populate
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1935 the array.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1936 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1937 initial = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1938 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1939 for (descr = descriptors; descr->typecode != '\0'; descr++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1940 if (descr->typecode == c) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1941 PyObject *a;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1942 Py_ssize_t len;
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1943
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1944 if (initial == NULL || !(PyList_Check(initial)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1945 || PyTuple_Check(initial)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1946 len = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1947 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1948 len = PySequence_Size(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1949
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1950 a = newarrayobject(type, len, descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1951 if (a == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1952 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1953
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1954 if (len > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1955 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1956 for (i = 0; i < len; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1957 PyObject *v =
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1958 PySequence_GetItem(initial, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1959 if (v == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1960 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1961 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1962 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1963 if (setarrayitem(a, i, v) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1964 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1965 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1966 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1967 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1968 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1969 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1970 } else if (initial != NULL && PyString_Check(initial)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1971 PyObject *t_initial, *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1972 t_initial = PyTuple_Pack(1, initial);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1973 if (t_initial == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1974 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1975 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1976 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1977 v = array_fromstring((arrayobject *)a,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1978 t_initial);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1979 Py_DECREF(t_initial);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1980 if (v == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1981 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1982 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1983 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1984 Py_DECREF(v);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1985 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1986 } else if (initial != NULL && PyUnicode_Check(initial)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1987 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1988 if (n > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1989 arrayobject *self = (arrayobject *)a;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1990 char *item = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1991 item = (char *)PyMem_Realloc(item, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1992 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1993 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1994 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1995 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1996 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1997 self->ob_item = item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1998 Py_SIZE(self) = n / sizeof(Py_UNICODE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
1999 memcpy(item, PyUnicode_AS_DATA(initial), n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2000 self->allocated = Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2001 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2002 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2003 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2004 if (it != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2005 if (array_iter_extend((arrayobject *)a, it) == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2006 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2007 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2008 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2009 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2010 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2011 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2012 return a;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2013 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2014 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2015 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2016 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2017 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2018 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2019
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2020
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
2021 PyDoc_STRVAR(module_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2022 "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
2023 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
2024 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
2025 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
2026 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
2027 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
2028 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2029 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
2030 'c' character 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2031 'b' signed integer 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2032 'B' unsigned integer 1 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2033 'u' Unicode character 2 \n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2034 'h' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2035 'H' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2036 'i' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2037 'I' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2038 'l' signed integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2039 'L' unsigned integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2040 'f' floating point 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2041 'd' floating point 8 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2042 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2043 The constructor is:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2044 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2045 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
2046 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2047
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
2048 PyDoc_STRVAR(arraytype_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2049 "array(typecode [, initializer]) -> array\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2050 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2051 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
2052 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
2053 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
2054 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2055 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
2056 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
2057 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2058 Methods:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2059 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2060 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
2061 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
2062 byteswap() -- byteswap all the items of the array\n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
2063 count() -- return number of occurrences of an object\n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
2064 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
2065 fromfile() -- read items from a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2066 fromlist() -- append items from the list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2067 fromstring() -- append items from the string\n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
2068 index() -- return index of first occurrence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2069 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
2070 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
2071 read() -- DEPRECATED, use fromfile()\n\
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 48046
diff changeset
2072 remove() -- remove first occurrence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2073 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
2074 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
2075 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
2076 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
2077 write() -- DEPRECATED, use tofile()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2078 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2079 Attributes:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2080 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2081 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
2082 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
2083 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2084
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2085 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
2086
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2087 static PyTypeObject Arraytype = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2088 PyVarObject_HEAD_INIT(NULL, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2089 "array.array",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2090 sizeof(arrayobject),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2091 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2092 (destructor)array_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2093 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2094 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2095 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2096 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2097 (reprfunc)array_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2098 0, /* tp_as_number*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2099 &array_as_sequence, /* tp_as_sequence*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2100 &array_as_mapping, /* tp_as_mapping*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2101 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2102 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2103 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2104 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2105 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2106 &array_as_buffer, /* tp_as_buffer*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2107 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2108 arraytype_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2109 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2110 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2111 array_richcompare, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2112 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2113 (getiterfunc)array_iter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2114 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2115 array_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2116 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2117 array_getsets, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2118 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2119 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2120 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2121 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2122 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2123 0, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2124 PyType_GenericAlloc, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2125 array_new, /* tp_new */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2126 PyObject_Del, /* tp_free */
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2127 };
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2128
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2129
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2130 /*********************** Array Iterator **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2131
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2132 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2133 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2134 Py_ssize_t index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2135 arrayobject *ao;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2136 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
2137 } arrayiterobject;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2138
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2139 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
2140
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2141 #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
2142
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2143 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2144 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
2145 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2146 arrayiterobject *it;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2147
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2148 if (!array_Check(ao)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2149 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2150 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2151 }
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2152
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2153 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2154 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2155 return NULL;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2156
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2157 Py_INCREF(ao);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2158 it->ao = ao;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2159 it->index = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2160 it->getitem = ao->ob_descr->getitem;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2161 PyObject_GC_Track(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2162 return (PyObject *)it;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2163 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2164
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2165 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2166 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
2167 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2168 assert(PyArrayIter_Check(it));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2169 if (it->index < Py_SIZE(it->ao))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2170 return (*it->getitem)(it->ao, it->index++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2171 return NULL;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2172 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2173
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2174 static void
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2175 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
2176 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2177 PyObject_GC_UnTrack(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2178 Py_XDECREF(it->ao);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2179 PyObject_GC_Del(it);
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2180 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2181
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2182 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2183 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
2184 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2185 Py_VISIT(it->ao);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2186 return 0;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2187 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2188
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2189 static PyTypeObject PyArrayIter_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2190 PyVarObject_HEAD_INIT(NULL, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2191 "arrayiterator", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2192 sizeof(arrayiterobject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2193 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2194 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2195 (destructor)arrayiter_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2196 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2197 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2198 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2199 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2200 0, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2201 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2202 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2203 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2204 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2205 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2206 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2207 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2208 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2209 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2210 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2211 0, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2212 (traverseproc)arrayiter_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2213 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2214 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2215 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2216 PyObject_SelfIter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2217 (iternextfunc)arrayiter_next, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2218 0, /* tp_methods */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2219 };
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2220
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2221
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2222 /*********************** Install Module **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2223
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2224 /* No functions in array module. */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2225 static PyMethodDef a_methods[] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2226 {NULL, NULL, 0, NULL} /* Sentinel */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2227 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2228
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2229
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24424
diff changeset
2230 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13590
diff changeset
2231 initarray(void)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2232 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2233 PyObject *m;
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
2234
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2235 Arraytype.ob_type = &PyType_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2236 PyArrayIter_Type.ob_type = &PyType_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2237 m = Py_InitModule3("array", a_methods, module_doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2238 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2239 return;
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2240
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2241 Py_INCREF((PyObject *)&Arraytype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2242 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2243 Py_INCREF((PyObject *)&Arraytype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2244 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58906
diff changeset
2245 /* 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
2246 }