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