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