annotate Modules/arraymodule.c @ 58664:993f703a61ba

Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done partially before. Also add a comment describing how this might have to work with different versions of the interpreter. ........ r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line Fixed typo ........ r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........ r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line Fix internal reference. ........ r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines data descriptors do not override the class dictionary if __get__ is not defined Adjust documentation and add a test to verify this behavior. See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for discussion. ........ r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines Do not compile stubs for the sha2 series hashes in the openssl hashlib module when the openssl version is too old to support them. That leads both compiled code bloat and to unittests attempting to test implementations that don't exist for comparison purposes on such platforms. ........ r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line Add two more test_strtod test values. ........ r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line #7762: fix refcount annotation of PyUnicode_Tailmatch(). ........ r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line #7725: fix referencing issue. ........ r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line mention from_float() in error message ........ r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line #7802: fix invalid example (heh). ........ r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines Fix-up ftplib documentation: move exception descriptions to toplevel, not inside a class remove attribution in "versionadded" spell and grammar check docstring of FTP_TLS ........ r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line Minor modification to unittest documentation. ........
author Benjamin Peterson <benjamin@python.org>
date Wed, 03 Feb 2010 02:35:45 +0000
parents 2b08de9a1fa0
children 3baabd1d68a2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Array object implementation */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* An array is a uniform list -- all items have the same type.
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 The item type is restricted to simple C types like int or float */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
6 #define PY_SSIZE_T_CLEAN
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
7 #include "Python.h"
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
8 #include "structmember.h"
4634
9facbab152cd Removed some unneeded header files and reedited with a fixed-width font
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4628
diff changeset
9
1797
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
10 #ifdef STDC_HEADERS
b167e3350491 Changes for Mac
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
11 #include <stddef.h>
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
12 #else /* !STDC_HEADERS */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
13 #ifdef HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
14 #include <sys/types.h> /* For size_t */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
15 #endif /* HAVE_SYS_TYPES_H */
11242
0061ca4865d7 Tiny patch by Mark Hammond to avoid sys/types.h if we don't have it
Guido van Rossum <guido@python.org>
parents: 10434
diff changeset
16 #endif /* !STDC_HEADERS */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 struct arrayobject; /* Forward */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
20 /* All possible arraydescr values are defined in the vector "descriptors"
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
21 * below. That's defined later because the appropriate get and set
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
22 * functions aren't visible yet.
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
23 */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 struct arraydescr {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 int typecode;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 int itemsize;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
42675
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
29 char *formats;
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
30 int is_integer_type;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
31 int is_signed;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 typedef struct arrayobject {
36544
23e23e15e7e7 Bug #1432350: arrayobject should use PyObject_VAR_HEAD
Georg Brandl <georg@python.org>
parents: 36529
diff changeset
35 PyObject_VAR_HEAD
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 char *ob_item;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
37 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
38 struct arraydescr *ob_descr;
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
39 PyObject *weakreflist; /* List of weak references */
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
40 int ob_exports; /* Number of exported buffers */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 } arrayobject;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23929
diff changeset
43 static PyTypeObject Arraytype;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
45 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
46 #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
48 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
49 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
50 {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
51 char *items;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
52 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
53
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
54 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
55 PyErr_SetString(PyExc_BufferError,
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
56 "cannot resize an array that is exporting buffers");
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
57 return -1;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
58 }
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
59
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
60 /* 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
61 to accommodate the newsize. If the newsize is 16 smaller than the
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
62 current size, then proceed with the realloc() to shrink the array.
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
63 */
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
64
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
65 if (self->allocated >= newsize &&
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
66 Py_SIZE(self) < newsize + 16 &&
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
67 self->ob_item != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
68 Py_SIZE(self) = newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
69 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
70 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
71
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
72 if (newsize == 0) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
73 PyMem_FREE(self->ob_item);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
74 self->ob_item = NULL;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
75 Py_SIZE(self) = 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
76 self->allocated = 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
77 return 0;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
78 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
79
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
80 /* 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
81 * 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
82 * 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
83 * 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
84 * system realloc().
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
85 * 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
86 * 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
87 * 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
88 * 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
89 * memory critical.
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
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
92 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
93 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
94 /* 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
95 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
96 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
97 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
98 else
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
99 items = NULL;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
100 if (items == NULL) {
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
101 PyErr_NoMemory();
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
102 return -1;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
103 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
104 self->ob_item = items;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
105 Py_SIZE(self) = newsize;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
106 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
107 return 0;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
108 }
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
109
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
110 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
122 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
123 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
124 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 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
126 if (x >= 128)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 x -= 256;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
128 return PyLong_FromLong(x);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 }
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 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
132 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
133 {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
134 short x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
135 /* 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
136 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
137 the overflow checking */
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
138 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
139 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
140 else if (x < -128) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
141 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
142 "signed char is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
143 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
144 }
13263
84879ab71b9f In b_setitem(), instead of the platform dependent CHAR_MIN and
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
145 else if (x > 127) {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
146 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
147 "signed char is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
148 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
149 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
151 ((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
152 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
155 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
156 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
157 {
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 long x = ((unsigned char *)ap->ob_item)[i];
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
159 return PyLong_FromLong(x);
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
160 }
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
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
162 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
163 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
164 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
165 unsigned char x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
166 /* '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
167 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
168 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
169 if (i >= 0)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
170 ((char *)ap->ob_item)[i] = x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
171 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
172 }
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
173
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
174 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
175 u_getitem(arrayobject *ap, Py_ssize_t i)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
176 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
178 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
179
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
180 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
181 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
182 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
183 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
184 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
185
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
187 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
188 if (len != 1) {
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
189 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
190 "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
191 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
192 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
193 if (i >= 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
194 ((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
195 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
196 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
197
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
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 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
202 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
205
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
207 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
208 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 short x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
210 /* '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
211 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
212 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 ((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
215 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
218 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
219 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
220 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
221 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
222 }
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
223
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
224 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
225 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
226 {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
227 int x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
228 /* 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
229 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
230 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
231 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
232 else if (x < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
233 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
234 "unsigned short is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
235 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
236 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
237 else if (x > USHRT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
238 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
239 "unsigned short is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
240 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
241 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
242 if (i >= 0)
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
243 ((short *)ap->ob_item)[i] = (short)x;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
244 return 0;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
245 }
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
246
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
247 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
248 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
249 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
250 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
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
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
253 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
254 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
255 {
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
256 int x;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
257 /* '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
258 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
259 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
260 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
261 ((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
262 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
263 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
264
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
265 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
266 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
267 {
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 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
269 (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
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
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
272 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
273 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
274 {
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 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
276 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
277 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
278 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
279 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
280 }
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
281 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
282 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
283 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
284 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
285 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
286 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
287 "unsigned int is less than minimum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
288 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
289 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
290 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
291
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
292 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
293 if (x > UINT_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
294 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
295 "unsigned int is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
296 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
297 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
298
4875
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
299 if (i >= 0)
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
300 ((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
301 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
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
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
304 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
305 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
306 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
307 return PyLong_FromLong(((long *)ap->ob_item)[i]);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
311 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
312 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 long x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
314 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
315 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 ((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
318 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
321 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
322 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
323 {
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
324 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
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
911040e1bb11 Add new formats B, H, I, L for unsigned data types (analogous to the
Guido van Rossum <guido@python.org>
parents: 4634
diff changeset
327 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
328 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
329 {
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 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
331 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
332 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
333 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
334 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
335 }
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
336 else {
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
337 long y;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
338 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
339 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
340 if (y < 0) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
341 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
342 "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
343 return -1;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
344 }
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
345 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
346
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
347 }
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
348 if (x > ULONG_MAX) {
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
349 PyErr_SetString(PyExc_OverflowError,
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
350 "unsigned long is greater than maximum");
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
351 return -1;
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
352 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
353
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
354 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
355 ((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
356 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
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
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
359 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
360 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
361 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
362 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
363 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
366 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
367 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 float x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
369 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
370 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 ((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
373 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
376 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
377 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
378 {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
379 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
380 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
383 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
384 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 double x;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
386 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
387 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 if (i >= 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 ((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
390 return 0;
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
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
393
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
394 /* Description of types.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
395 *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
396 * Don't forget to update typecode_to_mformat_code() if you add a new
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
397 * typecode.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
398 */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1271
diff changeset
399 static struct arraydescr descriptors[] = {
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
400 {'b', 1, b_getitem, b_setitem, "b", 1, 1},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
401 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
402 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
403 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
404 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
405 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
406 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
407 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
408 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
409 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
410 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0},
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
411 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 };
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
413
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
414 /****************************************************************************
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
415 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
416 ****************************************************************************/
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
418 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
419 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
420 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 arrayobject *op;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
422 size_t nbytes;
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 if (size < 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
425 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
428
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 nbytes = size * descr->itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 /* Check for overflow */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4875
diff changeset
431 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
432 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
434 op = (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
435 if (op == NULL) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
436 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 }
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
438 op->ob_descr = descr;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
439 op->allocated = size;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
440 op->weakreflist = NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
441 Py_SIZE(op) = size;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 if (size <= 0) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 op->ob_item = NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 else {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
446 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
447 if (op->ob_item == NULL) {
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
448 Py_DECREF(op);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
449 return PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
452 op->ob_exports = 0;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
453 return (PyObject *) op;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
456 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
457 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
458 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 register arrayobject *ap;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
460 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
461 ap = (arrayobject *)op;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
462 assert(i>=0 && i<Py_SIZE(ap));
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 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
464 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
467 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
468 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 char *items;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
470 Py_ssize_t n = Py_SIZE(self);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
472 PyErr_BadInternalCall();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 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
476 return -1;
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
477
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
478 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
479 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 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
481 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
482 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
483 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
484 where = 0;
2bb5ceef9ac3 Fix array.array.insert(), so that it treats negative indices as
Walter Dörwald <walter@livinglogic.de>
parents: 28760
diff changeset
485 }
31659
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
486 if (where > n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
487 where = n;
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
488 /* 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
489 if (where != n)
206d8575f644 Update the array overallocation scheme to match the approach used for
Raymond Hettinger <python@rcn.com>
parents: 31651
diff changeset
490 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
491 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
492 (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
493 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
494 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 /* Methods */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 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
499 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
500 {
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
501 if (op->weakreflist != NULL)
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
502 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
503 if (op->ob_item != NULL)
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
504 PyMem_DEL(op->ob_item);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
505 Py_TYPE(op)->tp_free((PyObject *)op);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
508 static PyObject *
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
509 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
510 {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
511 arrayobject *va, *wa;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
512 PyObject *vi = NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
513 PyObject *wi = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
514 Py_ssize_t i, k;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
515 PyObject *res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
516
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
517 if (!array_Check(v) || !array_Check(w)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
518 Py_INCREF(Py_NotImplemented);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
519 return Py_NotImplemented;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
520 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
521
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
522 va = (arrayobject *)v;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
523 wa = (arrayobject *)w;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
524
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
525 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
526 /* Shortcut: if the lengths differ, the arrays differ */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
527 if (op == Py_EQ)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
528 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
529 else
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
530 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
531 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
532 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
533 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
534
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
535 /* 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
536 k = 1;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
537 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
538 vi = getarrayitem(v, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
539 wi = getarrayitem(w, i);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
540 if (vi == NULL || wi == NULL) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
541 Py_XDECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
542 Py_XDECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
543 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
544 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
545 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
546 if (k == 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
547 break; /* Keeping vi and wi alive! */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
548 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
549 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
550 if (k < 0)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
551 return NULL;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
552 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
553
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
554 if (k) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
555 /* No more items to compare -- compare sizes */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
556 Py_ssize_t vs = Py_SIZE(va);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
557 Py_ssize_t ws = Py_SIZE(wa);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 int cmp;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
559 switch (op) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
560 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
561 case Py_LE: cmp = vs <= ws; break;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
562 case Py_EQ: cmp = vs == ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
563 case Py_NE: cmp = vs != ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
564 case Py_GT: cmp = vs > ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
565 case Py_GE: cmp = vs >= ws; break;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
566 default: return NULL; /* cannot happen */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
567 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
568 if (cmp)
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
569 res = Py_True;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 else
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
571 res = Py_False;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
572 Py_INCREF(res);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
573 return res;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
574 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
575
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
576 /* 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
577 if (op == Py_EQ) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
578 Py_INCREF(Py_False);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
579 res = Py_False;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
581 else if (op == Py_NE) {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
582 Py_INCREF(Py_True);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
583 res = Py_True;
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
584 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
585 else {
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
586 /* 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
587 res = PyObject_RichCompare(vi, wi, op);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
588 }
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
589 Py_DECREF(vi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
590 Py_DECREF(wi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
591 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
594 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
595 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
596 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
597 return Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
600 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
601 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
602 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
603 if (i < 0 || i >= Py_SIZE(a)) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
604 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
605 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
607 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
608 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
610 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
611 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
612 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 ilow = 0;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
616 else if (ilow > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
617 ilow = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 ihigh = ilow;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
622 else if (ihigh > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
623 ihigh = Py_SIZE(a);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
624 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
625 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 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
628 (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
629 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
631
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
632 static PyObject *
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
633 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
634 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
635 return array_slice(a, 0, Py_SIZE(a));
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
636 }
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
637
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
638 PyDoc_STRVAR(copy_doc,
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
639 "copy(array)\n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
640 \n\
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
641 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
642
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
643 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
644 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
645 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
646 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
647 arrayobject *np;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
648 if (!array_Check(bb)) {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
649 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
650 "can only append array (not \"%.200s\") to array",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
651 Py_TYPE(bb)->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 #define b ((arrayobject *)bb)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 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
656 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 }
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
659 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
660 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
661 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
662 size = Py_SIZE(a) + Py_SIZE(b);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
663 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
664 if (np == NULL) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
667 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
668 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
669 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
670 return (PyObject *)np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
674 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
675 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
676 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
677 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
678 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
679 arrayobject *np;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
681 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
682 if (n < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 n = 0;
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
684 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
685 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
686 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
687 size = Py_SIZE(a) * n;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
688 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
689 if (np == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 p = np->ob_item;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
692 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 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
694 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
695 p += nbytes;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
697 return (PyObject *) np;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 static int
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
701 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
702 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 char *item;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
704 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
705 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
706 #define b ((arrayobject *)v)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 n = 0;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
709 else if (array_Check(v)) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
710 n = Py_SIZE(b);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 if (a == b) {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 /* 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
713 int ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 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
715 if (!v)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
716 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 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
718 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 return ret;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 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
722 PyErr_BadArgument();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 else {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
727 PyErr_Format(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
728 "can only assign array (not \"%.200s\") to array slice",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
729 Py_TYPE(v)->tp_name);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 if (ilow < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 ilow = 0;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
734 else if (ilow > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
735 ilow = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 if (ihigh < 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 ihigh = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 if (ihigh < ilow)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 ihigh = ilow;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
740 else if (ihigh > Py_SIZE(a))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
741 ihigh = Py_SIZE(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 item = a->ob_item;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 d = n - (ihigh-ilow);
55113
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
744 /* Issue #4509: If the array has exported buffers and the slice
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
745 assignment would change the size of the array, fail early to make
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
746 sure we don't modify it. */
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
747 if (d != 0 && a->ob_exports > 0) {
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
748 PyErr_SetString(PyExc_BufferError,
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
749 "cannot resize an array that is exporting buffers");
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
750 return -1;
8e8157445a55 Issue 4509: Do not modify an array if we know the change would result
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52781
diff changeset
751 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 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
753 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
754 item + ihigh*a->ob_descr->itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
755 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
756 if (array_resize(a, Py_SIZE(a) + d) == -1)
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
757 return -1;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 else if (d > 0) { /* Insert d items */
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
760 if (array_resize(a, Py_SIZE(a) + d))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 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
763 item + ihigh*a->ob_descr->itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
764 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 if (n > 0)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 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
768 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
769 return 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 #undef b
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
772
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
774 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
775 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
776 if (i < 0 || i >= Py_SIZE(a)) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
777 PyErr_SetString(PyExc_IndexError,
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
778 "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
779 return -1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 if (v == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 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
783 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
784 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
785
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
787 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
788 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
789 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
790 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
791 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
792
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
793 static int
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
794 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
795 {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
796 PyObject *it, *v;
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 it = PyObject_GetIter(bb);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
799 if (it == NULL)
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
800 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
801
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
802 while ((v = PyIter_Next(it)) != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
803 if (ins1(self, (int) Py_SIZE(self), v) != 0) {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
804 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
805 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
806 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
807 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
808 Py_DECREF(v);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
809 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
810 Py_DECREF(it);
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
811 if (PyErr_Occurred())
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
812 return -1;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
813 return 0;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
814 }
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
815
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
816 static int
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
817 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
818 {
55115
a76b34cfc897 Fix array.extend and array.__iadd__ to handle the case where an array
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55113
diff changeset
819 Py_ssize_t size, oldsize, bbsize;
a76b34cfc897 Fix array.extend and array.__iadd__ to handle the case where an array
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55113
diff changeset
820
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
821 if (!array_Check(bb))
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
822 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
823 #define b ((arrayobject *)bb)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
824 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
825 PyErr_SetString(PyExc_TypeError,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
826 "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
827 return -1;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
828 }
48321
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
829 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
830 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
831 PyErr_NoMemory();
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
832 return -1;
f6726f32ef70 Merged revisions 64119,64147,64150,64165,64219-64221,64229-64230,64233,64235,64253,64278,64280,64301,64303,64320,64328,64338-64339 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47680
diff changeset
833 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
834 oldsize = Py_SIZE(self);
55115
a76b34cfc897 Fix array.extend and array.__iadd__ to handle the case where an array
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55113
diff changeset
835 /* Get the size of bb before resizing the array since bb could be self. */
a76b34cfc897 Fix array.extend and array.__iadd__ to handle the case where an array
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55113
diff changeset
836 bbsize = Py_SIZE(bb);
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
837 size = oldsize + Py_SIZE(b);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
838 if (array_resize(self, size) == -1)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
839 return -1;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
840 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
55115
a76b34cfc897 Fix array.extend and array.__iadd__ to handle the case where an array
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55113
diff changeset
841 b->ob_item, bbsize * b->ob_descr->itemsize);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
842
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
843 return 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
844 #undef b
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
845 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
846
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
847 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
848 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
849 {
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
850 if (!array_Check(bb)) {
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
851 PyErr_Format(PyExc_TypeError,
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
852 "can only extend array with array (not \"%.200s\")",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
853 Py_TYPE(bb)->tp_name);
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
854 return NULL;
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
855 }
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
856 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
857 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
858 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
859 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
860 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
861
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
862 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
863 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
864 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
865 char *items, *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
866 Py_ssize_t size, i;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
867
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
868 if (Py_SIZE(self) > 0) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
869 if (n < 0)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
870 n = 0;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
871 items = self->ob_item;
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
872 if ((self->ob_descr->itemsize != 0) &&
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
873 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
874 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
875 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
876 size = Py_SIZE(self) * self->ob_descr->itemsize;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
877 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
878 return PyErr_NoMemory();
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
879 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
880 if (array_resize(self, n * Py_SIZE(self)) == -1)
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
881 return NULL;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
882 items = p = self->ob_item;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
883 for (i = 1; i < n; i++) {
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
884 p += size;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
885 memcpy(p, items, size);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
886 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
887 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
888 Py_INCREF(self);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
889 return (PyObject *)self;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
890 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
891
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
892
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
893 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
894 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
895 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 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
897 return NULL;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
898 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
899 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
901
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
902 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
903 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
904 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
905 Py_ssize_t count = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
906 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
907
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
908 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
909 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
910 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
911 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
912 if (cmp > 0)
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
913 count++;
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
914 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
915 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
916 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
917 return PyLong_FromSsize_t(count);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
918 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
919
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
920 PyDoc_STRVAR(count_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
921 "count(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
922 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
923 Return number of occurrences of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
924
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
925 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
926 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
927 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
928 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
929
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
930 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
931 PyObject *selfi = getarrayitem((PyObject *)self, i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
932 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
933 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
934 if (cmp > 0) {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
935 return PyLong_FromLong((long)i);
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
936 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
937 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
938 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
939 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
940 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
941 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
942 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
943
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
944 PyDoc_STRVAR(index_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
945 "index(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
946 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
947 Return index of first occurrence of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
948
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
949 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
950 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
951 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
952 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
953 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
954
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
955 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
956 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
957 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
958 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
959 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
960 return cmp;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
961 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
962
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
963 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
964 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
965 {
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
966 int i;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
967
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
968 for (i = 0; i < Py_SIZE(self); i++) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
969 PyObject *selfi = getarrayitem((PyObject *)self,i);
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
970 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
971 Py_DECREF(selfi);
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
972 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 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
974 (PyObject *)NULL) != 0)
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
975 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
976 Py_INCREF(Py_None);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
977 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
978 }
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
979 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
980 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
981 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
982 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
983 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
984 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
985
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
986 PyDoc_STRVAR(remove_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
987 "remove(x)\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
988 \n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
989 Remove the first occurrence of x in the array.");
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
990
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
991 static PyObject *
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
992 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
993 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
994 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
995 PyObject *v;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
996 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
997 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
998 if (Py_SIZE(self) == 0) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
999 /* 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
1000 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
1001 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1002 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1003 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1004 i += Py_SIZE(self);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1005 if (i < 0 || i >= Py_SIZE(self)) {
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1006 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
1007 return NULL;
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 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
1010 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
1011 Py_DECREF(v);
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1012 return NULL;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1013 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1014 return v;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1015 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1016
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1017 PyDoc_STRVAR(pop_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1018 "pop([i])\n\
13772
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1019 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1020 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
1021
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1022 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1023 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
1024 {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1025 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
1026 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1027 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
1028 return Py_None;
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1029 }
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1030
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1031 PyDoc_STRVAR(extend_doc,
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1032 "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
1033 \n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
1034 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
1035
eea51275acbf added count, extend, index, pop and remove to arraymodule
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13651
diff changeset
1036 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
1037 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
1038 {
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
1039 Py_ssize_t i;
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1040 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
1041 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
1042 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 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
1044 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1046 PyDoc_STRVAR(insert_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1047 "insert(i,x)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1048 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1049 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
1050
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1051
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1052 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1053 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
1054 {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1055 PyObject* retval = NULL;
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1056 retval = PyTuple_New(2);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1057 if (!retval)
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1058 return NULL;
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1059
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1060 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1061 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
13068
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1062
705132667f7b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12814
diff changeset
1063 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
1064 }
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1065
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1066 PyDoc_STRVAR(buffer_info_doc,
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1067 "buffer_info() -> (address, length)\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1068 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1069 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
1070 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
1071 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
1072 the buffer length in bytes.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1073
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1074
5982
6aebd970b7a9 Added buffer_info() method that returns address and length in bytes of
Guido van Rossum <guido@python.org>
parents: 5696
diff changeset
1075 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1076 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
1077 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1078 return ins(self, (int) Py_SIZE(self), v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1081 PyDoc_STRVAR(append_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1082 "append(x)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1083 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1084 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
1085
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1086
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1087 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1088 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
1089 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1091 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
1092
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 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
1094 case 1:
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 2:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1097 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
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 p[0] = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 p[1] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 break;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 case 4:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1104 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 char p0 = p[0];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 char p1 = p[1];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 p[0] = p[3];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108 p[1] = p[2];
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 p[2] = p1;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 p[3] = p0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 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
1113 case 8:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1114 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1115 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
1116 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
1117 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
1118 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
1119 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
1120 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
1121 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
1122 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
1123 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
1124 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
1125 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
1126 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
1127 }
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1128 break;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 default:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1130 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
1131 "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
1132 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1134 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1135 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1136 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1138 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
1139 "byteswap()\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1140 \n\
11522
cbf58cd0bb57 Correct the docstring for byteswap(); error noted by Bernhard Reiter
Fred Drake <fdrake@acm.org>
parents: 11242
diff changeset
1141 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
1142 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
1143
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1144 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1145 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
1146 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1147 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
1148 register char *p, *q;
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1149 /* little buffer to hold items while swapping */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1150 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
1151 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
1152
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1153 if (Py_SIZE(self) > 1) {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1154 for (p = self->ob_item,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1155 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1156 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
1157 p += itemsize, q -= itemsize) {
14649
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1158 /* memory areas guaranteed disjoint, so memcpy
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1159 * is safe (& memmove may be slower).
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1160 */
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1161 memcpy(tmp, p, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1162 memcpy(p, q, itemsize);
1ec6c63347ea arraymodule: Fix SF bug 113960.
Tim Peters <tim.peters@gmail.com>
parents: 14499
diff changeset
1163 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
1164 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 }
14499
7f23ec5cd9ca General cleanup in preparation for a bugfix: removed unused code, useless
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1166
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1167 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1168 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 }
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1170
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1171 PyDoc_STRVAR(reverse_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1172 "reverse()\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1173 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1174 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
1175
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1176
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1177 /* Forward */
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1178 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
1179
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1180 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
1181 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
1182 {
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1183 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
1184 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
1185 Py_ssize_t n, nbytes;
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1186 int not_enough_bytes;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1187
36529
442c0fb3c4ed Use correct format specifier for Py_ssize_t variable to PyArg_ParseTuple().
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1188 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
1189 return NULL;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1190
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1191 nbytes = n * itemsize;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1192 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
1193 PyErr_NoMemory();
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1196
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1197 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
1198 if (b == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1199 return NULL;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1200
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1201 if (!PyBytes_Check(b)) {
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1202 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
1203 "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
1204 Py_DECREF(b);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1205 return NULL;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1207
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1208 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1209
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1210 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
1211 Py_DECREF(b);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1212 if (args == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1213 return NULL;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1214
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1215 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
1216 Py_DECREF(args);
52040
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1217 if (res == NULL)
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1218 return NULL;
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1219
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1220 if (not_enough_bytes) {
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1221 PyErr_SetString(PyExc_EOFError,
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1222 "read() didn't return enough bytes");
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1223 Py_DECREF(res);
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1224 return NULL;
9fa7949641d3 Issue #5334: array.fromfile() failed to insert values when EOFError was raised.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51776
diff changeset
1225 }
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1226
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1227 return res;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1230 PyDoc_STRVAR(fromfile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1231 "fromfile(f, n)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1232 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1233 Read n objects from the file object f and append them to the end of the\n\
45083
c66da57b9488 Remove the deprecated array.read/write methods.
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
1234 array.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1235
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1236
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1237 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1238 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
1239 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1240 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1241 /* 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
1242 /* 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
1243 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
1244 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
1245 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
1246
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1247 if (Py_SIZE(self) == 0)
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1248 goto done;
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1249
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1250 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
1251 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
1252 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
1253 PyObject *bytes, *res;
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1254 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
1255 size = nbytes - i*BLOCKSIZE;
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1256 bytes = PyBytes_FromStringAndSize(ptr, size);
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1257 if (bytes == NULL)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 return NULL;
41859
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1259 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
1260 Py_DECREF(bytes);
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1261 if (res == NULL)
e4e8b12770e4 Fixed array.fromfile(); removed references to PyFileObject in array.tofile().
Guido van Rossum <guido@python.org>
parents: 41788
diff changeset
1262 return NULL;
42540
aab687936ecd Fix reference leak.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
1263 Py_DECREF(res); /* drop write result */
41434
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1264 }
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1265
f390793e75de Make array().tofile() work with a new I/O object.
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1266 done:
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1267 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1268 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1271 PyDoc_STRVAR(tofile_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1272 "tofile(f)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1273 \n\
45083
c66da57b9488 Remove the deprecated array.read/write methods.
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
1274 Write all items (as machine values) to the file object f.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1275
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1276
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1277 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1278 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
1279 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1280 Py_ssize_t n;
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1281
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1282 if (!PyList_Check(list)) {
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1283 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
1284 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1286 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
1287 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1288 Py_ssize_t i, old_size;
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1289 old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1290 if (array_resize(self, old_size + n) == -1)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 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
1293 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
1294 if ((*self->ob_descr->setitem)(self,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1295 Py_SIZE(self) - n + i, v) != 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1296 array_resize(self, old_size);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1301 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1302 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1305 PyDoc_STRVAR(fromlist_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1306 "fromlist(list)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1307 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1308 Append items to array from list.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1309
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1310 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1311 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
1312 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1313 PyObject *list = PyList_New(Py_SIZE(self));
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1314 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
1315
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316 if (list == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1318 for (i = 0; i < Py_SIZE(self); i++) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1319 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
1320 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1321 Py_DECREF(list);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1324 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
1325 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 return list;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1329 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
1330 "tolist() -> list\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1331 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1332 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
1333
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1334
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1335 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
1336 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
1337 {
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338 char *str;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1339 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
1340 int itemsize = self->ob_descr->itemsize;
13773
b6d93dba120e replaced PyArgs_Parse by PyArgs_ParseTuple
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13772
diff changeset
1341 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
1342 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 if (n % itemsize != 0) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1344 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
1345 "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
1346 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 n = n / itemsize;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1350 Py_ssize_t old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1351 if ((n > PY_SSIZE_T_MAX - old_size) ||
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1352 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1353 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1354 }
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1355 if (array_resize(self, old_size + n) == -1)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 return NULL;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1357 memcpy(self->ob_item + old_size * itemsize,
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1358 str, n * itemsize);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1360 Py_INCREF(Py_None);
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1361 return Py_None;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1364 PyDoc_STRVAR(fromstring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1365 "fromstring(string)\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1366 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1367 Appends items from the string, interpreting it as an array of machine\n\
42125
79c2656056f6 Make array.typecode a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 42112
diff changeset
1368 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
1369
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1370
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1371 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1372 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
1373 {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1374 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1375 return PyBytes_FromStringAndSize(self->ob_item,
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1376 Py_SIZE(self) * self->ob_descr->itemsize);
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1377 } else {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1378 return PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48321
diff changeset
1379 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1382 PyDoc_STRVAR(tostring_doc,
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1383 "tostring() -> string\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1384 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1385 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
1386 representation.");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
1387
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1388
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1389
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1390 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1391 array_fromunicode(arrayobject *self, PyObject *args)
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1392 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1393 Py_UNICODE *ustr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1394 Py_ssize_t n;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1395 char typecode;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1396
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1397 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
1398 return NULL;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1399 typecode = self->ob_descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1400 if ((typecode != 'u')) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1401 PyErr_SetString(PyExc_ValueError,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1402 "fromunicode() may only be called on "
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1403 "unicode type arrays");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1404 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1405 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1406 if (n > 0) {
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1407 Py_ssize_t old_size = Py_SIZE(self);
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1408 if (array_resize(self, old_size + n) == -1)
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1409 return NULL;
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
1410 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1411 ustr, n * sizeof(Py_UNICODE));
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1412 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1413
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1414 Py_INCREF(Py_None);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1415 return Py_None;
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1418 PyDoc_STRVAR(fromunicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1419 "fromunicode(ustr)\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1420 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1421 Extends this array with data from the unicode string ustr.\n\
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1422 The array must be a unicode type array; otherwise a ValueError\n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1423 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
1424 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
1425
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1426
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1427 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1428 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
1429 {
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1430 char typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1431 typecode = self->ob_descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1432 if ((typecode != 'u')) {
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1433 PyErr_SetString(PyExc_ValueError,
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
1434 "tounicode() may only be called on unicode type arrays");
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1435 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1436 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
1437 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1438 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1439
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
1440 PyDoc_STRVAR(tounicode_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1441 "tounicode() -> unicode\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1442 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1443 Convert the array to a unicode string. The array must be\n\
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
1444 a unicode type array; otherwise a ValueError is raised. Use\n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1445 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
1446 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
1447
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
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1450 /*********************** Pickling support ************************/
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1451
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1452 enum machine_format_code {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1453 UNKNOWN_FORMAT = -1,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1454 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1455 * array type code cannot be interpreted. When this occurs, a list of
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1456 * Python objects is used to represent the content of the array
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1457 * instead of using the memory content of the array directly. In that
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1458 * case, the array_reconstructor mechanism is bypassed completely, and
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1459 * the standard array constructor is used instead.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1460 *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1461 * This is will most likely occur when the machine doesn't use IEEE
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1462 * floating-point numbers.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1463 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1464
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1465 UNSIGNED_INT8 = 0,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1466 SIGNED_INT8 = 1,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1467 UNSIGNED_INT16_LE = 2,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1468 UNSIGNED_INT16_BE = 3,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1469 SIGNED_INT16_LE = 4,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1470 SIGNED_INT16_BE = 5,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1471 UNSIGNED_INT32_LE = 6,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1472 UNSIGNED_INT32_BE = 7,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1473 SIGNED_INT32_LE = 8,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1474 SIGNED_INT32_BE = 9,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1475 UNSIGNED_INT64_LE = 10,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1476 UNSIGNED_INT64_BE = 11,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1477 SIGNED_INT64_LE = 12,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1478 SIGNED_INT64_BE = 13,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1479 IEEE_754_FLOAT_LE = 14,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1480 IEEE_754_FLOAT_BE = 15,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1481 IEEE_754_DOUBLE_LE = 16,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1482 IEEE_754_DOUBLE_BE = 17,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1483 UTF16_LE = 18,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1484 UTF16_BE = 19,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1485 UTF32_LE = 20,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1486 UTF32_BE = 21
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1487 };
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1488 #define MACHINE_FORMAT_CODE_MIN 0
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1489 #define MACHINE_FORMAT_CODE_MAX 21
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1490
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1491 static const struct mformatdescr {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1492 size_t size;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1493 int is_signed;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1494 int is_big_endian;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1495 } mformat_descriptors[] = {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1496 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1497 {1, 1, 0}, /* 1: SIGNED_INT8 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1498 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1499 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1500 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1501 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1502 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1503 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1504 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1505 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1506 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1507 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1508 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1509 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1510 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1511 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1512 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1513 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1514 {4, 0, 0}, /* 18: UTF16_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1515 {4, 0, 1}, /* 19: UTF16_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1516 {8, 0, 0}, /* 20: UTF32_LE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1517 {8, 0, 1} /* 21: UTF32_BE */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1518 };
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1519
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1520
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1521 /*
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1522 * Internal: This function is used to find the machine format of a given
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1523 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1524 * be found.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1525 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1526 static enum machine_format_code
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1527 typecode_to_mformat_code(int typecode)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1528 {
55276
fc8fb8eff20e Use the proper autoconf macro for checking byte order.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55261
diff changeset
1529 #ifdef WORDS_BIGENDIAN
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1530 const int is_big_endian = 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1531 #else
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1532 const int is_big_endian = 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1533 #endif
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1534 size_t intsize;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1535 int is_signed;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1536
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1537 switch (typecode) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1538 case 'b':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1539 return SIGNED_INT8;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1540 case 'B':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1541 return UNSIGNED_INT8;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1542
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1543 case 'u':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1544 if (sizeof(Py_UNICODE) == 2) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1545 return UTF16_LE + is_big_endian;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1546 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1547 if (sizeof(Py_UNICODE) == 4) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1548 return UTF32_LE + is_big_endian;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1549 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1550 return UNKNOWN_FORMAT;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1551
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1552 case 'f':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1553 if (sizeof(float) == 4) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1554 const float y = 16711938.0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1555 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1556 return IEEE_754_FLOAT_BE;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1557 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1558 return IEEE_754_FLOAT_LE;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1559 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1560 return UNKNOWN_FORMAT;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1561
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1562 case 'd':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1563 if (sizeof(double) == 8) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1564 const double x = 9006104071832581.0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1565 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1566 return IEEE_754_DOUBLE_BE;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1567 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1568 return IEEE_754_DOUBLE_LE;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1569 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1570 return UNKNOWN_FORMAT;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1571
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1572 /* Integers */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1573 case 'h':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1574 intsize = sizeof(short);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1575 is_signed = 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1576 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1577 case 'H':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1578 intsize = sizeof(short);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1579 is_signed = 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1580 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1581 case 'i':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1582 intsize = sizeof(int);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1583 is_signed = 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1584 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1585 case 'I':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1586 intsize = sizeof(int);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1587 is_signed = 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1588 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1589 case 'l':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1590 intsize = sizeof(long);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1591 is_signed = 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1592 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1593 case 'L':
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1594 intsize = sizeof(long);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1595 is_signed = 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1596 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1597 default:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1598 return UNKNOWN_FORMAT;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1599 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1600 switch (intsize) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1601 case 2:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1602 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1603 case 4:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1604 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1605 case 8:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1606 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1607 default:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1608 return UNKNOWN_FORMAT;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1609 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1610 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1611
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1612 /* Forward declaration. */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1613 static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1614
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1615 /*
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1616 * Internal: This function wraps the array constructor--i.e., array_new()--to
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1617 * allow the creation of array objects from C code without having to deal
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1618 * directly the tuple argument of array_new(). The typecode argument is a
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1619 * Unicode character value, like 'i' or 'f' for example, representing an array
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1620 * type code. The items argument is a bytes or a list object from which
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1621 * contains the initial value of the array.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1622 *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1623 * On success, this functions returns the array object created. Otherwise,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1624 * NULL is returned to indicate a failure.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1625 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1626 static PyObject *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1627 make_array(PyTypeObject *arraytype, int typecode, PyObject *items)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1628 {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1629 PyObject *new_args;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1630 PyObject *array_obj;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1631 PyObject *typecode_obj;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1632 Py_UNICODE typecode_str[1] = {typecode};
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1633
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1634 assert(arraytype != NULL);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1635 assert(items != NULL);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1636
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1637 typecode_obj = PyUnicode_FromUnicode(typecode_str, 1);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1638 if (typecode_obj == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1639 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1640
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1641 new_args = PyTuple_New(2);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1642 if (new_args == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1643 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1644 Py_INCREF(items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1645 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1646 PyTuple_SET_ITEM(new_args, 1, items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1647
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1648 array_obj = array_new(arraytype, new_args, NULL);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1649 Py_DECREF(new_args);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1650 if (array_obj == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1651 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1652
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1653 return array_obj;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1654 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1655
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1656 /*
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1657 * This functions is a special constructor used when unpickling an array. It
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1658 * provides a portable way to rebuild an array from its memory representation.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1659 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1660 static PyObject *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1661 array_reconstructor(PyObject *self, PyObject *args)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1662 {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1663 PyTypeObject *arraytype;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1664 PyObject *items;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1665 PyObject *converted_items;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1666 PyObject *result;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1667 int typecode;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1668 enum machine_format_code mformat_code;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1669 struct arraydescr *descr;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1670
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1671 if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1672 &arraytype, &typecode, &mformat_code, &items))
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1673 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1674
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1675 if (!PyType_Check(arraytype)) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1676 PyErr_Format(PyExc_TypeError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1677 "first argument must a type object, not %.200s",
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1678 Py_TYPE(arraytype)->tp_name);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1679 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1680 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1681 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1682 PyErr_Format(PyExc_TypeError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1683 "%.200s is not a subtype of %.200s",
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1684 arraytype->tp_name, Arraytype.tp_name);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1685 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1686 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1687 for (descr = descriptors; descr->typecode != '\0'; descr++) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1688 if (descr->typecode == typecode)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1689 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1690 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1691 if (descr->typecode == '\0') {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1692 PyErr_SetString(PyExc_ValueError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1693 "second argument must be a valid type code");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1694 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1695 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1696 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1697 mformat_code > MACHINE_FORMAT_CODE_MAX) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1698 PyErr_SetString(PyExc_ValueError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1699 "third argument must be a valid machine format code.");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1700 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1701 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1702 if (!PyBytes_Check(items)) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1703 PyErr_Format(PyExc_TypeError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1704 "fourth argument should be bytes, not %.200s",
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1705 Py_TYPE(items)->tp_name);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1706 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1707 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1708
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1709 /* Fast path: No decoding has to be done. */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1710 if (mformat_code == typecode_to_mformat_code(typecode) ||
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1711 mformat_code == UNKNOWN_FORMAT) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1712 return make_array(arraytype, typecode, items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1713 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1714
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1715 /* Slow path: Decode the byte string according to the given machine
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1716 * format code. This occurs when the computer unpickling the array
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1717 * object is architecturally different from the one that pickled the
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1718 * array.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1719 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1720 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1721 PyErr_SetString(PyExc_ValueError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1722 "string length not a multiple of item size");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1723 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1724 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1725 switch (mformat_code) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1726 case IEEE_754_FLOAT_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1727 case IEEE_754_FLOAT_BE: {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1728 int i;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1729 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1730 Py_ssize_t itemcount = Py_SIZE(items) / 4;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1731 const unsigned char *memstr =
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1732 (unsigned char *)PyBytes_AS_STRING(items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1733
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1734 converted_items = PyList_New(itemcount);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1735 if (converted_items == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1736 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1737 for (i = 0; i < itemcount; i++) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1738 PyObject *pyfloat = PyFloat_FromDouble(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1739 _PyFloat_Unpack4(&memstr[i * 4], le));
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1740 if (pyfloat == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1741 Py_DECREF(converted_items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1742 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1743 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1744 PyList_SET_ITEM(converted_items, i, pyfloat);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1745 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1746 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1747 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1748 case IEEE_754_DOUBLE_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1749 case IEEE_754_DOUBLE_BE: {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1750 int i;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1751 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1752 Py_ssize_t itemcount = Py_SIZE(items) / 8;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1753 const unsigned char *memstr =
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1754 (unsigned char *)PyBytes_AS_STRING(items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1755
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1756 converted_items = PyList_New(itemcount);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1757 if (converted_items == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1758 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1759 for (i = 0; i < itemcount; i++) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1760 PyObject *pyfloat = PyFloat_FromDouble(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1761 _PyFloat_Unpack8(&memstr[i * 8], le));
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1762 if (pyfloat == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1763 Py_DECREF(converted_items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1764 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1765 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1766 PyList_SET_ITEM(converted_items, i, pyfloat);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1767 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1768 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1769 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1770 case UTF16_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1771 case UTF16_BE: {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1772 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1773 converted_items = PyUnicode_DecodeUTF16(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1774 PyBytes_AS_STRING(items), Py_SIZE(items),
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1775 "strict", &byteorder);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1776 if (converted_items == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1777 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1778 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1779 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1780 case UTF32_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1781 case UTF32_BE: {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1782 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1783 converted_items = PyUnicode_DecodeUTF32(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1784 PyBytes_AS_STRING(items), Py_SIZE(items),
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1785 "strict", &byteorder);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1786 if (converted_items == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1787 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1788 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1789 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1790
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1791 case UNSIGNED_INT8:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1792 case SIGNED_INT8:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1793 case UNSIGNED_INT16_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1794 case UNSIGNED_INT16_BE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1795 case SIGNED_INT16_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1796 case SIGNED_INT16_BE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1797 case UNSIGNED_INT32_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1798 case UNSIGNED_INT32_BE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1799 case SIGNED_INT32_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1800 case SIGNED_INT32_BE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1801 case UNSIGNED_INT64_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1802 case UNSIGNED_INT64_BE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1803 case SIGNED_INT64_LE:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1804 case SIGNED_INT64_BE: {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1805 int i;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1806 const struct mformatdescr mf_descr =
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1807 mformat_descriptors[mformat_code];
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1808 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1809 const unsigned char *memstr =
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1810 (unsigned char *)PyBytes_AS_STRING(items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1811 struct arraydescr *descr;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1812
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1813 /* If possible, try to pack array's items using a data type
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1814 * that fits better. This may result in an array with narrower
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1815 * or wider elements.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1816 *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1817 * For example, if a 32-bit machine pickles a L-code array of
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1818 * unsigned longs, then the array will be unpickled by 64-bit
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1819 * machine as an I-code array of unsigned ints.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1820 *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1821 * XXX: Is it possible to write a unit test for this?
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1822 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1823 for (descr = descriptors; descr->typecode != '\0'; descr++) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1824 if (descr->is_integer_type &&
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1825 descr->itemsize == mf_descr.size &&
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1826 descr->is_signed == mf_descr.is_signed)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1827 typecode = descr->typecode;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1828 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1829
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1830 converted_items = PyList_New(itemcount);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1831 if (converted_items == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1832 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1833 for (i = 0; i < itemcount; i++) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1834 PyObject *pylong;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1835
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1836 pylong = _PyLong_FromByteArray(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1837 &memstr[i * mf_descr.size],
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1838 mf_descr.size,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1839 !mf_descr.is_big_endian,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1840 mf_descr.is_signed);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1841 if (pylong == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1842 Py_DECREF(converted_items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1843 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1844 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1845 PyList_SET_ITEM(converted_items, i, pylong);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1846 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1847 break;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1848 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1849 case UNKNOWN_FORMAT:
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1850 /* Impossible, but needed to shut up GCC about the unhandled
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1851 * enumeration value.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1852 */
56745
7b603680bf29 Silence gcc warning about possibly uninitialized variable 'converted_items'
Mark Dickinson <dickinsm@gmail.com>
parents: 55276
diff changeset
1853 default:
7b603680bf29 Silence gcc warning about possibly uninitialized variable 'converted_items'
Mark Dickinson <dickinsm@gmail.com>
parents: 55276
diff changeset
1854 PyErr_BadArgument();
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1855 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1856 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1857
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1858 result = make_array(arraytype, typecode, converted_items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1859 Py_DECREF(converted_items);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1860 return result;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1861 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1862
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1863 static PyObject *
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1864 array_reduce_ex(arrayobject *array, PyObject *value)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1865 {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1866 PyObject *dict;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1867 PyObject *result;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1868 PyObject *array_str;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1869 int typecode = array->ob_descr->typecode;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1870 int mformat_code;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1871 static PyObject *array_reconstructor = NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1872 long protocol;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1873
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1874 if (array_reconstructor == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1875 PyObject *array_module = PyImport_ImportModule("array");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1876 if (array_module == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1877 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1878 array_reconstructor = PyObject_GetAttrString(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1879 array_module,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1880 "_array_reconstructor");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1881 Py_DECREF(array_module);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1882 if (array_reconstructor == NULL)
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1883 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1884 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1885
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1886 if (!PyLong_Check(value)) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1887 PyErr_SetString(PyExc_TypeError,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1888 "__reduce_ex__ argument should an integer");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1889 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1890 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1891 protocol = PyLong_AsLong(value);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1892 if (protocol == -1 && PyErr_Occurred())
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1893 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1894
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1895 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1896 if (dict == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1897 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1898 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1899 PyErr_Clear();
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1900 dict = Py_None;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1901 Py_INCREF(dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1902 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1903
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1904 mformat_code = typecode_to_mformat_code(typecode);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1905 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1906 /* Convert the array to a list if we got something weird
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1907 * (e.g., non-IEEE floats), or we are pickling the array using
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1908 * a Python 2.x compatible protocol.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1909 *
55261
a8922723a92d Fix omission in a comment.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55259
diff changeset
1910 * It is necessary to use a list representation for Python 2.x
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1911 * compatible pickle protocol, since Python 2's str objects
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1912 * are unpickled as unicode by Python 3. Thus it is impossible
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1913 * to make arrays unpicklable by Python 3 by using their memory
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1914 * representation, unless we resort to ugly hacks such as
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1915 * coercing unicode objects to bytes in array_reconstructor.
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1916 */
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1917 PyObject *list;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1918 list = array_tolist(array, NULL);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1919 if (list == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1920 Py_DECREF(dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1921 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1922 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1923 result = Py_BuildValue(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1924 "O(CO)O", Py_TYPE(array), typecode, list, dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1925 Py_DECREF(list);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1926 Py_DECREF(dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1927 return result;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1928 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1929
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1930 array_str = array_tostring(array, NULL);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1931 if (array_str == NULL) {
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1932 Py_DECREF(dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1933 return NULL;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1934 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1935 result = Py_BuildValue(
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1936 "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1937 mformat_code, array_str, dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1938 Py_DECREF(dict);
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1939 return result;
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1940 }
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1941
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1942 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1943
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1944 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1945 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
1946 {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1947 char tc = a->ob_descr->typecode;
42125
79c2656056f6 Make array.typecode a unicode string.
Walter Dörwald <walter@livinglogic.de>
parents: 42112
diff changeset
1948 return PyUnicode_FromStringAndSize(&tc, 1);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1949 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1950
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1951 static PyObject *
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1952 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
1953 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1954 return PyLong_FromLong((long)a->ob_descr->itemsize);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1955 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1956
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1957 static PyGetSetDef array_getsets [] = {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1958 {"typecode", (getter) array_get_typecode, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1959 "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
1960 {"itemsize", (getter) array_get_itemsize, NULL,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1961 "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
1962 {NULL}
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1963 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1964
47680
b5e02775fffc Merged revisions 64226 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47580
diff changeset
1965 static PyMethodDef array_methods[] = {
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1966 {"append", (PyCFunction)array_append, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1967 append_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1968 {"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
1969 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
1970 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1971 byteswap_doc},
31651
35cb8114b45c SF bug #910986: copy.copy fails for array.array
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1972 {"__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
1973 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1974 {"count", (PyCFunction)array_count, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1975 count_doc},
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
1976 {"__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
1977 copy_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1978 {"extend", (PyCFunction)array_extend, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1979 extend_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1980 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1981 fromfile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1982 {"fromlist", (PyCFunction)array_fromlist, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1983 fromlist_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1984 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1985 fromstring_doc},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1986 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
1987 fromunicode_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1988 {"index", (PyCFunction)array_index, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1989 index_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1990 {"insert", (PyCFunction)array_insert, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1991 insert_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1992 {"pop", (PyCFunction)array_pop, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1993 pop_doc},
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1994 {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
1995 reduce_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1996 {"remove", (PyCFunction)array_remove, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1997 remove_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
1998 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
1999 reverse_doc},
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2000 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2001 sort_doc},*/
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
2002 {"tofile", (PyCFunction)array_tofile, METH_O,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2003 tofile_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
2004 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2005 tolist_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
2006 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2007 tostring_doc},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 24471
diff changeset
2008 {"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
2009 tounicode_doc},
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2010 {NULL, NULL} /* sentinel */
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2011 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2012
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2013 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
2014 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
2015 {
41788
00413f70ce5c Remove unused variables.
Walter Dörwald <walter@livinglogic.de>
parents: 41782
diff changeset
2016 char typecode;
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
2017 PyObject *s, *v = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2018 Py_ssize_t len;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2019
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2020 len = Py_SIZE(a);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2021 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
2022 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
2023 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
2024 }
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2025 if ((typecode == 'u'))
28742
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
2026 v = array_tounicode(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
2027 else
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
2028 v = array_tolist(a, NULL);
bd678e65f784 SF Patch 685051: fix for 680789: reprs in arraymodule
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
2029
41782
bee8d01ef42c Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
2030 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
2031 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2032 return s;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2033 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2034
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2035 static PyObject*
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2036 array_subscr(arrayobject* self, PyObject* item)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2037 {
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
2038 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
2039 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
2040 if (i==-1 && PyErr_Occurred()) {
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2041 return NULL;
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36877
diff changeset
2042 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2043 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2044 i += Py_SIZE(self);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2045 return array_item(self, i);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2046 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2047 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2048 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
2049 PyObject* result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2050 arrayobject* ar;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2051 int itemsize = self->ob_descr->itemsize;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2052
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2053 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2054 &start, &stop, &step, &slicelength) < 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2055 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2056 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2057
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2058 if (slicelength <= 0) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2059 return newarrayobject(&Arraytype, 0, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2060 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2061 else if (step == 1) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2062 PyObject *result = newarrayobject(&Arraytype,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2063 slicelength, self->ob_descr);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2064 if (result == NULL)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2065 return NULL;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2066 memcpy(((arrayobject *)result)->ob_item,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2067 self->ob_item + start * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2068 slicelength * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2069 return result;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2070 }
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2071 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2072 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2073 if (!result) return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2074
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2075 ar = (arrayobject*)result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2076
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2077 for (cur = start, i = 0; i < slicelength;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2078 cur += step, i++) {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2079 memcpy(ar->ob_item + i*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2080 self->ob_item + cur*itemsize,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2081 itemsize);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2082 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2083
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2084 return result;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2085 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2086 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2087 else {
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2088 PyErr_SetString(PyExc_TypeError,
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2089 "array indices must be integers");
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2090 return NULL;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2091 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2092 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2093
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2094 static int
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2095 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
2096 {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2097 Py_ssize_t start, stop, step, slicelength, needed;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2098 arrayobject* other;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2099 int itemsize;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2100
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
2101 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
2102 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2103
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2104 if (i == -1 && PyErr_Occurred())
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2105 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2106 if (i < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2107 i += Py_SIZE(self);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2108 if (i < 0 || i >= Py_SIZE(self)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2109 PyErr_SetString(PyExc_IndexError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2110 "array assignment index out of range");
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2111 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2112 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2113 if (value == NULL) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2114 /* Fall through to slice assignment */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2115 start = i;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2116 stop = i + 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2117 step = 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2118 slicelength = 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2119 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2120 else
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2121 return (*self->ob_descr->setitem)(self, i, value);
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2122 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2123 else if (PySlice_Check(item)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2124 if (PySlice_GetIndicesEx((PySliceObject *)item,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2125 Py_SIZE(self), &start, &stop,
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2126 &step, &slicelength) < 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2127 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2128 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2129 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2130 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2131 PyErr_SetString(PyExc_TypeError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2132 "array indices must be integer");
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2133 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2134 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2135 if (value == NULL) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2136 other = NULL;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2137 needed = 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2138 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2139 else if (array_Check(value)) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2140 other = (arrayobject *)value;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2141 needed = Py_SIZE(other);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2142 if (self == other) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2143 /* Special case "self[i:j] = self" -- copy self first */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2144 int ret;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2145 value = array_slice(other, 0, needed);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2146 if (value == NULL)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2147 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2148 ret = array_ass_subscr(self, item, value);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2149 Py_DECREF(value);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2150 return ret;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2151 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2152 if (other->ob_descr != self->ob_descr) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2153 PyErr_BadArgument();
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2154 return -1;
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2155 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2156 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2157 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2158 PyErr_Format(PyExc_TypeError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2159 "can only assign array (not \"%.200s\") to array slice",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2160 Py_TYPE(value)->tp_name);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2161 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2162 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2163 itemsize = self->ob_descr->itemsize;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2164 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2165 if ((step > 0 && stop < start) ||
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2166 (step < 0 && stop > start))
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2167 stop = start;
55124
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2168
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2169 /* Issue #4509: If the array has exported buffers and the slice
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2170 assignment would change the size of the array, fail early to make
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2171 sure we don't modify it. */
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2172 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2173 PyErr_SetString(PyExc_BufferError,
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2174 "cannot resize an array that is exporting buffers");
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2175 return -1;
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2176 }
7bb7758e6d64 Add the fix for issue 4509 to the mapping methods.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55115
diff changeset
2177
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2178 if (step == 1) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2179 if (slicelength > needed) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2180 memmove(self->ob_item + (start + needed) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2181 self->ob_item + stop * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2182 (Py_SIZE(self) - stop) * itemsize);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2183 if (array_resize(self, Py_SIZE(self) +
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
2184 needed - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2185 return -1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2186 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2187 else if (slicelength < needed) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2188 if (array_resize(self, Py_SIZE(self) +
50214
aebf7124dd32 Merged revisions 67840 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48683
diff changeset
2189 needed - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2190 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2191 memmove(self->ob_item + (start + needed) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2192 self->ob_item + stop * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2193 (Py_SIZE(self) - start - needed) * itemsize);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2194 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2195 if (needed > 0)
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2196 memcpy(self->ob_item + start * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2197 other->ob_item, needed * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2198 return 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2199 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2200 else if (needed == 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2201 /* Delete slice */
58563
2b08de9a1fa0 Merged revisions 77821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56745
diff changeset
2202 size_t cur;
2b08de9a1fa0 Merged revisions 77821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56745
diff changeset
2203 Py_ssize_t i;
2b08de9a1fa0 Merged revisions 77821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56745
diff changeset
2204
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2205 if (step < 0) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2206 stop = start + 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2207 start = stop + step * (slicelength - 1) - 1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2208 step = -step;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2209 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2210 for (cur = start, i = 0; i < slicelength;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2211 cur += step, i++) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2212 Py_ssize_t lim = step - 1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2213
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2214 if (cur + step >= Py_SIZE(self))
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2215 lim = Py_SIZE(self) - cur - 1;
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2216 memmove(self->ob_item + (cur - i) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2217 self->ob_item + (cur + 1) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2218 lim * itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2219 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2220 cur = start + slicelength * step;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2221 if (cur < Py_SIZE(self)) {
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2222 memmove(self->ob_item + (cur-slicelength) * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2223 self->ob_item + cur * itemsize,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2224 (Py_SIZE(self) - cur) * itemsize);
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2225 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2226 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2227 return -1;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2228 return 0;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2229 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2230 else {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2231 Py_ssize_t cur, i;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2232
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2233 if (needed != slicelength) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2234 PyErr_Format(PyExc_ValueError,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2235 "attempt to assign array of size %zd "
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2236 "to extended slice of size %zd",
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2237 needed, slicelength);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2238 return -1;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2239 }
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2240 for (cur = start, i = 0; i < slicelength;
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2241 cur += step, i++) {
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2242 memcpy(self->ob_item + cur * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2243 other->ob_item + i * itemsize,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2244 itemsize);
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2245 }
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42856
diff changeset
2246 return 0;
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2247 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2248 }
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2249
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2250 static PyMappingMethods array_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2251 (lenfunc)array_length,
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2252 (binaryfunc)array_subscr,
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2253 (objobjargproc)array_ass_subscr
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2254 };
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23832
diff changeset
2255
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
2256 static const void *emptybuf = "";
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41434
diff changeset
2257
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2258
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2259 static int
43423
0476a2cc5658 Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43100
diff changeset
2260 array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2261 {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2262 if (view==NULL) goto finish;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2263
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2264 view->buf = (void *)self->ob_item;
48683
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48565
diff changeset
2265 view->obj = (PyObject*)self;
cd3b0b7fd8e1 Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48565
diff changeset
2266 Py_INCREF(self);
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2267 if (view->buf == NULL)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2268 view->buf = (void *)emptybuf;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2269 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2270 view->readonly = 0;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2271 view->ndim = 1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2272 view->itemsize = self->ob_descr->itemsize;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2273 view->suboffsets = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2274 view->shape = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2275 if ((flags & PyBUF_ND)==PyBUF_ND) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2276 view->shape = &((Py_SIZE(self)));
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2277 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2278 view->strides = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2279 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2280 view->strides = &(view->itemsize);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2281 view->format = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2282 view->internal = NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2283 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
42675
cd4591b754e0 Fix memory leak in arraymodule.c and respond to a few comments by nnorwitz.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42669
diff changeset
2284 view->format = self->ob_descr->formats;
43555
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
2285 #ifdef Py_UNICODE_WIDE
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
2286 if (self->ob_descr->typecode == 'u') {
43565
b4035d3665bb Fix typo: formats -> format. (Only mattered in wide unicode builds.)
Guido van Rossum <guido@python.org>
parents: 43555
diff changeset
2287 view->format = "w";
43555
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
2288 }
8e72dacf7f3a Eliminate use of PyBUF_CHARACTER flag which is no longer part of the buffer interface. Fix up array module to export the correct format for wide-builds.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43554
diff changeset
2289 #endif
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2290 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2291
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2292 finish:
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2293 self->ob_exports++;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2294 return 0;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2295 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2296
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2297 static void
43423
0476a2cc5658 Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43100
diff changeset
2298 array_buffer_relbuf(arrayobject *self, Py_buffer *view)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2299 {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2300 self->ob_exports--;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2301 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2302
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2303 static PySequenceMethods array_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2304 (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
2305 (binaryfunc)array_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2306 (ssizeargfunc)array_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2307 (ssizeargfunc)array_item, /*sq_item*/
43100
4e6b4ba60c3b Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
2308 0, /*sq_slice*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2309 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
43100
4e6b4ba60c3b Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents: 42954
diff changeset
2310 0, /*sq_ass_slice*/
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2311 (objobjproc)array_contains, /*sq_contains*/
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2312 (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
2313 (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
2314 };
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2315
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2316 static PyBufferProcs array_as_buffer = {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2317 (getbufferproc)array_buffer_getbuf,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42540
diff changeset
2318 (releasebufferproc)array_buffer_relbuf
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2319 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2320
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2321 static PyObject *
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2322 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
2323 {
42136
78bc078c7350 Fix a failure that was only apparent on big-endian machines:
Guido van Rossum <guido@python.org>
parents: 42125
diff changeset
2324 int c;
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2325 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
2326 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
2327
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
2328 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
2329 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2330
42139
1f7b66a9ffb0 Revert r56044 (which changed the %c format specifier to accept a
Walter Dörwald <walter@livinglogic.de>
parents: 42136
diff changeset
2331 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2332 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2333
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2334 if (!(initial == NULL || PyList_Check(initial)
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 45083
diff changeset
2335 || PyByteArray_Check(initial)
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
2336 || PyBytes_Check(initial)
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42675
diff changeset
2337 || PyTuple_Check(initial)
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2338 || ((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
2339 it = PyObject_GetIter(initial);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2340 if (it == NULL)
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2341 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2342 /* 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
2343 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
2344 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
2345 the array.
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2346 */
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2347 initial = NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2348 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2349 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
2350 if (descr->typecode == c) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2351 PyObject *a;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2352 Py_ssize_t len;
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
2353
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
2354 if (initial == NULL || !(PyList_Check(initial)
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
2355 || PyTuple_Check(initial)))
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2356 len = 0;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2357 else
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
2358 len = PySequence_Size(initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2359
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2360 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
2361 if (a == NULL)
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2362 return NULL;
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2363
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2364 if (len > 0) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2365 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
2366 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
2367 PyObject *v =
28760
22d177682ae1 SF 686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 28742
diff changeset
2368 PySequence_GetItem(initial, i);
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2369 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2370 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2371 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2372 }
30066
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2373 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
2374 Py_DECREF(v);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2375 Py_DECREF(a);
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2376 return NULL;
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2377 }
ae0242e3459e SF bug #782369: Massive memory leak in array module
Raymond Hettinger <python@rcn.com>
parents: 29244
diff changeset
2378 Py_DECREF(v);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2379 }
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
2380 }
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 45083
diff changeset
2381 else if (initial != NULL && (PyByteArray_Check(initial) ||
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
2382 PyBytes_Check(initial))) {
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2383 PyObject *t_initial, *v;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2384 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
2385 if (t_initial == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2386 Py_DECREF(a);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2387 return NULL;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2388 }
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36689
diff changeset
2389 v = array_fromstring((arrayobject *)a,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2390 t_initial);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2391 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
2392 if (v == NULL) {
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2393 Py_DECREF(a);
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2394 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2395 }
4628
7df5d4cf8277 Renamed Grandly (I think).
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
2396 Py_DECREF(v);
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
2397 }
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43565
diff changeset
2398 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
2399 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
2400 if (n > 0) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2401 arrayobject *self = (arrayobject *)a;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2402 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
2403 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
2404 if (item == NULL) {
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2405 PyErr_NoMemory();
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2406 Py_DECREF(a);
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2407 return NULL;
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2408 }
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2409 self->ob_item = item;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2410 Py_SIZE(self) = n / sizeof(Py_UNICODE);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2411 memcpy(item, PyUnicode_AS_DATA(initial), n);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2412 self->allocated = Py_SIZE(self);
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2413 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2414 }
33316
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2415 if (it != NULL) {
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2416 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
2417 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2418 Py_DECREF(a);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2419 return NULL;
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2420 }
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2421 Py_DECREF(it);
0b181bf230c9 SF feature request #992967: array.array objects should support sequences.
Raymond Hettinger <python@rcn.com>
parents: 32159
diff changeset
2422 }
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2423 return a;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2424 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2425 }
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
2426 PyErr_SetString(PyExc_ValueError,
42112
ebc87b94e6ea "Fix" the array module test -- by ripping out the 'c' typecode.
Guido van Rossum <guido@python.org>
parents: 41859
diff changeset
2427 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2428 return NULL;
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2429 }
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2430
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2431
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
2432 PyDoc_STRVAR(module_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2433 "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
2434 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
2435 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
2436 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
2437 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
2438 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
2439 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2440 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
2441 'b' signed integer 1 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2442 'B' unsigned integer 1 \n\
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2443 'u' Unicode character 2 (see note) \n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2444 'h' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2445 'H' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2446 'i' signed integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2447 'I' unsigned integer 2 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2448 'l' signed integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2449 'L' unsigned integer 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2450 'f' floating point 4 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2451 'd' floating point 8 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2452 \n\
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2453 NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2454 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2455 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2456 The constructor is:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2457 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2458 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
2459 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2460
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23433
diff changeset
2461 PyDoc_STRVAR(arraytype_doc,
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2462 "array(typecode [, initializer]) -> array\n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2463 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2464 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
2465 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
2466 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
2467 \n\
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2468 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
2469 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
2470 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2471 Methods:\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2472 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2473 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
2474 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
2475 byteswap() -- byteswap all the items of the array\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2476 count() -- return number of occurrences of an object\n\
31660
35f56e08727f SF feature request #686323: Minor array module enhancements
Raymond Hettinger <python@rcn.com>
parents: 31659
diff changeset
2477 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
2478 fromfile() -- read items from a file object\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2479 fromlist() -- append items from the list\n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2480 fromstring() -- append items from the string\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2481 index() -- return index of first occurrence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2482 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
2483 pop() -- remove and return item (default last)\n\
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51271
diff changeset
2484 remove() -- remove first occurrence of an object\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2485 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
2486 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
2487 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
2488 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
2489 \n\
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2490 Attributes:\n\
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2491 \n\
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2492 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
2493 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
2494 ");
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2495
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2496 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
2497
24283
c0a67a1cab09 Removed more stray instances of statichere, but left _sre.c alone.
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2498 static PyTypeObject Arraytype = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42305
diff changeset
2499 PyVarObject_HEAD_INIT(NULL, 0)
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 21073
diff changeset
2500 "array.array",
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2501 sizeof(arrayobject),
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2502 0,
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2503 (destructor)array_dealloc, /* tp_dealloc */
22124
edf05c69bbf3 Remove tp_print.
Martin v. Löwis <martin@v.loewis.de>
parents: 22098
diff changeset
2504 0, /* tp_print */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2505 0, /* tp_getattr */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2506 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50214
diff changeset
2507 0, /* tp_reserved */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2508 (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
2509 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
2510 &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
2511 &array_as_mapping, /* tp_as_mapping*/
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2512 0, /* tp_hash */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2513 0, /* tp_call */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2514 0, /* tp_str */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2515 PyObject_GenericGetAttr, /* tp_getattro */
15975
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2516 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
2517 &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
2518 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
2519 arraytype_doc, /* tp_doc */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2520 0, /* tp_traverse */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2521 0, /* tp_clear */
54205828b1e1 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15396
diff changeset
2522 array_richcompare, /* tp_richcompare */
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31660
diff changeset
2523 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
2524 (getiterfunc)array_iter, /* tp_iter */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2525 0, /* tp_iternext */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2526 array_methods, /* tp_methods */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2527 0, /* tp_members */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2528 array_getsets, /* tp_getset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2529 0, /* tp_base */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2530 0, /* tp_dict */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2531 0, /* tp_descr_get */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2532 0, /* tp_descr_set */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2533 0, /* tp_dictoffset */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2534 0, /* tp_init */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2535 PyType_GenericAlloc, /* tp_alloc */
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2536 array_new, /* tp_new */
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 27044
diff changeset
2537 PyObject_Del, /* tp_free */
9343
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2538 };
4f78504a5f81 Doc strings by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 5982
diff changeset
2539
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2540
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2541 /*********************** Array Iterator **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2542
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2543 typedef struct {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2544 PyObject_HEAD
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2545 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
2546 arrayobject *ao;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2547 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
2548 } arrayiterobject;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2549
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2550 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
2551
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2552 #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
2553
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2554 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2555 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
2556 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2557 arrayiterobject *it;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2558
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2559 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
2560 PyErr_BadInternalCall();
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2561 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2562 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2563
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2564 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
2565 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
2566 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2567
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2568 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
2569 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
2570 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
2571 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
2572 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
2573 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
2574 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2575
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2576 static PyObject *
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2577 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
2578 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2579 assert(PyArrayIter_Check(it));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2580 if (it->index < Py_SIZE(it->ao))
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2581 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
2582 return NULL;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2583 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2584
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2585 static void
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2586 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
2587 {
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2588 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
2589 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
2590 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
2591 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2592
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2593 static int
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2594 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
2595 {
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
2596 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
2597 return 0;
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2598 }
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2599
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2600 static PyTypeObject PyArrayIter_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42305
diff changeset
2601 PyVarObject_HEAD_INIT(NULL, 0)
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2602 "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
2603 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
2604 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
2605 /* methods */
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2606 (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
2607 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
2608 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
2609 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50214
diff changeset
2610 0, /* tp_reserved */
27128
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2611 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
2612 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
2613 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
2614 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
2615 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
2616 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
2617 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
2618 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
2619 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
2620 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
2621 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
2622 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
2623 (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
2624 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
2625 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
2626 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28309
diff changeset
2627 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
2628 (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
2629 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
2630 };
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2631
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2632
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2633 /*********************** Install Module **************************/
047b6e16ab2e SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
2634
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2635 /* No functions in array module. */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2636 static PyMethodDef a_methods[] = {
55259
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
2637 {"_array_reconstructor", array_reconstructor, METH_VARARGS,
edc714ffe6cc Issue #2389: Implement a portable mechanism for pickling array objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55204
diff changeset
2638 PyDoc_STR("Internal. Used for pickling support.")},
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2639 {NULL, NULL, 0, NULL} /* Sentinel */
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2640 };
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2641
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2642 static struct PyModuleDef arraymodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2643 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2644 "array",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2645 module_doc,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2646 -1,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2647 a_methods,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2648 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2649 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2650 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2651 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2652 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2653
22098
966725095854 Patch 520694: arraymodule.c improvements:
Martin v. Löwis <martin@v.loewis.de>
parents: 21313
diff changeset
2654
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24424
diff changeset
2655 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2656 PyInit_array(void)
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2657 {
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2658 PyObject *m;
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2659 PyObject *typecodes;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2660 Py_ssize_t size = 0;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2661 register Py_UNICODE *p;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2662 struct arraydescr *descr;
11711
6546ea06e157 Patch from Paul Sokolovsky <Paul.Sokolovsky@technologist.com>:
Fred Drake <fdrake@acm.org>
parents: 11673
diff changeset
2663
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
2664 if (PyType_Ready(&Arraytype) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2665 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
2666 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2667 m = PyModule_Create(&arraymodule);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35449
diff changeset
2668 if (m == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2669 return NULL;
22722
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2670
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2671 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2672 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
2673 Py_INCREF((PyObject *)&Arraytype);
ba68dda15df0 Use the PyModule_*() API instead of manipulating the module dictionary
Fred Drake <fdrake@acm.org>
parents: 22124
diff changeset
2674 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
43548
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2675
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2676 for (descr=descriptors; descr->typecode != '\0'; descr++) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2677 size++;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2678 }
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2679
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2680 typecodes = PyUnicode_FromStringAndSize(NULL, size);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2681 p = PyUnicode_AS_UNICODE(typecodes);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2682 for (descr = descriptors; descr->typecode != '\0'; descr++) {
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2683 *p++ = (char)descr->typecode;
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2684 }
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2685
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2686 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
fb9d5306f4c7 Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
Travis E. Oliphant <oliphant@enthought.com>
parents: 43423
diff changeset
2687
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2688 if (PyErr_Occurred()) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2689 Py_DECREF(m);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2690 m = NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2691 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47500
diff changeset
2692 return m;
1235
87ed00a62d31 Added new module "array" (for now optional) defining array objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2693 }