annotate Modules/arraymodule.c @ 55603:12fec73cb8e5 3.1

Merged revisions 73862,73872 via svnmerge from svn+ssh://svn.python.org/python/branches/py3k ........ r73862 | alexandre.vassalotti | 2009-07-05 21:57:00 +0200 (So, 05 Jul 2009) | 2 lines Add the fix for issue 4509 to the mapping methods. ........ r73872 | gregory.p.smith | 2009-07-07 07:06:04 +0200 (Di, 07 Jul 2009) | 2 lines Add a unittest for r73566. ........
author Georg Brandl <georg@python.org>
date Thu, 13 Aug 2009 09:05:38 +0000
parents 680ad8eed33b
children 6de64896c6f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Array object implementation */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* An array is a uniform list -- all items have the same type.
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 The item type is restricted to simple C types like int or float */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
6 #define PY_SSIZE_T_CLEAN
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
7 #include "Python.h"
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
8 #include "structmember.h"
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
9
1797
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
10 #ifdef STDC_HEADERS
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
11 #include <stddef.h>
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
12 #else /* !STDC_HEADERS */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
13 #ifdef HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
14 #include <sys/types.h> /* For size_t */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
15 #endif /* HAVE_SYS_TYPES_H */
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
16 #endif /* !STDC_HEADERS */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 struct arrayobject; /* Forward */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
20 /* All possible arraydescr values are defined in the vector "descriptors"
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
21 * below. That's defined later because the appropriate get and set
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
22 * functions aren't visible yet.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
23 */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 struct arraydescr {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 int typecode;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 int itemsize;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
42675
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
29 char *formats;
1235
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
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 typedef struct arrayobject {
36544
23e23e15e7e7 Bug #1432350: arrayobject should use PyObject_VAR_HEAD
Georg Brandl <georg@python.org>
parents: 36529
diff changeset
33 PyObject_VAR_HEAD
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 char *ob_item;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
35 Py_ssize_t allocated;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 struct arraydescr *ob_descr;
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
37 PyObject *weakreflist; /* List of weak references */
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
38 int ob_exports; /* Number of exported buffers */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 } arrayobject;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23929
diff changeset
41 static PyTypeObject Arraytype;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
43 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
44 #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
45
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
46 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
47 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
48 {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
49 char *items;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
50 size_t _new_size;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
51
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
52 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
53 PyErr_SetString(PyExc_BufferError,
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
54 "cannot resize an array that is exporting buffers");
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
55 return -1;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
56 }
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
57
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
58 /* Bypass realloc() when a previous overallocation is large enough
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
59 to accommodate the newsize. If the newsize is 16 smaller than the
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
60 current size, then proceed with the realloc() to shrink the array.
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
61 */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
62
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
63 if (self->allocated >= newsize &&
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
64 Py_SIZE(self) < newsize + 16 &&
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
65 self->ob_item != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
66 Py_SIZE(self) = newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
67 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
68 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
69
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
70 if (newsize == 0) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
71 PyMem_FREE(self->ob_item);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
72 self->ob_item = NULL;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
73 Py_SIZE(self) = 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
74 self->allocated = 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
75 return 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
76 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
77
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
78 /* This over-allocates proportional to the array size, making room
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
79 * for additional growth. The over-allocation is mild, but is
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
80 * enough to give linear-time amortized behavior over a long
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
81 * sequence of appends() in the presence of a poorly-performing
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
82 * system realloc().
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
83 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
84 * Note, the pattern starts out the same as for lists but then
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
85 * grows at a smaller rate so that larger arrays only overallocate
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
86 * by about 1/16th -- this is done because arrays are presumed to be more
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
87 * memory critical.
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
88 */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
89
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
90 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
91 items = self->ob_item;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
92 /* XXX The following multiplication and division does not optimize away
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
93 like it does for lists since the size is not known at compile time */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
94 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
95 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
96 else
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
97 items = NULL;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
98 if (items == NULL) {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
99 PyErr_NoMemory();
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
100 return -1;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
101 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
102 self->ob_item = items;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
103 Py_SIZE(self) = newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
104 self->allocated = _new_size;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
105 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
106 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
107
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
108 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
120 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
121 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
122 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 long x = ((char *)ap->ob_item)[i];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 if (x >= 128)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 x -= 256;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
126 return PyLong_FromLong(x);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
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 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
130 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
131 {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
132 short x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
133 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
134 must use the next size up that is signed ('h') and manually do
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
135 the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
136 if (!PyArg_Parse(v, "h;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 return -1;
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
138 else if (x < -128) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
139 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
140 "signed char is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
141 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
142 }
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
143 else if (x > 127) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
144 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
145 "signed char is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
146 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
147 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
149 ((char *)ap->ob_item)[i] = (char)x;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
153 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
154 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
155 {
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 long x = ((unsigned char *)ap->ob_item)[i];
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
157 return PyLong_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
158 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
159
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
160 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
161 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
162 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
163 unsigned char x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
164 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
165 if (!PyArg_Parse(v, "b;array item must be integer", &x))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
166 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
167 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
168 ((char *)ap->ob_item)[i] = x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
169 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
170 }
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
171
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
172 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
173 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
174 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
175 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
176 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
177
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
178 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
179 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
180 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
181 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
182 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
183
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
184 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
185 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
186 if (len != 1) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
187 PyErr_SetString(PyExc_TypeError,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
188 "array item must be unicode character");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
189 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
190 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
191 if (i >= 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
192 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
193 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
194 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
195
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
196
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
197 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
198 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
199 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
200 return PyLong_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
201 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
203
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
205 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
206 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 short x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
209 if (!PyArg_Parse(v, "h;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 ((short *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
216 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
217 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
218 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
219 return PyLong_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
220 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
221
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
222 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
223 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
224 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
225 int x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
227 must use the next size up and manually do the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
228 if (!PyArg_Parse(v, "i;array item must be integer", &x))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
229 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
230 else if (x < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
231 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
232 "unsigned short is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
233 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
234 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
235 else if (x > USHRT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
236 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
237 "unsigned short is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
238 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
239 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
240 if (i >= 0)
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
241 ((short *)ap->ob_item)[i] = (short)x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
242 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
243 }
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
244
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
245 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
246 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
247 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
248 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
249 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
250
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
251 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
252 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
253 {
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
254 int x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
256 if (!PyArg_Parse(v, "i;array item must be integer", &x))
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
257 return -1;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
258 if (i >= 0)
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
259 ((int *)ap->ob_item)[i] = x;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
260 return 0;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
261 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
262
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
263 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
264 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
265 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
266 return PyLong_FromUnsignedLong(
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
267 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
268 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
269
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
270 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
271 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
272 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
273 unsigned long x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
274 if (PyLong_Check(v)) {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
275 x = PyLong_AsUnsignedLong(v);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
276 if (x == (unsigned long) -1 && PyErr_Occurred())
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
277 return -1;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
278 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
279 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
280 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
281 if (!PyArg_Parse(v, "l;array item must be integer", &y))
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
282 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
283 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
284 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
285 "unsigned int is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
286 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
287 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
288 x = (unsigned long)y;
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
289
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
290 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
291 if (x > UINT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
292 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
293 "unsigned int is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
294 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
295 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
296
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
297 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
299 return 0;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
300 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
301
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
302 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
303 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
304 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
305 return PyLong_FromLong(((long *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
309 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
310 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 long x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
312 if (!PyArg_Parse(v, "l;array item must be integer", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 ((long *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
319 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
320 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
321 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
323 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
324
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
325 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
326 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
327 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
328 unsigned long x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
329 if (PyLong_Check(v)) {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
330 x = PyLong_AsUnsignedLong(v);
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
331 if (x == (unsigned long) -1 && PyErr_Occurred())
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
332 return -1;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
333 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
334 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
335 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
336 if (!PyArg_Parse(v, "l;array item must be integer", &y))
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
337 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
338 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
339 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
340 "unsigned long is less than minimum");
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
341 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
342 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
343 x = (unsigned long)y;
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
344
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
345 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
346 if (x > ULONG_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
347 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
348 "unsigned long is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
349 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
350 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
351
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
352 if (i >= 0)
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
353 ((unsigned long *)ap->ob_item)[i] = x;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
354 return 0;
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
355 }
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
356
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
357 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
358 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
359 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
364 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
365 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 float x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
367 if (!PyArg_Parse(v, "f;array item must be float", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 ((float *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
374 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
375 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
376 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
381 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
382 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 double x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
384 if (!PyArg_Parse(v, "d;array item must be float", &x))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 ((double *)ap->ob_item)[i] = x;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
391
1235
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[] = {
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
394 {'b', 1, b_getitem, b_setitem, "b"},
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
395 {'B', 1, BB_getitem, BB_setitem, "B"},
43555
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
396 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
42675
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
397 {'h', sizeof(short), h_getitem, h_setitem, "h"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
398 {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
399 {'i', sizeof(int), i_getitem, i_setitem, "i"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
400 {'I', sizeof(int), II_getitem, II_setitem, "I"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
401 {'l', sizeof(long), l_getitem, l_setitem, "l"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
402 {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
403 {'f', sizeof(float), f_getitem, f_setitem, "f"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
404 {'d', sizeof(double), d_getitem, d_setitem, "d"},
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
405 {'\0', 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
406 };
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
407
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
408 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
409 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
410 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
412 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
413 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
414 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 arrayobject *op;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
416 size_t nbytes;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
417
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 if (size < 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
419 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
422
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 nbytes = size * descr->itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 /* Check for overflow */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4875
diff changeset
425 if (nbytes / descr->itemsize != (size_t)size) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
426 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
428 op = (arrayobject *) type->tp_alloc(type, 0);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 if (op == NULL) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
430 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 }
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
432 op->ob_descr = descr;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
433 op->allocated = size;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
434 op->weakreflist = NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
435 Py_SIZE(op) = size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 if (size <= 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 op->ob_item = NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 else {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
440 op->ob_item = PyMem_NEW(char, nbytes);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 if (op->ob_item == NULL) {
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
442 Py_DECREF(op);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
443 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
446 op->ob_exports = 0;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
447 return (PyObject *) op;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
450 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
451 getarrayitem(PyObject *op, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 register arrayobject *ap;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
454 assert(array_Check(op));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 ap = (arrayobject *)op;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
456 assert(i>=0 && i<Py_SIZE(ap));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 return (*ap->ob_descr->getitem)(ap, i);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
461 ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 char *items;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
464 Py_ssize_t n = Py_SIZE(self);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
466 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 return -1;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
471
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
472 if (array_resize(self, n+1) == -1)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
473 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 items = self->ob_item;
29163
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
475 if (where < 0) {
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
476 where += n;
29163
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
477 if (where < 0)
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
478 where = 0;
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
479 }
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
480 if (where > n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
481 where = n;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
482 /* appends don't need to call memmove() */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
483 if (where != n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
484 memmove(items + (where+1)*self->ob_descr->itemsize,
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
485 items + where*self->ob_descr->itemsize,
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
486 (n-where)*self->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 return (*self->ob_descr->setitem)(self, where, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 /* Methods */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 static void
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
493 array_dealloc(arrayobject *op)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 {
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
495 if (op->weakreflist != NULL)
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
496 PyObject_ClearWeakRefs((PyObject *) op);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 if (op->ob_item != NULL)
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
498 PyMem_DEL(op->ob_item);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
499 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
500 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
502 static PyObject *
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
503 array_richcompare(PyObject *v, PyObject *w, int op)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
505 arrayobject *va, *wa;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
506 PyObject *vi = NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
507 PyObject *wi = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
508 Py_ssize_t i, k;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
509 PyObject *res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
510
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
511 if (!array_Check(v) || !array_Check(w)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
512 Py_INCREF(Py_NotImplemented);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
513 return Py_NotImplemented;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
514 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
515
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
516 va = (arrayobject *)v;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
517 wa = (arrayobject *)w;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
518
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
519 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
520 /* Shortcut: if the lengths differ, the arrays differ */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
521 if (op == Py_EQ)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
522 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
523 else
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
524 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
525 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
526 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
527 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
528
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
529 /* Search for the first index where items are different */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
530 k = 1;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
531 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
532 vi = getarrayitem(v, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
533 wi = getarrayitem(w, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
534 if (vi == NULL || wi == NULL) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
535 Py_XDECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
536 Py_XDECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
537 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
538 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
539 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
540 if (k == 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
541 break; /* Keeping vi and wi alive! */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
542 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
543 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
544 if (k < 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
545 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
546 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
547
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
548 if (k) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
549 /* No more items to compare -- compare sizes */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
550 Py_ssize_t vs = Py_SIZE(va);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
551 Py_ssize_t ws = Py_SIZE(wa);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 int cmp;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
553 switch (op) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
554 case Py_LT: cmp = vs < ws; break;
16268
c2ce9b2e5bb4 Correct one-line typo, reported by yole @ SF, bug 130077.
Guido van Rossum <guido@python.org>
parents: 16232
diff changeset
555 case Py_LE: cmp = vs <= ws; break;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
556 case Py_EQ: cmp = vs == ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
557 case Py_NE: cmp = vs != ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
558 case Py_GT: cmp = vs > ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
559 case Py_GE: cmp = vs >= ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
560 default: return NULL; /* cannot happen */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
561 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
562 if (cmp)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
563 res = Py_True;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 else
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
565 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
566 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
567 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
568 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
569
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
570 /* We have an item that differs. First, shortcuts for EQ/NE */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
571 if (op == Py_EQ) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
572 Py_INCREF(Py_False);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
573 res = Py_False;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
575 else if (op == Py_NE) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
576 Py_INCREF(Py_True);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
577 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
578 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
579 else {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
580 /* Compare the final item again using the proper operator */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
581 res = PyObject_RichCompare(vi, wi, op);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
582 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
583 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
584 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
585 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
588 static Py_ssize_t
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
589 array_length(arrayobject *a)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
591 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
592 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
594 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
595 array_item(arrayobject *a, Py_ssize_t i)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
597 if (i < 0 || i >= Py_SIZE(a)) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
598 PyErr_SetString(PyExc_IndexError, "array index out of range");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
601 return getarrayitem((PyObject *)a, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
604 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
605 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 ilow = 0;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
610 else if (ilow > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
611 ilow = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 ihigh = ilow;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
616 else if (ihigh > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
617 ihigh = Py_SIZE(a);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
618 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 (ihigh-ilow) * a->ob_descr->itemsize);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
623 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
626 static PyObject *
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
627 array_copy(arrayobject *a, PyObject *unused)
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
628 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
629 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
630 }
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
631
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
632 PyDoc_STRVAR(copy_doc,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
633 "copy(array)\n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
634 \n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
635 Return a copy of the array.");
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
636
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
637 static PyObject *
13590
c503d8e474b3 ANSI-fication (got lost in the deep seas of source forge <wink>)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
638 array_concat(arrayobject *a, PyObject *bb)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
640 Py_ssize_t size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 arrayobject *np;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
642 if (!array_Check(bb)) {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
643 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
644 "can only append array (not \"%.200s\") to array",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
645 Py_TYPE(bb)->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 #define b ((arrayobject *)bb)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 if (a->ob_descr != b->ob_descr) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
650 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 }
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
653 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
654 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
655 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
656 size = Py_SIZE(a) + Py_SIZE(b);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
657 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 if (np == NULL) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
661 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
662 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
663 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
664 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
668 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
669 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
670 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
671 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
672 Py_ssize_t size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
675 Py_ssize_t nbytes;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 if (n < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 n = 0;
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
678 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
679 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
680 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
681 size = Py_SIZE(a) * n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
682 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 p = np->ob_item;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
686 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 for (i = 0; i < n; i++) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 memcpy(p, a->ob_item, nbytes);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 p += nbytes;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
691 return (PyObject *) np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 static int
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
695 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
696 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 char *item;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
698 Py_ssize_t n; /* Size of replacement array */
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
699 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
700 #define b ((arrayobject *)v)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 n = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
703 else if (array_Check(v)) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
704 n = Py_SIZE(b);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 if (a == b) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 /* Special case "a[i:j] = a" -- copy b first */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 int ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 v = array_slice(b, 0, n);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
709 if (!v)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
710 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 ret = array_ass_slice(a, ilow, ihigh, v);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
712 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 return ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 if (b->ob_descr != a->ob_descr) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
716 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 else {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
721 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
722 "can only assign array (not \"%.200s\") to array slice",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
723 Py_TYPE(v)->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 ilow = 0;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
728 else if (ilow > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
729 ilow = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 ihigh = ilow;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
734 else if (ihigh > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
735 ihigh = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 item = a->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 d = n - (ihigh-ilow);
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
738 /* Issue #4509: If the array has exported buffers and the slice
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
739 assignment would change the size of the array, fail early to make
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
740 sure we don't modify it. */
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
741 if (d != 0 && a->ob_exports > 0) {
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
742 PyErr_SetString(PyExc_BufferError,
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
743 "cannot resize an array that is exporting buffers");
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
744 return -1;
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
745 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 if (d < 0) { /* Delete -d items */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 item + ihigh*a->ob_descr->itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
749 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
750 if (array_resize(a, Py_SIZE(a) + d) == -1)
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
751 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 else if (d > 0) { /* Insert d items */
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
754 if (array_resize(a, Py_SIZE(a) + d))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 item + ihigh*a->ob_descr->itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
758 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 if (n > 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 n*b->ob_descr->itemsize);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
768 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
769 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
770 if (i < 0 || i >= Py_SIZE(a)) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
771 PyErr_SetString(PyExc_IndexError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
772 "array assignment index out of range");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 return array_ass_slice(a, i, i+1, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 return (*a->ob_descr->setitem)(a, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
779
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
781 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
782 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
783 assert(array_Check(a));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 return array_ass_item((arrayobject *)a, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
786
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
787 static int
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
788 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
789 {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
790 PyObject *it, *v;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
791
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
792 it = PyObject_GetIter(bb);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
793 if (it == NULL)
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
794 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
795
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
796 while ((v = PyIter_Next(it)) != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
797 if (ins1(self, (int) Py_SIZE(self), v) != 0) {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
798 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
799 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
800 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
801 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
802 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
803 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
804 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
805 if (PyErr_Occurred())
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
806 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
807 return 0;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
808 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
809
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
810 static int
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
811 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
812 {
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
813 Py_ssize_t size, oldsize, bbsize;
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
814
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
815 if (!array_Check(bb))
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
816 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
817 #define b ((arrayobject *)bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
818 if (self->ob_descr != b->ob_descr) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
819 PyErr_SetString(PyExc_TypeError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
820 "can only extend with array of same kind");
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
821 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
822 }
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
823 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
824 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
825 PyErr_NoMemory();
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
826 return -1;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
827 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
828 oldsize = Py_SIZE(self);
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
829 /* Get the size of bb before resizing the array since bb could be self. */
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
830 bbsize = Py_SIZE(bb);
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
831 size = oldsize + Py_SIZE(b);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
832 if (array_resize(self, size) == -1)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
833 return -1;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
834 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55580
diff changeset
835 b->ob_item, bbsize * b->ob_descr->itemsize);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
836
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
837 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
838 #undef b
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
839 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
840
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
841 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
842 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
843 {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
844 if (!array_Check(bb)) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
845 PyErr_Format(PyExc_TypeError,
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
846 "can only extend array with array (not \"%.200s\")",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
847 Py_TYPE(bb)->tp_name);
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
848 return NULL;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
849 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
850 if (array_do_extend(self, bb) == -1)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
851 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
852 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
853 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
854 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
855
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
856 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
857 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
858 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
859 char *items, *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
860 Py_ssize_t size, i;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
861
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
862 if (Py_SIZE(self) > 0) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
863 if (n < 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
864 n = 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
865 items = self->ob_item;
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
866 if ((self->ob_descr->itemsize != 0) &&
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
867 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
868 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
869 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
870 size = Py_SIZE(self) * self->ob_descr->itemsize;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
871 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
872 return PyErr_NoMemory();
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
873 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
874 if (array_resize(self, n * Py_SIZE(self)) == -1)
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
875 return NULL;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
876 items = p = self->ob_item;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
877 for (i = 1; i < n; i++) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
878 p += size;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
879 memcpy(p, items, size);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
880 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
881 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
882 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
883 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
884 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
885
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
886
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
887 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
888 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
889 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
890 if (ins1(self, where, v) != 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
892 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
893 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
895
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
896 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
897 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
898 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
899 Py_ssize_t count = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
900 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
901
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
902 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
903 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
904 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
905 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
906 if (cmp > 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
907 count++;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
908 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
909 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
910 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
911 return PyLong_FromSsize_t(count);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
912 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
913
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
914 PyDoc_STRVAR(count_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
915 "count(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
916 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
917 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
918
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
919 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
920 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
921 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
922 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
923
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
924 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
925 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
926 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
927 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
928 if (cmp > 0) {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
929 return PyLong_FromLong((long)i);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
930 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
931 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
932 return NULL;
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 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
935 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
936 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
937
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
938 PyDoc_STRVAR(index_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
939 "index(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
940 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
941 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
942
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
943 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
944 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
945 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
946 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
947 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
948
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
949 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
950 PyObject *selfi = getarrayitem((PyObject *)self, i);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
951 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
952 Py_DECREF(selfi);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
953 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
954 return cmp;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
955 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
956
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
957 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
958 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
959 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
960 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
961
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
962 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
963 PyObject *selfi = getarrayitem((PyObject *)self,i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
964 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
965 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
966 if (cmp > 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
967 if (array_ass_slice(self, i, i+1,
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
968 (PyObject *)NULL) != 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
969 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
970 Py_INCREF(Py_None);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
971 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
972 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
973 else if (cmp < 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
974 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
975 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
976 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
977 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
978 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
979
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
980 PyDoc_STRVAR(remove_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
981 "remove(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
982 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
983 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
984
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
985 static PyObject *
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
986 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
987 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
988 Py_ssize_t i = -1;
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
989 PyObject *v;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
990 if (!PyArg_ParseTuple(args, "|n:pop", &i))
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
991 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
992 if (Py_SIZE(self) == 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
993 /* Special-case most common failure cause */
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
994 PyErr_SetString(PyExc_IndexError, "pop from empty array");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
995 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
996 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
997 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
998 i += Py_SIZE(self);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
999 if (i < 0 || i >= Py_SIZE(self)) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1000 PyErr_SetString(PyExc_IndexError, "pop index out of range");
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1001 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1002 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1003 v = getarrayitem((PyObject *)self,i);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1004 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1005 Py_DECREF(v);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1006 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1007 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1008 return v;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1009 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1010
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1011 PyDoc_STRVAR(pop_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1012 "pop([i])\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1013 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1014 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
1015
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1016 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1017 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
1018 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1019 if (array_do_extend(self, bb) == -1)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1020 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1021 Py_INCREF(Py_None);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1022 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1023 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1024
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1025 PyDoc_STRVAR(extend_doc,
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1026 "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
1027 \n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1028 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
1029
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1030 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
1031 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
1032 {
36689
11484a72d966 Convert array.array.insert to use Py_ssize_t (like the rest already does.)
Thomas Wouters <thomas@python.org>
parents: 36544
diff changeset
1033 Py_ssize_t i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1034 PyObject *v;
36689
11484a72d966 Convert array.array.insert to use Py_ssize_t (like the rest already does.)
Thomas Wouters <thomas@python.org>
parents: 36544
diff changeset
1035 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 return ins(self, i, v);
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1040 PyDoc_STRVAR(insert_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1041 "insert(i,x)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1042 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1043 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
1044
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1045
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1046 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1047 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
1048 {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1049 PyObject* retval = NULL;
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1050 retval = PyTuple_New(2);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1051 if (!retval)
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1052 return NULL;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1053
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1054 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1055 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1056
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1057 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
1058 }
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1059
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1060 PyDoc_STRVAR(buffer_info_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1061 "buffer_info() -> (address, length)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1062 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1063 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
1064 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
1065 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
1066 the buffer length in bytes.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1067
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1068
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 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1070 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
1071 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1072 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
1073 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1075 PyDoc_STRVAR(append_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1076 "append(x)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1077 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1078 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
1079
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1080
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1081 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1082 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
1083 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1085 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
1086
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 switch (self->ob_descr->itemsize) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 case 1:
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 case 2:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1091 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 p[0] = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 p[1] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 case 4:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1098 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 char p1 = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 p[0] = p[3];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 p[1] = p[2];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 p[2] = p1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 p[3] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 break;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1107 case 8:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1108 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1109 char p0 = p[0];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1110 char p1 = p[1];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1111 char p2 = p[2];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1112 char p3 = p[3];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1113 p[0] = p[7];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1114 p[1] = p[6];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1115 p[2] = p[5];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1116 p[3] = p[4];
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1117 p[4] = p3;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1118 p[5] = p2;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1119 p[6] = p1;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1120 p[7] = p0;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1121 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1122 break;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 default:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1124 PyErr_SetString(PyExc_RuntimeError,
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125 "don't know how to byteswap this array type");
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1128 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1129 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1132 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
1133 "byteswap()\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1134 \n\
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1135 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
1136 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
1137
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1138 static PyObject *
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1139 array_reduce(arrayobject *array)
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1140 {
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1141 PyObject *dict, *result;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1142
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1143 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1144 if (dict == NULL) {
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1145 PyErr_Clear();
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1146 dict = Py_None;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1147 Py_INCREF(dict);
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1148 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1149 if (Py_SIZE(array) > 0) {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1150 if (array->ob_descr->itemsize
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1151 > PY_SSIZE_T_MAX / Py_SIZE(array)) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1152 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1153 }
52781
2df1517d868c Py_BuildValue's 'c' code should use byte strings #5666
Benjamin Peterson <benjamin@python.org>
parents: 52040
diff changeset
1154 result = Py_BuildValue("O(Cy#)O",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1155 Py_TYPE(array),
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1156 array->ob_descr->typecode,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1157 array->ob_item,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1158 Py_SIZE(array) * array->ob_descr->itemsize,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1159 dict);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1160 } else {
52781
2df1517d868c Py_BuildValue's 'c' code should use byte strings #5666
Benjamin Peterson <benjamin@python.org>
parents: 52040
diff changeset
1161 result = Py_BuildValue("O(C)O",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1162 Py_TYPE(array),
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1163 array->ob_descr->typecode,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1164 dict);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1165 }
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1166 Py_DECREF(dict);
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1167 return result;
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1168 }
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1169
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1170 PyDoc_STRVAR(array_doc, "Return state information for pickling.");
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1171
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1172 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1173 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
1174 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1175 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1176 register char *p, *q;
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1177 /* little buffer to hold items while swapping */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1178 char tmp[256]; /* 8 is probably enough -- but why skimp */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1179 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
1180
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1181 if (Py_SIZE(self) > 1) {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1182 for (p = self->ob_item,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1183 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1184 p < q;
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1185 p += itemsize, q -= itemsize) {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1186 /* memory areas guaranteed disjoint, so memcpy
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1187 * is safe (& memmove may be slower).
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1188 */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1189 memcpy(tmp, p, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1190 memcpy(p, q, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1191 memcpy(q, tmp, itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1194
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1195 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1196 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 }
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1198
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1199 PyDoc_STRVAR(reverse_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1200 "reverse()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1201 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1202 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
1203
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1204
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1205 /* Forward */
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1206 static PyObject *array_fromstring(arrayobject *self, PyObject *args);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1207
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1208 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
1209 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
1210 {
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1211 PyObject *f, *b, *res;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1212 Py_ssize_t itemsize = self->ob_descr->itemsize;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1213 Py_ssize_t n, nbytes;
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1214 int not_enough_bytes;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1215
36529
442c0fb3c4ed Use correct format specifier for Py_ssize_t variable to PyArg_ParseTuple().
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1216 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 return NULL;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1218
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1219 nbytes = n * itemsize;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1220 if (nbytes < 0 || nbytes/itemsize != n) {
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1221 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1224
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1225 b = PyObject_CallMethod(f, "read", "n", nbytes);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1226 if (b == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1227 return NULL;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1228
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1229 if (!PyBytes_Check(b)) {
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1230 PyErr_SetString(PyExc_TypeError,
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1231 "read() didn't return bytes");
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1232 Py_DECREF(b);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1233 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1235
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1236 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1237
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1238 args = Py_BuildValue("(O)", b);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1239 Py_DECREF(b);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1240 if (args == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1241 return NULL;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1242
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1243 res = array_fromstring(self, args);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1244 Py_DECREF(args);
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1245 if (res == NULL)
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1246 return NULL;
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1247
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1248 if (not_enough_bytes) {
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1249 PyErr_SetString(PyExc_EOFError,
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1250 "read() didn't return enough bytes");
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1251 Py_DECREF(res);
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1252 return NULL;
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1253 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1254
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1255 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1258 PyDoc_STRVAR(fromfile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1259 "fromfile(f, n)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1260 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1261 Read n objects from the file object f and append them to the end of the\n\
45083
c66da57b9488 Remove the deprecated array.read/write methods.
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
1262 array.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1263
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1264
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1265 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1266 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
1267 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1268 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1269 /* Write 64K blocks at a time */
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1270 /* XXX Make the block size settable */
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1271 int BLOCKSIZE = 64*1024;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1272 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1273 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
1274
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1275 if (Py_SIZE(self) == 0)
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1276 goto done;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1277
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1278 for (i = 0; i < nblocks; i++) {
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1279 char* ptr = self->ob_item + i*BLOCKSIZE;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1280 Py_ssize_t size = BLOCKSIZE;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1281 PyObject *bytes, *res;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1282 if (i*BLOCKSIZE + size > nbytes)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1283 size = nbytes - i*BLOCKSIZE;
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1284 bytes = PyBytes_FromStringAndSize(ptr, size);
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1285 if (bytes == NULL)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286 return NULL;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1287 res = PyObject_CallMethod(f, "write", "O", bytes);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1288 Py_DECREF(bytes);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1289 if (res == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1290 return NULL;
42540
aab687936ecd Fix reference leak.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
1291 Py_DECREF(res); /* drop write result */
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1292 }
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1293
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1294 done:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1295 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1296 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1299 PyDoc_STRVAR(tofile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1300 "tofile(f)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1301 \n\
45083
c66da57b9488 Remove the deprecated array.read/write methods.
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
1302 Write all items (as machine values) to the file object f.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1303
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1304
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1305 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1306 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
1307 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1308 Py_ssize_t n;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1309
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1310 if (!PyList_Check(list)) {
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1311 PyErr_SetString(PyExc_TypeError, "arg must be list");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1314 n = PyList_Size(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1316 Py_ssize_t i, old_size;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1317 old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1318 if (array_resize(self, old_size + n) == -1)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 for (i = 0; i < n; i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1321 PyObject *v = PyList_GetItem(list, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322 if ((*self->ob_descr->setitem)(self,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1323 Py_SIZE(self) - n + i, v) != 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1324 array_resize(self, old_size);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1329 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1330 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1333 PyDoc_STRVAR(fromlist_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1334 "fromlist(list)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1335 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1336 Append items to array from list.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1337
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1338 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1339 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
1340 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1341 PyObject *list = PyList_New(Py_SIZE(self));
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1342 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
1343
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 if (list == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1346 for (i = 0; i < Py_SIZE(self); i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1347 PyObject *v = getarrayitem((PyObject *)self, i);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1349 Py_DECREF(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1352 PyList_SetItem(list, i, v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1353 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 return list;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1357 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
1358 "tolist() -> list\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1359 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1360 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
1361
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1362
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1363 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
1364 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
1365 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 char *str;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1367 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
1368 int itemsize = self->ob_descr->itemsize;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1369 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 if (n % itemsize != 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1372 PyErr_SetString(PyExc_ValueError,
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1373 "string length not a multiple of item size");
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 n = n / itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1378 Py_ssize_t old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1379 if ((n > PY_SSIZE_T_MAX - old_size) ||
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1380 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1381 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1382 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1383 if (array_resize(self, old_size + n) == -1)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1384 return NULL;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1385 memcpy(self->ob_item + old_size * itemsize,
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1386 str, n * itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1388 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1389 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1390 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1391
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1392 PyDoc_STRVAR(fromstring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1393 "fromstring(string)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1394 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1395 Appends items from the string, interpreting it as an array of machine\n\
42125
79c2656056f6 Make array.typecode a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 42112
diff changeset
1396 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
1397
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1398
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1399 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1400 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
1401 {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1402 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1403 return PyBytes_FromStringAndSize(self->ob_item,
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1404 Py_SIZE(self) * self->ob_descr->itemsize);
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1405 } else {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1406 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1407 }
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(tostring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1411 "tostring() -> 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 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
1414 representation.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1415
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1416
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1417
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1418 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1419 array_fromunicode(arrayobject *self, PyObject *args)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1420 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1421 Py_UNICODE *ustr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1422 Py_ssize_t n;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1423 char typecode;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1424
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1425 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1426 return NULL;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1427 typecode = self->ob_descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1428 if ((typecode != 'u')) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1429 PyErr_SetString(PyExc_ValueError,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1430 "fromunicode() may only be called on "
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1431 "unicode type arrays");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1432 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1433 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1434 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1435 Py_ssize_t old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1436 if (array_resize(self, old_size + n) == -1)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1437 return NULL;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1438 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1439 ustr, n * sizeof(Py_UNICODE));
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1440 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1441
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1442 Py_INCREF(Py_None);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1443 return Py_None;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1444 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1445
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1446 PyDoc_STRVAR(fromunicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1447 "fromunicode(ustr)\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1448 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1449 Extends this array with data from the unicode string ustr.\n\
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1450 The array must be a unicode type array; otherwise a ValueError\n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1451 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
1452 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
1453
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1454
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1455 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1456 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
1457 {
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1458 char typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1459 typecode = self->ob_descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1460 if ((typecode != 'u')) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1461 PyErr_SetString(PyExc_ValueError,
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1462 "tounicode() may only be called on unicode type arrays");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1463 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1464 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1465 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
1466 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1467
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1468 PyDoc_STRVAR(tounicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1469 "tounicode() -> unicode\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1470 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1471 Convert the array to a unicode string. The array must be\n\
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1472 a unicode type array; otherwise a ValueError is raised. Use\n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1473 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
1474 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
1475
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1476
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1477
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1478 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1479 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
1480 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1481 char tc = a->ob_descr->typecode;
42125
79c2656056f6 Make array.typecode a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 42112
diff changeset
1482 return PyUnicode_FromStringAndSize(&tc, 1);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1483 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1484
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1485 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1486 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
1487 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1488 return PyLong_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
1489 }
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 static PyGetSetDef array_getsets [] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1492 {"typecode", (getter) array_get_typecode, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1493 "the typecode character used to create the array"},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1494 {"itemsize", (getter) array_get_itemsize, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1495 "the size, in bytes, of one array item"},
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1496 {NULL}
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1497 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1498
47680
b5e02775fffc Merged revisions 64226 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47580
diff changeset
1499 static PyMethodDef array_methods[] = {
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1500 {"append", (PyCFunction)array_append, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1501 append_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1502 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1503 buffer_info_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1504 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1505 byteswap_doc},
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1506 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1507 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1508 {"count", (PyCFunction)array_count, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1509 count_doc},
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
1510 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1511 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1512 {"extend", (PyCFunction)array_extend, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1513 extend_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1514 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1515 fromfile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1516 {"fromlist", (PyCFunction)array_fromlist, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1517 fromlist_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1518 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1519 fromstring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1520 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1521 fromunicode_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1522 {"index", (PyCFunction)array_index, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1523 index_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1524 {"insert", (PyCFunction)array_insert, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1525 insert_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1526 {"pop", (PyCFunction)array_pop, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1527 pop_doc},
34174
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1528 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
30b97c1bb12d SF #1085304: Make array.array pickle-able
Raymond Hettinger <python@rcn.com>
parents: 33316
diff changeset
1529 array_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1530 {"remove", (PyCFunction)array_remove, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1531 remove_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1532 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1533 reverse_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1534 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1535 sort_doc},*/
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1536 {"tofile", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1537 tofile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1538 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1539 tolist_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1540 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1541 tostring_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1542 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1543 tounicode_doc},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544 {NULL, NULL} /* sentinel */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1546
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1547 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
1548 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
1549 {
41788
00413f70ce5c Remove unused variables.
Walter Dörwald <walter@livinglogic.de>
parents: 41782
diff changeset
1550 char typecode;
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1551 PyObject *s, *v = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1552 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1553
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1554 len = Py_SIZE(a);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1555 typecode = a->ob_descr->typecode;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556 if (len == 0) {
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1557 return PyUnicode_FromFormat("array('%c')", typecode);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1558 }
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1559 if ((typecode == 'u'))
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1560 v = array_tounicode(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1561 else
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1562 v = array_tolist(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
1563
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1564 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
1565 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1566 return s;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1567 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1568
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1569 static PyObject*
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1570 array_subscr(arrayobject* self, PyObject* item)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1571 {
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1572 if (PyIndex_Check(item)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1573 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
1574 if (i==-1 && PyErr_Occurred()) {
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1575 return NULL;
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
1576 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1577 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1578 i += Py_SIZE(self);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1579 return array_item(self, i);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1580 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1581 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1582 Py_ssize_t start, stop, step, slicelength, cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1583 PyObject* result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1584 arrayobject* ar;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1585 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1586
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1587 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1588 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1589 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1590 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1591
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1592 if (slicelength <= 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1593 return newarrayobject(&Arraytype, 0, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1594 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1595 else if (step == 1) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1596 PyObject *result = newarrayobject(&Arraytype,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1597 slicelength, self->ob_descr);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1598 if (result == NULL)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1599 return NULL;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1600 memcpy(((arrayobject *)result)->ob_item,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1601 self->ob_item + start * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1602 slicelength * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1603 return result;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1604 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1605 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1606 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1607 if (!result) return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1608
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1609 ar = (arrayobject*)result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1610
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1611 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1612 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1613 memcpy(ar->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1614 self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1615 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1616 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1617
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1618 return result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1619 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1620 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1621 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1622 PyErr_SetString(PyExc_TypeError,
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1623 "array indices must be integers");
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1624 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1625 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1626 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1627
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1628 static int
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1629 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
1630 {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1631 Py_ssize_t start, stop, step, slicelength, needed;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1632 arrayobject* other;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1633 int itemsize;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1634
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1635 if (PyIndex_Check(item)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
1636 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1637
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1638 if (i == -1 && PyErr_Occurred())
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1639 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1640 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1641 i += Py_SIZE(self);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1642 if (i < 0 || i >= Py_SIZE(self)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1643 PyErr_SetString(PyExc_IndexError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1644 "array assignment index out of range");
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1645 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1646 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1647 if (value == NULL) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1648 /* Fall through to slice assignment */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1649 start = i;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1650 stop = i + 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1651 step = 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1652 slicelength = 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1653 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1654 else
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1655 return (*self->ob_descr->setitem)(self, i, value);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1656 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1657 else if (PySlice_Check(item)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1658 if (PySlice_GetIndicesEx((PySliceObject *)item,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1659 Py_SIZE(self), &start, &stop,
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1660 &step, &slicelength) < 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1661 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1662 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1663 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1664 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1665 PyErr_SetString(PyExc_TypeError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1666 "array indices must be integer");
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1667 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1668 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1669 if (value == NULL) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1670 other = NULL;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1671 needed = 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1672 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1673 else if (array_Check(value)) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1674 other = (arrayobject *)value;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1675 needed = Py_SIZE(other);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1676 if (self == other) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1677 /* Special case "self[i:j] = self" -- copy self first */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1678 int ret;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1679 value = array_slice(other, 0, needed);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1680 if (value == NULL)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1681 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1682 ret = array_ass_subscr(self, item, value);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1683 Py_DECREF(value);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1684 return ret;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1685 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1686 if (other->ob_descr != self->ob_descr) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1687 PyErr_BadArgument();
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1688 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1689 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1690 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1691 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1692 PyErr_Format(PyExc_TypeError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1693 "can only assign array (not \"%.200s\") to array slice",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1694 Py_TYPE(value)->tp_name);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1695 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1696 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1697 itemsize = self->ob_descr->itemsize;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1698 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1699 if ((step > 0 && stop < start) ||
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1700 (step < 0 && stop > start))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1701 stop = start;
55603
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1702
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1703 /* Issue #4509: If the array has exported buffers and the slice
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1704 assignment would change the size of the array, fail early to make
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1705 sure we don't modify it. */
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1706 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1707 PyErr_SetString(PyExc_BufferError,
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1708 "cannot resize an array that is exporting buffers");
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1709 return -1;
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1710 }
12fec73cb8e5 Merged revisions 73862,73872 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55602
diff changeset
1711
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1712 if (step == 1) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1713 if (slicelength > needed) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1714 memmove(self->ob_item + (start + needed) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1715 self->ob_item + stop * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1716 (Py_SIZE(self) - stop) * itemsize);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1717 if (array_resize(self, Py_SIZE(self) +
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1718 needed - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1719 return -1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1720 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1721 else if (slicelength < needed) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1722 if (array_resize(self, Py_SIZE(self) +
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1723 needed - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1724 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1725 memmove(self->ob_item + (start + needed) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1726 self->ob_item + stop * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1727 (Py_SIZE(self) - start - needed) * itemsize);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1728 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1729 if (needed > 0)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1730 memcpy(self->ob_item + start * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1731 other->ob_item, needed * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1732 return 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1733 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1734 else if (needed == 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1735 /* Delete slice */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1736 Py_ssize_t cur, i;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1737
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1738 if (step < 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1739 stop = start + 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1740 start = stop + step * (slicelength - 1) - 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1741 step = -step;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1742 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1743 for (cur = start, i = 0; i < slicelength;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1744 cur += step, i++) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1745 Py_ssize_t lim = step - 1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1746
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1747 if (cur + step >= Py_SIZE(self))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1748 lim = Py_SIZE(self) - cur - 1;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1749 memmove(self->ob_item + (cur - i) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1750 self->ob_item + (cur + 1) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1751 lim * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1752 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1753 cur = start + slicelength * step;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1754 if (cur < Py_SIZE(self)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1755 memmove(self->ob_item + (cur-slicelength) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1756 self->ob_item + cur * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1757 (Py_SIZE(self) - cur) * itemsize);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1758 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1759 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1760 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1761 return 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1762 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1763 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1764 Py_ssize_t cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1765
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1766 if (needed != slicelength) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1767 PyErr_Format(PyExc_ValueError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1768 "attempt to assign array of size %zd "
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1769 "to extended slice of size %zd",
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1770 needed, slicelength);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1771 return -1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1772 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1773 for (cur = start, i = 0; i < slicelength;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1774 cur += step, i++) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1775 memcpy(self->ob_item + cur * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1776 other->ob_item + i * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1777 itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1778 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
1779 return 0;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1780 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1781 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1782
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1783 static PyMappingMethods array_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1784 (lenfunc)array_length,
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1785 (binaryfunc)array_subscr,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1786 (objobjargproc)array_ass_subscr
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1787 };
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
1788
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1789 static const void *emptybuf = "";
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
1790
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1791
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1792 static int
43423
0476a2cc5658 Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43100
diff changeset
1793 array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1794 {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1795 if (view==NULL) goto finish;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1796
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1797 view->buf = (void *)self->ob_item;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48565
diff changeset
1798 view->obj = (PyObject*)self;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48565
diff changeset
1799 Py_INCREF(self);
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1800 if (view->buf == NULL)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1801 view->buf = (void *)emptybuf;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1802 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1803 view->readonly = 0;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1804 view->ndim = 1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1805 view->itemsize = self->ob_descr->itemsize;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1806 view->suboffsets = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1807 view->shape = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1808 if ((flags & PyBUF_ND)==PyBUF_ND) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1809 view->shape = &((Py_SIZE(self)));
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1810 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1811 view->strides = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1812 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1813 view->strides = &(view->itemsize);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1814 view->format = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1815 view->internal = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1816 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
42675
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
1817 view->format = self->ob_descr->formats;
43555
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
1818 #ifdef Py_UNICODE_WIDE
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
1819 if (self->ob_descr->typecode == 'u') {
43565
b4035d3665bb Fix typo: formats -> format. (Only mattered in wide unicode builds.)
Guido van Rossum <guido@python.org>
parents: 43555
diff changeset
1820 view->format = "w";
43555
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
1821 }
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
1822 #endif
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1823 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1824
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1825 finish:
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1826 self->ob_exports++;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1827 return 0;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1828 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1829
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1830 static void
43423
0476a2cc5658 Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43100
diff changeset
1831 array_buffer_relbuf(arrayobject *self, Py_buffer *view)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1832 {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1833 self->ob_exports--;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1834 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1835
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1836 static PySequenceMethods array_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1837 (lenfunc)array_length, /*sq_length*/
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
1838 (binaryfunc)array_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1839 (ssizeargfunc)array_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1840 (ssizeargfunc)array_item, /*sq_item*/
43100
4e6b4ba60c3b Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
1841 0, /*sq_slice*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1842 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
43100
4e6b4ba60c3b Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
1843 0, /*sq_ass_slice*/
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
1844 (objobjproc)array_contains, /*sq_contains*/
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1845 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1846 (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
1847 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1848
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1849 static PyBufferProcs array_as_buffer = {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1850 (getbufferproc)array_buffer_getbuf,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1851 (releasebufferproc)array_buffer_relbuf
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1852 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1853
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1854 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1855 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
1856 {
42136
78bc078c7350 Fix a failure that was only apparent on big-endian machines:
Guido van Rossum <guido@python.org>
parents: 42125
diff changeset
1857 int c;
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1858 PyObject *initial = NULL, *it = NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1859 struct arraydescr *descr;
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34174
diff changeset
1860
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1861 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34174
diff changeset
1862 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1863
42139
1f7b66a9ffb0 Revert r56044 (which changed the %c format specifier to accept a
Walter Dörwald <walter@livinglogic.de>
parents: 42136
diff changeset
1864 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1865 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1866
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1867 if (!(initial == NULL || PyList_Check(initial)
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 45083
diff changeset
1868 || PyByteArray_Check(initial)
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1869 || PyBytes_Check(initial)
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42675
diff changeset
1870 || PyTuple_Check(initial)
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1871 || ((c=='u') && PyUnicode_Check(initial)))) {
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1872 it = PyObject_GetIter(initial);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1873 if (it == NULL)
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1874 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1875 /* We set initial to NULL so that the subsequent code
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1876 will create an empty array of the appropriate type
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1877 and afterwards we can use array_iter_extend to populate
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1878 the array.
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1879 */
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1880 initial = NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1881 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1882 for (descr = descriptors; descr->typecode != '\0'; descr++) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883 if (descr->typecode == c) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1884 PyObject *a;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1885 Py_ssize_t len;
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1886
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1887 if (initial == NULL || !(PyList_Check(initial)
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1888 || PyTuple_Check(initial)))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1889 len = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1890 else
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1891 len = PySequence_Size(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1892
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1893 a = newarrayobject(type, len, descr);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1894 if (a == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1895 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1896
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1897 if (len > 0) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1898 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
1899 for (i = 0; i < len; i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1900 PyObject *v =
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
1901 PySequence_GetItem(initial, i);
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1902 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1903 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1904 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1905 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1906 if (setarrayitem(a, i, v) != 0) {
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1907 Py_DECREF(v);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1908 Py_DECREF(a);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1909 return NULL;
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1910 }
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
1911 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1912 }
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
1913 }
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 45083
diff changeset
1914 else if (initial != NULL && (PyByteArray_Check(initial) ||
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1915 PyBytes_Check(initial))) {
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1916 PyObject *t_initial, *v;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1917 t_initial = PyTuple_Pack(1, initial);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1918 if (t_initial == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1919 Py_DECREF(a);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1920 return NULL;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1921 }
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
1922 v = array_fromstring((arrayobject *)a,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1923 t_initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1924 Py_DECREF(t_initial);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1925 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1926 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1927 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1928 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1929 Py_DECREF(v);
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
1930 }
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
1931 else if (initial != NULL && PyUnicode_Check(initial)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1932 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1933 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1934 arrayobject *self = (arrayobject *)a;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1935 char *item = self->ob_item;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
1936 item = (char *)PyMem_Realloc(item, n);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1937 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1938 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1939 Py_DECREF(a);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1940 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1941 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1942 self->ob_item = item;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1943 Py_SIZE(self) = n / sizeof(Py_UNICODE);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1944 memcpy(item, PyUnicode_AS_DATA(initial), n);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1945 self->allocated = Py_SIZE(self);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1946 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1947 }
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1948 if (it != NULL) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1949 if (array_iter_extend((arrayobject *)a, it) == -1) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1950 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1951 Py_DECREF(a);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1952 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1953 }
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1954 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
1955 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 return a;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1957 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1958 }
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
1959 PyErr_SetString(PyExc_ValueError,
42112
ebc87b94e6ea "Fix" the array module test -- by ripping out the 'c' typecode.
Guido van Rossum <guido@python.org>
parents: 41859
diff changeset
1960 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1961 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1962 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1963
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1964
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1965 PyDoc_STRVAR(module_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1966 "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
1967 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
1968 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
1969 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
1970 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
1971 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
1972 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1973 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
1974 'b' signed integer 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1975 'B' unsigned integer 1 \n\
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1976 'u' Unicode character 2 (see note) \n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1977 'h' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1978 'H' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1979 'i' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1980 'I' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1981 'l' signed integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1982 'L' unsigned integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1983 'f' floating point 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1984 'd' floating point 8 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1985 \n\
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1986 NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1987 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1988 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1989 The constructor is:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1990 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1991 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
1992 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1993
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1994 PyDoc_STRVAR(arraytype_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1995 "array(typecode [, initializer]) -> array\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1996 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1997 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
1998 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
1999 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
2000 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2001 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
2002 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
2003 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2004 Methods:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2005 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2006 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
2007 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
2008 byteswap() -- byteswap all the items of the array\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2009 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
2010 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
2011 fromfile() -- read items from a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2012 fromlist() -- append items from the list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2013 fromstring() -- append items from the string\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2014 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
2015 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
2016 pop() -- remove and return item (default last)\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2017 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
2018 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
2019 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
2020 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
2021 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
2022 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2023 Attributes:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2024 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2025 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
2026 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
2027 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2028
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2029 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
2030
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2031 static PyTypeObject Arraytype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42305
diff changeset
2032 PyVarObject_HEAD_INIT(NULL, 0)
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 21073
diff changeset
2033 "array.array",
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2034 sizeof(arrayobject),
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2035 0,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2036 (destructor)array_dealloc, /* tp_dealloc */
22124
edf05c69bbf3 Remove tp_print.
Martin v. Löwis <martin@v.loewis.de>
parents: 22098
diff changeset
2037 0, /* tp_print */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2038 0, /* tp_getattr */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2039 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50214
diff changeset
2040 0, /* tp_reserved */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2041 (reprfunc)array_repr, /* tp_repr */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2042 0, /* tp_as_number*/
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2043 &array_as_sequence, /* tp_as_sequence*/
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2044 &array_as_mapping, /* tp_as_mapping*/
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2045 0, /* tp_hash */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2046 0, /* tp_call */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2047 0, /* tp_str */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2048 PyObject_GenericGetAttr, /* tp_getattro */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2049 0, /* tp_setattro */
18036
a02085871c3f Make it possible to find the use of tp_as_buffer here with a global search.
Tim Peters <tim.peters@gmail.com>
parents: 16268
diff changeset
2050 &array_as_buffer, /* tp_as_buffer*/
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
2051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2052 arraytype_doc, /* tp_doc */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2053 0, /* tp_traverse */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2054 0, /* tp_clear */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2055 array_richcompare, /* tp_richcompare */
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
2056 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2057 (getiterfunc)array_iter, /* tp_iter */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2058 0, /* tp_iternext */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2059 array_methods, /* tp_methods */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2060 0, /* tp_members */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2061 array_getsets, /* tp_getset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2062 0, /* tp_base */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2063 0, /* tp_dict */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2064 0, /* tp_descr_get */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2065 0, /* tp_descr_set */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2066 0, /* tp_dictoffset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2067 0, /* tp_init */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2068 PyType_GenericAlloc, /* tp_alloc */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2069 array_new, /* tp_new */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2070 PyObject_Del, /* tp_free */
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2071 };
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2072
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2073
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2074 /*********************** Array Iterator **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2075
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2076 typedef struct {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2077 PyObject_HEAD
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2078 Py_ssize_t index;
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2079 arrayobject *ao;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2080 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
2081 } arrayiterobject;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2082
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2083 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
2084
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2085 #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
2086
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2087 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2088 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
2089 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2090 arrayiterobject *it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2091
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2092 if (!array_Check(ao)) {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2093 PyErr_BadInternalCall();
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2094 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2095 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2096
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2097 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2098 if (it == NULL)
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2099 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2100
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2101 Py_INCREF(ao);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2102 it->ao = ao;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2103 it->index = 0;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2104 it->getitem = ao->ob_descr->getitem;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2105 PyObject_GC_Track(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2106 return (PyObject *)it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2107 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2108
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2109 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2110 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
2111 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2112 assert(PyArrayIter_Check(it));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2113 if (it->index < Py_SIZE(it->ao))
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2114 return (*it->getitem)(it->ao, it->index++);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2115 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2116 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2117
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2118 static void
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2119 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
2120 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2121 PyObject_GC_UnTrack(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2122 Py_XDECREF(it->ao);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2123 PyObject_GC_Del(it);
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2124 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2125
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2126 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2127 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
2128 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
2129 Py_VISIT(it->ao);
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2130 return 0;
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
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2133 static PyTypeObject PyArrayIter_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42305
diff changeset
2134 PyVarObject_HEAD_INIT(NULL, 0)
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2135 "arrayiterator", /* tp_name */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2136 sizeof(arrayiterobject), /* tp_basicsize */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2137 0, /* tp_itemsize */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2138 /* methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2139 (destructor)arrayiter_dealloc, /* tp_dealloc */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2140 0, /* tp_print */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2141 0, /* tp_getattr */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2142 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50214
diff changeset
2143 0, /* tp_reserved */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2144 0, /* tp_repr */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2145 0, /* tp_as_number */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2146 0, /* tp_as_sequence */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2147 0, /* tp_as_mapping */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2148 0, /* tp_hash */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2149 0, /* tp_call */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2150 0, /* tp_str */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2151 PyObject_GenericGetAttr, /* tp_getattro */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2152 0, /* tp_setattro */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2153 0, /* tp_as_buffer */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2154 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2155 0, /* tp_doc */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2156 (traverseproc)arrayiter_traverse, /* tp_traverse */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2157 0, /* tp_clear */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2158 0, /* tp_richcompare */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2159 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28309
diff changeset
2160 PyObject_SelfIter, /* tp_iter */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2161 (iternextfunc)arrayiter_next, /* tp_iternext */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2162 0, /* tp_methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
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
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2166 /*********************** Install Module **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2167
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2168 /* No functions in array module. */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2169 static PyMethodDef a_methods[] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2170 {NULL, NULL, 0, NULL} /* Sentinel */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2171 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2172
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2173 static struct PyModuleDef arraymodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2174 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2175 "array",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2176 module_doc,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2177 -1,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2178 a_methods,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2179 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2180 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2181 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2182 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2183 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2184
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2185
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24424
diff changeset
2186 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2187 PyInit_array(void)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2188 {
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2189 PyObject *m;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2190 PyObject *typecodes;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2191 Py_ssize_t size = 0;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2192 register Py_UNICODE *p;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2193 struct arraydescr *descr;
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
2194
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
2195 if (PyType_Ready(&Arraytype) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2196 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2197 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2198 m = PyModule_Create(&arraymodule);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35449
diff changeset
2199 if (m == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2200 return NULL;
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2201
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2202 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2203 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2204 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2205 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2206
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2207 for (descr=descriptors; descr->typecode != '\0'; descr++) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2208 size++;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2209 }
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2210
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2211 typecodes = PyUnicode_FromStringAndSize(NULL, size);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2212 p = PyUnicode_AS_UNICODE(typecodes);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2213 for (descr = descriptors; descr->typecode != '\0'; descr++) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2214 *p++ = (char)descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2215 }
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2216
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2217 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2218
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2219 if (PyErr_Occurred()) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2220 Py_DECREF(m);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2221 m = NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2222 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2223 return m;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2224 }