Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Side by Side Diff: Modules/arraymodule.c

Issue 20152: Derby: Convert the _imp module to use Argument Clinic
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Array object implementation */ 1 /* Array object implementation */
2 2
3 /* An array is a uniform list -- all items have the same type. 3 /* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */ 4 The item type is restricted to simple C types like int or float */
5 5
6 #define PY_SSIZE_T_CLEAN 6 #define PY_SSIZE_T_CLEAN
7 #include "Python.h" 7 #include "Python.h"
8 #include "structmember.h" 8 #include "structmember.h"
9 9
10 #ifdef STDC_HEADERS 10 #ifdef STDC_HEADERS
11 #include <stddef.h> 11 #include <stddef.h>
12 #else /* !STDC_HEADERS */ 12 #else /* !STDC_HEADERS */
13 #ifdef HAVE_SYS_TYPES_H 13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h> /* For size_t */ 14 #include <sys/types.h> /* For size_t */
15 #endif /* HAVE_SYS_TYPES_H */ 15 #endif /* HAVE_SYS_TYPES_H */
16 #endif /* !STDC_HEADERS */ 16 #endif /* !STDC_HEADERS */
17
18 /*[clinic input]
19 module array
20 [clinic start generated code]*/
21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/
17 22
18 struct arrayobject; /* Forward */ 23 struct arrayobject; /* Forward */
19 24
20 /* All possible arraydescr values are defined in the vector "descriptors" 25 /* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set 26 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet. 27 * functions aren't visible yet.
23 */ 28 */
24 struct arraydescr { 29 struct arraydescr {
25 char typecode; 30 char typecode;
26 int itemsize; 31 int itemsize;
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 {'Q', sizeof(PY_LONG_LONG), QQ_getitem, QQ_setitem, "Q", 1, 0}, 469 {'Q', sizeof(PY_LONG_LONG), QQ_getitem, QQ_setitem, "Q", 1, 0},
465 #endif 470 #endif
466 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, 471 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0},
467 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, 472 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0},
468 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ 473 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
469 }; 474 };
470 475
471 /**************************************************************************** 476 /****************************************************************************
472 Implementations of array object methods. 477 Implementations of array object methods.
473 ****************************************************************************/ 478 ****************************************************************************/
479
480 /*[clinic input]
481 class array.array "arrayobject *" "&Arraytype"
482 [clinic start generated code]*/
483 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
474 484
475 static PyObject * 485 static PyObject *
476 newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) 486 newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
477 { 487 {
478 arrayobject *op; 488 arrayobject *op;
479 size_t nbytes; 489 size_t nbytes;
480 490
481 if (size < 0) { 491 if (size < 0) {
482 PyErr_BadInternalCall(); 492 PyErr_BadInternalCall();
483 return NULL; 493 return NULL;
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 else if (ihigh > Py_SIZE(a)) 687 else if (ihigh > Py_SIZE(a))
678 ihigh = Py_SIZE(a); 688 ihigh = Py_SIZE(a);
679 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); 689 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
680 if (np == NULL) 690 if (np == NULL)
681 return NULL; 691 return NULL;
682 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, 692 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
683 (ihigh-ilow) * a->ob_descr->itemsize); 693 (ihigh-ilow) * a->ob_descr->itemsize);
684 return (PyObject *)np; 694 return (PyObject *)np;
685 } 695 }
686 696
697 /*[clinic input]
698 array.array.__copy__
loewis 2014/07/27 18:17:09 You might want to say "as array_copy" here to pres
699
700 Return a copy of the array.
701 [clinic start generated code]*/
702
703 PyDoc_STRVAR(array_array___copy____doc__,
704 "sig=($self)\n"
705 "Return a copy of the array.");
706
707 #define ARRAY_ARRAY___COPY___METHODDEF \
708 {"__copy__", (PyCFunction)array_array___copy__, METH_NOARGS, array_array___c opy____doc__},
709
687 static PyObject * 710 static PyObject *
688 array_copy(arrayobject *a, PyObject *unused) 711 array_array___copy___impl(arrayobject *self);
712
713 static PyObject *
714 array_array___copy__(arrayobject *self, PyObject *Py_UNUSED(ignored))
689 { 715 {
690 return array_slice(a, 0, Py_SIZE(a)); 716 return array_array___copy___impl(self);
691 } 717 }
692 718
693 PyDoc_STRVAR(copy_doc, 719 static PyObject *
694 "copy(array)\n\ 720 array_array___copy___impl(arrayobject *self)
695 \n\ 721 /*[clinic end generated code: output=4e201ec5e44d9150 input=ad1ee5b086965f09]*/
696 Return a copy of the array."); 722 {
723 return array_slice(self, 0, Py_SIZE(self));
724 }
725
726 /*[clinic input]
727 array.array.__deepcopy__
728
729 unused: object
730 /
731
732 Return a copy of the array.
733 [clinic start generated code]*/
734
735 PyDoc_STRVAR(array_array___deepcopy____doc__,
736 "sig=($self, unused)\n"
737 "Return a copy of the array.");
738
739 #define ARRAY_ARRAY___DEEPCOPY___METHODDEF \
740 {"__deepcopy__", (PyCFunction)array_array___deepcopy__, METH_O, array_array_ __deepcopy____doc__},
741
742 static PyObject *
743 array_array___deepcopy__(arrayobject *self, PyObject *unused)
744 /*[clinic end generated code: output=91feac49095306e2 input=2405ecb4933748c4]*/
745 {
746 return array_array___copy___impl(self);
747 }
697 748
698 static PyObject * 749 static PyObject *
699 array_concat(arrayobject *a, PyObject *bb) 750 array_concat(arrayobject *a, PyObject *bb)
700 { 751 {
701 Py_ssize_t size; 752 Py_ssize_t size;
702 arrayobject *np; 753 arrayobject *np;
703 if (!array_Check(bb)) { 754 if (!array_Check(bb)) {
704 PyErr_Format(PyExc_TypeError, 755 PyErr_Format(PyExc_TypeError,
705 "can only append array (not \"%.200s\") to array", 756 "can only append array (not \"%.200s\") to array",
706 Py_TYPE(bb)->tp_name); 757 Py_TYPE(bb)->tp_name);
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 1005
955 static PyObject * 1006 static PyObject *
956 ins(arrayobject *self, Py_ssize_t where, PyObject *v) 1007 ins(arrayobject *self, Py_ssize_t where, PyObject *v)
957 { 1008 {
958 if (ins1(self, where, v) != 0) 1009 if (ins1(self, where, v) != 0)
959 return NULL; 1010 return NULL;
960 Py_INCREF(Py_None); 1011 Py_INCREF(Py_None);
961 return Py_None; 1012 return Py_None;
962 } 1013 }
963 1014
1015 /*[clinic input]
1016 array.array.count
1017
1018 v: object
1019 /
1020
1021 Return number of occurrences of v in the array.
1022 [clinic start generated code]*/
1023
1024 PyDoc_STRVAR(array_array_count__doc__,
1025 "sig=($self, v)\n"
1026 "Return number of occurrences of v in the array.");
1027
1028 #define ARRAY_ARRAY_COUNT_METHODDEF \
1029 {"count", (PyCFunction)array_array_count, METH_O, array_array_count__doc__},
1030
964 static PyObject * 1031 static PyObject *
965 array_count(arrayobject *self, PyObject *v) 1032 array_array_count(arrayobject *self, PyObject *v)
1033 /*[clinic end generated code: output=12af41368a806391 input=d9bce9d65e39d1f5]*/
966 { 1034 {
967 Py_ssize_t count = 0; 1035 Py_ssize_t count = 0;
968 Py_ssize_t i; 1036 Py_ssize_t i;
969 1037
970 for (i = 0; i < Py_SIZE(self); i++) { 1038 for (i = 0; i < Py_SIZE(self); i++) {
971 PyObject *selfi; 1039 PyObject *selfi;
972 int cmp; 1040 int cmp;
973 1041
974 selfi = getarrayitem((PyObject *)self, i); 1042 selfi = getarrayitem((PyObject *)self, i);
975 if (selfi == NULL) 1043 if (selfi == NULL)
976 return NULL; 1044 return NULL;
977 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); 1045 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
978 Py_DECREF(selfi); 1046 Py_DECREF(selfi);
979 if (cmp > 0) 1047 if (cmp > 0)
980 count++; 1048 count++;
981 else if (cmp < 0) 1049 else if (cmp < 0)
982 return NULL; 1050 return NULL;
983 } 1051 }
984 return PyLong_FromSsize_t(count); 1052 return PyLong_FromSsize_t(count);
985 } 1053 }
986 1054
987 PyDoc_STRVAR(count_doc, 1055 /*[clinic input]
988 "count(x)\n\ 1056 array.array.index
989 \n\ 1057
990 Return number of occurrences of x in the array."); 1058 v: object
loewis 2014/07/27 18:17:09 The parameter name should be x, not v.
1059 /
1060
1061 Return index of first occurrence of v in the array.
1062 [clinic start generated code]*/
1063
1064 PyDoc_STRVAR(array_array_index__doc__,
1065 "sig=($self, v)\n"
1066 "Return index of first occurrence of v in the array.");
1067
1068 #define ARRAY_ARRAY_INDEX_METHODDEF \
1069 {"index", (PyCFunction)array_array_index, METH_O, array_array_index__doc__},
991 1070
992 static PyObject * 1071 static PyObject *
993 array_index(arrayobject *self, PyObject *v) 1072 array_array_index(arrayobject *self, PyObject *v)
1073 /*[clinic end generated code: output=5d191defe29cc81f input=cf619898c6649d08]*/
994 { 1074 {
995 Py_ssize_t i; 1075 Py_ssize_t i;
996 1076
997 for (i = 0; i < Py_SIZE(self); i++) { 1077 for (i = 0; i < Py_SIZE(self); i++) {
998 PyObject *selfi; 1078 PyObject *selfi;
999 int cmp; 1079 int cmp;
1000 1080
1001 selfi = getarrayitem((PyObject *)self, i); 1081 selfi = getarrayitem((PyObject *)self, i);
1002 if (selfi == NULL) 1082 if (selfi == NULL)
1003 return NULL; 1083 return NULL;
1004 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); 1084 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1005 Py_DECREF(selfi); 1085 Py_DECREF(selfi);
1006 if (cmp > 0) { 1086 if (cmp > 0) {
1007 return PyLong_FromLong((long)i); 1087 return PyLong_FromLong((long)i);
1008 } 1088 }
1009 else if (cmp < 0) 1089 else if (cmp < 0)
1010 return NULL; 1090 return NULL;
1011 } 1091 }
1012 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); 1092 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
1013 return NULL; 1093 return NULL;
1014 } 1094 }
1015
1016 PyDoc_STRVAR(index_doc,
1017 "index(x)\n\
1018 \n\
1019 Return index of first occurrence of x in the array.");
1020 1095
1021 static int 1096 static int
1022 array_contains(arrayobject *self, PyObject *v) 1097 array_contains(arrayobject *self, PyObject *v)
1023 { 1098 {
1024 Py_ssize_t i; 1099 Py_ssize_t i;
1025 int cmp; 1100 int cmp;
1026 1101
1027 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) { 1102 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1028 PyObject *selfi = getarrayitem((PyObject *)self, i); 1103 PyObject *selfi = getarrayitem((PyObject *)self, i);
1029 if (selfi == NULL) 1104 if (selfi == NULL)
1030 return -1; 1105 return -1;
1031 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); 1106 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1032 Py_DECREF(selfi); 1107 Py_DECREF(selfi);
1033 } 1108 }
1034 return cmp; 1109 return cmp;
1035 } 1110 }
1036 1111
1112 /*[clinic input]
1113 array.array.remove
1114
1115 v: object
loewis 2014/07/27 18:17:09 Likewise
1116 /
1117
1118 Remove the first occurrence of v in the array.
1119 [clinic start generated code]*/
1120
1121 PyDoc_STRVAR(array_array_remove__doc__,
1122 "sig=($self, v)\n"
1123 "Remove the first occurrence of v in the array.");
1124
1125 #define ARRAY_ARRAY_REMOVE_METHODDEF \
1126 {"remove", (PyCFunction)array_array_remove, METH_O, array_array_remove__doc_ _},
1127
1037 static PyObject * 1128 static PyObject *
1038 array_remove(arrayobject *self, PyObject *v) 1129 array_array_remove(arrayobject *self, PyObject *v)
1130 /*[clinic end generated code: output=2f02bf510da4b071 input=0b1e5aed25590027]*/
1039 { 1131 {
1040 int i; 1132 int i;
1041 1133
1042 for (i = 0; i < Py_SIZE(self); i++) { 1134 for (i = 0; i < Py_SIZE(self); i++) {
1043 PyObject *selfi; 1135 PyObject *selfi;
1044 int cmp; 1136 int cmp;
1045 1137
1046 selfi = getarrayitem((PyObject *)self,i); 1138 selfi = getarrayitem((PyObject *)self,i);
1047 if (selfi == NULL) 1139 if (selfi == NULL)
1048 return NULL; 1140 return NULL;
1049 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); 1141 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1050 Py_DECREF(selfi); 1142 Py_DECREF(selfi);
1051 if (cmp > 0) { 1143 if (cmp > 0) {
1052 if (array_ass_slice(self, i, i+1, 1144 if (array_ass_slice(self, i, i+1,
1053 (PyObject *)NULL) != 0) 1145 (PyObject *)NULL) != 0)
1054 return NULL; 1146 return NULL;
1055 Py_INCREF(Py_None); 1147 Py_INCREF(Py_None);
1056 return Py_None; 1148 return Py_None;
1057 } 1149 }
1058 else if (cmp < 0) 1150 else if (cmp < 0)
1059 return NULL; 1151 return NULL;
1060 } 1152 }
1061 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); 1153 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
1062 return NULL; 1154 return NULL;
1063 } 1155 }
1064 1156
1065 PyDoc_STRVAR(remove_doc, 1157 /*[clinic input]
1066 "remove(x)\n\ 1158 array.array.pop
1067 \n\ 1159
1068 Remove the first occurrence of x in the array."); 1160 i: Py_ssize_t = -1
1161 /
1162
1163 Return the i-th element and delete it from the array.
1164
1165 i defaults to -1.
1166 [clinic start generated code]*/
1167
1168 PyDoc_STRVAR(array_array_pop__doc__,
1169 "sig=($self, i=-1)\n"
1170 "Return the i-th element and delete it from the array.\n"
1171 "\n"
1172 "i defaults to -1.");
1173
1174 #define ARRAY_ARRAY_POP_METHODDEF \
1175 {"pop", (PyCFunction)array_array_pop, METH_VARARGS, array_array_pop__doc__},
1069 1176
1070 static PyObject * 1177 static PyObject *
1071 array_pop(arrayobject *self, PyObject *args) 1178 array_array_pop_impl(arrayobject *self, Py_ssize_t i);
1179
1180 static PyObject *
1181 array_array_pop(arrayobject *self, PyObject *args)
1072 { 1182 {
1183 PyObject *return_value = NULL;
1073 Py_ssize_t i = -1; 1184 Py_ssize_t i = -1;
1185
1186 if (!PyArg_ParseTuple(args,
1187 "|n:pop",
1188 &i))
1189 goto exit;
1190 return_value = array_array_pop_impl(self, i);
1191
1192 exit:
1193 return return_value;
1194 }
1195
1196 static PyObject *
1197 array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1198 /*[clinic end generated code: output=8c1af528d6f81e01 input=8e5feb4c1a11cd44]*/
1199 {
1074 PyObject *v; 1200 PyObject *v;
1075 if (!PyArg_ParseTuple(args, "|n:pop", &i)) 1201
1076 return NULL;
1077 if (Py_SIZE(self) == 0) { 1202 if (Py_SIZE(self) == 0) {
1078 /* Special-case most common failure cause */ 1203 /* Special-case most common failure cause */
1079 PyErr_SetString(PyExc_IndexError, "pop from empty array"); 1204 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1080 return NULL; 1205 return NULL;
1081 } 1206 }
1082 if (i < 0) 1207 if (i < 0)
1083 i += Py_SIZE(self); 1208 i += Py_SIZE(self);
1084 if (i < 0 || i >= Py_SIZE(self)) { 1209 if (i < 0 || i >= Py_SIZE(self)) {
1085 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 1210 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1086 return NULL; 1211 return NULL;
1087 } 1212 }
1088 v = getarrayitem((PyObject *)self, i); 1213 v = getarrayitem((PyObject *)self, i);
1089 if (v == NULL) 1214 if (v == NULL)
1090 return NULL; 1215 return NULL;
1091 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { 1216 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1092 Py_DECREF(v); 1217 Py_DECREF(v);
1093 return NULL; 1218 return NULL;
1094 } 1219 }
1095 return v; 1220 return v;
1096 } 1221 }
1097 1222
1098 PyDoc_STRVAR(pop_doc, 1223 /*[clinic input]
1099 "pop([i])\n\ 1224 array.array.extend
1100 \n\ 1225
1101 Return the i-th element and delete it from the array. i defaults to -1."); 1226 bb: object
loewis 2014/07/27 18:17:09 The parameter name in the documentation is "iterab
1227 /
1228
1229 Append items to the end of the array.
1230 [clinic start generated code]*/
1231
1232 PyDoc_STRVAR(array_array_extend__doc__,
1233 "sig=($self, bb)\n"
1234 "Append items to the end of the array.");
1235
1236 #define ARRAY_ARRAY_EXTEND_METHODDEF \
1237 {"extend", (PyCFunction)array_array_extend, METH_O, array_array_extend__doc_ _},
1102 1238
1103 static PyObject * 1239 static PyObject *
1104 array_extend(arrayobject *self, PyObject *bb) 1240 array_array_extend(arrayobject *self, PyObject *bb)
1241 /*[clinic end generated code: output=b21f71cc7d359ced input=43be86aba5c31e44]*/
1105 { 1242 {
1106 if (array_do_extend(self, bb) == -1) 1243 if (array_do_extend(self, bb) == -1)
1107 return NULL; 1244 return NULL;
1108 Py_INCREF(Py_None); 1245 Py_INCREF(Py_None);
1109 return Py_None; 1246 return Py_None;
1110 } 1247 }
1111 1248
1112 PyDoc_STRVAR(extend_doc, 1249 /*[clinic input]
1113 "extend(array or iterable)\n\ 1250 array.array.insert
1114 \n\ 1251
1115 Append items to the end of the array."); 1252 i: Py_ssize_t
1253 v: object
loewis 2014/07/27 18:17:09 The documentation calls the parameters i and x.
1254 /
1255
1256 Insert a new item v into the array before position i.
1257 [clinic start generated code]*/
1258
1259 PyDoc_STRVAR(array_array_insert__doc__,
1260 "sig=($self, i, v)\n"
1261 "Insert a new item v into the array before position i.");
1262
1263 #define ARRAY_ARRAY_INSERT_METHODDEF \
1264 {"insert", (PyCFunction)array_array_insert, METH_VARARGS, array_array_insert __doc__},
1116 1265
1117 static PyObject * 1266 static PyObject *
1118 array_insert(arrayobject *self, PyObject *args) 1267 array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v);
1268
1269 static PyObject *
1270 array_array_insert(arrayobject *self, PyObject *args)
1119 { 1271 {
1272 PyObject *return_value = NULL;
1120 Py_ssize_t i; 1273 Py_ssize_t i;
1121 PyObject *v; 1274 PyObject *v;
1122 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) 1275
1123 return NULL; 1276 if (!PyArg_ParseTuple(args,
1277 "nO:insert",
1278 &i, &v))
1279 goto exit;
1280 return_value = array_array_insert_impl(self, i, v);
1281
1282 exit:
1283 return return_value;
1284 }
1285
1286 static PyObject *
1287 array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1288 /*[clinic end generated code: output=1e35f58d9950a26e input=5577d1b4383e9313]*/
1289 {
1124 return ins(self, i, v); 1290 return ins(self, i, v);
1125 } 1291 }
1126 1292
1127 PyDoc_STRVAR(insert_doc, 1293 /*[clinic input]
1128 "insert(i,x)\n\ 1294 array.array.buffer_info
1129 \n\
1130 Insert a new item x into the array before position i.");
1131 1295
1296 Return a tuple (address, length) giving the current memory address and the lengt h in items of the buffer used to hold array's contents.
1297
1298 The length should be multiplied by the itemsize attribute to calculate
1299 the buffer length in bytes.
1300 [clinic start generated code]*/
1301
1302 PyDoc_STRVAR(array_array_buffer_info__doc__,
1303 "sig=($self)\n"
1304 "Return a tuple (address, length) giving the current memory address and the leng th in items of the buffer used to hold array\'s contents.\n"
1305 "\n"
1306 "The length should be multiplied by the itemsize attribute to calculate\n"
1307 "the buffer length in bytes.");
1308
1309 #define ARRAY_ARRAY_BUFFER_INFO_METHODDEF \
1310 {"buffer_info", (PyCFunction)array_array_buffer_info, METH_NOARGS, array_arr ay_buffer_info__doc__},
1132 1311
1133 static PyObject * 1312 static PyObject *
1134 array_buffer_info(arrayobject *self, PyObject *unused) 1313 array_array_buffer_info_impl(arrayobject *self);
1314
1315 static PyObject *
1316 array_array_buffer_info(arrayobject *self, PyObject *Py_UNUSED(ignored))
1317 {
1318 return array_array_buffer_info_impl(self);
1319 }
1320
1321 static PyObject *
1322 array_array_buffer_info_impl(arrayobject *self)
1323 /*[clinic end generated code: output=328dda50f20426dd input=a58bae5c6e1ac6a6]*/
1135 { 1324 {
1136 PyObject *retval = NULL, *v; 1325 PyObject *retval = NULL, *v;
1137 1326
1138 retval = PyTuple_New(2); 1327 retval = PyTuple_New(2);
1139 if (!retval) 1328 if (!retval)
1140 return NULL; 1329 return NULL;
1141 1330
1142 v = PyLong_FromVoidPtr(self->ob_item); 1331 v = PyLong_FromVoidPtr(self->ob_item);
1143 if (v == NULL) { 1332 if (v == NULL) {
1144 Py_DECREF(retval); 1333 Py_DECREF(retval);
1145 return NULL; 1334 return NULL;
1146 } 1335 }
1147 PyTuple_SET_ITEM(retval, 0, v); 1336 PyTuple_SET_ITEM(retval, 0, v);
1148 1337
1149 v = PyLong_FromLong((long)(Py_SIZE(self))); 1338 v = PyLong_FromLong((long)(Py_SIZE(self)));
1150 if (v == NULL) { 1339 if (v == NULL) {
1151 Py_DECREF(retval); 1340 Py_DECREF(retval);
1152 return NULL; 1341 return NULL;
1153 } 1342 }
1154 PyTuple_SET_ITEM(retval, 1, v); 1343 PyTuple_SET_ITEM(retval, 1, v);
1155 1344
1156 return retval; 1345 return retval;
1157 } 1346 }
1158 1347
1159 PyDoc_STRVAR(buffer_info_doc, 1348 /*[clinic input]
1160 "buffer_info() -> (address, length)\n\ 1349 array.array.append
1161 \n\
1162 Return a tuple (address, length) giving the current memory address and\n\
1163 the length in items of the buffer used to hold array's contents\n\
1164 The length should be multiplied by the itemsize attribute to calculate\n\
1165 the buffer length in bytes.");
1166 1350
1351 v: object
1352 /
1353
1354 Append new value v to the end of the array.
1355 [clinic start generated code]*/
1356
1357 PyDoc_STRVAR(array_array_append__doc__,
1358 "sig=($self, v)\n"
1359 "Append new value v to the end of the array.");
1360
1361 #define ARRAY_ARRAY_APPEND_METHODDEF \
1362 {"append", (PyCFunction)array_array_append, METH_O, array_array_append__doc_ _},
1167 1363
1168 static PyObject * 1364 static PyObject *
1169 array_append(arrayobject *self, PyObject *v) 1365 array_array_append(arrayobject *self, PyObject *v)
1366 /*[clinic end generated code: output=7f4ea50b13e7b90f input=0b98d9d78e78f0fa]*/
1170 { 1367 {
1171 return ins(self, Py_SIZE(self), v); 1368 return ins(self, Py_SIZE(self), v);
1172 } 1369 }
1173 1370
1174 PyDoc_STRVAR(append_doc, 1371 /*[clinic input]
1175 "append(x)\n\ 1372 array.array.byteswap
1176 \n\
1177 Append new value x to the end of the array.");
1178 1373
1374 Byteswap all items of the array.
1375
1376 If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1377 raised.
1378 [clinic start generated code]*/
1379
1380 PyDoc_STRVAR(array_array_byteswap__doc__,
1381 "sig=($self)\n"
1382 "Byteswap all items of the array.\n"
1383 "\n"
1384 "If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is\ n"
1385 "raised.");
1386
1387 #define ARRAY_ARRAY_BYTESWAP_METHODDEF \
1388 {"byteswap", (PyCFunction)array_array_byteswap, METH_NOARGS, array_array_byt eswap__doc__},
1179 1389
1180 static PyObject * 1390 static PyObject *
1181 array_byteswap(arrayobject *self, PyObject *unused) 1391 array_array_byteswap_impl(arrayobject *self);
1392
1393 static PyObject *
1394 array_array_byteswap(arrayobject *self, PyObject *Py_UNUSED(ignored))
1395 {
1396 return array_array_byteswap_impl(self);
1397 }
1398
1399 static PyObject *
1400 array_array_byteswap_impl(arrayobject *self)
1401 /*[clinic end generated code: output=f67be043b8d3f4dd input=6a85591b950a0186]*/
1182 { 1402 {
1183 char *p; 1403 char *p;
1184 Py_ssize_t i; 1404 Py_ssize_t i;
1185 1405
1186 switch (self->ob_descr->itemsize) { 1406 switch (self->ob_descr->itemsize) {
1187 case 1: 1407 case 1:
1188 break; 1408 break;
1189 case 2: 1409 case 2:
1190 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) { 1410 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1191 char p0 = p[0]; 1411 char p0 = p[0];
(...skipping 29 matching lines...) Expand all
1221 break; 1441 break;
1222 default: 1442 default:
1223 PyErr_SetString(PyExc_RuntimeError, 1443 PyErr_SetString(PyExc_RuntimeError,
1224 "don't know how to byteswap this array type"); 1444 "don't know how to byteswap this array type");
1225 return NULL; 1445 return NULL;
1226 } 1446 }
1227 Py_INCREF(Py_None); 1447 Py_INCREF(Py_None);
1228 return Py_None; 1448 return Py_None;
1229 } 1449 }
1230 1450
1231 PyDoc_STRVAR(byteswap_doc, 1451 /*[clinic input]
1232 "byteswap()\n\ 1452 array.array.reverse
1233 \n\ 1453
1234 Byteswap all items of the array. If the items in the array are not 1, 2,\n\ 1454 Reverse the order of the items in the array.
1235 4, or 8 bytes in size, RuntimeError is raised."); 1455 [clinic start generated code]*/
1456
1457 PyDoc_STRVAR(array_array_reverse__doc__,
1458 "sig=($self)\n"
1459 "Reverse the order of the items in the array.");
1460
1461 #define ARRAY_ARRAY_REVERSE_METHODDEF \
1462 {"reverse", (PyCFunction)array_array_reverse, METH_NOARGS, array_array_rever se__doc__},
1236 1463
1237 static PyObject * 1464 static PyObject *
1238 array_reverse(arrayobject *self, PyObject *unused) 1465 array_array_reverse_impl(arrayobject *self);
1466
1467 static PyObject *
1468 array_array_reverse(arrayobject *self, PyObject *Py_UNUSED(ignored))
1469 {
1470 return array_array_reverse_impl(self);
1471 }
1472
1473 static PyObject *
1474 array_array_reverse_impl(arrayobject *self)
1475 /*[clinic end generated code: output=1bd612a6c96e43a6 input=cd904f01b27d966a]*/
1239 { 1476 {
1240 Py_ssize_t itemsize = self->ob_descr->itemsize; 1477 Py_ssize_t itemsize = self->ob_descr->itemsize;
1241 char *p, *q; 1478 char *p, *q;
1242 /* little buffer to hold items while swapping */ 1479 /* little buffer to hold items while swapping */
1243 char tmp[256]; /* 8 is probably enough -- but why skimp */ 1480 char tmp[256]; /* 8 is probably enough -- but why skimp */
1244 assert((size_t)itemsize <= sizeof(tmp)); 1481 assert((size_t)itemsize <= sizeof(tmp));
1245 1482
1246 if (Py_SIZE(self) > 1) { 1483 if (Py_SIZE(self) > 1) {
1247 for (p = self->ob_item, 1484 for (p = self->ob_item,
1248 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize; 1485 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1249 p < q; 1486 p < q;
1250 p += itemsize, q -= itemsize) { 1487 p += itemsize, q -= itemsize) {
1251 /* memory areas guaranteed disjoint, so memcpy 1488 /* memory areas guaranteed disjoint, so memcpy
1252 * is safe (& memmove may be slower). 1489 * is safe (& memmove may be slower).
1253 */ 1490 */
1254 memcpy(tmp, p, itemsize); 1491 memcpy(tmp, p, itemsize);
1255 memcpy(p, q, itemsize); 1492 memcpy(p, q, itemsize);
1256 memcpy(q, tmp, itemsize); 1493 memcpy(q, tmp, itemsize);
1257 } 1494 }
1258 } 1495 }
1259 1496
1260 Py_INCREF(Py_None); 1497 Py_INCREF(Py_None);
1261 return Py_None; 1498 return Py_None;
1262 } 1499 }
1263 1500
1264 PyDoc_STRVAR(reverse_doc,
1265 "reverse()\n\
1266 \n\
1267 Reverse the order of the items in the array.");
1268
1269 1501
1270 /* Forward */ 1502 /* Forward */
1271 static PyObject *array_frombytes(arrayobject *self, PyObject *args); 1503 static PyObject *array_array_frombytes(arrayobject *self, PyObject *args);
1504
1505 /*[clinic input]
1506 array.array.fromfile
1507
1508 f: object
1509 n: Py_ssize_t
1510 /
1511
1512 Read n objects from the file object f and append them to the end of the array.
1513 [clinic start generated code]*/
1514
1515 PyDoc_STRVAR(array_array_fromfile__doc__,
1516 "sig=($self, f, n)\n"
1517 "Read n objects from the file object f and append them to the end of the array." );
1518
1519 #define ARRAY_ARRAY_FROMFILE_METHODDEF \
1520 {"fromfile", (PyCFunction)array_array_fromfile, METH_VARARGS, array_array_fr omfile__doc__},
1272 1521
1273 static PyObject * 1522 static PyObject *
1274 array_fromfile(arrayobject *self, PyObject *args) 1523 array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n);
1524
1525 static PyObject *
1526 array_array_fromfile(arrayobject *self, PyObject *args)
1275 { 1527 {
1276 PyObject *f, *b, *res; 1528 PyObject *return_value = NULL;
1529 PyObject *f;
1530 Py_ssize_t n;
1531
1532 if (!PyArg_ParseTuple(args,
1533 "On:fromfile",
1534 &f, &n))
1535 goto exit;
1536 return_value = array_array_fromfile_impl(self, f, n);
1537
1538 exit:
1539 return return_value;
1540 }
1541
1542 static PyObject *
1543 array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1544 /*[clinic end generated code: output=392c7c021c666fdc input=e188afe8e58adf40]*/
1545 {
1546 PyObject *args, *b, *res;
1277 Py_ssize_t itemsize = self->ob_descr->itemsize; 1547 Py_ssize_t itemsize = self->ob_descr->itemsize;
1278 Py_ssize_t n, nbytes; 1548 Py_ssize_t nbytes;
1279 _Py_IDENTIFIER(read); 1549 _Py_IDENTIFIER(read);
1280 int not_enough_bytes; 1550 int not_enough_bytes;
1281
1282 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1283 return NULL;
1284 1551
1285 if (n < 0) { 1552 if (n < 0) {
1286 PyErr_SetString(PyExc_ValueError, "negative count"); 1553 PyErr_SetString(PyExc_ValueError, "negative count");
1287 return NULL; 1554 return NULL;
1288 } 1555 }
1289 if (n > PY_SSIZE_T_MAX / itemsize) { 1556 if (n > PY_SSIZE_T_MAX / itemsize) {
1290 PyErr_NoMemory(); 1557 PyErr_NoMemory();
1291 return NULL; 1558 return NULL;
1292 } 1559 }
1293 nbytes = n * itemsize; 1560 nbytes = n * itemsize;
1294 1561
1562 /* XXX get Py_Buffer directly and pass into array_array_frombytes_impl() ins tead. */
1295 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes); 1563 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
1296 if (b == NULL) 1564 if (b == NULL)
1297 return NULL; 1565 return NULL;
1298 1566
1299 if (!PyBytes_Check(b)) { 1567 if (!PyBytes_Check(b)) {
1300 PyErr_SetString(PyExc_TypeError, 1568 PyErr_SetString(PyExc_TypeError,
1301 "read() didn't return bytes"); 1569 "read() didn't return bytes");
1302 Py_DECREF(b); 1570 Py_DECREF(b);
1303 return NULL; 1571 return NULL;
1304 } 1572 }
1305 1573
1306 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes); 1574 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
1307 1575
1308 args = Py_BuildValue("(O)", b); 1576 args = Py_BuildValue("(O)", b);
1309 Py_DECREF(b); 1577 Py_DECREF(b);
1310 if (args == NULL) 1578 if (args == NULL)
1311 return NULL; 1579 return NULL;
1312 1580
1313 res = array_frombytes(self, args); 1581 res = array_array_frombytes(self, args);
1314 Py_DECREF(args); 1582 Py_DECREF(args);
1315 if (res == NULL) 1583 if (res == NULL)
1316 return NULL; 1584 return NULL;
1317 1585
1318 if (not_enough_bytes) { 1586 if (not_enough_bytes) {
1319 PyErr_SetString(PyExc_EOFError, 1587 PyErr_SetString(PyExc_EOFError,
1320 "read() didn't return enough bytes"); 1588 "read() didn't return enough bytes");
1321 Py_DECREF(res); 1589 Py_DECREF(res);
1322 return NULL; 1590 return NULL;
1323 } 1591 }
1324 1592
1325 return res; 1593 return res;
1326 } 1594 }
1327 1595
1328 PyDoc_STRVAR(fromfile_doc, 1596 /*[clinic input]
1329 "fromfile(f, n)\n\ 1597 array.array.tofile
1330 \n\
1331 Read n objects from the file object f and append them to the end of the\n\
1332 array.");
1333 1598
1599 f: object
1600 /
1601
1602 Write all items (as machine values) to the file object f.
1603 [clinic start generated code]*/
1604
1605 PyDoc_STRVAR(array_array_tofile__doc__,
1606 "sig=($self, f)\n"
1607 "Write all items (as machine values) to the file object f.");
1608
1609 #define ARRAY_ARRAY_TOFILE_METHODDEF \
1610 {"tofile", (PyCFunction)array_array_tofile, METH_O, array_array_tofile__doc_ _},
1334 1611
1335 static PyObject * 1612 static PyObject *
1336 array_tofile(arrayobject *self, PyObject *f) 1613 array_array_tofile(arrayobject *self, PyObject *f)
1614 /*[clinic end generated code: output=1ea7d764827142a6 input=b0669a484aab0831]*/
1337 { 1615 {
1338 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; 1616 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1339 /* Write 64K blocks at a time */ 1617 /* Write 64K blocks at a time */
1340 /* XXX Make the block size settable */ 1618 /* XXX Make the block size settable */
1341 int BLOCKSIZE = 64*1024; 1619 int BLOCKSIZE = 64*1024;
1342 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; 1620 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1343 Py_ssize_t i; 1621 Py_ssize_t i;
1344 1622
1345 if (Py_SIZE(self) == 0) 1623 if (Py_SIZE(self) == 0)
1346 goto done; 1624 goto done;
(...skipping 14 matching lines...) Expand all
1361 if (res == NULL) 1639 if (res == NULL)
1362 return NULL; 1640 return NULL;
1363 Py_DECREF(res); /* drop write result */ 1641 Py_DECREF(res); /* drop write result */
1364 } 1642 }
1365 1643
1366 done: 1644 done:
1367 Py_INCREF(Py_None); 1645 Py_INCREF(Py_None);
1368 return Py_None; 1646 return Py_None;
1369 } 1647 }
1370 1648
1371 PyDoc_STRVAR(tofile_doc, 1649 /*[clinic input]
1372 "tofile(f)\n\ 1650 array.array.fromlist
1373 \n\
1374 Write all items (as machine values) to the file object f.");
1375 1651
1652 list: object
1653 /
1654
1655 Append items to array from list.
1656 [clinic start generated code]*/
1657
1658 PyDoc_STRVAR(array_array_fromlist__doc__,
1659 "sig=($self, list)\n"
1660 "Append items to array from list.");
1661
1662 #define ARRAY_ARRAY_FROMLIST_METHODDEF \
1663 {"fromlist", (PyCFunction)array_array_fromlist, METH_O, array_array_fromlist __doc__},
1376 1664
1377 static PyObject * 1665 static PyObject *
1378 array_fromlist(arrayobject *self, PyObject *list) 1666 array_array_fromlist(arrayobject *self, PyObject *list)
1667 /*[clinic end generated code: output=4fdb54f7c547f54e input=be2605a96c49680f]*/
1379 { 1668 {
1380 Py_ssize_t n; 1669 Py_ssize_t n;
1381 1670
1382 if (!PyList_Check(list)) { 1671 if (!PyList_Check(list)) {
1383 PyErr_SetString(PyExc_TypeError, "arg must be list"); 1672 PyErr_SetString(PyExc_TypeError, "arg must be list");
1384 return NULL; 1673 return NULL;
1385 } 1674 }
1386 n = PyList_Size(list); 1675 n = PyList_Size(list);
1387 if (n > 0) { 1676 if (n > 0) {
1388 Py_ssize_t i, old_size; 1677 Py_ssize_t i, old_size;
1389 old_size = Py_SIZE(self); 1678 old_size = Py_SIZE(self);
1390 if (array_resize(self, old_size + n) == -1) 1679 if (array_resize(self, old_size + n) == -1)
1391 return NULL; 1680 return NULL;
1392 for (i = 0; i < n; i++) { 1681 for (i = 0; i < n; i++) {
1393 PyObject *v = PyList_GetItem(list, i); 1682 PyObject *v = PyList_GetItem(list, i);
1394 if ((*self->ob_descr->setitem)(self, 1683 if ((*self->ob_descr->setitem)(self,
1395 Py_SIZE(self) - n + i, v) != 0) { 1684 Py_SIZE(self) - n + i, v) != 0) {
1396 array_resize(self, old_size); 1685 array_resize(self, old_size);
1397 return NULL; 1686 return NULL;
1398 } 1687 }
1399 } 1688 }
1400 } 1689 }
1401 Py_INCREF(Py_None); 1690 Py_INCREF(Py_None);
1402 return Py_None; 1691 return Py_None;
1403 } 1692 }
1404 1693
1405 PyDoc_STRVAR(fromlist_doc, 1694 /*[clinic input]
1406 "fromlist(list)\n\ 1695 array.array.tolist
1407 \n\ 1696
1408 Append items to array from list."); 1697 Convert array to an ordinary list with the same items.
1698 [clinic start generated code]*/
1699
1700 PyDoc_STRVAR(array_array_tolist__doc__,
1701 "sig=($self)\n"
1702 "Convert array to an ordinary list with the same items.");
1703
1704 #define ARRAY_ARRAY_TOLIST_METHODDEF \
1705 {"tolist", (PyCFunction)array_array_tolist, METH_NOARGS, array_array_tolist_ _doc__},
1409 1706
1410 static PyObject * 1707 static PyObject *
1411 array_tolist(arrayobject *self, PyObject *unused) 1708 array_array_tolist_impl(arrayobject *self);
1709
1710 static PyObject *
1711 array_array_tolist(arrayobject *self, PyObject *Py_UNUSED(ignored))
1712 {
1713 return array_array_tolist_impl(self);
1714 }
1715
1716 static PyObject *
1717 array_array_tolist_impl(arrayobject *self)
1718 /*[clinic end generated code: output=228b1dac2808e0de input=a8d7784a94f86b53]*/
1412 { 1719 {
1413 PyObject *list = PyList_New(Py_SIZE(self)); 1720 PyObject *list = PyList_New(Py_SIZE(self));
1414 Py_ssize_t i; 1721 Py_ssize_t i;
1415 1722
1416 if (list == NULL) 1723 if (list == NULL)
1417 return NULL; 1724 return NULL;
1418 for (i = 0; i < Py_SIZE(self); i++) { 1725 for (i = 0; i < Py_SIZE(self); i++) {
1419 PyObject *v = getarrayitem((PyObject *)self, i); 1726 PyObject *v = getarrayitem((PyObject *)self, i);
1420 if (v == NULL) 1727 if (v == NULL)
1421 goto error; 1728 goto error;
1422 if (PyList_SetItem(list, i, v) < 0) 1729 if (PyList_SetItem(list, i, v) < 0)
1423 goto error; 1730 goto error;
1424 } 1731 }
1425 return list; 1732 return list;
1426 1733
1427 error: 1734 error:
1428 Py_DECREF(list); 1735 Py_DECREF(list);
1429 return NULL; 1736 return NULL;
1430 } 1737 }
1431
1432 PyDoc_STRVAR(tolist_doc,
1433 "tolist() -> list\n\
1434 \n\
1435 Convert array to an ordinary list with the same items.");
1436 1738
1437 static PyObject * 1739 static PyObject *
1438 frombytes(arrayobject *self, Py_buffer *buffer) 1740 frombytes(arrayobject *self, Py_buffer *buffer)
1439 { 1741 {
1440 int itemsize = self->ob_descr->itemsize; 1742 int itemsize = self->ob_descr->itemsize;
1441 Py_ssize_t n; 1743 Py_ssize_t n;
1442 if (buffer->itemsize != 1) { 1744 if (buffer->itemsize != 1) {
1443 PyBuffer_Release(buffer); 1745 PyBuffer_Release(buffer);
1444 PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required."); 1746 PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required.");
1445 return NULL; 1747 return NULL;
(...skipping 18 matching lines...) Expand all
1464 return NULL; 1766 return NULL;
1465 } 1767 }
1466 memcpy(self->ob_item + old_size * itemsize, 1768 memcpy(self->ob_item + old_size * itemsize,
1467 buffer->buf, n * itemsize); 1769 buffer->buf, n * itemsize);
1468 } 1770 }
1469 PyBuffer_Release(buffer); 1771 PyBuffer_Release(buffer);
1470 Py_INCREF(Py_None); 1772 Py_INCREF(Py_None);
1471 return Py_None; 1773 return Py_None;
1472 } 1774 }
1473 1775
1776 /*[clinic input]
1777 array.array.fromstring
1778
1779 buffer: Py_buffer(types='str bytes bytearray buffer')
1780 /
1781
1782 Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).
1783
1784 This method is deprecated. Use frombytes instead.
1785 [clinic start generated code]*/
1786
1787 PyDoc_STRVAR(array_array_fromstring__doc__,
1788 "sig=($self, buffer)\n"
1789 "Appends items from the string, interpreting it as an array of machine values, a s if it had been read from a file using the fromfile() method).\n"
1790 "\n"
1791 "This method is deprecated. Use frombytes instead.");
1792
1793 #define ARRAY_ARRAY_FROMSTRING_METHODDEF \
1794 {"fromstring", (PyCFunction)array_array_fromstring, METH_VARARGS, array_arra y_fromstring__doc__},
1795
1474 static PyObject * 1796 static PyObject *
1475 array_fromstring(arrayobject *self, PyObject *args) 1797 array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer);
1798
1799 static PyObject *
1800 array_array_fromstring(arrayobject *self, PyObject *args)
1476 { 1801 {
1477 Py_buffer buffer; 1802 PyObject *return_value = NULL;
1803 Py_buffer buffer = {NULL, NULL};
1804
1805 if (!PyArg_ParseTuple(args,
1806 "s*:fromstring",
1807 &buffer))
1808 goto exit;
1809 return_value = array_array_fromstring_impl(self, &buffer);
1810
1811 exit:
1812 /* Cleanup for buffer */
1813 if (buffer.obj)
1814 PyBuffer_Release(&buffer);
1815
1816 return return_value;
1817 }
1818
1819 static PyObject *
1820 array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
1821 /*[clinic end generated code: output=d3612a97cc2bce81 input=1302d94c97696b84]*/
1822 {
1478 if (PyErr_WarnEx(PyExc_DeprecationWarning, 1823 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1479 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0) 1824 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1480 return NULL; 1825 return NULL;
1481 if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer))
1482 return NULL;
1483 else 1826 else
1484 return frombytes(self, &buffer); 1827 return frombytes(self, buffer);
1485 } 1828 }
1486 1829
1487 PyDoc_STRVAR(fromstring_doc,
1488 "fromstring(string)\n\
1489 \n\
1490 Appends items from the string, interpreting it as an array of machine\n\
1491 values, as if it had been read from a file using the fromfile() method).\n\
1492 \n\
1493 This method is deprecated. Use frombytes instead.");
1494 1830
1831 /*[clinic input]
1832 array.array.frombytes
1833
1834 buffer: Py_buffer
1835 /
1836
1837 Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).
1838 [clinic start generated code]*/
1839
1840 PyDoc_STRVAR(array_array_frombytes__doc__,
1841 "sig=($self, buffer)\n"
1842 "Appends items from the string, interpreting it as an array of machine values, a s if it had been read from a file using the fromfile() method).");
1843
1844 #define ARRAY_ARRAY_FROMBYTES_METHODDEF \
1845 {"frombytes", (PyCFunction)array_array_frombytes, METH_VARARGS, array_array_ frombytes__doc__},
1495 1846
1496 static PyObject * 1847 static PyObject *
1497 array_frombytes(arrayobject *self, PyObject *args) 1848 array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer);
1849
1850 static PyObject *
1851 array_array_frombytes(arrayobject *self, PyObject *args)
1498 { 1852 {
1499 Py_buffer buffer; 1853 PyObject *return_value = NULL;
1500 if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer)) 1854 Py_buffer buffer = {NULL, NULL};
1501 return NULL; 1855
1502 else 1856 if (!PyArg_ParseTuple(args,
1503 return frombytes(self, &buffer); 1857 "y*:frombytes",
1858 &buffer))
1859 goto exit;
1860 return_value = array_array_frombytes_impl(self, &buffer);
1861
1862 exit:
1863 /* Cleanup for buffer */
1864 if (buffer.obj)
1865 PyBuffer_Release(&buffer);
1866
1867 return return_value;
1504 } 1868 }
1505 1869
1506 PyDoc_STRVAR(frombytes_doc, 1870 static PyObject *
1507 "frombytes(bytestring)\n\ 1871 array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1508 \n\ 1872 /*[clinic end generated code: output=31a9108bf5fad410 input=2bbf2b53ebfcc988]*/
1509 Appends items from the string, interpreting it as an array of machine\n\ 1873 {
1510 values, as if it had been read from a file using the fromfile() method)."); 1874 return frombytes(self, buffer);
1875 }
1511 1876
1877 /*[clinic input]
1878 array.array.tobytes
1879
1880 Convert the array to an array of machine values and return the bytes representat ion.
1881 [clinic start generated code]*/
1882
1883 PyDoc_STRVAR(array_array_tobytes__doc__,
1884 "sig=($self)\n"
1885 "Convert the array to an array of machine values and return the bytes representa tion.");
1886
1887 #define ARRAY_ARRAY_TOBYTES_METHODDEF \
1888 {"tobytes", (PyCFunction)array_array_tobytes, METH_NOARGS, array_array_tobyt es__doc__},
1512 1889
1513 static PyObject * 1890 static PyObject *
1514 array_tobytes(arrayobject *self, PyObject *unused) 1891 array_array_tobytes_impl(arrayobject *self);
1892
1893 static PyObject *
1894 array_array_tobytes(arrayobject *self, PyObject *Py_UNUSED(ignored))
1895 {
1896 return array_array_tobytes_impl(self);
1897 }
1898
1899 static PyObject *
1900 array_array_tobytes_impl(arrayobject *self)
1901 /*[clinic end generated code: output=e24834e3ff659982 input=90ee495f96de34f5]*/
1515 { 1902 {
1516 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { 1903 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1517 return PyBytes_FromStringAndSize(self->ob_item, 1904 return PyBytes_FromStringAndSize(self->ob_item,
1518 Py_SIZE(self) * self->ob_descr->itemsize); 1905 Py_SIZE(self) * self->ob_descr->itemsize);
1519 } else { 1906 } else {
1520 return PyErr_NoMemory(); 1907 return PyErr_NoMemory();
1521 } 1908 }
1522 } 1909 }
1523 1910
1524 PyDoc_STRVAR(tobytes_doc, 1911 /*[clinic input]
1525 "tobytes() -> bytes\n\ 1912 array.array.tostring
1526 \n\
1527 Convert the array to an array of machine values and return the bytes\n\
1528 representation.");
1529 1913
1914 Convert the array to an array of machine values and return the bytes representat ion.
1915
1916 This method is deprecated. Use tobytes instead.
1917 [clinic start generated code]*/
1918
1919 PyDoc_STRVAR(array_array_tostring__doc__,
1920 "sig=($self)\n"
1921 "Convert the array to an array of machine values and return the bytes representa tion.\n"
1922 "\n"
1923 "This method is deprecated. Use tobytes instead.");
1924
1925 #define ARRAY_ARRAY_TOSTRING_METHODDEF \
1926 {"tostring", (PyCFunction)array_array_tostring, METH_NOARGS, array_array_tos tring__doc__},
1530 1927
1531 static PyObject * 1928 static PyObject *
1532 array_tostring(arrayobject *self, PyObject *unused) 1929 array_array_tostring_impl(arrayobject *self);
1930
1931 static PyObject *
1932 array_array_tostring(arrayobject *self, PyObject *Py_UNUSED(ignored))
1933 {
1934 return array_array_tostring_impl(self);
1935 }
1936
1937 static PyObject *
1938 array_array_tostring_impl(arrayobject *self)
1939 /*[clinic end generated code: output=a4cef98209364962 input=b6c0ddee7b30457e]*/
1533 { 1940 {
1534 if (PyErr_WarnEx(PyExc_DeprecationWarning, 1941 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1535 "tostring() is deprecated. Use tobytes() instead.", 2) != 0) 1942 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1536 return NULL; 1943 return NULL;
1537 return array_tobytes(self, unused); 1944 return array_array_tobytes_impl(self);
1538 } 1945 }
1539 1946
1540 PyDoc_STRVAR(tostring_doc, 1947 /*[clinic input]
1541 "tostring() -> bytes\n\ 1948 array.array.fromunicode
1542 \n\
1543 Convert the array to an array of machine values and return the bytes\n\
1544 representation.\n\
1545 \n\
1546 This method is deprecated. Use tobytes instead.");
1547 1949
1950 ustr: Py_UNICODE(length=True)
1951 /
1952
1953 Extends this array with data from the unicode string ustr.
1954
1955 The array must be a unicode type array; otherwise a ValueError is raised.
1956 Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1957 some other type.
1958 [clinic start generated code]*/
1959
1960 PyDoc_STRVAR(array_array_fromunicode__doc__,
1961 "sig=($self, ustr)\n"
1962 "Extends this array with data from the unicode string ustr.\n"
1963 "\n"
1964 "The array must be a unicode type array; otherwise a ValueError is raised.\n"
1965 "Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of\n"
1966 "some other type.");
1967
1968 #define ARRAY_ARRAY_FROMUNICODE_METHODDEF \
1969 {"fromunicode", (PyCFunction)array_array_fromunicode, METH_VARARGS, array_ar ray_fromunicode__doc__},
1548 1970
1549 static PyObject * 1971 static PyObject *
1550 array_fromunicode(arrayobject *self, PyObject *args) 1972 array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr, Py_ssize_clean _t ustr_length);
1973
1974 static PyObject *
1975 array_array_fromunicode(arrayobject *self, PyObject *args)
1551 { 1976 {
1977 PyObject *return_value = NULL;
1552 Py_UNICODE *ustr; 1978 Py_UNICODE *ustr;
1553 Py_ssize_t n; 1979 Py_ssize_clean_t ustr_length;
1980
1981 if (!PyArg_ParseTuple(args,
1982 "u#:fromunicode",
1983 &ustr, &ustr_length))
1984 goto exit;
1985 return_value = array_array_fromunicode_impl(self, ustr, ustr_length);
1986
1987 exit:
1988 return return_value;
1989 }
1990
1991 static PyObject *
1992 array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr, Py_ssize_clean _t ustr_length)
1993 /*[clinic end generated code: output=04789612324bc5ac input=56bcedb5ef70139f]*/
1994 {
1554 char typecode; 1995 char typecode;
1555 1996
1556 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1557 return NULL;
1558 typecode = self->ob_descr->typecode; 1997 typecode = self->ob_descr->typecode;
1559 if (typecode != 'u') { 1998 if (typecode != 'u') {
1560 PyErr_SetString(PyExc_ValueError, 1999 PyErr_SetString(PyExc_ValueError,
1561 "fromunicode() may only be called on " 2000 "fromunicode() may only be called on "
1562 "unicode type arrays"); 2001 "unicode type arrays");
1563 return NULL; 2002 return NULL;
1564 } 2003 }
1565 if (n > 0) { 2004 if (ustr_length > 0) {
1566 Py_ssize_t old_size = Py_SIZE(self); 2005 Py_ssize_t old_size = Py_SIZE(self);
1567 if (array_resize(self, old_size + n) == -1) 2006 if (array_resize(self, old_size + ustr_length) == -1)
1568 return NULL; 2007 return NULL;
1569 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), 2008 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1570 ustr, n * sizeof(Py_UNICODE)); 2009 ustr, ustr_length * sizeof(Py_UNICODE));
1571 } 2010 }
1572 2011
1573 Py_INCREF(Py_None); 2012 Py_RETURN_NONE;
1574 return Py_None;
1575 } 2013 }
1576 2014
1577 PyDoc_STRVAR(fromunicode_doc, 2015 /*[clinic input]
1578 "fromunicode(ustr)\n\ 2016 array.array.tounicode
1579 \n\
1580 Extends this array with data from the unicode string ustr.\n\
1581 The array must be a unicode type array; otherwise a ValueError\n\
1582 is raised. Use array.frombytes(ustr.encode(...)) to\n\
1583 append Unicode data to an array of some other type.");
1584 2017
2018 Extends this array with data from the unicode string ustr.
2019
2020 Convert the array to a unicode string. The array must be a unicode type array;
2021 otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
2022 unicode string from an array of some other type.
2023 [clinic start generated code]*/
2024
2025 PyDoc_STRVAR(array_array_tounicode__doc__,
2026 "sig=($self)\n"
2027 "Extends this array with data from the unicode string ustr.\n"
2028 "\n"
2029 "Convert the array to a unicode string. The array must be a unicode type array; \n"
2030 "otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a\n"
2031 "unicode string from an array of some other type.");
2032
2033 #define ARRAY_ARRAY_TOUNICODE_METHODDEF \
2034 {"tounicode", (PyCFunction)array_array_tounicode, METH_NOARGS, array_array_t ounicode__doc__},
1585 2035
1586 static PyObject * 2036 static PyObject *
1587 array_tounicode(arrayobject *self, PyObject *unused) 2037 array_array_tounicode_impl(arrayobject *self);
2038
2039 static PyObject *
2040 array_array_tounicode(arrayobject *self, PyObject *Py_UNUSED(ignored))
2041 {
2042 return array_array_tounicode_impl(self);
2043 }
2044
2045 static PyObject *
2046 array_array_tounicode_impl(arrayobject *self)
2047 /*[clinic end generated code: output=80b37b5895f04ca9 input=127242eebe70b66d]*/
1588 { 2048 {
1589 char typecode; 2049 char typecode;
1590 typecode = self->ob_descr->typecode; 2050 typecode = self->ob_descr->typecode;
1591 if (typecode != 'u') { 2051 if (typecode != 'u') {
1592 PyErr_SetString(PyExc_ValueError, 2052 PyErr_SetString(PyExc_ValueError,
1593 "tounicode() may only be called on unicode type arrays"); 2053 "tounicode() may only be called on unicode type arrays");
1594 return NULL; 2054 return NULL;
1595 } 2055 }
1596 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self)); 2056 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
1597 } 2057 }
1598 2058
1599 PyDoc_STRVAR(tounicode_doc, 2059 /*[clinic input]
1600 "tounicode() -> unicode\n\ 2060 array.array.__sizeof__
1601 \n\
1602 Convert the array to a unicode string. The array must be\n\
1603 a unicode type array; otherwise a ValueError is raised. Use\n\
1604 array.tobytes().decode() to obtain a unicode string from\n\
1605 an array of some other type.");
1606 2061
2062 Size of the array in memory, in bytes.
2063 [clinic start generated code]*/
2064
2065 PyDoc_STRVAR(array_array___sizeof____doc__,
2066 "sig=($self)\n"
2067 "Size of the array in memory, in bytes.");
2068
2069 #define ARRAY_ARRAY___SIZEOF___METHODDEF \
2070 {"__sizeof__", (PyCFunction)array_array___sizeof__, METH_NOARGS, array_array ___sizeof____doc__},
1607 2071
1608 static PyObject * 2072 static PyObject *
1609 array_sizeof(arrayobject *self, PyObject *unused) 2073 array_array___sizeof___impl(arrayobject *self);
2074
2075 static PyObject *
2076 array_array___sizeof__(arrayobject *self, PyObject *Py_UNUSED(ignored))
2077 {
2078 return array_array___sizeof___impl(self);
2079 }
2080
2081 static PyObject *
2082 array_array___sizeof___impl(arrayobject *self)
2083 /*[clinic end generated code: output=cd3ca4dc9f564848 input=805586565bf2b3c6]*/
1610 { 2084 {
1611 Py_ssize_t res; 2085 Py_ssize_t res;
1612 res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize; 2086 res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize;
1613 return PyLong_FromSsize_t(res); 2087 return PyLong_FromSsize_t(res);
1614 } 2088 }
1615 2089
1616 PyDoc_STRVAR(sizeof_doc,
1617 "__sizeof__() -> int\n\
1618 \n\
1619 Size of the array in memory, in bytes.");
1620
1621 2090
1622 /*********************** Pickling support ************************/ 2091 /*********************** Pickling support ************************/
1623 2092
1624 enum machine_format_code { 2093 enum machine_format_code {
1625 UNKNOWN_FORMAT = -1, 2094 UNKNOWN_FORMAT = -1,
1626 /* UNKNOWN_FORMAT is used to indicate that the machine format for an 2095 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
1627 * array type code cannot be interpreted. When this occurs, a list of 2096 * array type code cannot be interpreted. When this occurs, a list of
1628 * Python objects is used to represent the content of the array 2097 * Python objects is used to represent the content of the array
1629 * instead of using the memory content of the array directly. In that 2098 * instead of using the memory content of the array directly. In that
1630 * case, the array_reconstructor mechanism is bypassed completely, and 2099 * case, the array_reconstructor mechanism is bypassed completely, and
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1828 if (array_obj == NULL) 2297 if (array_obj == NULL)
1829 return NULL; 2298 return NULL;
1830 2299
1831 return array_obj; 2300 return array_obj;
1832 } 2301 }
1833 2302
1834 /* 2303 /*
1835 * This functions is a special constructor used when unpickling an array. It 2304 * This functions is a special constructor used when unpickling an array. It
1836 * provides a portable way to rebuild an array from its memory representation. 2305 * provides a portable way to rebuild an array from its memory representation.
1837 */ 2306 */
2307 /*[clinic input]
2308 array._array_reconstructor
2309
2310 arraytype: object(type="PyTypeObject *")
2311 typecode: int(types='str')
2312 mformat_code: int
2313 items: object
2314 /
2315
2316 Internal. Used for pickling support.
2317 [clinic start generated code]*/
2318
2319 PyDoc_STRVAR(array__array_reconstructor__doc__,
2320 "sig=($module, arraytype, typecode, mformat_code, items)\n"
2321 "Internal. Used for pickling support.");
2322
2323 #define ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF \
2324 {"_array_reconstructor", (PyCFunction)array__array_reconstructor, METH_VARAR GS, array__array_reconstructor__doc__},
2325
1838 static PyObject * 2326 static PyObject *
1839 array_reconstructor(PyObject *self, PyObject *args) 2327 array__array_reconstructor_impl(PyModuleDef *module, PyTypeObject *arraytype, in t typecode, int mformat_code, PyObject *items);
2328
2329 static PyObject *
2330 array__array_reconstructor(PyModuleDef *module, PyObject *args)
1840 { 2331 {
2332 PyObject *return_value = NULL;
1841 PyTypeObject *arraytype; 2333 PyTypeObject *arraytype;
2334 int typecode;
2335 int mformat_code;
1842 PyObject *items; 2336 PyObject *items;
2337
2338 if (!PyArg_ParseTuple(args,
2339 "OCiO:_array_reconstructor",
2340 &arraytype, &typecode, &mformat_code, &items))
2341 goto exit;
2342 return_value = array__array_reconstructor_impl(module, arraytype, typecode, mformat_code, items);
2343
2344 exit:
2345 return return_value;
2346 }
2347
2348 static PyObject *
2349 array__array_reconstructor_impl(PyModuleDef *module, PyTypeObject *arraytype, in t typecode, int mformat_code, PyObject *items)
2350 /*[clinic end generated code: output=1a3cc6e9f14107e3 input=450d59a5373c4eea]*/
2351 {
1843 PyObject *converted_items; 2352 PyObject *converted_items;
1844 PyObject *result; 2353 PyObject *result;
1845 int typecode; 2354 enum machine_format_code mformat_code_enum = mformat_code;
1846 enum machine_format_code mformat_code;
1847 struct arraydescr *descr; 2355 struct arraydescr *descr;
1848
1849 if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
1850 &arraytype, &typecode, &mformat_code, &items))
1851 return NULL;
1852 2356
1853 if (!PyType_Check(arraytype)) { 2357 if (!PyType_Check(arraytype)) {
1854 PyErr_Format(PyExc_TypeError, 2358 PyErr_Format(PyExc_TypeError,
1855 "first argument must a type object, not %.200s", 2359 "first argument must a type object, not %.200s",
1856 Py_TYPE(arraytype)->tp_name); 2360 Py_TYPE(arraytype)->tp_name);
1857 return NULL; 2361 return NULL;
1858 } 2362 }
1859 if (!PyType_IsSubtype(arraytype, &Arraytype)) { 2363 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1860 PyErr_Format(PyExc_TypeError, 2364 PyErr_Format(PyExc_TypeError,
1861 "%.200s is not a subtype of %.200s", 2365 "%.200s is not a subtype of %.200s",
1862 arraytype->tp_name, Arraytype.tp_name); 2366 arraytype->tp_name, Arraytype.tp_name);
1863 return NULL; 2367 return NULL;
1864 } 2368 }
1865 for (descr = descriptors; descr->typecode != '\0'; descr++) { 2369 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1866 if ((int)descr->typecode == typecode) 2370 if ((int)descr->typecode == typecode)
1867 break; 2371 break;
1868 } 2372 }
1869 if (descr->typecode == '\0') { 2373 if (descr->typecode == '\0') {
1870 PyErr_SetString(PyExc_ValueError, 2374 PyErr_SetString(PyExc_ValueError,
1871 "second argument must be a valid type code"); 2375 "second argument must be a valid type code");
1872 return NULL; 2376 return NULL;
1873 } 2377 }
1874 if (mformat_code < MACHINE_FORMAT_CODE_MIN || 2378 if (mformat_code_enum < MACHINE_FORMAT_CODE_MIN ||
1875 mformat_code > MACHINE_FORMAT_CODE_MAX) { 2379 mformat_code_enum > MACHINE_FORMAT_CODE_MAX) {
1876 PyErr_SetString(PyExc_ValueError, 2380 PyErr_SetString(PyExc_ValueError,
1877 "third argument must be a valid machine format code."); 2381 "third argument must be a valid machine format code.");
1878 return NULL; 2382 return NULL;
1879 } 2383 }
1880 if (!PyBytes_Check(items)) { 2384 if (!PyBytes_Check(items)) {
1881 PyErr_Format(PyExc_TypeError, 2385 PyErr_Format(PyExc_TypeError,
1882 "fourth argument should be bytes, not %.200s", 2386 "fourth argument should be bytes, not %.200s",
1883 Py_TYPE(items)->tp_name); 2387 Py_TYPE(items)->tp_name);
1884 return NULL; 2388 return NULL;
1885 } 2389 }
1886 2390
1887 /* Fast path: No decoding has to be done. */ 2391 /* Fast path: No decoding has to be done. */
1888 if (mformat_code == typecode_to_mformat_code((char)typecode) || 2392 if (mformat_code_enum == typecode_to_mformat_code((char)typecode) ||
1889 mformat_code == UNKNOWN_FORMAT) { 2393 mformat_code_enum == UNKNOWN_FORMAT) {
1890 return make_array(arraytype, (char)typecode, items); 2394 return make_array(arraytype, (char)typecode, items);
1891 } 2395 }
1892 2396
1893 /* Slow path: Decode the byte string according to the given machine 2397 /* Slow path: Decode the byte string according to the given machine
1894 * format code. This occurs when the computer unpickling the array 2398 * format code. This occurs when the computer unpickling the array
1895 * object is architecturally different from the one that pickled the 2399 * object is architecturally different from the one that pickled the
1896 * array. 2400 * array.
1897 */ 2401 */
1898 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) { 2402 if (Py_SIZE(items) % mformat_descriptors[mformat_code_enum].size != 0) {
1899 PyErr_SetString(PyExc_ValueError, 2403 PyErr_SetString(PyExc_ValueError,
1900 "string length not a multiple of item size"); 2404 "string length not a multiple of item size");
1901 return NULL; 2405 return NULL;
1902 } 2406 }
1903 switch (mformat_code) { 2407 switch (mformat_code_enum) {
1904 case IEEE_754_FLOAT_LE: 2408 case IEEE_754_FLOAT_LE:
1905 case IEEE_754_FLOAT_BE: { 2409 case IEEE_754_FLOAT_BE: {
1906 int i; 2410 int i;
1907 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0; 2411 int le = (mformat_code_enum == IEEE_754_FLOAT_LE) ? 1 : 0;
1908 Py_ssize_t itemcount = Py_SIZE(items) / 4; 2412 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1909 const unsigned char *memstr = 2413 const unsigned char *memstr =
1910 (unsigned char *)PyBytes_AS_STRING(items); 2414 (unsigned char *)PyBytes_AS_STRING(items);
1911 2415
1912 converted_items = PyList_New(itemcount); 2416 converted_items = PyList_New(itemcount);
1913 if (converted_items == NULL) 2417 if (converted_items == NULL)
1914 return NULL; 2418 return NULL;
1915 for (i = 0; i < itemcount; i++) { 2419 for (i = 0; i < itemcount; i++) {
1916 PyObject *pyfloat = PyFloat_FromDouble( 2420 PyObject *pyfloat = PyFloat_FromDouble(
1917 _PyFloat_Unpack4(&memstr[i * 4], le)); 2421 _PyFloat_Unpack4(&memstr[i * 4], le));
1918 if (pyfloat == NULL) { 2422 if (pyfloat == NULL) {
1919 Py_DECREF(converted_items); 2423 Py_DECREF(converted_items);
1920 return NULL; 2424 return NULL;
1921 } 2425 }
1922 PyList_SET_ITEM(converted_items, i, pyfloat); 2426 PyList_SET_ITEM(converted_items, i, pyfloat);
1923 } 2427 }
1924 break; 2428 break;
1925 } 2429 }
1926 case IEEE_754_DOUBLE_LE: 2430 case IEEE_754_DOUBLE_LE:
1927 case IEEE_754_DOUBLE_BE: { 2431 case IEEE_754_DOUBLE_BE: {
1928 int i; 2432 int i;
1929 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0; 2433 int le = (mformat_code_enum == IEEE_754_DOUBLE_LE) ? 1 : 0;
1930 Py_ssize_t itemcount = Py_SIZE(items) / 8; 2434 Py_ssize_t itemcount = Py_SIZE(items) / 8;
1931 const unsigned char *memstr = 2435 const unsigned char *memstr =
1932 (unsigned char *)PyBytes_AS_STRING(items); 2436 (unsigned char *)PyBytes_AS_STRING(items);
1933 2437
1934 converted_items = PyList_New(itemcount); 2438 converted_items = PyList_New(itemcount);
1935 if (converted_items == NULL) 2439 if (converted_items == NULL)
1936 return NULL; 2440 return NULL;
1937 for (i = 0; i < itemcount; i++) { 2441 for (i = 0; i < itemcount; i++) {
1938 PyObject *pyfloat = PyFloat_FromDouble( 2442 PyObject *pyfloat = PyFloat_FromDouble(
1939 _PyFloat_Unpack8(&memstr[i * 8], le)); 2443 _PyFloat_Unpack8(&memstr[i * 8], le));
1940 if (pyfloat == NULL) { 2444 if (pyfloat == NULL) {
1941 Py_DECREF(converted_items); 2445 Py_DECREF(converted_items);
1942 return NULL; 2446 return NULL;
1943 } 2447 }
1944 PyList_SET_ITEM(converted_items, i, pyfloat); 2448 PyList_SET_ITEM(converted_items, i, pyfloat);
1945 } 2449 }
1946 break; 2450 break;
1947 } 2451 }
1948 case UTF16_LE: 2452 case UTF16_LE:
1949 case UTF16_BE: { 2453 case UTF16_BE: {
1950 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1; 2454 int byteorder = (mformat_code_enum == UTF16_LE) ? -1 : 1;
1951 converted_items = PyUnicode_DecodeUTF16( 2455 converted_items = PyUnicode_DecodeUTF16(
1952 PyBytes_AS_STRING(items), Py_SIZE(items), 2456 PyBytes_AS_STRING(items), Py_SIZE(items),
1953 "strict", &byteorder); 2457 "strict", &byteorder);
1954 if (converted_items == NULL) 2458 if (converted_items == NULL)
1955 return NULL; 2459 return NULL;
1956 break; 2460 break;
1957 } 2461 }
1958 case UTF32_LE: 2462 case UTF32_LE:
1959 case UTF32_BE: { 2463 case UTF32_BE: {
1960 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1; 2464 int byteorder = (mformat_code_enum == UTF32_LE) ? -1 : 1;
1961 converted_items = PyUnicode_DecodeUTF32( 2465 converted_items = PyUnicode_DecodeUTF32(
1962 PyBytes_AS_STRING(items), Py_SIZE(items), 2466 PyBytes_AS_STRING(items), Py_SIZE(items),
1963 "strict", &byteorder); 2467 "strict", &byteorder);
1964 if (converted_items == NULL) 2468 if (converted_items == NULL)
1965 return NULL; 2469 return NULL;
1966 break; 2470 break;
1967 } 2471 }
1968 2472
1969 case UNSIGNED_INT8: 2473 case UNSIGNED_INT8:
1970 case SIGNED_INT8: 2474 case SIGNED_INT8:
1971 case UNSIGNED_INT16_LE: 2475 case UNSIGNED_INT16_LE:
1972 case UNSIGNED_INT16_BE: 2476 case UNSIGNED_INT16_BE:
1973 case SIGNED_INT16_LE: 2477 case SIGNED_INT16_LE:
1974 case SIGNED_INT16_BE: 2478 case SIGNED_INT16_BE:
1975 case UNSIGNED_INT32_LE: 2479 case UNSIGNED_INT32_LE:
1976 case UNSIGNED_INT32_BE: 2480 case UNSIGNED_INT32_BE:
1977 case SIGNED_INT32_LE: 2481 case SIGNED_INT32_LE:
1978 case SIGNED_INT32_BE: 2482 case SIGNED_INT32_BE:
1979 case UNSIGNED_INT64_LE: 2483 case UNSIGNED_INT64_LE:
1980 case UNSIGNED_INT64_BE: 2484 case UNSIGNED_INT64_BE:
1981 case SIGNED_INT64_LE: 2485 case SIGNED_INT64_LE:
1982 case SIGNED_INT64_BE: { 2486 case SIGNED_INT64_BE: {
1983 int i; 2487 int i;
1984 const struct mformatdescr mf_descr = 2488 const struct mformatdescr mf_descr =
1985 mformat_descriptors[mformat_code]; 2489 mformat_descriptors[mformat_code_enum];
1986 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size; 2490 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
1987 const unsigned char *memstr = 2491 const unsigned char *memstr =
1988 (unsigned char *)PyBytes_AS_STRING(items); 2492 (unsigned char *)PyBytes_AS_STRING(items);
1989 struct arraydescr *descr; 2493 struct arraydescr *descr;
1990 2494
1991 /* If possible, try to pack array's items using a data type 2495 /* If possible, try to pack array's items using a data type
1992 * that fits better. This may result in an array with narrower 2496 * that fits better. This may result in an array with narrower
1993 * or wider elements. 2497 * or wider elements.
1994 * 2498 *
1995 * For example, if a 32-bit machine pickles a L-code array of 2499 * For example, if a 32-bit machine pickles a L-code array of
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2031 default: 2535 default:
2032 PyErr_BadArgument(); 2536 PyErr_BadArgument();
2033 return NULL; 2537 return NULL;
2034 } 2538 }
2035 2539
2036 result = make_array(arraytype, (char)typecode, converted_items); 2540 result = make_array(arraytype, (char)typecode, converted_items);
2037 Py_DECREF(converted_items); 2541 Py_DECREF(converted_items);
2038 return result; 2542 return result;
2039 } 2543 }
2040 2544
2545 /*[clinic input]
2546 array.array.__reduce_ex__
2547
2548 value: object
2549 /
2550
2551 Return state information for pickling.
2552 [clinic start generated code]*/
2553
2554 PyDoc_STRVAR(array_array___reduce_ex____doc__,
2555 "sig=($self, value)\n"
2556 "Return state information for pickling.");
2557
2558 #define ARRAY_ARRAY___REDUCE_EX___METHODDEF \
2559 {"__reduce_ex__", (PyCFunction)array_array___reduce_ex__, METH_O, array_arra y___reduce_ex____doc__},
2560
2041 static PyObject * 2561 static PyObject *
2042 array_reduce_ex(arrayobject *array, PyObject *value) 2562 array_array___reduce_ex__(arrayobject *self, PyObject *value)
2563 /*[clinic end generated code: output=97a4f7f2b86a9d88 input=c36c3f85de7df6cd]*/
2043 { 2564 {
2044 PyObject *dict; 2565 PyObject *dict;
2045 PyObject *result; 2566 PyObject *result;
2046 PyObject *array_str; 2567 PyObject *array_str;
2047 int typecode = array->ob_descr->typecode; 2568 int typecode = self->ob_descr->typecode;
2048 int mformat_code; 2569 int mformat_code;
2049 static PyObject *array_reconstructor = NULL; 2570 static PyObject *array_reconstructor = NULL;
2050 long protocol; 2571 long protocol;
2051 _Py_IDENTIFIER(_array_reconstructor); 2572 _Py_IDENTIFIER(_array_reconstructor);
2052 _Py_IDENTIFIER(__dict__); 2573 _Py_IDENTIFIER(__dict__);
2053 2574
2054 if (array_reconstructor == NULL) { 2575 if (array_reconstructor == NULL) {
2055 PyObject *array_module = PyImport_ImportModule("array"); 2576 PyObject *array_module = PyImport_ImportModule("array");
2056 if (array_module == NULL) 2577 if (array_module == NULL)
2057 return NULL; 2578 return NULL;
2058 array_reconstructor = _PyObject_GetAttrId( 2579 array_reconstructor = _PyObject_GetAttrId(
2059 array_module, 2580 array_module,
2060 &PyId__array_reconstructor); 2581 &PyId__array_reconstructor);
2061 Py_DECREF(array_module); 2582 Py_DECREF(array_module);
2062 if (array_reconstructor == NULL) 2583 if (array_reconstructor == NULL)
2063 return NULL; 2584 return NULL;
2064 } 2585 }
2065 2586
2066 if (!PyLong_Check(value)) { 2587 if (!PyLong_Check(value)) {
2067 PyErr_SetString(PyExc_TypeError, 2588 PyErr_SetString(PyExc_TypeError,
2068 "__reduce_ex__ argument should an integer"); 2589 "__reduce_ex__ argument should an integer");
2069 return NULL; 2590 return NULL;
2070 } 2591 }
2071 protocol = PyLong_AsLong(value); 2592 protocol = PyLong_AsLong(value);
2072 if (protocol == -1 && PyErr_Occurred()) 2593 if (protocol == -1 && PyErr_Occurred())
2073 return NULL; 2594 return NULL;
2074 2595
2075 dict = _PyObject_GetAttrId((PyObject *)array, &PyId___dict__); 2596 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2076 if (dict == NULL) { 2597 if (dict == NULL) {
2077 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 2598 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2078 return NULL; 2599 return NULL;
2079 PyErr_Clear(); 2600 PyErr_Clear();
2080 dict = Py_None; 2601 dict = Py_None;
2081 Py_INCREF(dict); 2602 Py_INCREF(dict);
2082 } 2603 }
2083 2604
2084 mformat_code = typecode_to_mformat_code(typecode); 2605 mformat_code = typecode_to_mformat_code(typecode);
2085 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) { 2606 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2086 /* Convert the array to a list if we got something weird 2607 /* Convert the array to a list if we got something weird
2087 * (e.g., non-IEEE floats), or we are pickling the array using 2608 * (e.g., non-IEEE floats), or we are pickling the array using
2088 * a Python 2.x compatible protocol. 2609 * a Python 2.x compatible protocol.
2089 * 2610 *
2090 * It is necessary to use a list representation for Python 2.x 2611 * It is necessary to use a list representation for Python 2.x
2091 * compatible pickle protocol, since Python 2's str objects 2612 * compatible pickle protocol, since Python 2's str objects
2092 * are unpickled as unicode by Python 3. Thus it is impossible 2613 * are unpickled as unicode by Python 3. Thus it is impossible
2093 * to make arrays unpicklable by Python 3 by using their memory 2614 * to make arrays unpicklable by Python 3 by using their memory
2094 * representation, unless we resort to ugly hacks such as 2615 * representation, unless we resort to ugly hacks such as
2095 * coercing unicode objects to bytes in array_reconstructor. 2616 * coercing unicode objects to bytes in array_reconstructor.
2096 */ 2617 */
2097 PyObject *list; 2618 PyObject *list;
2098 list = array_tolist(array, NULL); 2619 list = array_array_tolist_impl(self);
2099 if (list == NULL) { 2620 if (list == NULL) {
2100 Py_DECREF(dict); 2621 Py_DECREF(dict);
2101 return NULL; 2622 return NULL;
2102 } 2623 }
2103 result = Py_BuildValue( 2624 result = Py_BuildValue(
2104 "O(CO)O", Py_TYPE(array), typecode, list, dict); 2625 "O(CO)O", Py_TYPE(self), typecode, list, dict);
2105 Py_DECREF(list); 2626 Py_DECREF(list);
2106 Py_DECREF(dict); 2627 Py_DECREF(dict);
2107 return result; 2628 return result;
2108 } 2629 }
2109 2630
2110 array_str = array_tobytes(array, NULL); 2631 array_str = array_array_tobytes_impl(self);
2111 if (array_str == NULL) { 2632 if (array_str == NULL) {
2112 Py_DECREF(dict); 2633 Py_DECREF(dict);
2113 return NULL; 2634 return NULL;
2114 } 2635 }
2115 result = Py_BuildValue( 2636 result = Py_BuildValue(
2116 "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode, 2637 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
2117 mformat_code, array_str, dict); 2638 mformat_code, array_str, dict);
2118 Py_DECREF(dict); 2639 Py_DECREF(dict);
2119 return result; 2640 return result;
2120 } 2641 }
2121 2642
2122 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2123 2643
2124 static PyObject * 2644 static PyObject *
2125 array_get_typecode(arrayobject *a, void *closure) 2645 array_get_typecode(arrayobject *a, void *closure)
2126 { 2646 {
2127 char typecode = a->ob_descr->typecode; 2647 char typecode = a->ob_descr->typecode;
2128 return PyUnicode_FromOrdinal(typecode); 2648 return PyUnicode_FromOrdinal(typecode);
2129 } 2649 }
2130 2650
2131 static PyObject * 2651 static PyObject *
2132 array_get_itemsize(arrayobject *a, void *closure) 2652 array_get_itemsize(arrayobject *a, void *closure)
2133 { 2653 {
2134 return PyLong_FromLong((long)a->ob_descr->itemsize); 2654 return PyLong_FromLong((long)a->ob_descr->itemsize);
2135 } 2655 }
2136 2656
2137 static PyGetSetDef array_getsets [] = { 2657 static PyGetSetDef array_getsets [] = {
2138 {"typecode", (getter) array_get_typecode, NULL, 2658 {"typecode", (getter) array_get_typecode, NULL,
2139 "the typecode character used to create the array"}, 2659 "the typecode character used to create the array"},
2140 {"itemsize", (getter) array_get_itemsize, NULL, 2660 {"itemsize", (getter) array_get_itemsize, NULL,
2141 "the size, in bytes, of one array item"}, 2661 "the size, in bytes, of one array item"},
2142 {NULL} 2662 {NULL}
2143 }; 2663 };
2144 2664
2145 static PyMethodDef array_methods[] = { 2665 static PyMethodDef array_methods[] = {
2146 {"append", (PyCFunction)array_append, METH_O, 2666 ARRAY_ARRAY_APPEND_METHODDEF
2147 append_doc}, 2667 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2148 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, 2668 ARRAY_ARRAY_BYTESWAP_METHODDEF
2149 buffer_info_doc}, 2669 ARRAY_ARRAY___COPY___METHODDEF
2150 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, 2670 ARRAY_ARRAY_COUNT_METHODDEF
2151 byteswap_doc}, 2671 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2152 {"__copy__", (PyCFunction)array_copy, METH_NOARGS, 2672 ARRAY_ARRAY_EXTEND_METHODDEF
2153 copy_doc}, 2673 ARRAY_ARRAY_FROMFILE_METHODDEF
2154 {"count", (PyCFunction)array_count, METH_O, 2674 ARRAY_ARRAY_FROMLIST_METHODDEF
2155 count_doc}, 2675 ARRAY_ARRAY_FROMSTRING_METHODDEF
2156 {"__deepcopy__", (PyCFunction)array_copy, METH_O, 2676 ARRAY_ARRAY_FROMBYTES_METHODDEF
2157 copy_doc}, 2677 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2158 {"extend", (PyCFunction)array_extend, METH_O, 2678 ARRAY_ARRAY_INDEX_METHODDEF
2159 extend_doc}, 2679 ARRAY_ARRAY_INSERT_METHODDEF
2160 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, 2680 ARRAY_ARRAY_POP_METHODDEF
2161 fromfile_doc}, 2681 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2162 {"fromlist", (PyCFunction)array_fromlist, METH_O, 2682 ARRAY_ARRAY_REMOVE_METHODDEF
2163 fromlist_doc}, 2683 ARRAY_ARRAY_REVERSE_METHODDEF
2164 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, 2684 ARRAY_ARRAY_TOFILE_METHODDEF
2165 fromstring_doc}, 2685 ARRAY_ARRAY_TOLIST_METHODDEF
2166 {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS, 2686 ARRAY_ARRAY_TOSTRING_METHODDEF
2167 frombytes_doc}, 2687 ARRAY_ARRAY_TOBYTES_METHODDEF
2168 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, 2688 ARRAY_ARRAY_TOUNICODE_METHODDEF
2169 fromunicode_doc}, 2689 ARRAY_ARRAY___SIZEOF___METHODDEF
2170 {"index", (PyCFunction)array_index, METH_O, 2690 {NULL, NULL} /* sentinel */
2171 index_doc},
2172 {"insert", (PyCFunction)array_insert, METH_VARARGS,
2173 insert_doc},
2174 {"pop", (PyCFunction)array_pop, METH_VARARGS,
2175 pop_doc},
2176 {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
2177 reduce_doc},
2178 {"remove", (PyCFunction)array_remove, METH_O,
2179 remove_doc},
2180 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
2181 reverse_doc},
2182 {"tofile", (PyCFunction)array_tofile, METH_O,
2183 tofile_doc},
2184 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
2185 tolist_doc},
2186 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
2187 tostring_doc},
2188 {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS,
2189 tobytes_doc},
2190 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
2191 tounicode_doc},
2192 {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,
2193 sizeof_doc},
2194 {NULL, NULL} /* sentinel */
2195 }; 2691 };
2196 2692
2197 static PyObject * 2693 static PyObject *
2198 array_repr(arrayobject *a) 2694 array_repr(arrayobject *a)
2199 { 2695 {
2200 char typecode; 2696 char typecode;
2201 PyObject *s, *v = NULL; 2697 PyObject *s, *v = NULL;
2202 Py_ssize_t len; 2698 Py_ssize_t len;
2203 2699
2204 len = Py_SIZE(a); 2700 len = Py_SIZE(a);
2205 typecode = a->ob_descr->typecode; 2701 typecode = a->ob_descr->typecode;
2206 if (len == 0) { 2702 if (len == 0) {
2207 return PyUnicode_FromFormat("array('%c')", (int)typecode); 2703 return PyUnicode_FromFormat("array('%c')", (int)typecode);
2208 } 2704 }
2209 if (typecode == 'u') { 2705 if (typecode == 'u') {
2210 v = array_tounicode(a, NULL); 2706 v = array_array_tounicode_impl(a);
2211 } else { 2707 } else {
2212 v = array_tolist(a, NULL); 2708 v = array_array_tolist_impl(a);
2213 } 2709 }
2214 if (v == NULL) 2710 if (v == NULL)
2215 return NULL; 2711 return NULL;
2216 2712
2217 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v); 2713 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
2218 Py_DECREF(v); 2714 Py_DECREF(v);
2219 return s; 2715 return s;
2220 } 2716 }
2221 2717
2222 static PyObject* 2718 static PyObject*
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
2585 } 3081 }
2586 } 3082 }
2587 else if (initial != NULL && (PyByteArray_Check(initial) || 3083 else if (initial != NULL && (PyByteArray_Check(initial) ||
2588 PyBytes_Check(initial))) { 3084 PyBytes_Check(initial))) {
2589 PyObject *t_initial, *v; 3085 PyObject *t_initial, *v;
2590 t_initial = PyTuple_Pack(1, initial); 3086 t_initial = PyTuple_Pack(1, initial);
2591 if (t_initial == NULL) { 3087 if (t_initial == NULL) {
2592 Py_DECREF(a); 3088 Py_DECREF(a);
2593 return NULL; 3089 return NULL;
2594 } 3090 }
2595 v = array_frombytes((arrayobject *)a, 3091 v = array_array_frombytes((arrayobject *)a, t_initial);
2596 t_initial);
2597 Py_DECREF(t_initial); 3092 Py_DECREF(t_initial);
2598 if (v == NULL) { 3093 if (v == NULL) {
2599 Py_DECREF(a); 3094 Py_DECREF(a);
2600 return NULL; 3095 return NULL;
2601 } 3096 }
2602 Py_DECREF(v); 3097 Py_DECREF(v);
2603 } 3098 }
2604 else if (initial != NULL && PyUnicode_Check(initial)) { 3099 else if (initial != NULL && PyUnicode_Check(initial)) {
2605 Py_UNICODE *ustr; 3100 Py_UNICODE *ustr;
2606 Py_ssize_t n; 3101 Py_ssize_t n;
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
2758 0, /* tp_descr_set */ 3253 0, /* tp_descr_set */
2759 0, /* tp_dictoffset */ 3254 0, /* tp_dictoffset */
2760 0, /* tp_init */ 3255 0, /* tp_init */
2761 PyType_GenericAlloc, /* tp_alloc */ 3256 PyType_GenericAlloc, /* tp_alloc */
2762 array_new, /* tp_new */ 3257 array_new, /* tp_new */
2763 PyObject_Del, /* tp_free */ 3258 PyObject_Del, /* tp_free */
2764 }; 3259 };
2765 3260
2766 3261
2767 /*********************** Array Iterator **************************/ 3262 /*********************** Array Iterator **************************/
3263
3264 /*[clinic input]
3265 class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
3266 [clinic start generated code]*/
3267 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
2768 3268
2769 typedef struct { 3269 typedef struct {
2770 PyObject_HEAD 3270 PyObject_HEAD
2771 Py_ssize_t index; 3271 Py_ssize_t index;
2772 arrayobject *ao; 3272 arrayobject *ao;
2773 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); 3273 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
2774 } arrayiterobject; 3274 } arrayiterobject;
2775 3275
2776 static PyTypeObject PyArrayIter_Type; 3276 static PyTypeObject PyArrayIter_Type;
2777 3277
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2816 PyObject_GC_Del(it); 3316 PyObject_GC_Del(it);
2817 } 3317 }
2818 3318
2819 static int 3319 static int
2820 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) 3320 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2821 { 3321 {
2822 Py_VISIT(it->ao); 3322 Py_VISIT(it->ao);
2823 return 0; 3323 return 0;
2824 } 3324 }
2825 3325
3326 /*[clinic input]
3327 array.arrayiterator.__reduce__
3328
3329 Return state information for pickling.
3330 [clinic start generated code]*/
3331
3332 PyDoc_STRVAR(array_arrayiterator___reduce____doc__,
3333 "sig=($self)\n"
3334 "Return state information for pickling.");
3335
3336 #define ARRAY_ARRAYITERATOR___REDUCE___METHODDEF \
3337 {"__reduce__", (PyCFunction)array_arrayiterator___reduce__, METH_NOARGS, arr ay_arrayiterator___reduce____doc__},
3338
2826 static PyObject * 3339 static PyObject *
2827 arrayiter_reduce(arrayiterobject *it) 3340 array_arrayiterator___reduce___impl(arrayiterobject *self);
3341
3342 static PyObject *
3343 array_arrayiterator___reduce__(arrayiterobject *self, PyObject *Py_UNUSED(ignore d))
2828 { 3344 {
2829 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), 3345 return array_arrayiterator___reduce___impl(self);
2830 it->ao, it->index);
2831 } 3346 }
2832 3347
2833 static PyObject * 3348 static PyObject *
2834 arrayiter_setstate(arrayiterobject *it, PyObject *state) 3349 array_arrayiterator___reduce___impl(arrayiterobject *self)
3350 /*[clinic end generated code: output=8c47223f170c0533 input=a062ea1e9951417a]*/
3351 {
3352 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
3353 self->ao, self->index);
3354 }
3355
3356 /*[clinic input]
3357 array.arrayiterator.__setstate__
3358
3359 state: object
3360 /
3361
3362 Set state information for unpickling.
3363 [clinic start generated code]*/
3364
3365 PyDoc_STRVAR(array_arrayiterator___setstate____doc__,
3366 "sig=($self, state)\n"
3367 "Set state information for unpickling.");
3368
3369 #define ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF \
3370 {"__setstate__", (PyCFunction)array_arrayiterator___setstate__, METH_O, arra y_arrayiterator___setstate____doc__},
3371
3372 static PyObject *
3373 array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
3374 /*[clinic end generated code: output=bdf2745292db97ad input=f47d5ceda19e787b]*/
2835 { 3375 {
2836 Py_ssize_t index = PyLong_AsSsize_t(state); 3376 Py_ssize_t index = PyLong_AsSsize_t(state);
2837 if (index == -1 && PyErr_Occurred()) 3377 if (index == -1 && PyErr_Occurred())
2838 return NULL; 3378 return NULL;
2839 if (index < 0) 3379 if (index < 0)
2840 index = 0; 3380 index = 0;
2841 it->index = index; 3381 self->index = index;
2842 Py_RETURN_NONE; 3382 Py_RETURN_NONE;
2843 } 3383 }
2844 3384
2845 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); 3385 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2846 static PyMethodDef arrayiter_methods[] = { 3386 static PyMethodDef arrayiter_methods[] = {
2847 {"__reduce__", (PyCFunction)arrayiter_reduce, METH_NOARGS, 3387 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2848 reduce_doc}, 3388 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
2849 {"__setstate__", (PyCFunction)arrayiter_setstate, METH_O,
2850 setstate_doc},
2851 {NULL, NULL} /* sentinel */ 3389 {NULL, NULL} /* sentinel */
2852 }; 3390 };
2853 3391
2854 static PyTypeObject PyArrayIter_Type = { 3392 static PyTypeObject PyArrayIter_Type = {
2855 PyVarObject_HEAD_INIT(NULL, 0) 3393 PyVarObject_HEAD_INIT(NULL, 0)
2856 "arrayiterator", /* tp_name */ 3394 "arrayiterator", /* tp_name */
2857 sizeof(arrayiterobject), /* tp_basicsize */ 3395 sizeof(arrayiterobject), /* tp_basicsize */
2858 0, /* tp_itemsize */ 3396 0, /* tp_itemsize */
2859 /* methods */ 3397 /* methods */
2860 (destructor)arrayiter_dealloc, /* tp_dealloc */ 3398 (destructor)arrayiter_dealloc, /* tp_dealloc */
(...skipping 20 matching lines...) Expand all
2881 PyObject_SelfIter, /* tp_iter */ 3419 PyObject_SelfIter, /* tp_iter */
2882 (iternextfunc)arrayiter_next, /* tp_iternext */ 3420 (iternextfunc)arrayiter_next, /* tp_iternext */
2883 arrayiter_methods, /* tp_methods */ 3421 arrayiter_methods, /* tp_methods */
2884 }; 3422 };
2885 3423
2886 3424
2887 /*********************** Install Module **************************/ 3425 /*********************** Install Module **************************/
2888 3426
2889 /* No functions in array module. */ 3427 /* No functions in array module. */
2890 static PyMethodDef a_methods[] = { 3428 static PyMethodDef a_methods[] = {
2891 {"_array_reconstructor", array_reconstructor, METH_VARARGS, 3429 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
2892 PyDoc_STR("Internal. Used for pickling support.")},
2893 {NULL, NULL, 0, NULL} /* Sentinel */ 3430 {NULL, NULL, 0, NULL} /* Sentinel */
2894 }; 3431 };
2895 3432
2896 static struct PyModuleDef arraymodule = { 3433 static struct PyModuleDef arraymodule = {
2897 PyModuleDef_HEAD_INIT, 3434 PyModuleDef_HEAD_INIT,
2898 "array", 3435 "array",
2899 module_doc, 3436 module_doc,
2900 -1, 3437 -1,
2901 a_methods, 3438 a_methods,
2902 NULL, 3439 NULL,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2938 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); 3475 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
2939 3476
2940 PyModule_AddObject(m, "typecodes", typecodes); 3477 PyModule_AddObject(m, "typecodes", typecodes);
2941 3478
2942 if (PyErr_Occurred()) { 3479 if (PyErr_Occurred()) {
2943 Py_DECREF(m); 3480 Py_DECREF(m);
2944 m = NULL; 3481 m = NULL;
2945 } 3482 }
2946 return m; 3483 return m;
2947 } 3484 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+