annotate Objects/abstract.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 3aa6a863ff06
children 0c617b7a727a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Abstract Object Interface (many thanks to Jim Fulton) */
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #include "Python.h"
8412
4505160a7e5c Address some gcc -Wall warnings (e.g. include <ctype.h>).
Guido van Rossum <guido@python.org>
parents: 8400
diff changeset
4 #include <ctype.h>
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
5 #include "structmember.h" /* we need the offsetof() macro from there */
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
6 #include "longintrepr.h"
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
7
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
8 #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
15944
f464a03be85a Rich comparisons fall-out:
Guido van Rossum <guido@python.org>
parents: 15721
diff changeset
9 Py_TPFLAGS_CHECKTYPES)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
11
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
12 /* Shorthands to return certain errors */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
13
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
14 static PyObject *
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
15 type_error(const char *msg, PyObject *obj)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
16 {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
17 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
18 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
19 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
20
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
21 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
22 null_error(void)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
23 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
24 if (!PyErr_Occurred())
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
25 PyErr_SetString(PyExc_SystemError,
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
26 "null argument to internal routine");
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
27 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
28 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
29
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
30 /* Operations on any object */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
31
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
32 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
33 PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
34 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
35 int r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
36
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
37 if (o1 == NULL || o2 == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
38 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
39 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
40 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
41 r = PyObject_Compare(o1, o2);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
42 if (PyErr_Occurred())
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
43 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
44 *result = r;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
49 PyObject_Type(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
53 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
54 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 v = (PyObject *)o->ob_type;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 Py_INCREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
60 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
61 PyObject_Size(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
63 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
65 if (o == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
66 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
67 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
68 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
70 m = o->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
71 if (m && m->sq_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
72 return m->sq_length(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
74 return PyMapping_Size(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
77 #undef PyObject_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
78 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
79 PyObject_Length(PyObject *o)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
80 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
81 return PyObject_Size(o);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
82 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
83 #define PyObject_Length PyObject_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
84
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
85
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
86 /* The length hint function returns a non-negative value from o.__len__()
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
87 or o.__length_hint__(). If those methods aren't found or return a negative
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
88 value, then the defaultvalue is returned. If one of the calls fails,
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
89 this function returns -1.
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
90 */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
91
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
92 Py_ssize_t
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
93 _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
94 {
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
95 static PyObject *hintstrobj = NULL;
53947
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
96 PyObject *ro, *hintmeth;
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
97 Py_ssize_t rv;
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
98
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
99 /* try o.__len__() */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
100 rv = PyObject_Size(o);
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
101 if (rv >= 0)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
102 return rv;
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
103 if (PyErr_Occurred()) {
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
104 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
105 !PyErr_ExceptionMatches(PyExc_AttributeError))
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
106 return -1;
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
107 PyErr_Clear();
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
108 }
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
109
53948
5dd3b38a0977 ignore classic classes
Benjamin Peterson <benjamin@python.org>
parents: 53947
diff changeset
110 if (PyInstance_Check(o))
5dd3b38a0977 ignore classic classes
Benjamin Peterson <benjamin@python.org>
parents: 53947
diff changeset
111 return defaultvalue;
53947
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
112 /* try o.__length_hint__() */
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
113 hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
114 if (hintmeth == NULL)
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
115 return defaultvalue;
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
116 ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
117 Py_DECREF(hintmeth);
f90b3eec7c32 convert some more special methods to use _PyObject_LookupSpecial
Benjamin Peterson <benjamin@python.org>
parents: 53213
diff changeset
118 if (ro == NULL) {
53955
d2b176161abf ignore AttributeErrors for classic classes
Benjamin Peterson <benjamin@python.org>
parents: 53948
diff changeset
119 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
d2b176161abf ignore AttributeErrors for classic classes
Benjamin Peterson <benjamin@python.org>
parents: 53948
diff changeset
120 !PyErr_ExceptionMatches(PyExc_AttributeError))
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
121 return -1;
53957
aa41c8c96055 clear error state properly
Benjamin Peterson <benjamin@python.org>
parents: 53955
diff changeset
122 PyErr_Clear();
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
123 return defaultvalue;
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
124 }
51462
c5935c7497fb Remove redundant assignment in _PyObject_LengthHint
Mark Dickinson <dickinsm@gmail.com>
parents: 51288
diff changeset
125 rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
126 Py_DECREF(ro);
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
127 return rv;
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
128 }
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
129
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
131 PyObject_GetItem(PyObject *o, PyObject *key)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
133 PyMappingMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
135 if (o == NULL || key == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
136 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
138 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
139 if (m && m->mp_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
140 return m->mp_subscript(o, key);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
142 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
143 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
144 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
145 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
146 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
147 return NULL;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
148 return PySequence_GetItem(o, key_value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
149 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
150 else if (o->ob_type->tp_as_sequence->sq_item)
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
151 return type_error("sequence index must "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
152 "be integer, not '%.200s'", key);
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
153 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
154
53213
02cb420d862e "not subscriptable" should be a bit more understandable than "unsubscriptable".
Georg Brandl <georg@python.org>
parents: 52749
diff changeset
155 return type_error("'%.200s' object is not subscriptable", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
159 PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
161 PyMappingMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
163 if (o == NULL || key == NULL || value == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
164 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
165 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
166 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
167 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
168 if (m && m->mp_ass_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
169 return m->mp_ass_subscript(o, key, value);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
171 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
172 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
173 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
174 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
175 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
176 return -1;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
177 return PySequence_SetItem(o, key_value, value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
178 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
179 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
180 type_error("sequence index must be "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
181 "integer, not '%.200s'", key);
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
182 return -1;
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
183 }
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
184 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
185
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
186 type_error("'%.200s' object does not support item assignment", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
187 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
190 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
191 PyObject_DelItem(PyObject *o, PyObject *key)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
192 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
193 PyMappingMethods *m;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
194
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
195 if (o == NULL || key == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
196 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
197 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
198 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
199 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
200 if (m && m->mp_ass_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
201 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
202
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
203 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
204 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
205 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
206 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
207 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
208 return -1;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
209 return PySequence_DelItem(o, key_value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
210 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
211 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
212 type_error("sequence index must be "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
213 "integer, not '%.200s'", key);
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
214 return -1;
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
215 }
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
216 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
217
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
218 type_error("'%.200s' object does not support item deletion", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
219 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
220 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
221
21644
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
222 int
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
223 PyObject_DelItemString(PyObject *o, char *key)
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
224 {
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
225 PyObject *okey;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
226 int ret;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
227
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
228 if (o == NULL || key == NULL) {
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
229 null_error();
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
230 return -1;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
231 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
232 okey = PyString_FromString(key);
21644
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
233 if (okey == NULL)
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
234 return -1;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
235 ret = PyObject_DelItem(o, okey);
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
236 Py_DECREF(okey);
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
237 return ret;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
238 }
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
239
38632
931ad06bec56 Fix coding style guide bug.
Brett Cannon <bcannon@gmail.com>
parents: 37813
diff changeset
240 int
931ad06bec56 Fix coding style guide bug.
Brett Cannon <bcannon@gmail.com>
parents: 37813
diff changeset
241 PyObject_AsCharBuffer(PyObject *obj,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
242 const char **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
243 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
244 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
245 PyBufferProcs *pb;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
246 char *pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
247 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
248
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
249 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
250 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
251 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
252 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
253 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
254 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
255 pb->bf_getcharbuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
256 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
257 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
258 "expected a character buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
259 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
260 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
261 if ((*pb->bf_getsegcount)(obj,NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
262 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
263 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
264 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
265 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
266 len = (*pb->bf_getcharbuffer)(obj, 0, &pp);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
267 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
268 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
269 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
270 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
271 return 0;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
272 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
273
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
274 int
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
275 PyObject_CheckReadBuffer(PyObject *obj)
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
276 {
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
277 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
278
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
279 if (pb == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
280 pb->bf_getreadbuffer == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
281 pb->bf_getsegcount == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
282 (*pb->bf_getsegcount)(obj, NULL) != 1)
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
283 return 0;
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
284 return 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
285 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
286
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
287 int PyObject_AsReadBuffer(PyObject *obj,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
288 const void **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
289 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
290 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
291 PyBufferProcs *pb;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
292 void *pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
293 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
294
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
295 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
296 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
297 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
298 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
299 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
300 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
301 pb->bf_getreadbuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
302 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
303 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
304 "expected a readable buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
305 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
306 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
307 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
308 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
309 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
310 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
311 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
312 len = (*pb->bf_getreadbuffer)(obj, 0, &pp);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
313 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
314 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
315 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
316 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
317 return 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
318 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
319
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
320 int PyObject_AsWriteBuffer(PyObject *obj,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
321 void **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
322 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
323 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
324 PyBufferProcs *pb;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
325 void*pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
326 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
327
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
328 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
329 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
330 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
331 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
332 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
333 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
334 pb->bf_getwritebuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
335 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
336 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
337 "expected a writeable buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
338 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
339 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
340 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
341 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
342 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
343 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
344 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
345 len = (*pb->bf_getwritebuffer)(obj,0,&pp);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
346 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
347 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
348 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
349 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
350 return 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
351 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
352
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
353 /* Buffer C-API for Python 3.0 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
354
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
355 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
356 PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
357 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
358 if (!PyObject_CheckBuffer(obj)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
359 PyErr_Format(PyExc_TypeError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
360 "'%100s' does not have the buffer interface",
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
361 Py_TYPE(obj)->tp_name);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
362 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
363 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
364 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
365 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
366
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
367 static int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
368 _IsFortranContiguous(Py_buffer *view)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
369 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
370 Py_ssize_t sd, dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
371 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
372
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
373 if (view->ndim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
374 if (view->strides == NULL) return (view->ndim == 1);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
375
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
376 sd = view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
377 if (view->ndim == 1) return (view->shape[0] == 1 ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
378 sd == view->strides[0]);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
379 for (i=0; i<view->ndim; i++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
380 dim = view->shape[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
381 if (dim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
382 if (view->strides[i] != sd) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
383 sd *= dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
384 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
385 return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
386 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
387
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
388 static int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
389 _IsCContiguous(Py_buffer *view)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
390 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
391 Py_ssize_t sd, dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
392 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
393
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
394 if (view->ndim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
395 if (view->strides == NULL) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
396
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
397 sd = view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
398 if (view->ndim == 1) return (view->shape[0] == 1 ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
399 sd == view->strides[0]);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
400 for (i=view->ndim-1; i>=0; i--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
401 dim = view->shape[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
402 if (dim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
403 if (view->strides[i] != sd) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
404 sd *= dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
405 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
406 return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
407 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
408
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
409 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
410 PyBuffer_IsContiguous(Py_buffer *view, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
411 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
412
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
413 if (view->suboffsets != NULL) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
414
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
415 if (fort == 'C')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
416 return _IsCContiguous(view);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
417 else if (fort == 'F')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
418 return _IsFortranContiguous(view);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
419 else if (fort == 'A')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
420 return (_IsCContiguous(view) || _IsFortranContiguous(view));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
421 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
422 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
423
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
424
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
425 void*
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
426 PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
427 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
428 char* pointer;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
429 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
430 pointer = (char *)view->buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
431 for (i = 0; i < view->ndim; i++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
432 pointer += view->strides[i]*indices[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
433 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
434 pointer = *((char**)pointer) + view->suboffsets[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
435 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
436 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
437 return (void*)pointer;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
438 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
439
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
440
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51775
diff changeset
441 void
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
442 _add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
443 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
444 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
445
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
446 for (k=0; k<nd; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
447 if (index[k] < shape[k]-1) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
448 index[k]++;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
449 break;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
450 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
451 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
452 index[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
453 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
454 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
455 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
456
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51775
diff changeset
457 void
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
458 _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
459 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
460 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
461
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
462 for (k=nd-1; k>=0; k--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
463 if (index[k] < shape[k]-1) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
464 index[k]++;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
465 break;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
466 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
467 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
468 index[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
469 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
470 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
471 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
472
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
473 /* view is not checked for consistency in either of these. It is
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
474 assumed that the size of the buffer is view->len in
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
475 view->len / view->itemsize elements.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
476 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
477
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
478 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
479 PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
480 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
481 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
482 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
483 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
484 char *dest, *ptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
485
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
486 if (len > view->len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
487 len = view->len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
488 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
489
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
490 if (PyBuffer_IsContiguous(view, fort)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
491 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
492 memcpy(buf, view->buf, len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
493 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
494 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
495
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
496 /* Otherwise a more elaborate scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
497
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
498 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
499 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
500 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
501 PyErr_NoMemory();
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
502 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
503 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
504 for (k=0; k<view->ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
505 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
506 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
507
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
508 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
509 addone = _add_one_to_index_F;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
510 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
511 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
512 addone = _add_one_to_index_C;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
513 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
514 dest = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
515 /* XXX : This is not going to be the fastest code in the world
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
516 several optimizations are possible.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
517 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
518 elements = len / view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
519 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
520 addone(view->ndim, indices, view->shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
521 ptr = PyBuffer_GetPointer(view, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
522 memcpy(dest, ptr, view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
523 dest += view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
524 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
525 PyMem_Free(indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
526 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
527 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
528
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
529 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
530 PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
531 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
532 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
533 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
534 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
535 char *src, *ptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
536
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
537 if (len > view->len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
538 len = view->len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
539 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
540
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
541 if (PyBuffer_IsContiguous(view, fort)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
542 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
543 memcpy(view->buf, buf, len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
544 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
545 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
546
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
547 /* Otherwise a more elaborate scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
548
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
549 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
550 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
551 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
552 PyErr_NoMemory();
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
553 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
554 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
555 for (k=0; k<view->ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
556 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
557 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
558
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
559 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
560 addone = _add_one_to_index_F;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
561 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
562 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
563 addone = _add_one_to_index_C;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
564 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
565 src = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
566 /* XXX : This is not going to be the fastest code in the world
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
567 several optimizations are possible.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
568 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
569 elements = len / view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
570 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
571 addone(view->ndim, indices, view->shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
572 ptr = PyBuffer_GetPointer(view, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
573 memcpy(ptr, src, view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
574 src += view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
575 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
576
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
577 PyMem_Free(indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
578 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
579 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
580
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
581 int PyObject_CopyData(PyObject *dest, PyObject *src)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
582 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
583 Py_buffer view_dest, view_src;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
584 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
585 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
586 char *dptr, *sptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
587
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
588 if (!PyObject_CheckBuffer(dest) ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
589 !PyObject_CheckBuffer(src)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
590 PyErr_SetString(PyExc_TypeError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
591 "both destination and source must have the "\
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
592 "buffer interface");
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
593 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
594 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
595
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
596 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
597 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
598 PyBuffer_Release(&view_dest);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
599 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
600 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
601
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
602 if (view_dest.len < view_src.len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
603 PyErr_SetString(PyExc_BufferError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
604 "destination is too small to receive data from source");
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
605 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
606 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
607 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
608 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
609
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
610 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
611 PyBuffer_IsContiguous(&view_src, 'C')) ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
612 (PyBuffer_IsContiguous(&view_dest, 'F') &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
613 PyBuffer_IsContiguous(&view_src, 'F'))) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
614 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
615 memcpy(view_dest.buf, view_src.buf, view_src.len);
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
616 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
617 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
618 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
619 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
620
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
621 /* Otherwise a more elaborate copy scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
622
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
623 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
624 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
625 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
626 PyErr_NoMemory();
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
627 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
628 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
629 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
630 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
631 for (k=0; k<view_src.ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
632 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
633 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
634 elements = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
635 for (k=0; k<view_src.ndim; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
636 /* XXX(nnorwitz): can this overflow? */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
637 elements *= view_src.shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
638 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
639 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
640 _add_one_to_index_C(view_src.ndim, indices, view_src.shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
641 dptr = PyBuffer_GetPointer(&view_dest, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
642 sptr = PyBuffer_GetPointer(&view_src, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
643 memcpy(dptr, sptr, view_src.itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
644 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
645 PyMem_Free(indices);
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
646 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
647 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
648 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
649 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
650
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
651 void
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
652 PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
653 Py_ssize_t *strides, int itemsize,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
654 char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
655 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
656 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
657 Py_ssize_t sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
658
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
659 sd = itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
660 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
661 for (k=0; k<nd; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
662 strides[k] = sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
663 sd *= shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
664 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
665 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
666 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
667 for (k=nd-1; k>=0; k--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
668 strides[k] = sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
669 sd *= shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
670 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
671 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
672 return;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
673 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
674
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
675 int
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
676 PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
677 int readonly, int flags)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
678 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
679 if (view == NULL) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
680 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
681 (readonly == 1)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
682 PyErr_SetString(PyExc_BufferError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
683 "Object is not writable.");
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
684 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
685 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
686
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
687 view->obj = obj;
48231
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
688 if (obj)
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
689 Py_INCREF(obj);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
690 view->buf = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
691 view->len = len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
692 view->readonly = readonly;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
693 view->itemsize = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
694 view->format = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
695 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
696 view->format = "B";
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
697 view->ndim = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
698 view->shape = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
699 if ((flags & PyBUF_ND) == PyBUF_ND)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
700 view->shape = &(view->len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
701 view->strides = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
702 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
703 view->strides = &(view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
704 view->suboffsets = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
705 view->internal = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
706 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
707 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
708
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
709 void
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
710 PyBuffer_Release(Py_buffer *view)
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
711 {
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
712 PyObject *obj = view->obj;
48236
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
713 if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
714 Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
715 Py_XDECREF(obj);
48231
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
716 view->obj = NULL;
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
717 }
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
718
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
719 PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
720 PyObject_Format(PyObject* obj, PyObject *format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
721 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
722 static PyObject * str__format__ = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
723 PyObject *empty = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
724 PyObject *result = NULL;
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
725 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
726 int spec_is_unicode;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
727 int result_is_unicode;
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
728 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
729
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
730 /* Initialize cached value */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
731 if (str__format__ == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
732 /* Initialize static variable needed by _PyType_Lookup */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
733 str__format__ = PyString_InternFromString("__format__");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
734 if (str__format__ == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
735 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
736 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
737
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
738 /* If no format_spec is provided, use an empty string */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
739 if (format_spec == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
740 empty = PyString_FromStringAndSize(NULL, 0);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
741 format_spec = empty;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
742 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
743
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
744 /* Check the format_spec type, and make sure it's str or unicode */
51034
21cf96cc2216 Fixed compile error on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51032
diff changeset
745 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
746 if (PyUnicode_Check(format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
747 spec_is_unicode = 1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
748 else if (PyString_Check(format_spec))
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
749 spec_is_unicode = 0;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
750 else {
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
751 #else
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
752 if (!PyString_Check(format_spec)) {
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
753 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
754 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
755 "format expects arg 2 to be string "
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
756 "or unicode, not %.100s", Py_TYPE(format_spec)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
757 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
758 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
759
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
760 /* Make sure the type is initialized. float gets initialized late */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
761 if (Py_TYPE(obj)->tp_dict == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
762 if (PyType_Ready(Py_TYPE(obj)) < 0)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
763 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
764
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
765 /* Check for a __format__ method and call it. */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
766 if (PyInstance_Check(obj)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
767 /* We're an instance of a classic class */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
768 PyObject *bound_method = PyObject_GetAttr(obj,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
769 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
770 if (bound_method != NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
771 result = PyObject_CallFunctionObjArgs(bound_method,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
772 format_spec,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
773 NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
774 Py_DECREF(bound_method);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
775 } else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
776 PyObject *self_as_str;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
777 PyObject *format_method;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
778
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
779 PyErr_Clear();
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
780 /* Per the PEP, convert to str (or unicode,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
781 depending on the type of the format
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
782 specifier). For new-style classes, this
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
783 logic is done by object.__format__(). */
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
784 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
785 if (spec_is_unicode)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
786 self_as_str = PyObject_Unicode(obj);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
787 else
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
788 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
789 self_as_str = PyObject_Str(obj);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
790 if (self_as_str == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
791 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
792
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
793 /* Then call str.__format__ on that result */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
794 format_method = PyObject_GetAttr(self_as_str,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
795 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
796 if (format_method == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
797 Py_DECREF(self_as_str);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
798 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
799 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
800 result = PyObject_CallFunctionObjArgs(format_method,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
801 format_spec,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
802 NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
803 Py_DECREF(self_as_str);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
804 Py_DECREF(format_method);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
805 if (result == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
806 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
807 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
808 } else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
809 /* Not an instance of a classic class, use the code
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
810 from py3k */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
811
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
812 /* Find the (unbound!) __format__ method (a borrowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
813 reference) */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
814 PyObject *method = _PyType_Lookup(Py_TYPE(obj),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
815 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
816 if (method == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
817 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
818 "Type %.100s doesn't define __format__",
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
819 Py_TYPE(obj)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
820 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
821 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
822 /* And call it, binding it to the value */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
823 result = PyObject_CallFunctionObjArgs(method, obj,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
824 format_spec, NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
825 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
826
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
827 if (result == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
828 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
829
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
830 /* Check the result type, and make sure it's str or unicode */
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
831 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
832 if (PyUnicode_Check(result))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
833 result_is_unicode = 1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
834 else if (PyString_Check(result))
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
835 result_is_unicode = 0;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
836 else {
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
837 #else
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
838 if (!PyString_Check(result)) {
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
839 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
840 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
841 "%.100s.__format__ must return string or "
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
842 "unicode, not %.100s", Py_TYPE(obj)->tp_name,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
843 Py_TYPE(result)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
844 Py_DECREF(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
845 result = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
846 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
847 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
848
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
849 /* Convert to unicode, if needed. Required if spec is unicode
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
850 and result is str */
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
851 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
852 if (spec_is_unicode && !result_is_unicode) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
853 PyObject *tmp = PyObject_Unicode(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
854 /* This logic works whether or not tmp is NULL */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
855 Py_DECREF(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
856 result = tmp;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
857 }
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
858 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
859
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
860 done:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
861 Py_XDECREF(empty);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
862 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
863 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
864
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
865 /* Operations on numbers */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
866
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
867 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
868 PyNumber_Check(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 {
27937
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
870 return o && o->ob_type->tp_as_number &&
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
871 (o->ob_type->tp_as_number->nb_int ||
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
872 o->ob_type->tp_as_number->nb_float);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
874
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
875 /* Binary operators */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
876
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
877 /* New style number protocol support */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
878
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
879 #define NB_SLOT(x) offsetof(PyNumberMethods, x)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
880 #define NB_BINOP(nb_methods, slot) \
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
881 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
882 #define NB_TERNOP(nb_methods, slot) \
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
883 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
884
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
885 /*
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
886 Calling scheme used for binary operations:
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
887
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
888 v w Action
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
889 -------------------------------------------------------------------
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
890 new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
891 new old v.op(v,w), coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
892 old new w.op(v,w), coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
893 old old coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
894
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
895 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
896 v->ob_type
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
897
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
898 Legend:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
899 -------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
900 * new == new style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
901 * old == old style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
902 * Action indicates the order in which operations are tried until either
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
903 a valid result is produced or an error occurs.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
904
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
905 */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
906
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
907 static PyObject *
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
908 binary_op1(PyObject *v, PyObject *w, const int op_slot)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
910 PyObject *x;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
911 binaryfunc slotv = NULL;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
912 binaryfunc slotw = NULL;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
913
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
914 if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v))
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
915 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
916 if (w->ob_type != v->ob_type &&
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
917 w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
918 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
919 if (slotw == slotv)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
920 slotw = NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 }
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
922 if (slotv) {
20203
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
923 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
924 x = slotw(v, w);
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
925 if (x != Py_NotImplemented)
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
926 return x;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
927 Py_DECREF(x); /* can't do it */
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
928 slotw = NULL;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
929 }
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
930 x = slotv(v, w);
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
931 if (x != Py_NotImplemented)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
932 return x;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
933 Py_DECREF(x); /* can't do it */
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
934 }
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
935 if (slotw) {
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
936 x = slotw(v, w);
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
937 if (x != Py_NotImplemented)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
938 return x;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
939 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
940 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
941 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
942 int err = PyNumber_CoerceEx(&v, &w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
943 if (err < 0) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
944 return NULL;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
945 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
946 if (err == 0) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
947 PyNumberMethods *mv = v->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
948 if (mv) {
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
949 binaryfunc slot;
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
950 slot = NB_BINOP(mv, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
951 if (slot) {
36244
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 36150
diff changeset
952 x = slot(v, w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
953 Py_DECREF(v);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
954 Py_DECREF(w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
955 return x;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
956 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
957 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
958 /* CoerceEx incremented the reference counts */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
959 Py_DECREF(v);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
960 Py_DECREF(w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
961 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
962 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
963 Py_INCREF(Py_NotImplemented);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
964 return Py_NotImplemented;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 }
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
966
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
967 static PyObject *
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
968 binop_type_error(PyObject *v, PyObject *w, const char *op_name)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
969 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
970 PyErr_Format(PyExc_TypeError,
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
971 "unsupported operand type(s) for %.100s: "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
972 "'%.100s' and '%.100s'",
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
973 op_name,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
974 v->ob_type->tp_name,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
975 w->ob_type->tp_name);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
976 return NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
977 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
978
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
979 static PyObject *
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
980 binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
982 PyObject *result = binary_op1(v, w, op_slot);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
983 if (result == Py_NotImplemented) {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
984 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
985 return binop_type_error(v, w, op_name);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
987 return result;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
989
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
990
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
991 /*
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
992 Calling scheme used for ternary operations:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
993
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
994 *** In some cases, w.op is called before v.op; see binary_op1. ***
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
995
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
996 v w z Action
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
997 -------------------------------------------------------------------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
998 new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
999 new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1000 old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1001 old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1002 new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1003 new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1004 old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1005 old old old coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1006
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1007 Legend:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1008 -------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1009 * new == new style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1010 * old == old style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1011 * Action indicates the order in which operations are tried until either
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1012 a valid result is produced or an error occurs.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1013 * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1014 only if z != Py_None; if z == Py_None, then it is treated as absent
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1015 variable and only coerce(v,w) is tried.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1016
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1017 */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1018
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1019 static PyObject *
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1020 ternary_op(PyObject *v,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1021 PyObject *w,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1022 PyObject *z,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1023 const int op_slot,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1024 const char *op_name)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1026 PyNumberMethods *mv, *mw, *mz;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1027 PyObject *x = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1028 ternaryfunc slotv = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1029 ternaryfunc slotw = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1030 ternaryfunc slotz = NULL;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1031
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1032 mv = v->ob_type->tp_as_number;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
1033 mw = w->ob_type->tp_as_number;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1034 if (mv != NULL && NEW_STYLE_NUMBER(v))
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1035 slotv = NB_TERNOP(mv, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1036 if (w->ob_type != v->ob_type &&
26567
0601a90558fd Fix typo in abstract.c which caused __rpow__ to not be invoked.
Raymond Hettinger <python@rcn.com>
parents: 26386
diff changeset
1037 mw != NULL && NEW_STYLE_NUMBER(w)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1038 slotw = NB_TERNOP(mw, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1039 if (slotw == slotv)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1040 slotw = NULL;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
1041 }
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1042 if (slotv) {
20203
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1043 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1044 x = slotw(v, w, z);
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1045 if (x != Py_NotImplemented)
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1046 return x;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1047 Py_DECREF(x); /* can't do it */
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1048 slotw = NULL;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1049 }
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1050 x = slotv(v, w, z);
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1051 if (x != Py_NotImplemented)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1052 return x;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1053 Py_DECREF(x); /* can't do it */
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1054 }
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1055 if (slotw) {
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1056 x = slotw(v, w, z);
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1057 if (x != Py_NotImplemented)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1058 return x;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1059 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1060 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1061 mz = z->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1062 if (mz != NULL && NEW_STYLE_NUMBER(z)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1063 slotz = NB_TERNOP(mz, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1064 if (slotz == slotv || slotz == slotw)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1065 slotz = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1066 if (slotz) {
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1067 x = slotz(v, w, z);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1068 if (x != Py_NotImplemented)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1069 return x;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1070 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1071 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1072 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1073
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1074 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) ||
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1075 (z != Py_None && !NEW_STYLE_NUMBER(z))) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1076 /* we have an old style operand, coerce */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1077 PyObject *v1, *z1, *w2, *z2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1078 int c;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1079
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1080 c = PyNumber_Coerce(&v, &w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1081 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1082 goto error3;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1083
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1084 /* Special case: if the third argument is None, it is
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1085 treated as absent argument and not coerced. */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1086 if (z == Py_None) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1087 if (v->ob_type->tp_as_number) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1088 slotz = NB_TERNOP(v->ob_type->tp_as_number,
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1089 op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1090 if (slotz)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1091 x = slotz(v, w, z);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1092 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1093 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1094 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1095 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1096 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1097 goto error2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1098 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1099 v1 = v;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1100 z1 = z;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1101 c = PyNumber_Coerce(&v1, &z1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1102 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1103 goto error2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1104 w2 = w;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1105 z2 = z1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1106 c = PyNumber_Coerce(&w2, &z2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1107 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1108 goto error1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1109
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1110 if (v1->ob_type->tp_as_number != NULL) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1111 slotv = NB_TERNOP(v1->ob_type->tp_as_number,
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1112 op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1113 if (slotv)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1114 x = slotv(v1, w2, z2);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1115 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1116 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1117 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1118 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1119 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1120
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1121 Py_DECREF(w2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1122 Py_DECREF(z2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1123 error1:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1124 Py_DECREF(v1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1125 Py_DECREF(z1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1126 error2:
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 Py_DECREF(w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1129 error3:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1130 if (c >= 0)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 }
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1133
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1134 if (z == Py_None)
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1135 PyErr_Format(
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1136 PyExc_TypeError,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1137 "unsupported operand type(s) for ** or pow(): "
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1138 "'%.100s' and '%.100s'",
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1139 v->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1140 w->ob_type->tp_name);
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1141 else
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1142 PyErr_Format(
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1143 PyExc_TypeError,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1144 "unsupported operand type(s) for pow(): "
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1145 "'%.100s', '%.100s', '%.100s'",
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1146 v->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1147 w->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1148 z->ob_type->tp_name);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1149 return NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1152 #define BINARY_FUNC(func, op, op_name) \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1153 PyObject * \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1154 func(PyObject *v, PyObject *w) { \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1155 return binary_op(v, w, NB_SLOT(op), op_name); \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1156 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1157
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1158 BINARY_FUNC(PyNumber_Or, nb_or, "|")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1159 BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1160 BINARY_FUNC(PyNumber_And, nb_and, "&")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1161 BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1162 BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1163 BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1164 BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1165 BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1168 PyNumber_Add(PyObject *v, PyObject *w)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1170 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1171 if (result == Py_NotImplemented) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1172 PySequenceMethods *m = v->ob_type->tp_as_sequence;
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1173 Py_DECREF(result);
22194
fc6dc2ea25cf Fix leak of NotImplemented in previous checkin to PyNumber_Add().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22193
diff changeset
1174 if (m && m->sq_concat) {
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1175 return (*m->sq_concat)(v, w);
22194
fc6dc2ea25cf Fix leak of NotImplemented in previous checkin to PyNumber_Add().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22193
diff changeset
1176 }
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1177 result = binop_type_error(v, w, "+");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1179 return result;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1181
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1182 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1183 sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1184 {
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1185 Py_ssize_t count;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1186 if (PyIndex_Check(n)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1187 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1188 if (count == -1 && PyErr_Occurred())
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1189 return NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1190 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1191 else {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1192 return type_error("can't multiply sequence by "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1193 "non-int of type '%.200s'", n);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1194 }
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1195 return (*repeatfunc)(seq, count);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1196 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1197
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1198 PyObject *
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1199 PyNumber_Multiply(PyObject *v, PyObject *w)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1200 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1201 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1202 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1203 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1204 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
26987
92e572e3ca9f Merge to trunk from release branch:
Guido van Rossum <guido@python.org>
parents: 26865
diff changeset
1205 Py_DECREF(result);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1206 if (mv && mv->sq_repeat) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1207 return sequence_repeat(mv->sq_repeat, v, w);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1208 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1209 else if (mw && mw->sq_repeat) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1210 return sequence_repeat(mw->sq_repeat, w, v);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1211 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1212 result = binop_type_error(v, w, "*");
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1213 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1214 return result;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1215 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1216
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 PyObject *
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1218 PyNumber_FloorDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1219 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1220 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1221 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1222 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1223
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1224 PyObject *
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1225 PyNumber_TrueDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1226 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1227 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1228 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1229 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1230
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1231 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1232 PyNumber_Remainder(PyObject *v, PyObject *w)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1234 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1238 PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1240 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1243 /* Binary in-place operators */
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1244
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1245 /* The in-place operators are defined to fall back to the 'normal',
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1246 non in-place operations, if the in-place methods are not in place.
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1247
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1248 - If the left hand object has the appropriate struct members, and
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1249 they are filled, call the appropriate function and return the
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1250 result. No coercion is done on the arguments; the left-hand object
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1251 is the one the operation is performed on, and it's up to the
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1252 function to deal with the right-hand object.
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1253
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1254 - Otherwise, in-place modification is not supported. Handle it exactly as
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1255 a non in-place operation of the same kind.
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1256
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1257 */
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1258
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1259 #define HASINPLACE(t) \
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1260 PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1261
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1262 static PyObject *
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1263 binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1264 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1265 PyNumberMethods *mv = v->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1266 if (mv != NULL && HASINPLACE(v)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1267 binaryfunc slot = NB_BINOP(mv, iop_slot);
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1268 if (slot) {
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1269 PyObject *x = (slot)(v, w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1270 if (x != Py_NotImplemented) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1271 return x;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1272 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1273 Py_DECREF(x);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1274 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1275 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1276 return binary_op1(v, w, op_slot);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1277 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1278
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1279 static PyObject *
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1280 binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1281 const char *op_name)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1282 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1283 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1284 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1285 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1286 return binop_type_error(v, w, op_name);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1287 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1288 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1289 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1290
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1291 #define INPLACE_BINOP(func, iop, op, op_name) \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1292 PyObject * \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1293 func(PyObject *v, PyObject *w) { \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1294 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1295 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1296
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1297 INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1298 INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1299 INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1300 INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1301 INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1302 INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1303 INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1304
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1305 PyObject *
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1306 PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1307 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1308 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1309 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1310 NB_SLOT(nb_floor_divide), "//=");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1311 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1312
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1313 PyObject *
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1314 PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1315 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1316 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1317 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1318 NB_SLOT(nb_true_divide), "/=");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1319 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1320
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1321 PyObject *
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1322 PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1323 {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1324 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1325 NB_SLOT(nb_add));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1326 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1327 PySequenceMethods *m = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1328 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1329 if (m != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1330 binaryfunc f = NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1331 if (HASINPLACE(v))
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1332 f = m->sq_inplace_concat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1333 if (f == NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1334 f = m->sq_concat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1335 if (f != NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1336 return (*f)(v, w);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1337 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1338 result = binop_type_error(v, w, "+=");
14374
8df166ca09b2 Rewritten some pieces of PyNumber_InPlaceAdd() for clarity.
Guido van Rossum <guido@python.org>
parents: 14343
diff changeset
1339 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1340 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1341 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1342
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1343 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1344 PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1345 {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1346 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1347 NB_SLOT(nb_multiply));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1348 if (result == Py_NotImplemented) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1349 ssizeargfunc f = NULL;
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1350 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1351 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1352 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1353 if (mv != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1354 if (HASINPLACE(v))
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1355 f = mv->sq_inplace_repeat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1356 if (f == NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1357 f = mv->sq_repeat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1358 if (f != NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1359 return sequence_repeat(f, v, w);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1360 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1361 else if (mw != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1362 /* Note that the right hand operand should not be
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1363 * mutated in this case so sq_inplace_repeat is not
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1364 * used. */
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1365 if (mw->sq_repeat)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1366 return sequence_repeat(mw->sq_repeat, w, v);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1367 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1368 result = binop_type_error(v, w, "*=");
14377
dd4766aa2fd2 Cosmetic cleanup by Vladimir.
Thomas Wouters <thomas@python.org>
parents: 14374
diff changeset
1369 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1370 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1371 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1372
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1373 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1374 PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1375 {
26383
e7486f3ef759 Remove special handling of str and unicode in PyNumber_InPlaceRemainder. They
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26271
diff changeset
1376 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
e7486f3ef759 Remove special handling of str and unicode in PyNumber_InPlaceRemainder. They
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26271
diff changeset
1377 NB_SLOT(nb_remainder), "%=");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1378 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1379
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1380 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1381 PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1382 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1383 if (HASINPLACE(v) && v->ob_type->tp_as_number &&
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1384 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1385 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1386 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1387 else {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1388 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1389 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1390 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1391
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1392
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1393 /* Unary operators and functions */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1394
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1396 PyNumber_Negative(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1397 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1398 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1399
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1400 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1401 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1402 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1403 if (m && m->nb_negative)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1404 return (*m->nb_negative)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1405
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1406 return type_error("bad operand type for unary -: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1410 PyNumber_Positive(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1411 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1412 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1413
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1414 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1415 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1416 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1417 if (m && m->nb_positive)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1418 return (*m->nb_positive)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1419
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1420 return type_error("bad operand type for unary +: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1421 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1423 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1424 PyNumber_Invert(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1425 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1426 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1427
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1428 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1429 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1430 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1431 if (m && m->nb_invert)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1432 return (*m->nb_invert)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1433
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1434 return type_error("bad operand type for unary ~: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1435 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1436
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1438 PyNumber_Absolute(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1440 PyNumberMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1441
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1442 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1443 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1444 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1445 if (m && m->nb_absolute)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1446 return m->nb_absolute(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1447
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1448 return type_error("bad operand type for abs(): '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1449 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1450
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1451 /* Add a check for embedded NULL-bytes in the argument. */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1452 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1453 int_from_string(const char *s, Py_ssize_t len)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1454 {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1455 char *end;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1456 PyObject *x;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1457
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1458 x = PyInt_FromString((char*)s, &end, 10);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1459 if (x == NULL)
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1460 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1461 if (end != s + len) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1462 PyErr_SetString(PyExc_ValueError,
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1463 "null byte in argument for int()");
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1464 Py_DECREF(x);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1465 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1466 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1467 return x;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1468 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1469
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1470 /* Return a Python Int or Long from the object item
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1471 Raise TypeError if the result is not an int-or-long
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1472 or if the object cannot be interpreted as an index.
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1473 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1474 PyObject *
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1475 PyNumber_Index(PyObject *item)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1476 {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1477 PyObject *result = NULL;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1478 if (item == NULL)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1479 return null_error();
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1480 if (PyInt_Check(item) || PyLong_Check(item)) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1481 Py_INCREF(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1482 return item;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1483 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1484 if (PyIndex_Check(item)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1485 result = item->ob_type->tp_as_number->nb_index(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1486 if (result &&
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1487 !PyInt_Check(result) && !PyLong_Check(result)) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1488 PyErr_Format(PyExc_TypeError,
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1489 "__index__ returned non-(int,long) " \
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1490 "(type %.200s)",
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1491 result->ob_type->tp_name);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1492 Py_DECREF(result);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1493 return NULL;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1494 }
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1495 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1496 else {
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1497 PyErr_Format(PyExc_TypeError,
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1498 "'%.200s' object cannot be interpreted "
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1499 "as an index", item->ob_type->tp_name);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1500 }
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1501 return result;
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1502 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1503
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1504 /* Return an error on Overflow only if err is not NULL*/
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1505
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1506 Py_ssize_t
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1507 PyNumber_AsSsize_t(PyObject *item, PyObject *err)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1508 {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1509 Py_ssize_t result;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1510 PyObject *runerr;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1511 PyObject *value = PyNumber_Index(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1512 if (value == NULL)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1513 return -1;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1514
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1515 /* We're done if PyInt_AsSsize_t() returns without error. */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1516 result = PyInt_AsSsize_t(value);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1517 if (result != -1 || !(runerr = PyErr_Occurred()))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1518 goto finish;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1519
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1520 /* Error handling code -- only manage OverflowError differently */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1521 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1522 goto finish;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1523
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1524 PyErr_Clear();
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1525 /* If no error-handling desired then the default clipping
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1526 is sufficient.
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1527 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1528 if (!err) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1529 assert(PyLong_Check(value));
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1530 /* Whether or not it is less than or equal to
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1531 zero is determined by the sign of ob_size
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1532 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1533 if (_PyLong_Sign(value) < 0)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1534 result = PY_SSIZE_T_MIN;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1535 else
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1536 result = PY_SSIZE_T_MAX;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1537 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1538 else {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1539 /* Otherwise replace the error with caller's error object. */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1540 PyErr_Format(err,
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1541 "cannot fit '%.200s' into an index-sized integer",
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1542 item->ob_type->tp_name);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1543 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1544
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1545 finish:
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1546 Py_DECREF(value);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1547 return result;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1548 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1549
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1550
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1551 PyObject *
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1552 _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1553 {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1554 const char *type_name;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1555 static PyObject *int_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1556 if (int_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1557 int_name = PyString_InternFromString("__int__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1558 if (int_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1559 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1560 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1561
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1562 if (integral && (!PyInt_Check(integral) &&
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1563 !PyLong_Check(integral))) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1564 /* Don't go through tp_as_number->nb_int to avoid
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1565 hitting the classic class fallback to __trunc__. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1566 PyObject *int_func = PyObject_GetAttr(integral, int_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1567 if (int_func == NULL) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1568 PyErr_Clear(); /* Raise a different error. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1569 goto non_integral_error;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1570 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1571 Py_DECREF(integral);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1572 integral = PyEval_CallObject(int_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1573 Py_DECREF(int_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1574 if (integral && (!PyInt_Check(integral) &&
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1575 !PyLong_Check(integral))) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1576 goto non_integral_error;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1577 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1578 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1579 return integral;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1580
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1581 non_integral_error:
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1582 if (PyInstance_Check(integral)) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1583 type_name = PyString_AS_STRING(((PyInstanceObject *)integral)
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1584 ->in_class->cl_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1585 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1586 else {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1587 type_name = integral->ob_type->tp_name;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1588 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1589 PyErr_Format(PyExc_TypeError, error_format, type_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1590 Py_DECREF(integral);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1591 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1592 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1593
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1594
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1595 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1596 PyNumber_Int(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1597 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1598 PyNumberMethods *m;
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1599 static PyObject *trunc_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1600 PyObject *trunc_func;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1601 const char *buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1602 Py_ssize_t buffer_len;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1603
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1604 if (trunc_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1605 trunc_name = PyString_InternFromString("__trunc__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1606 if (trunc_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1607 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1608 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1609
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1610 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1611 return null_error();
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1612 if (PyInt_CheckExact(o)) {
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1613 Py_INCREF(o);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1614 return o;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1615 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1616 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1617 if (m && m->nb_int) { /* This should include subclasses of int */
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1618 /* Classic classes always take this branch. */
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1619 PyObject *res = m->nb_int(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1620 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1621 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1622 "__int__ returned non-int (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1623 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1624 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1625 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1626 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1627 return res;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1628 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1629 if (PyInt_Check(o)) { /* A int subclass without nb_int */
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1630 PyIntObject *io = (PyIntObject*)o;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1631 return PyInt_FromLong(io->ob_ival);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1632 }
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1633 trunc_func = PyObject_GetAttr(o, trunc_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1634 if (trunc_func) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1635 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1636 Py_DECREF(trunc_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1637 /* __trunc__ is specified to return an Integral type, but
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1638 int() needs to return an int. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1639 return _PyNumber_ConvertIntegralToInt(
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1640 truncated,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1641 "__trunc__ returned non-Integral (type %.200s)");
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1642 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1643 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1644
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1645 if (PyString_Check(o))
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1646 return int_from_string(PyString_AS_STRING(o),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1647 PyString_GET_SIZE(o));
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1648 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1649 if (PyUnicode_Check(o))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1650 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1651 PyUnicode_GET_SIZE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1652 10);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1653 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1654 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1655 return int_from_string((char*)buffer, buffer_len);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1656
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1657 return type_error("int() argument must be a string or a "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1658 "number, not '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1659 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1660
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1661 /* Add a check for embedded NULL-bytes in the argument. */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1662 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1663 long_from_string(const char *s, Py_ssize_t len)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1664 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1665 char *end;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1666 PyObject *x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1667
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1668 x = PyLong_FromString((char*)s, &end, 10);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1669 if (x == NULL)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1670 return NULL;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1671 if (end != s + len) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1672 PyErr_SetString(PyExc_ValueError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1673 "null byte in argument for long()");
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1674 Py_DECREF(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1675 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1676 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1677 return x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1678 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1679
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1680 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1681 PyNumber_Long(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1682 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1683 PyNumberMethods *m;
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1684 static PyObject *trunc_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1685 PyObject *trunc_func;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1686 const char *buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1687 Py_ssize_t buffer_len;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1688
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1689 if (trunc_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1690 trunc_name = PyString_InternFromString("__trunc__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1691 if (trunc_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1692 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1693 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1694
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1695 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1696 return null_error();
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1697 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1698 if (m && m->nb_long) { /* This should include subclasses of long */
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1699 /* Classic classes always take this branch. */
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1700 PyObject *res = m->nb_long(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1701 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1702 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1703 "__long__ returned non-long (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1704 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1705 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1706 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1707 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1708 return res;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1709 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1710 if (PyLong_Check(o)) /* A long subclass without nb_long */
22108
354cde7f90a1 Revert the last odd change to PyNumber_Long: the problem it was trying
Tim Peters <tim.peters@gmail.com>
parents: 22106
diff changeset
1711 return _PyLong_Copy((PyLongObject *)o);
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1712 trunc_func = PyObject_GetAttr(o, trunc_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1713 if (trunc_func) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1714 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1715 PyObject *int_instance;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1716 Py_DECREF(trunc_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1717 /* __trunc__ is specified to return an Integral type,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1718 but long() needs to return a long. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1719 int_instance = _PyNumber_ConvertIntegralToInt(
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1720 truncated,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1721 "__trunc__ returned non-Integral (type %.200s)");
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1722 if (int_instance && PyInt_Check(int_instance)) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1723 /* Make sure that long() returns a long instance. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1724 long value = PyInt_AS_LONG(int_instance);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1725 Py_DECREF(int_instance);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1726 return PyLong_FromLong(value);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1727 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1728 return int_instance;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1729 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1730 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1731
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1732 if (PyString_Check(o))
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1733 /* need to do extra error checking that PyLong_FromString()
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1734 * doesn't do. In particular long('9.5') must raise an
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1735 * exception, not truncate the float.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1736 */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1737 return long_from_string(PyString_AS_STRING(o),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1738 PyString_GET_SIZE(o));
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1739 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1740 if (PyUnicode_Check(o))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1741 /* The above check is done in PyLong_FromUnicode(). */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1742 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1743 PyUnicode_GET_SIZE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1744 10);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1745 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1746 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1747 return long_from_string(buffer, buffer_len);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1748
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1749 return type_error("long() argument must be a string or a "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1750 "number, not '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1751 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1752
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1753 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1754 PyNumber_Float(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1755 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1756 PyNumberMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1757
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1758 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1759 return null_error();
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1760 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1761 if (m && m->nb_float) { /* This should include subclasses of float */
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1762 PyObject *res = m->nb_float(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1763 if (res && !PyFloat_Check(res)) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1764 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1765 "__float__ returned non-float (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1766 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1767 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1768 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1769 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1770 return res;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1771 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1772 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
19839
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1773 PyFloatObject *po = (PyFloatObject *)o;
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1774 return PyFloat_FromDouble(po->ob_fval);
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1775 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1776 return PyFloat_FromString(o, NULL);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1777 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1778
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1779 PyObject *
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1780 PyNumber_ToBase(PyObject *n, int base)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1781 {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1782 PyObject *res = NULL;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1783 PyObject *index = PyNumber_Index(n);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1784
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1785 if (!index)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1786 return NULL;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1787 if (PyLong_Check(index))
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1788 res = _PyLong_Format(index, base, 0, 1);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1789 else if (PyInt_Check(index))
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1790 res = _PyInt_Format((PyIntObject*)index, base, 1);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1791 else
45359
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1792 /* It should not be possible to get here, as
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1793 PyNumber_Index already has a check for the same
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1794 condition */
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1795 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1796 "int or long");
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1797 Py_DECREF(index);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1798 return res;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1799 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1800
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1801
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1802 /* Operations on sequences */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1803
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1804 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1805 PySequence_Check(PyObject *s)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1806 {
33521
55eca7cd3531 SF bug #1030557: PyMapping_Check crashes when argument is NULL
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
1807 if (s && PyInstance_Check(s))
31870
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
1808 return PyObject_HasAttrString(s, "__getitem__");
40764
c994c245d9ab Silence compiler warning
Raymond Hettinger <python@rcn.com>
parents: 40762
diff changeset
1809 if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
40762
ffaaae02c662 Bug #1575169: operator.isSequenceType() now returns False for subclasses of dict.
Raymond Hettinger <python@rcn.com>
parents: 39995
diff changeset
1810 return 0;
19804
9031aab27eae PySequence_Check(), PyMapping_Check(): only return true if the
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
1811 return s != NULL && s->ob_type->tp_as_sequence &&
9031aab27eae PySequence_Check(), PyMapping_Check(): only return true if the
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
1812 s->ob_type->tp_as_sequence->sq_item != NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1813 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1814
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1815 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
1816 PySequence_Size(PyObject *s)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1817 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1818 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1819
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1820 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1821 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1822 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1823 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1824
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1825 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1826 if (m && m->sq_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1827 return m->sq_length(s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1828
39394
f5a7f4dd93e0 Remove "non-mapping" and "non-sequence" from TypeErrors raised by
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
1829 type_error("object of type '%.200s' has no len()", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1830 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1831 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1832
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1833 #undef PySequence_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1834 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1835 PySequence_Length(PyObject *s)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1836 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1837 return PySequence_Size(s);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1838 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1839 #define PySequence_Length PySequence_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1840
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1841 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1842 PySequence_Concat(PyObject *s, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1843 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1844 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1845
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1846 if (s == NULL || o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1847 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1848
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1849 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1850 if (m && m->sq_concat)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1851 return m->sq_concat(s, o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1852
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1853 /* Instances of user classes defining an __add__() method only
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1854 have an nb_add slot, not an sq_concat slot. So we fall back
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1855 to nb_add if both arguments appear to be sequences. */
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1856 if (PySequence_Check(s) && PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1857 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1858 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1859 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1860 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1861 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1862 return type_error("'%.200s' object can't be concatenated", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1863 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1864
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1865 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1866 PySequence_Repeat(PyObject *o, Py_ssize_t count)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1867 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1868 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1869
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1870 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1871 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1872
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1873 m = o->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1874 if (m && m->sq_repeat)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1875 return m->sq_repeat(o, count);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1876
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1877 /* Instances of user classes defining a __mul__() method only
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1878 have an nb_multiply slot, not an sq_repeat slot. so we fall back
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1879 to nb_multiply if o appears to be a sequence. */
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1880 if (PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1881 PyObject *n, *result;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1882 n = PyInt_FromSsize_t(count);
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1883 if (n == NULL)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1884 return NULL;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1885 result = binary_op1(o, n, NB_SLOT(nb_multiply));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1886 Py_DECREF(n);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1887 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1888 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1889 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1890 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1891 return type_error("'%.200s' object can't be repeated", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1892 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1893
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1894 PyObject *
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1895 PySequence_InPlaceConcat(PyObject *s, PyObject *o)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1896 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1897 PySequenceMethods *m;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1898
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1899 if (s == NULL || o == NULL)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1900 return null_error();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1901
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1902 m = s->ob_type->tp_as_sequence;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1903 if (m && HASINPLACE(s) && m->sq_inplace_concat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1904 return m->sq_inplace_concat(s, o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1905 if (m && m->sq_concat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1906 return m->sq_concat(s, o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1907
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1908 if (PySequence_Check(s) && PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1909 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1910 NB_SLOT(nb_add));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1911 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1912 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1913 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1914 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1915 return type_error("'%.200s' object can't be concatenated", s);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1916 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1917
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1918 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1919 PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1920 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1921 PySequenceMethods *m;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1922
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1923 if (o == NULL)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1924 return null_error();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1925
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1926 m = o->ob_type->tp_as_sequence;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1927 if (m && HASINPLACE(o) && m->sq_inplace_repeat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1928 return m->sq_inplace_repeat(o, count);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1929 if (m && m->sq_repeat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1930 return m->sq_repeat(o, count);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1931
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1932 if (PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1933 PyObject *n, *result;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1934 n = PyInt_FromSsize_t(count);
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1935 if (n == NULL)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1936 return NULL;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1937 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1938 NB_SLOT(nb_multiply));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1939 Py_DECREF(n);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1940 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1941 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1942 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1943 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1944 return type_error("'%.200s' object can't be repeated", o);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1945 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1946
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1947 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1948 PySequence_GetItem(PyObject *s, Py_ssize_t i)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1949 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1950 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1951
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1952 if (s == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1953 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1954
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1955 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1956 if (m && m->sq_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1957 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1958 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1959 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1960 if (l < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1961 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1962 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1963 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1964 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1965 return m->sq_item(s, i);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1966 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1967
47910
26799861d318 Correct grammar.
Skip Montanaro <skip@pobox.com>
parents: 47527
diff changeset
1968 return type_error("'%.200s' object does not support indexing", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1969 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1970
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1971 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1972 PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1973 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1974 PySequenceMethods *m;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1975 PyMappingMethods *mp;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1976
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1977 if (!s) return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1978
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1979 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1980 if (m && m->sq_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1981 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1982 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1983 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1984 if (l < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1985 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1986 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1987 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1988 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1989 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1990 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1991 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1992 return m->sq_slice(s, i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1993 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1994 PyObject *res;
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 37087
diff changeset
1995 PyObject *slice = _PySlice_FromIndices(i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1996 if (!slice)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1997 return NULL;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1998 res = mp->mp_subscript(s, slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1999 Py_DECREF(slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2000 return res;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2001 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2002
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2003 return type_error("'%.200s' object is unsliceable", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2004 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2005
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2006 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2007 PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2008 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2009 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2010
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2011 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2012 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2013 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2014 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2015
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2016 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2017 if (m && m->sq_ass_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2018 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2019 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2020 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2021 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2022 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2023 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2024 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2025 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2026 return m->sq_ass_item(s, i, o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2027 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2028
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2029 type_error("'%.200s' object does not support item assignment", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2030 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2031 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2032
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2033 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2034 PySequence_DelItem(PyObject *s, Py_ssize_t i)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2035 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2036 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2037
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2038 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2039 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2040 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2041 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2042
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2043 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2044 if (m && m->sq_ass_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2045 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2046 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2047 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2048 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2049 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2050 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2051 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2052 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2053 return m->sq_ass_item(s, i, (PyObject *)NULL);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2054 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2055
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2056 type_error("'%.200s' object doesn't support item deletion", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2057 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2058 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2059
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2060 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2061 PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2062 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2063 PySequenceMethods *m;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2064 PyMappingMethods *mp;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2065
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2066 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2067 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2068 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2069 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2070
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2071 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2072 if (m && m->sq_ass_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2073 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2074 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2075 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2076 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2077 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2078 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2079 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2080 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2081 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2082 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2083 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2084 return m->sq_ass_slice(s, i1, i2, o);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2085 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2086 int res;
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 37087
diff changeset
2087 PyObject *slice = _PySlice_FromIndices(i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2088 if (!slice)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2089 return -1;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2090 res = mp->mp_ass_subscript(s, slice, o);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2091 Py_DECREF(slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2092 return res;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2093 }
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2094
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2095 type_error("'%.200s' object doesn't support slice assignment", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2096 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2097 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2098
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2099 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2100 PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2101 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2102 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2103
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2104 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2105 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2106 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2107 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2108
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2109 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2110 if (m && m->sq_ass_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2111 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2112 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2113 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2114 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2115 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2116 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2117 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2118 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2119 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2120 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2121 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2122 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2123 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2124 type_error("'%.200s' object doesn't support slice deletion", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2125 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2126 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2127
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2128 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2129 PySequence_Tuple(PyObject *v)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2130 {
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2131 PyObject *it; /* iter(v) */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2132 Py_ssize_t n; /* guess for result tuple size */
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
2133 PyObject *result = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2134 Py_ssize_t j;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2135
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2136 if (v == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2137 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2138
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2139 /* Special-case the common tuple and list cases, for efficiency. */
19844
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2140 if (PyTuple_CheckExact(v)) {
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2141 /* Note that we can't know whether it's safe to return
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2142 a tuple *subclass* instance as-is, hence the restriction
19845
7a980c98c8ae Fix tortured comment -- I must be on drugs today.
Tim Peters <tim.peters@gmail.com>
parents: 19844
diff changeset
2143 to exact tuples here. In contrast, lists always make
7a980c98c8ae Fix tortured comment -- I must be on drugs today.
Tim Peters <tim.peters@gmail.com>
parents: 19844
diff changeset
2144 a copy, so there's no need for exactness below. */
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2145 Py_INCREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2146 return v;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2147 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2148 if (PyList_Check(v))
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2149 return PyList_AsTuple(v);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2150
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2151 /* Get iterator. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2152 it = PyObject_GetIter(v);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2153 if (it == NULL)
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20604
diff changeset
2154 return NULL;
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2155
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2156 /* Guess result size and allocate space. */
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
2157 n = _PyObject_LengthHint(v, 10);
51281
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
2158 if (n == -1)
6c8eddf24035 Issue 1242657: list(obj) can swallow KeyboardInterrupt.
Raymond Hettinger <python@rcn.com>
parents: 51034
diff changeset
2159 goto Fail;
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2160 result = PyTuple_New(n);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2161 if (result == NULL)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2162 goto Fail;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2163
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2164 /* Fill the tuple. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2165 for (j = 0; ; ++j) {
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2166 PyObject *item = PyIter_Next(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2167 if (item == NULL) {
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2168 if (PyErr_Occurred())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2169 goto Fail;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2170 break;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2171 }
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2172 if (j >= n) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2173 Py_ssize_t oldn = n;
34172
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2174 /* The over-allocation strategy can grow a bit faster
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2175 than for lists because unlike lists the
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2176 over-allocation isn't permanent -- we reclaim
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2177 the excess before the end of this routine.
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2178 So, grow by ten and then add 25%.
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2179 */
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2180 n += 10;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2181 n += n >> 2;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2182 if (n < oldn) {
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2183 /* Check for overflow */
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2184 PyErr_NoMemory();
34173
2144a352aa37 Add missing decref.
Raymond Hettinger <python@rcn.com>
parents: 34172
diff changeset
2185 Py_DECREF(item);
34172
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2186 goto Fail;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2187 }
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17692
diff changeset
2188 if (_PyTuple_Resize(&result, n) != 0) {
17685
0c9877120f38 Fix a tiny and unlikely memory leak. Was there before too, and actually
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
2189 Py_DECREF(item);
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2190 goto Fail;
17685
0c9877120f38 Fix a tiny and unlikely memory leak. Was there before too, and actually
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
2191 }
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2192 }
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2193 PyTuple_SET_ITEM(result, j, item);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2194 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2195
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2196 /* Cut tuple back if guess was too large. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2197 if (j < n &&
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17692
diff changeset
2198 _PyTuple_Resize(&result, j) != 0)
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2199 goto Fail;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2200
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2201 Py_DECREF(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2202 return result;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2203
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2204 Fail:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2205 Py_XDECREF(result);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2206 Py_DECREF(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2207 return NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2208 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2209
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2210 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2211 PySequence_List(PyObject *v)
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2212 {
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2213 PyObject *result; /* result list */
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2214 PyObject *rv; /* return value from PyList_Extend */
5235
4f1fa7b69ba7 Several fixes reported by jim F.
Guido van Rossum <guido@python.org>
parents: 4771
diff changeset
2215
8662
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2216 if (v == NULL)
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2217 return null_error();
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2218
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2219 result = PyList_New(0);
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2220 if (result == NULL)
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2221 return NULL;
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2222
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2223 rv = _PyList_Extend((PyListObject *)result, v);
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2224 if (rv == NULL) {
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2225 Py_DECREF(result);
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2226 return NULL;
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2227 }
31676
2bfc8866ea99 Add missing decref
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
2228 Py_DECREF(rv);
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2229 return result;
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2230 }
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2231
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2232 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2233 PySequence_Fast(PyObject *v, const char *m)
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2234 {
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2235 PyObject *it;
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2236
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2237 if (v == NULL)
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2238 return null_error();
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2239
26042
4b8044d1f1ac Use PyList_CheckExact and PyTuple_CheckExact for checking whether
Michael W. Hudson <mwh@python.net>
parents: 24867
diff changeset
2240 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2241 Py_INCREF(v);
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2242 return v;
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2243 }
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2244
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2245 it = PyObject_GetIter(v);
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2246 if (it == NULL) {
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2247 if (PyErr_ExceptionMatches(PyExc_TypeError))
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2248 PyErr_SetString(PyExc_TypeError, m);
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2249 return NULL;
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2250 }
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2251
34182
4714aff4bf75 Small boost to PySequence_Fast(). Lists build faster than tuples for
Raymond Hettinger <python@rcn.com>
parents: 34173
diff changeset
2252 v = PySequence_List(it);
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2253 Py_DECREF(it);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2254
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2255 return v;
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2256 }
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2257
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2258 /* Iterate over seq. Result depends on the operation:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2259 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
51775
af9b6c43804e Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents: 51462
diff changeset
2260 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2261 set ValueError and return -1 if none found; also return -1 on error.
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2262 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2263 */
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2264 Py_ssize_t
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2265 _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2266 {
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2267 Py_ssize_t n;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2268 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2269 PyObject *it; /* iter(seq) */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2270
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2271 if (seq == NULL || obj == NULL) {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2272 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2273 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2274 }
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2275
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2276 it = PyObject_GetIter(seq);
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2277 if (it == NULL) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2278 type_error("argument of type '%.200s' is not iterable", seq);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2279 return -1;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2280 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2281
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2282 n = wrapped = 0;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2283 for (;;) {
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2284 int cmp;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2285 PyObject *item = PyIter_Next(it);
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2286 if (item == NULL) {
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2287 if (PyErr_Occurred())
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2288 goto Fail;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2289 break;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2290 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2291
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2292 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2293 Py_DECREF(item);
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2294 if (cmp < 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2295 goto Fail;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2296 if (cmp > 0) {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2297 switch (operation) {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2298 case PY_ITERSEARCH_COUNT:
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2299 if (n == PY_SSIZE_T_MAX) {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2300 PyErr_SetString(PyExc_OverflowError,
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2301 "count exceeds C integer size");
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2302 goto Fail;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2303 }
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2304 ++n;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2305 break;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2306
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2307 case PY_ITERSEARCH_INDEX:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2308 if (wrapped) {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2309 PyErr_SetString(PyExc_OverflowError,
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2310 "index exceeds C integer size");
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2311 goto Fail;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2312 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2313 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2314
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2315 case PY_ITERSEARCH_CONTAINS:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2316 n = 1;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2317 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2318
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2319 default:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2320 assert(!"unknown operation");
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2321 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2322 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2323
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2324 if (operation == PY_ITERSEARCH_INDEX) {
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2325 if (n == PY_SSIZE_T_MAX)
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2326 wrapped = 1;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2327 ++n;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2328 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2329 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2330
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2331 if (operation != PY_ITERSEARCH_INDEX)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2332 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2333
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2334 PyErr_SetString(PyExc_ValueError,
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2335 "sequence.index(x): x not in sequence");
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2336 /* fall into failure code */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2337 Fail:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2338 n = -1;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2339 /* fall through */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2340 Done:
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2341 Py_DECREF(it);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2342 return n;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2343
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2344 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2345
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2346 /* Return # of times o appears in s. */
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2347 Py_ssize_t
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2348 PySequence_Count(PyObject *s, PyObject *o)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2349 {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2350 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2351 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2352
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2353 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2354 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2355 */
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2356 int
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2357 PySequence_Contains(PyObject *seq, PyObject *ob)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2358 {
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2359 Py_ssize_t result;
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2360 if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2361 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2362 if (sqm != NULL && sqm->sq_contains != NULL)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2363 return (*sqm->sq_contains)(seq, ob);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2364 }
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2365 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2366 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2367 }
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2368
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2369 /* Backwards compatibility */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2370 #undef PySequence_In
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2371 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2372 PySequence_In(PyObject *w, PyObject *v)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2373 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2374 return PySequence_Contains(w, v);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2375 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2376
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2377 Py_ssize_t
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2378 PySequence_Index(PyObject *s, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2379 {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2380 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2381 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2382
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2383 /* Operations on mappings */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2384
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2385 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2386 PyMapping_Check(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2387 {
33521
55eca7cd3531 SF bug #1030557: PyMapping_Check crashes when argument is NULL
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
2388 if (o && PyInstance_Check(o))
31870
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2389 return PyObject_HasAttrString(o, "__getitem__");
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2390
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2391 return o && o->ob_type->tp_as_mapping &&
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2392 o->ob_type->tp_as_mapping->mp_subscript &&
31871
020dc5dfd5c5 Improve previous checkin to use a slot check instead of equivalent
Raymond Hettinger <python@rcn.com>
parents: 31870
diff changeset
2393 !(o->ob_type->tp_as_sequence &&
020dc5dfd5c5 Improve previous checkin to use a slot check instead of equivalent
Raymond Hettinger <python@rcn.com>
parents: 31870
diff changeset
2394 o->ob_type->tp_as_sequence->sq_slice);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2395 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2396
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2397 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
2398 PyMapping_Size(PyObject *o)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2399 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2400 PyMappingMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2401
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2402 if (o == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2403 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2404 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2405 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2406
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2407 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2408 if (m && m->mp_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2409 return m->mp_length(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2410
39394
f5a7f4dd93e0 Remove "non-mapping" and "non-sequence" from TypeErrors raised by
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
2411 type_error("object of type '%.200s' has no len()", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2412 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2413 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2414
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2415 #undef PyMapping_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2416 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2417 PyMapping_Length(PyObject *o)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2418 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2419 return PyMapping_Size(o);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2420 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2421 #define PyMapping_Length PyMapping_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2422
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2423 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2424 PyMapping_GetItemString(PyObject *o, char *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2425 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2426 PyObject *okey, *r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2427
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2428 if (key == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2429 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2430
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2431 okey = PyString_FromString(key);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2432 if (okey == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2433 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2434 r = PyObject_GetItem(o, okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2435 Py_DECREF(okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2436 return r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2437 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2438
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2439 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2440 PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2441 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2442 PyObject *okey;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2443 int r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2444
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2445 if (key == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2446 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2447 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2448 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2449
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2450 okey = PyString_FromString(key);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2451 if (okey == NULL)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2452 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2453 r = PyObject_SetItem(o, okey, value);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2454 Py_DECREF(okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2455 return r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2456 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2457
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2458 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2459 PyMapping_HasKeyString(PyObject *o, char *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2460 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2461 PyObject *v;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2462
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2463 v = PyMapping_GetItemString(o, key);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2464 if (v) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2465 Py_DECREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2466 return 1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2467 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2468 PyErr_Clear();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2469 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2470 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2471
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2472 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2473 PyMapping_HasKey(PyObject *o, PyObject *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2474 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2475 PyObject *v;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2476
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2477 v = PyObject_GetItem(o, key);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2478 if (v) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2479 Py_DECREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2480 return 1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2481 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2482 PyErr_Clear();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2483 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2484 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2485
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2486 /* Operations on callable objects */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2487
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2488 /* XXX PyCallable_Check() is in object.c */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2489
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2490 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2491 PyObject_CallObject(PyObject *o, PyObject *a)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2492 {
19910
49b7e3980853 PyObject_CallObject(): this may as well call PyEval_CallObject()
Guido van Rossum <guido@python.org>
parents: 19845
diff changeset
2493 return PyEval_CallObjectWithKeywords(o, a, NULL);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2494 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2495
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2496 PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2497 PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2498 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2499 ternaryfunc call;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2500
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2501 if ((call = func->ob_type->tp_call) != NULL) {
43276
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2502 PyObject *result;
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2503 if (Py_EnterRecursiveCall(" while calling a Python object"))
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2504 return NULL;
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2505 result = (*call)(func, arg, kw);
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2506 Py_LeaveRecursiveCall();
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2507 if (result == NULL && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2508 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2509 PyExc_SystemError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2510 "NULL result without error in PyObject_Call");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2511 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2512 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2513 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
20824
b3d8d2615b23 PyFunction_Call() did not check the result of PyObject_Repr() for NULL, and
Fred Drake <fdrake@acm.org>
parents: 20729
diff changeset
2514 func->ob_type->tp_name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2515 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2516 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2517
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2518 static PyObject*
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2519 call_function_tail(PyObject *callable, PyObject *args)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2520 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2521 PyObject *retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2522
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2523 if (args == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2524 return NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2525
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2526 if (!PyTuple_Check(args)) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2527 PyObject *a;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2528
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2529 a = PyTuple_New(1);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2530 if (a == NULL) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2531 Py_DECREF(args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2532 return NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2533 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2534 PyTuple_SET_ITEM(a, 0, args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2535 args = a;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2536 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2537 retval = PyObject_Call(callable, args, NULL);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2538
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2539 Py_DECREF(args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2540
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2541 return retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2542 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2543
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2544 PyObject *
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2545 PyObject_CallFunction(PyObject *callable, char *format, ...)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2546 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2547 va_list va;
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2548 PyObject *args;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2549
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2550 if (callable == NULL)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2551 return null_error();
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2552
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2553 if (format && *format) {
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2554 va_start(va, format);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2555 args = Py_VaBuildValue(format, va);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2556 va_end(va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2557 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2558 else
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2559 args = PyTuple_New(0);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2560
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2561 return call_function_tail(callable, args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2562 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2563
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2564 PyObject *
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2565 _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2566 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2567 va_list va;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2568 PyObject *args;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2569
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2570 if (callable == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2571 return null_error();
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2572
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2573 if (format && *format) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2574 va_start(va, format);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2575 args = _Py_VaBuildValue_SizeT(format, va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2576 va_end(va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2577 }
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2578 else
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2579 args = PyTuple_New(0);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2580
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2581 return call_function_tail(callable, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2582 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2583
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2584 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2585 PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2586 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2587 va_list va;
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2588 PyObject *args;
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2589 PyObject *func = NULL;
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2590 PyObject *retval = NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2591
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2592 if (o == NULL || name == NULL)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2593 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2594
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2595 func = PyObject_GetAttrString(o, name);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2596 if (func == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2597 PyErr_SetString(PyExc_AttributeError, name);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2598 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2599 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2600
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2601 if (!PyCallable_Check(func)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2602 type_error("attribute of type '%.200s' is not callable", func);
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2603 goto exit;
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2604 }
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2605
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2606 if (format && *format) {
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2607 va_start(va, format);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2608 args = Py_VaBuildValue(format, va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2609 va_end(va);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2610 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2611 else
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2612 args = PyTuple_New(0);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2613
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2614 retval = call_function_tail(func, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2615
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2616 exit:
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2617 /* args gets consumed in call_function_tail */
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2618 Py_XDECREF(func);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2619
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2620 return retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2621 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2622
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2623 PyObject *
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2624 _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2625 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2626 va_list va;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2627 PyObject *args;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2628 PyObject *func = NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2629 PyObject *retval = NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2630
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2631 if (o == NULL || name == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2632 return null_error();
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2633
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2634 func = PyObject_GetAttrString(o, name);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2635 if (func == NULL) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2636 PyErr_SetString(PyExc_AttributeError, name);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2637 return 0;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2638 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2639
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2640 if (!PyCallable_Check(func)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2641 type_error("attribute of type '%.200s' is not callable", func);
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2642 goto exit;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2643 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2644
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2645 if (format && *format) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2646 va_start(va, format);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2647 args = _Py_VaBuildValue_SizeT(format, va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2648 va_end(va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2649 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2650 else
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2651 args = PyTuple_New(0);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2652
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2653 retval = call_function_tail(func, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2654
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2655 exit:
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2656 /* args gets consumed in call_function_tail */
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2657 Py_XDECREF(func);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2658
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2659 return retval;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2660 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2661
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2662
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2663 static PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2664 objargs_mktuple(va_list va)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2665 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2666 int i, n = 0;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2667 va_list countva;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2668 PyObject *result, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2669
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2670 #ifdef VA_LIST_IS_ARRAY
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2671 memcpy(countva, va, sizeof(va_list));
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2672 #else
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2673 #ifdef __va_copy
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2674 __va_copy(countva, va);
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2675 #else
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2676 countva = va;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2677 #endif
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2678 #endif
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2679
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2680 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2681 ++n;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2682 result = PyTuple_New(n);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2683 if (result != NULL && n > 0) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2684 for (i = 0; i < n; ++i) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2685 tmp = (PyObject *)va_arg(va, PyObject *);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2686 PyTuple_SET_ITEM(result, i, tmp);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2687 Py_INCREF(tmp);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2688 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2689 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2690 return result;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2691 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2692
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2693 PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2694 PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2695 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2696 PyObject *args, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2697 va_list vargs;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2698
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2699 if (callable == NULL || name == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2700 return null_error();
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2701
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2702 callable = PyObject_GetAttr(callable, name);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2703 if (callable == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2704 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2705
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2706 /* count the args */
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2707 va_start(vargs, name);
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2708 args = objargs_mktuple(vargs);
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2709 va_end(vargs);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2710 if (args == NULL) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2711 Py_DECREF(callable);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2712 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2713 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2714 tmp = PyObject_Call(callable, args, NULL);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2715 Py_DECREF(args);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2716 Py_DECREF(callable);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2717
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2718 return tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2719 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2720
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2721 PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2722 PyObject_CallFunctionObjArgs(PyObject *callable, ...)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2723 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2724 PyObject *args, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2725 va_list vargs;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2726
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2727 if (callable == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2728 return null_error();
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2729
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2730 /* count the args */
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2731 va_start(vargs, callable);
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2732 args = objargs_mktuple(vargs);
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2733 va_end(vargs);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2734 if (args == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2735 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2736 tmp = PyObject_Call(callable, args, NULL);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2737 Py_DECREF(args);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2738
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2739 return tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2740 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2741
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2742
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2743 /* isinstance(), issubclass() */
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2744
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2745 /* abstract_get_bases() has logically 4 return states, with a sort of 0th
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2746 * state that will almost never happen.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2747 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2748 * 0. creating the __bases__ static string could get a MemoryError
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2749 * 1. getattr(cls, '__bases__') could raise an AttributeError
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2750 * 2. getattr(cls, '__bases__') could raise some other exception
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2751 * 3. getattr(cls, '__bases__') could return a tuple
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2752 * 4. getattr(cls, '__bases__') could return something other than a tuple
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2753 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2754 * Only state #3 is a non-error state and only it returns a non-NULL object
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2755 * (it returns the retrieved tuple).
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2756 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2757 * Any raised AttributeErrors are masked by clearing the exception and
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2758 * returning NULL. If an object other than a tuple comes out of __bases__,
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2759 * then again, the return value is NULL. So yes, these two situations
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2760 * produce exactly the same results: NULL is returned and no error is set.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2761 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2762 * If some exception other than AttributeError is raised, then NULL is also
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2763 * returned, but the exception is not cleared. That's because we want the
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2764 * exception to be propagated along.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2765 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2766 * Callers are expected to test for PyErr_Occurred() when the return value
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2767 * is NULL to decide whether a valid exception should be propagated or not.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2768 * When there's no exception to propagate, it's customary for the caller to
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2769 * set a TypeError.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2770 */
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2771 static PyObject *
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2772 abstract_get_bases(PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2773 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2774 static PyObject *__bases__ = NULL;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2775 PyObject *bases;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2776
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2777 if (__bases__ == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2778 __bases__ = PyString_InternFromString("__bases__");
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2779 if (__bases__ == NULL)
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2780 return NULL;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2781 }
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2782 bases = PyObject_GetAttr(cls, __bases__);
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2783 if (bases == NULL) {
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2784 if (PyErr_ExceptionMatches(PyExc_AttributeError))
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2785 PyErr_Clear();
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2786 return NULL;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2787 }
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2788 if (!PyTuple_Check(bases)) {
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2789 Py_DECREF(bases);
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2790 return NULL;
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2791 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2792 return bases;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2793 }
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2794
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2795
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2796 static int
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2797 abstract_issubclass(PyObject *derived, PyObject *cls)
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2798 {
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2799 PyObject *bases = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2800 Py_ssize_t i, n;
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2801 int r = 0;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2802
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2803 while (1) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2804 if (derived == cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2805 return 1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2806 bases = abstract_get_bases(derived);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2807 if (bases == NULL) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2808 if (PyErr_Occurred())
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2809 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2810 return 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2811 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2812 n = PyTuple_GET_SIZE(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2813 if (n == 0) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2814 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2815 return 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2816 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2817 /* Avoid recursivity in the single inheritance case */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2818 if (n == 1) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2819 derived = PyTuple_GET_ITEM(bases, 0);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2820 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2821 continue;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2822 }
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2823 for (i = 0; i < n; i++) {
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2824 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2825 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2826 break;
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2827 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2828 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2829 return r;
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2830 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2831 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2832
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2833 static int
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2834 check_class(PyObject *cls, const char *error)
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2835 {
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2836 PyObject *bases = abstract_get_bases(cls);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2837 if (bases == NULL) {
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2838 /* Do not mask errors. */
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2839 if (!PyErr_Occurred())
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2840 PyErr_SetString(PyExc_TypeError, error);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2841 return 0;
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2842 }
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2843 Py_DECREF(bases);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2844 return -1;
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2845 }
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2846
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2847 static int
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2848 recursive_isinstance(PyObject *inst, PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2849 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2850 PyObject *icls;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2851 static PyObject *__class__ = NULL;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2852 int retval = 0;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2853
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2854 if (__class__ == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2855 __class__ = PyString_InternFromString("__class__");
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2856 if (__class__ == NULL)
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2857 return -1;
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2858 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2859
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2860 if (PyClass_Check(cls) && PyInstance_Check(inst)) {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2861 PyObject *inclass =
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2862 (PyObject*)((PyInstanceObject*)inst)->in_class;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2863 retval = PyClass_IsSubclass(inclass, cls);
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2864 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2865 else if (PyType_Check(cls)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2866 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2867 if (retval == 0) {
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2868 PyObject *c = PyObject_GetAttr(inst, __class__);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2869 if (c == NULL) {
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2870 PyErr_Clear();
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2871 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2872 else {
27830
bf9ca086b8aa Add missing cast in previous fix.
Guido van Rossum <guido@python.org>
parents: 27829
diff changeset
2873 if (c != (PyObject *)(inst->ob_type) &&
bf9ca086b8aa Add missing cast in previous fix.
Guido van Rossum <guido@python.org>
parents: 27829
diff changeset
2874 PyType_Check(c))
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2875 retval = PyType_IsSubtype(
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2876 (PyTypeObject *)c,
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2877 (PyTypeObject *)cls);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2878 Py_DECREF(c);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2879 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2880 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2881 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2882 else {
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2883 if (!check_class(cls,
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2884 "isinstance() arg 2 must be a class, type,"
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2885 " or tuple of classes and types"))
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2886 return -1;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2887 icls = PyObject_GetAttr(inst, __class__);
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2888 if (icls == NULL) {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2889 PyErr_Clear();
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2890 retval = 0;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2891 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2892 else {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2893 retval = abstract_issubclass(icls, cls);
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2894 Py_DECREF(icls);
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2895 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2896 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2897
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2898 return retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2899 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2900
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2901 int
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2902 PyObject_IsInstance(PyObject *inst, PyObject *cls)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2903 {
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2904 static PyObject *name = NULL;
45889
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2905
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2906 /* Quick test for an exact match */
45904
d5cff7f9a142 Fix compiler warning.
Raymond Hettinger <python@rcn.com>
parents: 45889
diff changeset
2907 if (Py_TYPE(inst) == (PyTypeObject *)cls)
45889
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2908 return 1;
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2909
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2910 if (PyTuple_Check(cls)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2911 Py_ssize_t i;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2912 Py_ssize_t n;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2913 int r = 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2914
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2915 if (Py_EnterRecursiveCall(" in __instancecheck__"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2916 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2917 n = PyTuple_GET_SIZE(cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2918 for (i = 0; i < n; ++i) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2919 PyObject *item = PyTuple_GET_ITEM(cls, i);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2920 r = PyObject_IsInstance(inst, item);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2921 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2922 /* either found it, or got an error */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2923 break;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2924 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2925 Py_LeaveRecursiveCall();
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2926 return r;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2927 }
54118
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53957
diff changeset
2928
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2929 if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2930 PyObject *checker;
54118
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53957
diff changeset
2931 checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2932 if (checker != NULL) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2933 PyObject *res;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2934 int ok = -1;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2935 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2936 Py_DECREF(checker);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2937 return ok;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2938 }
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2939 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2940 Py_LeaveRecursiveCall();
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2941 Py_DECREF(checker);
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2942 if (res != NULL) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2943 ok = PyObject_IsTrue(res);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2944 Py_DECREF(res);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
2945 }
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2946 return ok;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2947 }
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2948 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2949 return recursive_isinstance(inst, cls);
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2950 }
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2951
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2952 static int
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2953 recursive_issubclass(PyObject *derived, PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2954 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2955 int retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2956
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2957 if (PyType_Check(cls) && PyType_Check(derived)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2958 /* Fast path (non-recursive) */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2959 return PyType_IsSubtype(
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2960 (PyTypeObject *)derived, (PyTypeObject *)cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2961 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2962 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
27793
f0409d754b93 Fold long lines.
Guido van Rossum <guido@python.org>
parents: 26987
diff changeset
2963 if (!check_class(derived,
f0409d754b93 Fold long lines.
Guido van Rossum <guido@python.org>
parents: 26987
diff changeset
2964 "issubclass() arg 1 must be a class"))
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2965 return -1;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
2966
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2967 if (!check_class(cls,
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2968 "issubclass() arg 2 must be a class"
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2969 " or tuple of classes"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2970 return -1;
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2971 retval = abstract_issubclass(derived, cls);
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2972 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2973 else {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2974 /* shortcut */
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2975 if (!(retval = (derived == cls)))
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2976 retval = PyClass_IsSubclass(derived, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2977 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2978
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2979 return retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2980 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
2981
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2982 int
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2983 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2984 {
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2985 static PyObject *name = NULL;
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2986
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2987 if (PyTuple_Check(cls)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2988 Py_ssize_t i;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2989 Py_ssize_t n;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2990 int r = 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2991
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2992 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2993 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2994 n = PyTuple_GET_SIZE(cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2995 for (i = 0; i < n; ++i) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2996 PyObject *item = PyTuple_GET_ITEM(cls, i);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2997 r = PyObject_IsSubclass(derived, item);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2998 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2999 /* either found it, or got an error */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3000 break;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3001 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3002 Py_LeaveRecursiveCall();
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3003 return r;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3004 }
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3005 if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3006 PyObject *checker;
54118
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53957
diff changeset
3007 checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3008 if (checker != NULL) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3009 PyObject *res;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3010 int ok = -1;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3011 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3012 Py_DECREF(checker);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3013 return ok;
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3014 }
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3015 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3016 Py_LeaveRecursiveCall();
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3017 Py_DECREF(checker);
54121
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3018 if (res != NULL) {
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3019 ok = PyObject_IsTrue(res);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3020 Py_DECREF(res);
3aa6a863ff06 completely ignore old-style stuff for type checking overloading
Benjamin Peterson <benjamin@python.org>
parents: 54120
diff changeset
3021 }
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3022 return ok;
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3023 }
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3024 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3025 return recursive_issubclass(derived, cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3026 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3027
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3028 int
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3029 _PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3030 {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3031 return recursive_isinstance(inst, cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3032 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3033
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3034 int
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3035 _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3036 {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3037 return recursive_issubclass(derived, cls);
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3038 }
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3039
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3040
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3041 PyObject *
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3042 PyObject_GetIter(PyObject *o)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3043 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3044 PyTypeObject *t = o->ob_type;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3045 getiterfunc f = NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3046 if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3047 f = t->tp_iter;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3048 if (f == NULL) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3049 if (PySequence_Check(o))
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3050 return PySeqIter_New(o);
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
3051 return type_error("'%.200s' object is not iterable", o);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3052 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3053 else {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3054 PyObject *res = (*f)(o);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3055 if (res != NULL && !PyIter_Check(res)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3056 PyErr_Format(PyExc_TypeError,
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3057 "iter() returned non-iterator "
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3058 "of type '%.100s'",
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3059 res->ob_type->tp_name);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3060 Py_DECREF(res);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3061 res = NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3062 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3063 return res;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3064 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3065 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3066
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3067 /* Return next item.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3068 * If an error occurs, return NULL. PyErr_Occurred() will be true.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3069 * If the iteration terminates normally, return NULL and clear the
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3070 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3071 * will be false.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3072 * Else return the next object. PyErr_Occurred() will be false.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3073 */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3074 PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3075 PyIter_Next(PyObject *iter)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3076 {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3077 PyObject *result;
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3078 result = (*iter->ob_type->tp_iternext)(iter);
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3079 if (result == NULL &&
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3080 PyErr_Occurred() &&
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3081 PyErr_ExceptionMatches(PyExc_StopIteration))
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3082 PyErr_Clear();
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3083 return result;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3084 }