blob: bb6c301074f6e07b856524235a0ec7baace9fc6d [file] [log] [blame]
Guido van Rossume15dee51995-07-18 14:12:02 +00001/* Abstract Object Interface (many thanks to Jim Fulton) */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002
Guido van Rossume15dee51995-07-18 14:12:02 +00003#include "Python.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00004#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00005#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00006#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00009
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000010/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000011
12static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000014{
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000016 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000017}
18
Guido van Rossum052b7e11996-11-11 15:08:19 +000019static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000020null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000021{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000022 if (!PyErr_Occurred())
23 PyErr_SetString(PyExc_SystemError,
24 "null argument to internal routine");
25 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000026}
27
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000028/* Operations on any object */
29
30int
Fred Drake79912472000-07-09 04:06:11 +000031PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000032{
33 int r;
34
35 if (o1 == NULL || o2 == NULL) {
36 null_error();
37 return -1;
38 }
39 r = PyObject_Compare(o1, o2);
40 if (PyErr_Occurred())
41 return -1;
42 *result = r;
Guido van Rossume15dee51995-07-18 14:12:02 +000043 return 0;
44}
Guido van Rossume15dee51995-07-18 14:12:02 +000045
46PyObject *
Fred Drake79912472000-07-09 04:06:11 +000047PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000048{
49 PyObject *v;
50
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000051 if (o == NULL)
52 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +000053 v = (PyObject *)o->ob_type;
54 Py_INCREF(v);
55 return v;
56}
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000059PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000060{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000061 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000062
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000063 if (o == NULL) {
64 null_error();
65 return -1;
66 }
Guido van Rossume15dee51995-07-18 14:12:02 +000067
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000068 m = o->ob_type->tp_as_sequence;
69 if (m && m->sq_length)
70 return m->sq_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000071
Jeremy Hylton6253f832000-07-12 12:56:19 +000072 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000073}
74
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000075#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000076Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000077PyObject_Length(PyObject *o)
78{
79 return PyObject_Size(o);
80}
81#define PyObject_Length PyObject_Size
82
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000083
Christian Heimes255f53b2007-12-08 15:33:56 +000084/* The length hint function returns a non-negative value from o.__len__()
85 or o.__length_hint__(). If those methods aren't found or return a negative
86 value, then the defaultvalue is returned. This function never fails.
87 Accordingly, it will mask exceptions raised in either method.
88*/
89
90Py_ssize_t
91_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
92{
93 static PyObject *hintstrobj = NULL;
94 PyObject *ro;
95 Py_ssize_t rv;
96
97 /* try o.__len__() */
98 rv = PyObject_Size(o);
99 if (rv >= 0)
100 return rv;
101 if (PyErr_Occurred())
102 PyErr_Clear();
103
104 /* cache a hashed version of the attribute string */
105 if (hintstrobj == NULL) {
106 hintstrobj = PyUnicode_InternFromString("__length_hint__");
107 if (hintstrobj == NULL)
108 goto defaultcase;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000109 }
Christian Heimes255f53b2007-12-08 15:33:56 +0000110
111 /* try o.__length_hint__() */
112 ro = PyObject_CallMethodObjArgs(o, hintstrobj, NULL);
113 if (ro == NULL)
114 goto defaultcase;
115 rv = PyLong_AsSsize_t(ro);
116 Py_DECREF(ro);
117 if (rv >= 0)
118 return rv;
119
120defaultcase:
121 if (PyErr_Occurred())
122 PyErr_Clear();
123 return defaultvalue;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000124}
125
Guido van Rossume15dee51995-07-18 14:12:02 +0000126PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000127PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000128{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000129 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000130
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000131 if (o == NULL || key == NULL)
132 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000133
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000134 m = o->ob_type->tp_as_mapping;
135 if (m && m->mp_subscript)
136 return m->mp_subscript(o, key);
Guido van Rossume15dee51995-07-18 14:12:02 +0000137
Guido van Rossum21308241998-08-13 16:44:44 +0000138 if (o->ob_type->tp_as_sequence) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000139 if (PyIndex_Check(key)) {
140 Py_ssize_t key_value;
141 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000142 if (key_value == -1 && PyErr_Occurred())
143 return NULL;
144 return PySequence_GetItem(o, key_value);
145 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000146 else if (o->ob_type->tp_as_sequence->sq_item)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 return type_error("sequence index must "
148 "be integer, not '%.200s'", key);
Guido van Rossum21308241998-08-13 16:44:44 +0000149 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000150
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151 return type_error("'%.200s' object is unsubscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000152}
153
154int
Fred Drake79912472000-07-09 04:06:11 +0000155PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000156{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000157 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000158
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000159 if (o == NULL || key == NULL || value == NULL) {
160 null_error();
161 return -1;
162 }
163 m = o->ob_type->tp_as_mapping;
164 if (m && m->mp_ass_subscript)
165 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000166
Guido van Rossum21308241998-08-13 16:44:44 +0000167 if (o->ob_type->tp_as_sequence) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000168 if (PyIndex_Check(key)) {
169 Py_ssize_t key_value;
170 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000171 if (key_value == -1 && PyErr_Occurred())
172 return -1;
173 return PySequence_SetItem(o, key_value, value);
174 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000175 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176 type_error("sequence index must be "
177 "integer, not '%.200s'", key);
Guido van Rossum64585f62001-11-24 18:24:47 +0000178 return -1;
179 }
Guido van Rossum21308241998-08-13 16:44:44 +0000180 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000181
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 type_error("'%.200s' object does not support item assignment", o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000183 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000184}
185
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000186int
Fred Drake79912472000-07-09 04:06:11 +0000187PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000188{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000189 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000190
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000191 if (o == NULL || key == NULL) {
192 null_error();
193 return -1;
194 }
195 m = o->ob_type->tp_as_mapping;
196 if (m && m->mp_ass_subscript)
197 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000198
Guido van Rossum21308241998-08-13 16:44:44 +0000199 if (o->ob_type->tp_as_sequence) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000200 if (PyIndex_Check(key)) {
201 Py_ssize_t key_value;
202 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000203 if (key_value == -1 && PyErr_Occurred())
204 return -1;
205 return PySequence_DelItem(o, key_value);
206 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000207 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 type_error("sequence index must be "
209 "integer, not '%.200s'", key);
Guido van Rossum64585f62001-11-24 18:24:47 +0000210 return -1;
211 }
Guido van Rossum21308241998-08-13 16:44:44 +0000212 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000213
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000214 type_error("'%.200s' object does not support item deletion", o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000215 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000216}
217
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000218int
219PyObject_DelItemString(PyObject *o, char *key)
220{
221 PyObject *okey;
222 int ret;
223
224 if (o == NULL || key == NULL) {
225 null_error();
226 return -1;
227 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000228 okey = PyUnicode_FromString(key);
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000229 if (okey == NULL)
230 return -1;
231 ret = PyObject_DelItem(o, okey);
232 Py_DECREF(okey);
233 return ret;
234}
235
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000236/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000237 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000238 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000239int
240PyObject_AsCharBuffer(PyObject *obj,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000241 const char **buffer,
242 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000243{
244 PyBufferProcs *pb;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000245 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000246
247 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
248 null_error();
249 return -1;
250 }
251 pb = obj->ob_type->tp_as_buffer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000252 if (pb == NULL || pb->bf_getbuffer == NULL) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000253 PyErr_SetString(PyExc_TypeError,
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000254 "expected an object with the buffer interface");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000255 return -1;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000256 }
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000257 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000258
259 *buffer = view.buf;
260 *buffer_len = view.len;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000261 if (pb->bf_releasebuffer != NULL)
262 (*pb->bf_releasebuffer)(obj, &view);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000263 return 0;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000264}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000265
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000266int
267PyObject_CheckReadBuffer(PyObject *obj)
268{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000269 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000270
271 if (pb == NULL ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000272 pb->bf_getbuffer == NULL)
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000273 return 0;
274 if ((*pb->bf_getbuffer)(obj, NULL, PyBUF_SIMPLE) == -1) {
275 PyErr_Clear();
276 return 0;
277 }
278 if (*pb->bf_releasebuffer != NULL)
279 (*pb->bf_releasebuffer)(obj, NULL);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000280 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000281}
282
283int PyObject_AsReadBuffer(PyObject *obj,
284 const void **buffer,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286{
287 PyBufferProcs *pb;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000288 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000289
290 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
291 null_error();
292 return -1;
293 }
294 pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000295 if (pb == NULL ||
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000296 pb->bf_getbuffer == NULL) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297 PyErr_SetString(PyExc_TypeError,
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000298 "expected an object with a buffer interface");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000299 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000300 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000301
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000302 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000303
304 *buffer = view.buf;
305 *buffer_len = view.len;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000306 if (pb->bf_releasebuffer != NULL)
307 (*pb->bf_releasebuffer)(obj, &view);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000308 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000309}
310
311int PyObject_AsWriteBuffer(PyObject *obj,
312 void **buffer,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000313 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000314{
315 PyBufferProcs *pb;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000316 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000317
318 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
319 null_error();
320 return -1;
321 }
322 pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000323 if (pb == NULL ||
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000324 pb->bf_getbuffer == NULL ||
325 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000326 PyErr_SetString(PyExc_TypeError,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000327 "expected an object with a writable buffer interface");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000328 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000329 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000330
331 *buffer = view.buf;
332 *buffer_len = view.len;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000333 if (pb->bf_releasebuffer != NULL)
334 (*pb->bf_releasebuffer)(obj, &view);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000335 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000336}
337
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000338/* Buffer C-API for Python 3.0 */
339
340int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000341PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000342{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000343 if (!PyObject_CheckBuffer(obj)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000344 PyErr_Format(PyExc_TypeError,
345 "'%100s' does not have the buffer interface",
Christian Heimes90aa7642007-12-19 02:45:37 +0000346 Py_TYPE(obj)->tp_name);
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000347 return -1;
348 }
349 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000350}
351
352void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000353PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000354{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000355 if (obj->ob_type->tp_as_buffer != NULL &&
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000356 obj->ob_type->tp_as_buffer->bf_releasebuffer != NULL) {
357 (*(obj->ob_type->tp_as_buffer->bf_releasebuffer))(obj, view);
358 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000359}
360
361
362static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000363_IsFortranContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000364{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000365 Py_ssize_t sd, dim;
366 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000367
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000368 if (view->ndim == 0) return 1;
369 if (view->strides == NULL) return (view->ndim == 1);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000370
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000371 sd = view->itemsize;
372 if (view->ndim == 1) return (view->shape[0] == 1 ||
373 sd == view->strides[0]);
374 for (i=0; i<view->ndim; i++) {
375 dim = view->shape[i];
376 if (dim == 0) return 1;
377 if (view->strides[i] != sd) return 0;
378 sd *= dim;
379 }
380 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000381}
382
383static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000384_IsCContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000385{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000386 Py_ssize_t sd, dim;
387 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000388
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000389 if (view->ndim == 0) return 1;
390 if (view->strides == NULL) return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000391
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000392 sd = view->itemsize;
393 if (view->ndim == 1) return (view->shape[0] == 1 ||
394 sd == view->strides[0]);
395 for (i=view->ndim-1; i>=0; i--) {
396 dim = view->shape[i];
397 if (dim == 0) return 1;
398 if (view->strides[i] != sd) return 0;
399 sd *= dim;
400 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000401 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000402}
403
404int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000405PyBuffer_IsContiguous(Py_buffer *view, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000406{
407
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000408 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000409
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000410 if (fort == 'C')
411 return _IsCContiguous(view);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000412 else if (fort == 'F')
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000413 return _IsFortranContiguous(view);
414 else if (fort == 'A')
415 return (_IsCContiguous(view) || _IsFortranContiguous(view));
416 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000417}
418
419
Guido van Rossum98297ee2007-11-06 21:34:58 +0000420void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000421PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000422{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000423 char* pointer;
424 int i;
425 pointer = (char *)view->buf;
426 for (i = 0; i < view->ndim; i++) {
427 pointer += view->strides[i]*indices[i];
428 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
429 pointer = *((char**)pointer) + view->suboffsets[i];
430 }
431 }
432 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000433}
434
435
Guido van Rossum98297ee2007-11-06 21:34:58 +0000436void
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000437_add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
438{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000439 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000440
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000441 for (k=0; k<nd; k++) {
442 if (index[k] < shape[k]-1) {
443 index[k]++;
444 break;
445 }
446 else {
447 index[k] = 0;
448 }
449 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000450}
451
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452void
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000453_add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
454{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000455 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000456
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000457 for (k=nd-1; k>=0; k--) {
458 if (index[k] < shape[k]-1) {
459 index[k]++;
460 break;
461 }
462 else {
463 index[k] = 0;
464 }
465 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000466}
467
468 /* view is not checked for consistency in either of these. It is
Guido van Rossum98297ee2007-11-06 21:34:58 +0000469 assumed that the size of the buffer is view->len in
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000470 view->len / view->itemsize elements.
471 */
472
Guido van Rossum98297ee2007-11-06 21:34:58 +0000473int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000474PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000475{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000476 int k;
477 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
478 Py_ssize_t *indices, elements;
479 char *dest, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000480
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000481 if (len > view->len) {
482 len = view->len;
483 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000484
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000485 if (PyBuffer_IsContiguous(view, fort)) {
486 /* simplest copy is all that is needed */
487 memcpy(buf, view->buf, len);
488 return 0;
489 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000490
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000491 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000492
Neal Norwitzfaa54a32007-08-19 04:23:20 +0000493 /* XXX(nnorwitz): need to check for overflow! */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000494 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
495 if (indices == NULL) {
496 PyErr_NoMemory();
497 return -1;
498 }
499 for (k=0; k<view->ndim;k++) {
500 indices[k] = 0;
501 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000502
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000503 if (fort == 'F') {
504 addone = _add_one_to_index_F;
505 }
506 else {
507 addone = _add_one_to_index_C;
508 }
509 dest = buf;
510 /* XXX : This is not going to be the fastest code in the world
Guido van Rossum98297ee2007-11-06 21:34:58 +0000511 several optimizations are possible.
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000512 */
513 elements = len / view->itemsize;
514 while (elements--) {
515 addone(view->ndim, indices, view->shape);
516 ptr = PyBuffer_GetPointer(view, indices);
517 memcpy(dest, ptr, view->itemsize);
518 dest += view->itemsize;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000519 }
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000520 PyMem_Free(indices);
521 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000522}
523
524int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000525PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000526{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000527 int k;
528 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
529 Py_ssize_t *indices, elements;
530 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000531
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000532 if (len > view->len) {
533 len = view->len;
534 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000535
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000536 if (PyBuffer_IsContiguous(view, fort)) {
537 /* simplest copy is all that is needed */
538 memcpy(view->buf, buf, len);
539 return 0;
540 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000541
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000542 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000543
Neal Norwitzfaa54a32007-08-19 04:23:20 +0000544 /* XXX(nnorwitz): need to check for overflow! */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000545 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
546 if (indices == NULL) {
547 PyErr_NoMemory();
548 return -1;
549 }
550 for (k=0; k<view->ndim;k++) {
551 indices[k] = 0;
552 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000553
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000554 if (fort == 'F') {
555 addone = _add_one_to_index_F;
556 }
557 else {
558 addone = _add_one_to_index_C;
559 }
560 src = buf;
561 /* XXX : This is not going to be the fastest code in the world
Guido van Rossum98297ee2007-11-06 21:34:58 +0000562 several optimizations are possible.
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000563 */
564 elements = len / view->itemsize;
565 while (elements--) {
566 addone(view->ndim, indices, view->shape);
567 ptr = PyBuffer_GetPointer(view, indices);
568 memcpy(ptr, src, view->itemsize);
569 src += view->itemsize;
570 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000571
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000572 PyMem_Free(indices);
573 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000574}
575
Guido van Rossum98297ee2007-11-06 21:34:58 +0000576int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000577{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000578 Py_buffer view_dest, view_src;
579 int k;
580 Py_ssize_t *indices, elements;
581 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000582
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000583 if (!PyObject_CheckBuffer(dest) ||
584 !PyObject_CheckBuffer(src)) {
585 PyErr_SetString(PyExc_TypeError,
586 "both destination and source must have the "\
587 "buffer interface");
588 return -1;
589 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000590
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000591 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
592 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
593 PyObject_ReleaseBuffer(dest, &view_dest);
594 return -1;
595 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000596
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000597 if (view_dest.len < view_src.len) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000598 PyErr_SetString(PyExc_BufferError,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000599 "destination is too small to receive data from source");
600 PyObject_ReleaseBuffer(dest, &view_dest);
601 PyObject_ReleaseBuffer(src, &view_src);
602 return -1;
603 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000604
Guido van Rossum98297ee2007-11-06 21:34:58 +0000605 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000606 PyBuffer_IsContiguous(&view_src, 'C')) ||
Guido van Rossum98297ee2007-11-06 21:34:58 +0000607 (PyBuffer_IsContiguous(&view_dest, 'F') &&
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000608 PyBuffer_IsContiguous(&view_src, 'F'))) {
609 /* simplest copy is all that is needed */
610 memcpy(view_dest.buf, view_src.buf, view_src.len);
611 PyObject_ReleaseBuffer(dest, &view_dest);
612 PyObject_ReleaseBuffer(src, &view_src);
613 return 0;
614 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000615
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000616 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000617
Neal Norwitzfaa54a32007-08-19 04:23:20 +0000618 /* XXX(nnorwitz): need to check for overflow! */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000619 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
620 if (indices == NULL) {
621 PyErr_NoMemory();
622 PyObject_ReleaseBuffer(dest, &view_dest);
623 PyObject_ReleaseBuffer(src, &view_src);
624 return -1;
625 }
626 for (k=0; k<view_src.ndim;k++) {
627 indices[k] = 0;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000628 }
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000629 elements = 1;
630 for (k=0; k<view_src.ndim; k++) {
Neal Norwitzfaa54a32007-08-19 04:23:20 +0000631 /* XXX(nnorwitz): can this overflow? */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000632 elements *= view_src.shape[k];
633 }
634 while (elements--) {
635 _add_one_to_index_C(view_src.ndim, indices, view_src.shape);
636 dptr = PyBuffer_GetPointer(&view_dest, indices);
637 sptr = PyBuffer_GetPointer(&view_src, indices);
638 memcpy(dptr, sptr, view_src.itemsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000639 }
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000640 PyMem_Free(indices);
641 PyObject_ReleaseBuffer(dest, &view_dest);
642 PyObject_ReleaseBuffer(src, &view_src);
643 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000644}
645
646void
647PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000648 Py_ssize_t *strides, int itemsize,
649 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000650{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000651 int k;
652 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000653
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000654 sd = itemsize;
655 if (fort == 'F') {
656 for (k=0; k<nd; k++) {
657 strides[k] = sd;
658 sd *= shape[k];
Guido van Rossum98297ee2007-11-06 21:34:58 +0000659 }
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000660 }
661 else {
662 for (k=nd-1; k>=0; k--) {
663 strides[k] = sd;
664 sd *= shape[k];
665 }
666 }
667 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000668}
669
670int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000671PyBuffer_FillInfo(Py_buffer *view, void *buf, Py_ssize_t len,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000672 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000673{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000674 if (view == NULL) return 0;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000675 if (((flags & PyBUF_LOCK) == PyBUF_LOCK) &&
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000676 readonly >= 0) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000677 PyErr_SetString(PyExc_BufferError,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000678 "Cannot lock this object.");
679 return -1;
680 }
681 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
682 (readonly == 1)) {
683 PyErr_SetString(PyExc_BufferError,
684 "Object is not writable.");
685 return -1;
686 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000687
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000688 view->buf = buf;
689 view->len = len;
690 view->readonly = readonly;
691 view->itemsize = 1;
692 view->format = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000693 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +0000694 view->format = "B";
695 view->ndim = 1;
696 view->shape = NULL;
697 if ((flags & PyBUF_ND) == PyBUF_ND)
698 view->shape = &(view->len);
699 view->strides = NULL;
700 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
701 view->strides = &(view->itemsize);
702 view->suboffsets = NULL;
703 view->internal = NULL;
704 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000705}
706
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000707/* Operations on numbers */
708
709int
Fred Drake79912472000-07-09 04:06:11 +0000710PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000711{
Guido van Rossum6921eca2003-02-18 16:36:28 +0000712 return o && o->ob_type->tp_as_number &&
713 (o->ob_type->tp_as_number->nb_int ||
714 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000715}
716
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000717/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000718
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000719#define NB_SLOT(x) offsetof(PyNumberMethods, x)
720#define NB_BINOP(nb_methods, slot) \
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000721 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000722#define NB_TERNOP(nb_methods, slot) \
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000723 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000724
725/*
726 Calling scheme used for binary operations:
727
Neal Norwitz4886cc32006-08-21 17:06:07 +0000728 Order operations are tried until either a valid result or error:
729 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000730
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000731 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
732 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000733 */
734
735static PyObject *
736binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000737{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000738 PyObject *x;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000739 binaryfunc slotv = NULL;
740 binaryfunc slotw = NULL;
741
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000742 if (v->ob_type->tp_as_number != NULL)
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000743 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000744 if (w->ob_type != v->ob_type &&
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000745 w->ob_type->tp_as_number != NULL) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000746 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000747 if (slotw == slotv)
748 slotw = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000749 }
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000750 if (slotv) {
Guido van Rossum89c42642001-10-01 17:10:18 +0000751 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
752 x = slotw(v, w);
753 if (x != Py_NotImplemented)
754 return x;
755 Py_DECREF(x); /* can't do it */
756 slotw = NULL;
757 }
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000758 x = slotv(v, w);
759 if (x != Py_NotImplemented)
760 return x;
761 Py_DECREF(x); /* can't do it */
762 }
763 if (slotw) {
764 x = slotw(v, w);
765 if (x != Py_NotImplemented)
766 return x;
767 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000768 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000769 Py_INCREF(Py_NotImplemented);
770 return Py_NotImplemented;
771}
Guido van Rossum77660912002-04-16 16:32:50 +0000772
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000773static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000774binop_type_error(PyObject *v, PyObject *w, const char *op_name)
775{
776 PyErr_Format(PyExc_TypeError,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777 "unsupported operand type(s) for %.100s: "
778 "'%.100s' and '%.100s'",
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000779 op_name,
780 v->ob_type->tp_name,
781 w->ob_type->tp_name);
782 return NULL;
783}
784
785static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000786binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
787{
788 PyObject *result = binary_op1(v, w, op_slot);
789 if (result == Py_NotImplemented) {
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000790 Py_DECREF(result);
791 return binop_type_error(v, w, op_name);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000792 }
793 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000794}
795
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000796
797/*
798 Calling scheme used for ternary operations:
799
Neal Norwitz4886cc32006-08-21 17:06:07 +0000800 Order operations are tried until either a valid result or error:
801 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000802 */
803
804static PyObject *
805ternary_op(PyObject *v,
806 PyObject *w,
807 PyObject *z,
808 const int op_slot,
809 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000810{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000811 PyNumberMethods *mv, *mw, *mz;
Guido van Rossum84675ac2001-09-29 01:05:03 +0000812 PyObject *x = NULL;
813 ternaryfunc slotv = NULL;
814 ternaryfunc slotw = NULL;
815 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000816
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000817 mv = v->ob_type->tp_as_number;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000818 mw = w->ob_type->tp_as_number;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000819 if (mv != NULL)
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000820 slotv = NB_TERNOP(mv, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000821 if (w->ob_type != v->ob_type &&
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000822 mw != NULL) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000823 slotw = NB_TERNOP(mw, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000824 if (slotw == slotv)
825 slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000826 }
Guido van Rossum84675ac2001-09-29 01:05:03 +0000827 if (slotv) {
Guido van Rossum89c42642001-10-01 17:10:18 +0000828 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
829 x = slotw(v, w, z);
830 if (x != Py_NotImplemented)
831 return x;
832 Py_DECREF(x); /* can't do it */
833 slotw = NULL;
834 }
Guido van Rossum84675ac2001-09-29 01:05:03 +0000835 x = slotv(v, w, z);
836 if (x != Py_NotImplemented)
837 return x;
838 Py_DECREF(x); /* can't do it */
839 }
840 if (slotw) {
841 x = slotw(v, w, z);
842 if (x != Py_NotImplemented)
843 return x;
844 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000845 }
846 mz = z->ob_type->tp_as_number;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000847 if (mz != NULL) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000848 slotz = NB_TERNOP(mz, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000849 if (slotz == slotv || slotz == slotw)
850 slotz = NULL;
851 if (slotz) {
852 x = slotz(v, w, z);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000853 if (x != Py_NotImplemented)
854 return x;
Guido van Rossum84675ac2001-09-29 01:05:03 +0000855 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000856 }
857 }
858
Guido van Rossum5c66a262001-10-22 04:12:44 +0000859 if (z == Py_None)
860 PyErr_Format(
861 PyExc_TypeError,
862 "unsupported operand type(s) for ** or pow(): "
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000863 "'%.100s' and '%.100s'",
Guido van Rossum5c66a262001-10-22 04:12:44 +0000864 v->ob_type->tp_name,
865 w->ob_type->tp_name);
866 else
867 PyErr_Format(
868 PyExc_TypeError,
869 "unsupported operand type(s) for pow(): "
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000870 "'%.100s', '%.100s', '%.100s'",
Guido van Rossum5c66a262001-10-22 04:12:44 +0000871 v->ob_type->tp_name,
872 w->ob_type->tp_name,
873 z->ob_type->tp_name);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000874 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000875}
876
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000877#define BINARY_FUNC(func, op, op_name) \
878 PyObject * \
879 func(PyObject *v, PyObject *w) { \
880 return binary_op(v, w, NB_SLOT(op), op_name); \
881 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000882
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000883BINARY_FUNC(PyNumber_Or, nb_or, "|")
884BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
885BINARY_FUNC(PyNumber_And, nb_and, "&")
886BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
887BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
888BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000889BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000890
891PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000892PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000893{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000894 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
895 if (result == Py_NotImplemented) {
896 PySequenceMethods *m = v->ob_type->tp_as_sequence;
Armin Rigofd163f92005-12-29 15:59:19 +0000897 Py_DECREF(result);
Jeremy Hylton0522d982002-03-08 21:28:54 +0000898 if (m && m->sq_concat) {
Armin Rigofd163f92005-12-29 15:59:19 +0000899 return (*m->sq_concat)(v, w);
Jeremy Hylton0522d982002-03-08 21:28:54 +0000900 }
Armin Rigofd163f92005-12-29 15:59:19 +0000901 result = binop_type_error(v, w, "+");
Guido van Rossume15dee51995-07-18 14:12:02 +0000902 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000903 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000904}
905
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000906static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000908{
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000909 Py_ssize_t count;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000910 if (PyIndex_Check(n)) {
911 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000912 if (count == -1 && PyErr_Occurred())
913 return NULL;
914 }
915 else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916 return type_error("can't multiply sequence by "
917 "non-int of type '%.200s'", n);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000918 }
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000919 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000920}
921
922PyObject *
923PyNumber_Multiply(PyObject *v, PyObject *w)
924{
925 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
926 if (result == Py_NotImplemented) {
927 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
928 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
Guido van Rossuma3a243e2002-12-31 19:50:03 +0000929 Py_DECREF(result);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000930 if (mv && mv->sq_repeat) {
931 return sequence_repeat(mv->sq_repeat, v, w);
932 }
933 else if (mw && mw->sq_repeat) {
934 return sequence_repeat(mw->sq_repeat, w, v);
935 }
936 result = binop_type_error(v, w, "*");
937 }
938 return result;
939}
940
Guido van Rossume15dee51995-07-18 14:12:02 +0000941PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000942PyNumber_FloorDivide(PyObject *v, PyObject *w)
943{
Guido van Rossum4668b002001-08-08 05:00:18 +0000944 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
945}
946
947PyObject *
948PyNumber_TrueDivide(PyObject *v, PyObject *w)
949{
Guido van Rossum4668b002001-08-08 05:00:18 +0000950 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
951}
952
953PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000954PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000955{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000956 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000957}
958
959PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000960PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000961{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000962 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000963}
964
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000965/* Binary in-place operators */
966
967/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000968 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000969
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000970 - If the left hand object has the appropriate struct members, and
971 they are filled, call the appropriate function and return the
972 result. No coercion is done on the arguments; the left-hand object
973 is the one the operation is performed on, and it's up to the
974 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +0000975
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000976 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000977 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000978
979 */
980
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000981static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000982binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000983{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000984 PyNumberMethods *mv = v->ob_type->tp_as_number;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000985 if (mv != NULL) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000986 binaryfunc slot = NB_BINOP(mv, iop_slot);
987 if (slot) {
988 PyObject *x = (slot)(v, w);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000989 if (x != Py_NotImplemented) {
990 return x;
991 }
992 Py_DECREF(x);
993 }
Thomas Woutersf2b332d2000-09-02 08:34:40 +0000994 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000995 return binary_op1(v, w, op_slot);
996}
997
998static PyObject *
999binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
1000 const char *op_name)
1001{
1002 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1003 if (result == Py_NotImplemented) {
1004 Py_DECREF(result);
1005 return binop_type_error(v, w, op_name);
1006 }
1007 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001008}
1009
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001010#define INPLACE_BINOP(func, iop, op, op_name) \
1011 PyObject * \
1012 func(PyObject *v, PyObject *w) { \
1013 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001014 }
1015
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001016INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1017INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1018INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1019INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1020INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1021INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001022
1023PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001024PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1025{
Guido van Rossum4668b002001-08-08 05:00:18 +00001026 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1027 NB_SLOT(nb_floor_divide), "//=");
1028}
1029
1030PyObject *
1031PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1032{
Guido van Rossum4668b002001-08-08 05:00:18 +00001033 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1034 NB_SLOT(nb_true_divide), "/=");
1035}
1036
1037PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001038PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1039{
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001040 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1041 NB_SLOT(nb_add));
1042 if (result == Py_NotImplemented) {
1043 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1044 Py_DECREF(result);
1045 if (m != NULL) {
1046 binaryfunc f = NULL;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001047 f = m->sq_inplace_concat;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001048 if (f == NULL)
1049 f = m->sq_concat;
1050 if (f != NULL)
1051 return (*f)(v, w);
1052 }
1053 result = binop_type_error(v, w, "+=");
Guido van Rossumbb8be932000-09-01 23:27:32 +00001054 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001055 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001056}
1057
1058PyObject *
1059PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1060{
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001061 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1062 NB_SLOT(nb_multiply));
1063 if (result == Py_NotImplemented) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064 ssizeargfunc f = NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001065 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1066 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1067 Py_DECREF(result);
1068 if (mv != NULL) {
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001069 f = mv->sq_inplace_repeat;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001070 if (f == NULL)
1071 f = mv->sq_repeat;
1072 if (f != NULL)
1073 return sequence_repeat(f, v, w);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001074 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001075 else if (mw != NULL) {
1076 /* Note that the right hand operand should not be
1077 * mutated in this case so sq_inplace_repeat is not
1078 * used. */
1079 if (mw->sq_repeat)
1080 return sequence_repeat(mw->sq_repeat, w, v);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001081 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001082 result = binop_type_error(v, w, "*=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001083 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001084 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001085}
1086
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001087PyObject *
1088PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1089{
Neil Schemenauerd46fbc32002-11-24 01:07:42 +00001090 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1091 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001092}
1093
1094PyObject *
1095PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1096{
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001097 if (v->ob_type->tp_as_number &&
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001098 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1099 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001100 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001101 else {
1102 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1103 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001104}
1105
1106
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001107/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001108
1109PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001110PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001111{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001112 PyNumberMethods *m;
1113
1114 if (o == NULL)
1115 return null_error();
1116 m = o->ob_type->tp_as_number;
1117 if (m && m->nb_negative)
1118 return (*m->nb_negative)(o);
1119
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001120 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001121}
1122
1123PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001124PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001125{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001126 PyNumberMethods *m;
1127
1128 if (o == NULL)
1129 return null_error();
1130 m = o->ob_type->tp_as_number;
1131 if (m && m->nb_positive)
1132 return (*m->nb_positive)(o);
1133
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001134 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001135}
1136
1137PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001138PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001139{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001140 PyNumberMethods *m;
1141
1142 if (o == NULL)
1143 return null_error();
1144 m = o->ob_type->tp_as_number;
1145 if (m && m->nb_invert)
1146 return (*m->nb_invert)(o);
1147
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001148 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001149}
1150
1151PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001152PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001153{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001154 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001155
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001156 if (o == NULL)
1157 return null_error();
1158 m = o->ob_type->tp_as_number;
1159 if (m && m->nb_absolute)
1160 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001161
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001162 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001163}
1164
Guido van Rossum98297ee2007-11-06 21:34:58 +00001165/* Return a Python Int or Long from the object item
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001166 Raise TypeError if the result is not an int-or-long
Guido van Rossum98297ee2007-11-06 21:34:58 +00001167 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001168*/
1169PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001170PyNumber_Index(PyObject *item)
1171{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001172 PyObject *result = NULL;
1173 if (item == NULL)
1174 return null_error();
Neal Norwitz1fe5f382007-08-31 04:32:55 +00001175 if (PyLong_Check(item)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001176 Py_INCREF(item);
1177 return item;
1178 }
1179 if (PyIndex_Check(item)) {
1180 result = item->ob_type->tp_as_number->nb_index(item);
Neal Norwitz1fe5f382007-08-31 04:32:55 +00001181 if (result && !PyLong_Check(result)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001182 PyErr_Format(PyExc_TypeError,
Guido van Rossum032a2842007-02-26 05:44:27 +00001183 "__index__ returned non-int "
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001184 "(type %.200s)",
1185 result->ob_type->tp_name);
1186 Py_DECREF(result);
1187 return NULL;
1188 }
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001189 }
1190 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191 PyErr_Format(PyExc_TypeError,
1192 "'%.200s' object cannot be interpreted "
Guido van Rossum032a2842007-02-26 05:44:27 +00001193 "as an integer", item->ob_type->tp_name);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001194 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001195 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001196}
1197
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001198/* Return an error on Overflow only if err is not NULL*/
1199
1200Py_ssize_t
1201PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1202{
1203 Py_ssize_t result;
1204 PyObject *runerr;
1205 PyObject *value = PyNumber_Index(item);
1206 if (value == NULL)
1207 return -1;
1208
Christian Heimes217cfd12007-12-02 14:31:20 +00001209 /* We're done if PyLong_AsSsize_t() returns without error. */
1210 result = PyLong_AsSsize_t(value);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001211 if (result != -1 || !(runerr = PyErr_Occurred()))
1212 goto finish;
1213
1214 /* Error handling code -- only manage OverflowError differently */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001215 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001216 goto finish;
1217
1218 PyErr_Clear();
Guido van Rossum98297ee2007-11-06 21:34:58 +00001219 /* If no error-handling desired then the default clipping
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001220 is sufficient.
1221 */
1222 if (!err) {
1223 assert(PyLong_Check(value));
Guido van Rossum98297ee2007-11-06 21:34:58 +00001224 /* Whether or not it is less than or equal to
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001225 zero is determined by the sign of ob_size
1226 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001227 if (_PyLong_Sign(value) < 0)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001228 result = PY_SSIZE_T_MIN;
1229 else
1230 result = PY_SSIZE_T_MAX;
1231 }
1232 else {
1233 /* Otherwise replace the error with caller's error object. */
1234 PyErr_Format(err,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001235 "cannot fit '%.200s' into an index-sized integer",
1236 item->ob_type->tp_name);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001237 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001238
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001239 finish:
1240 Py_DECREF(value);
1241 return result;
1242}
1243
1244
Christian Heimes15ebc882008-02-04 18:48:49 +00001245PyObject *
1246_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
1247{
1248 static PyObject *int_name = NULL;
1249 if (int_name == NULL) {
1250 int_name = PyUnicode_InternFromString("__int__");
1251 if (int_name == NULL)
1252 return NULL;
1253 }
1254
1255 if (integral && !PyLong_Check(integral)) {
1256 /* Don't go through tp_as_number->nb_int to avoid
1257 hitting the classic class fallback to __trunc__. */
1258 PyObject *int_func = PyObject_GetAttr(integral, int_name);
1259 if (int_func == NULL) {
1260 PyErr_Clear(); /* Raise a different error. */
1261 goto non_integral_error;
1262 }
1263 Py_DECREF(integral);
1264 integral = PyEval_CallObject(int_func, NULL);
1265 Py_DECREF(int_func);
1266 if (integral && !PyLong_Check(integral)) {
1267 goto non_integral_error;
1268 }
1269 }
1270 return integral;
1271
1272non_integral_error:
1273 PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
1274 Py_DECREF(integral);
1275 return NULL;
1276}
1277
1278
Guido van Rossum9e896b32000-04-05 20:11:21 +00001279/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001280static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001281long_from_string(const char *s, Py_ssize_t len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001282{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001283 char *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001284 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001285
Guido van Rossum4c08d552000-03-10 22:55:18 +00001286 x = PyLong_FromString((char*)s, &end, 10);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001287 if (x == NULL)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001288 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001289 if (end != s + len) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001290 PyErr_SetString(PyExc_ValueError,
Guido van Rossumddefaf32007-01-14 03:31:43 +00001291 "null byte in argument for int()");
Guido van Rossum9e896b32000-04-05 20:11:21 +00001292 Py_DECREF(x);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001293 return NULL;
1294 }
1295 return x;
1296}
1297
Guido van Rossume15dee51995-07-18 14:12:02 +00001298PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001299PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001300{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001301 PyNumberMethods *m;
Christian Heimes15ebc882008-02-04 18:48:49 +00001302 static PyObject *trunc_name = NULL;
1303 PyObject *trunc_func;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001304 const char *buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 Py_ssize_t buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +00001306
Christian Heimes15ebc882008-02-04 18:48:49 +00001307 if (trunc_name == NULL) {
1308 trunc_name = PyUnicode_InternFromString("__trunc__");
1309 if (trunc_name == NULL)
1310 return NULL;
1311 }
1312
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001313 if (o == NULL)
1314 return null_error();
Guido van Rossumddefaf32007-01-14 03:31:43 +00001315 if (PyLong_CheckExact(o)) {
1316 Py_INCREF(o);
1317 return o;
1318 }
Brett Cannonc3647ac2005-04-26 03:45:26 +00001319 m = o->ob_type->tp_as_number;
Guido van Rossumddefaf32007-01-14 03:31:43 +00001320 if (m && m->nb_int) { /* This should include subclasses of int */
1321 PyObject *res = m->nb_int(o);
1322 if (res && !PyLong_Check(res)) {
1323 PyErr_Format(PyExc_TypeError,
1324 "__int__ returned non-int (type %.200s)",
1325 res->ob_type->tp_name);
1326 Py_DECREF(res);
1327 return NULL;
1328 }
1329 return res;
1330 }
Brett Cannonc3647ac2005-04-26 03:45:26 +00001331 if (m && m->nb_long) { /* This should include subclasses of long */
Christian Heimes15ebc882008-02-04 18:48:49 +00001332 /* Classic classes always take this branch. */
Brett Cannonc3647ac2005-04-26 03:45:26 +00001333 PyObject *res = m->nb_long(o);
Neal Norwitz1fe5f382007-08-31 04:32:55 +00001334 if (res && !PyLong_Check(res)) {
Brett Cannonc3647ac2005-04-26 03:45:26 +00001335 PyErr_Format(PyExc_TypeError,
1336 "__long__ returned non-long (type %.200s)",
1337 res->ob_type->tp_name);
1338 Py_DECREF(res);
1339 return NULL;
1340 }
1341 return res;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001342 }
Brett Cannonc3647ac2005-04-26 03:45:26 +00001343 if (PyLong_Check(o)) /* A long subclass without nb_long */
Tim Petersdb30ac42002-03-02 04:14:21 +00001344 return _PyLong_Copy((PyLongObject *)o);
Christian Heimes15ebc882008-02-04 18:48:49 +00001345 trunc_func = PyObject_GetAttr(o, trunc_name);
1346 if (trunc_func) {
1347 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
1348 PyObject *int_instance;
1349 Py_DECREF(trunc_func);
1350 /* __trunc__ is specified to return an Integral type,
1351 but long() needs to return a long. */
1352 int_instance = _PyNumber_ConvertIntegralToInt(
1353 truncated,
1354 "__trunc__ returned non-Integral (type %.200s)");
1355 return int_instance;
1356 }
1357 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
1358
1359 if (PyString_Check(o))
1360 /* need to do extra error checking that PyLong_FromString()
1361 * doesn't do. In particular long('9.5') must raise an
1362 * exception, not truncate the float.
1363 */
1364 return long_from_string(PyString_AS_STRING(o),
1365 PyString_GET_SIZE(o));
Guido van Rossum9e896b32000-04-05 20:11:21 +00001366 if (PyUnicode_Check(o))
1367 /* The above check is done in PyLong_FromUnicode(). */
1368 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
1369 PyUnicode_GET_SIZE(o),
1370 10);
Guido van Rossum4c08d552000-03-10 22:55:18 +00001371 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
1372 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +00001373
Guido van Rossumddefaf32007-01-14 03:31:43 +00001374 return type_error("int() argument must be a string or a "
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001375 "number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001376}
1377
1378PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001379PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001380{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001381 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001382
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001383 if (o == NULL)
1384 return null_error();
Brett Cannonc3647ac2005-04-26 03:45:26 +00001385 m = o->ob_type->tp_as_number;
1386 if (m && m->nb_float) { /* This should include subclasses of float */
1387 PyObject *res = m->nb_float(o);
1388 if (res && !PyFloat_Check(res)) {
1389 PyErr_Format(PyExc_TypeError,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001390 "__float__ returned non-float (type %.200s)",
1391 res->ob_type->tp_name);
Brett Cannonc3647ac2005-04-26 03:45:26 +00001392 Py_DECREF(res);
1393 return NULL;
1394 }
1395 return res;
Guido van Rossum9e896b32000-04-05 20:11:21 +00001396 }
Brett Cannonc3647ac2005-04-26 03:45:26 +00001397 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Tim Peters7a50f252001-09-10 21:28:20 +00001398 PyFloatObject *po = (PyFloatObject *)o;
1399 return PyFloat_FromDouble(po->ob_fval);
1400 }
Georg Brandl428f0642007-03-18 18:35:15 +00001401 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001402}
1403
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001404
1405PyObject *
1406PyNumber_ToBase(PyObject *n, int base)
1407{
Christian Heimesa156e092008-02-16 07:38:31 +00001408 PyObject *res = NULL;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001409 PyObject *index = PyNumber_Index(n);
1410
1411 if (!index)
1412 return NULL;
Christian Heimesa156e092008-02-16 07:38:31 +00001413 if (PyLong_Check(index))
1414 res = _PyLong_Format(index, base);
1415 else
1416 /* It should not be possible to get here, as
1417 PyNumber_Index already has a check for the same
1418 condition */
1419 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
1420 "int or long");
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001421 Py_DECREF(index);
1422 return res;
1423}
1424
1425
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001426/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001427
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001428int
Fred Drake79912472000-07-09 04:06:11 +00001429PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001430{
Thomas Wouterscf297e42007-02-23 15:07:44 +00001431 if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
1432 return 0;
Guido van Rossum8700b422001-09-07 20:20:11 +00001433 return s != NULL && s->ob_type->tp_as_sequence &&
1434 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001435}
1436
Martin v. Löwis18e16552006-02-15 17:27:45 +00001437Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001438PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001439{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001440 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001441
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001442 if (s == NULL) {
1443 null_error();
1444 return -1;
1445 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001446
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001447 m = s->ob_type->tp_as_sequence;
1448 if (m && m->sq_length)
1449 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001450
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001451 type_error("object of type '%.200s' has no len()", s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001452 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001453}
1454
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001455#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001456Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001457PySequence_Length(PyObject *s)
1458{
1459 return PySequence_Size(s);
1460}
1461#define PySequence_Length PySequence_Size
1462
Guido van Rossume15dee51995-07-18 14:12:02 +00001463PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001464PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001465{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001466 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001467
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001468 if (s == NULL || o == NULL)
1469 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001470
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001471 m = s->ob_type->tp_as_sequence;
1472 if (m && m->sq_concat)
1473 return m->sq_concat(s, o);
1474
Armin Rigofd163f92005-12-29 15:59:19 +00001475 /* Instances of user classes defining an __add__() method only
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001476 have an nb_add slot, not an sq_concat slot. So we fall back
Armin Rigofd163f92005-12-29 15:59:19 +00001477 to nb_add if both arguments appear to be sequences. */
1478 if (PySequence_Check(s) && PySequence_Check(o)) {
1479 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1480 if (result != Py_NotImplemented)
1481 return result;
1482 Py_DECREF(result);
1483 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001484 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001485}
1486
1487PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001489{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001490 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001491
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001492 if (o == NULL)
1493 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001494
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001495 m = o->ob_type->tp_as_sequence;
1496 if (m && m->sq_repeat)
1497 return m->sq_repeat(o, count);
1498
Armin Rigofd163f92005-12-29 15:59:19 +00001499 /* Instances of user classes defining a __mul__() method only
1500 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1501 to nb_multiply if o appears to be a sequence. */
1502 if (PySequence_Check(o)) {
1503 PyObject *n, *result;
Christian Heimes217cfd12007-12-02 14:31:20 +00001504 n = PyLong_FromSsize_t(count);
Armin Rigofd163f92005-12-29 15:59:19 +00001505 if (n == NULL)
1506 return NULL;
1507 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1508 Py_DECREF(n);
1509 if (result != Py_NotImplemented)
1510 return result;
1511 Py_DECREF(result);
1512 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001513 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001514}
1515
1516PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001517PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1518{
1519 PySequenceMethods *m;
1520
1521 if (s == NULL || o == NULL)
1522 return null_error();
1523
1524 m = s->ob_type->tp_as_sequence;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001525 if (m && m->sq_inplace_concat)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001526 return m->sq_inplace_concat(s, o);
1527 if (m && m->sq_concat)
1528 return m->sq_concat(s, o);
1529
Armin Rigofd163f92005-12-29 15:59:19 +00001530 if (PySequence_Check(s) && PySequence_Check(o)) {
1531 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1532 NB_SLOT(nb_add));
1533 if (result != Py_NotImplemented)
1534 return result;
1535 Py_DECREF(result);
1536 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001537 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001538}
1539
1540PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001541PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001542{
1543 PySequenceMethods *m;
1544
1545 if (o == NULL)
1546 return null_error();
1547
1548 m = o->ob_type->tp_as_sequence;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001549 if (m && m->sq_inplace_repeat)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001550 return m->sq_inplace_repeat(o, count);
1551 if (m && m->sq_repeat)
1552 return m->sq_repeat(o, count);
1553
Armin Rigofd163f92005-12-29 15:59:19 +00001554 if (PySequence_Check(o)) {
1555 PyObject *n, *result;
Christian Heimes217cfd12007-12-02 14:31:20 +00001556 n = PyLong_FromSsize_t(count);
Armin Rigofd163f92005-12-29 15:59:19 +00001557 if (n == NULL)
1558 return NULL;
1559 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1560 NB_SLOT(nb_multiply));
1561 Py_DECREF(n);
1562 if (result != Py_NotImplemented)
1563 return result;
1564 Py_DECREF(result);
1565 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001566 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001567}
1568
1569PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001570PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001571{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001572 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001573
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001574 if (s == NULL)
1575 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001576
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001577 m = s->ob_type->tp_as_sequence;
1578 if (m && m->sq_item) {
1579 if (i < 0) {
1580 if (m->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001581 Py_ssize_t l = (*m->sq_length)(s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001582 if (l < 0)
1583 return NULL;
1584 i += l;
1585 }
1586 }
1587 return m->sq_item(s, i);
1588 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001589
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001590 return type_error("'%.200s' object is unindexable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001591}
1592
1593PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001594PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001595{
Thomas Wouters1d75a792000-08-17 22:37:32 +00001596 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001597
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001598 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001599
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001600 mp = s->ob_type->tp_as_mapping;
1601 if (mp->mp_subscript) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001602 PyObject *res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603 PyObject *slice = _PySlice_FromIndices(i1, i2);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001604 if (!slice)
1605 return NULL;
1606 res = mp->mp_subscript(s, slice);
1607 Py_DECREF(slice);
1608 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001609 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001610
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001611 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001612}
1613
1614int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001615PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001616{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001617 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001618
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001619 if (s == NULL) {
1620 null_error();
1621 return -1;
1622 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001623
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001624 m = s->ob_type->tp_as_sequence;
1625 if (m && m->sq_ass_item) {
1626 if (i < 0) {
1627 if (m->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001628 Py_ssize_t l = (*m->sq_length)(s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001629 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001630 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001631 i += l;
1632 }
1633 }
1634 return m->sq_ass_item(s, i, o);
1635 }
1636
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001637 type_error("'%.200s' object does not support item assignment", s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001638 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001639}
1640
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001641int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001642PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001643{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001644 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001645
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001646 if (s == NULL) {
1647 null_error();
1648 return -1;
1649 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001650
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001651 m = s->ob_type->tp_as_sequence;
1652 if (m && m->sq_ass_item) {
1653 if (i < 0) {
1654 if (m->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001655 Py_ssize_t l = (*m->sq_length)(s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001656 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001657 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001658 i += l;
1659 }
1660 }
1661 return m->sq_ass_item(s, i, (PyObject *)NULL);
1662 }
1663
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001664 type_error("'%.200s' object doesn't support item deletion", s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001665 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001666}
1667
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001668int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001669PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001670{
Thomas Wouters1d75a792000-08-17 22:37:32 +00001671 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001672
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001673 if (s == NULL) {
1674 null_error();
1675 return -1;
1676 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001677
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001678 mp = s->ob_type->tp_as_mapping;
1679 if (mp->mp_ass_subscript) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001680 int res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001681 PyObject *slice = _PySlice_FromIndices(i1, i2);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001682 if (!slice)
1683 return -1;
1684 res = mp->mp_ass_subscript(s, slice, o);
1685 Py_DECREF(slice);
1686 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001687 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001688
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001689 type_error("'%.200s' object doesn't support slice assignment", s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001690 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001691}
1692
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001693int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001694PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001695{
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001696 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001697
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001698 if (s == NULL) {
1699 null_error();
1700 return -1;
1701 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001702
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001703 mp = s->ob_type->tp_as_mapping;
1704 if (mp->mp_ass_subscript) {
1705 int res;
1706 PyObject *slice = _PySlice_FromIndices(i1, i2);
1707 if (!slice)
1708 return -1;
1709 res = mp->mp_ass_subscript(s, slice, NULL);
1710 Py_DECREF(slice);
1711 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001712 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001713 type_error("'%.200s' object doesn't support slice deletion", s);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001714 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001715}
1716
Guido van Rossume15dee51995-07-18 14:12:02 +00001717PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001718PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001719{
Tim Peters6912d4d2001-05-05 03:56:37 +00001720 PyObject *it; /* iter(v) */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001721 Py_ssize_t n; /* guess for result tuple size */
Tim Peters6912d4d2001-05-05 03:56:37 +00001722 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001723 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001724
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001725 if (v == NULL)
1726 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001727
Tim Peters6912d4d2001-05-05 03:56:37 +00001728 /* Special-case the common tuple and list cases, for efficiency. */
Tim Peters4c3a0a32001-09-10 23:37:46 +00001729 if (PyTuple_CheckExact(v)) {
1730 /* Note that we can't know whether it's safe to return
1731 a tuple *subclass* instance as-is, hence the restriction
Tim Peters8ff70a92001-09-10 23:53:53 +00001732 to exact tuples here. In contrast, lists always make
1733 a copy, so there's no need for exactness below. */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001734 Py_INCREF(v);
1735 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001736 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001737 if (PyList_Check(v))
1738 return PyList_AsTuple(v);
1739
Tim Peters6912d4d2001-05-05 03:56:37 +00001740 /* Get iterator. */
1741 it = PyObject_GetIter(v);
1742 if (it == NULL)
Tim Peters1fc240e2001-10-26 05:06:50 +00001743 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001744
Tim Peters6912d4d2001-05-05 03:56:37 +00001745 /* Guess result size and allocate space. */
Christian Heimes255f53b2007-12-08 15:33:56 +00001746 n = _PyObject_LengthHint(v, 10);
Tim Peters6912d4d2001-05-05 03:56:37 +00001747 result = PyTuple_New(n);
1748 if (result == NULL)
1749 goto Fail;
1750
1751 /* Fill the tuple. */
1752 for (j = 0; ; ++j) {
1753 PyObject *item = PyIter_Next(it);
1754 if (item == NULL) {
1755 if (PyErr_Occurred())
1756 goto Fail;
1757 break;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001758 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001759 if (j >= n) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001760 Py_ssize_t oldn = n;
Raymond Hettinger4d012592004-12-16 10:38:38 +00001761 /* The over-allocation strategy can grow a bit faster
Guido van Rossum98297ee2007-11-06 21:34:58 +00001762 than for lists because unlike lists the
Raymond Hettinger4d012592004-12-16 10:38:38 +00001763 over-allocation isn't permanent -- we reclaim
1764 the excess before the end of this routine.
1765 So, grow by ten and then add 25%.
1766 */
1767 n += 10;
1768 n += n >> 2;
1769 if (n < oldn) {
1770 /* Check for overflow */
1771 PyErr_NoMemory();
Raymond Hettingere6bdb372004-12-16 15:10:21 +00001772 Py_DECREF(item);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001773 goto Fail;
Raymond Hettinger4d012592004-12-16 10:38:38 +00001774 }
Tim Peters4324aa32001-05-28 22:30:08 +00001775 if (_PyTuple_Resize(&result, n) != 0) {
Tim Peters12d0a6c2001-05-05 04:10:25 +00001776 Py_DECREF(item);
Tim Peters6912d4d2001-05-05 03:56:37 +00001777 goto Fail;
Tim Peters12d0a6c2001-05-05 04:10:25 +00001778 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001779 }
1780 PyTuple_SET_ITEM(result, j, item);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001781 }
1782
Tim Peters6912d4d2001-05-05 03:56:37 +00001783 /* Cut tuple back if guess was too large. */
1784 if (j < n &&
Tim Peters4324aa32001-05-28 22:30:08 +00001785 _PyTuple_Resize(&result, j) != 0)
Tim Peters6912d4d2001-05-05 03:56:37 +00001786 goto Fail;
1787
1788 Py_DECREF(it);
1789 return result;
1790
1791Fail:
1792 Py_XDECREF(result);
1793 Py_DECREF(it);
1794 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001795}
1796
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001797PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001798PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001799{
Tim Petersf553f892001-05-01 20:45:31 +00001800 PyObject *result; /* result list */
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001801 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001802
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001803 if (v == NULL)
1804 return null_error();
1805
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001806 result = PyList_New(0);
1807 if (result == NULL)
Tim Petersf553f892001-05-01 20:45:31 +00001808 return NULL;
1809
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001810 rv = _PyList_Extend((PyListObject *)result, v);
1811 if (rv == NULL) {
1812 Py_DECREF(result);
Tim Petersf553f892001-05-01 20:45:31 +00001813 return NULL;
1814 }
Raymond Hettinger969d8c02004-03-17 05:24:23 +00001815 Py_DECREF(rv);
Tim Petersf553f892001-05-01 20:45:31 +00001816 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001817}
1818
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001819PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001820PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001821{
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001822 PyObject *it;
1823
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001824 if (v == NULL)
1825 return null_error();
1826
Michael W. Hudson2801fe12002-11-05 18:05:49 +00001827 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001828 Py_INCREF(v);
1829 return v;
1830 }
1831
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001832 it = PyObject_GetIter(v);
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001833 if (it == NULL) {
1834 if (PyErr_ExceptionMatches(PyExc_TypeError))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001835 PyErr_SetString(PyExc_TypeError, m);
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001836 return NULL;
1837 }
1838
Raymond Hettinger193814c2004-12-18 19:00:59 +00001839 v = PySequence_List(it);
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001840 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001841
1842 return v;
1843}
1844
Tim Peters16a77ad2001-09-08 04:00:12 +00001845/* Iterate over seq. Result depends on the operation:
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001846 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1847 PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
1848 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001849 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1850*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001851Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001852_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001853{
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001854 Py_ssize_t n;
Tim Peters16a77ad2001-09-08 04:00:12 +00001855 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1856 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001857
Tim Peters16a77ad2001-09-08 04:00:12 +00001858 if (seq == NULL || obj == NULL) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001859 null_error();
1860 return -1;
1861 }
Tim Peters75f8e352001-05-05 11:33:43 +00001862
Tim Peters16a77ad2001-09-08 04:00:12 +00001863 it = PyObject_GetIter(seq);
Tim Peters75f8e352001-05-05 11:33:43 +00001864 if (it == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001865 type_error("argument of type '%.200s' is not iterable", seq);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001866 return -1;
Tim Peters75f8e352001-05-05 11:33:43 +00001867 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001868
Tim Peters16a77ad2001-09-08 04:00:12 +00001869 n = wrapped = 0;
Tim Peters75f8e352001-05-05 11:33:43 +00001870 for (;;) {
1871 int cmp;
1872 PyObject *item = PyIter_Next(it);
1873 if (item == NULL) {
1874 if (PyErr_Occurred())
1875 goto Fail;
1876 break;
1877 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001878
1879 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001880 Py_DECREF(item);
Tim Peters75f8e352001-05-05 11:33:43 +00001881 if (cmp < 0)
1882 goto Fail;
1883 if (cmp > 0) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001884 switch (operation) {
1885 case PY_ITERSEARCH_COUNT:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001886 if (n == PY_SSIZE_T_MAX) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001887 PyErr_SetString(PyExc_OverflowError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001888 "count exceeds C integer size");
Tim Peters16a77ad2001-09-08 04:00:12 +00001889 goto Fail;
1890 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001891 ++n;
Tim Peters16a77ad2001-09-08 04:00:12 +00001892 break;
1893
1894 case PY_ITERSEARCH_INDEX:
1895 if (wrapped) {
1896 PyErr_SetString(PyExc_OverflowError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001897 "index exceeds C integer size");
Tim Peters16a77ad2001-09-08 04:00:12 +00001898 goto Fail;
1899 }
1900 goto Done;
1901
1902 case PY_ITERSEARCH_CONTAINS:
1903 n = 1;
1904 goto Done;
1905
1906 default:
1907 assert(!"unknown operation");
Tim Peters75f8e352001-05-05 11:33:43 +00001908 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001909 }
1910
1911 if (operation == PY_ITERSEARCH_INDEX) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001912 if (n == PY_SSIZE_T_MAX)
Tim Peters16a77ad2001-09-08 04:00:12 +00001913 wrapped = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001914 ++n;
Tim Peters75f8e352001-05-05 11:33:43 +00001915 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001916 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001917
1918 if (operation != PY_ITERSEARCH_INDEX)
1919 goto Done;
1920
1921 PyErr_SetString(PyExc_ValueError,
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001922 "sequence.index(x): x not in sequence");
Tim Peters16a77ad2001-09-08 04:00:12 +00001923 /* fall into failure code */
1924Fail:
1925 n = -1;
1926 /* fall through */
1927Done:
Tim Peters75f8e352001-05-05 11:33:43 +00001928 Py_DECREF(it);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001929 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001930
Guido van Rossume15dee51995-07-18 14:12:02 +00001931}
1932
Tim Peters16a77ad2001-09-08 04:00:12 +00001933/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001934Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001935PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001936{
Tim Peters16a77ad2001-09-08 04:00:12 +00001937 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001938}
1939
Tim Peterscb8d3682001-05-05 21:05:01 +00001940/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001941 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001942 */
1943int
1944PySequence_Contains(PyObject *seq, PyObject *ob)
1945{
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001946 Py_ssize_t result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001947 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001948 if (sqm != NULL && sqm->sq_contains != NULL)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001949 return (*sqm->sq_contains)(seq, ob);
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001950 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1951 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001952}
1953
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001954/* Backwards compatibility */
1955#undef PySequence_In
1956int
Fred Drake79912472000-07-09 04:06:11 +00001957PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001958{
1959 return PySequence_Contains(w, v);
1960}
1961
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001962Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001963PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001964{
Tim Peters16a77ad2001-09-08 04:00:12 +00001965 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001966}
1967
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001968/* Operations on mappings */
1969
1970int
Fred Drake79912472000-07-09 04:06:11 +00001971PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001972{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00001973 return o && o->ob_type->tp_as_mapping &&
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001974 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00001975}
1976
Martin v. Löwis18e16552006-02-15 17:27:45 +00001977Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001978PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001979{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001980 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001981
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001982 if (o == NULL) {
1983 null_error();
1984 return -1;
1985 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001986
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001987 m = o->ob_type->tp_as_mapping;
1988 if (m && m->mp_length)
1989 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001990
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001991 type_error("object of type '%.200s' has no len()", o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001992 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001993}
1994
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001995#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001996Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001997PyMapping_Length(PyObject *o)
1998{
1999 return PyMapping_Size(o);
2000}
2001#define PyMapping_Length PyMapping_Size
2002
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002003PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002004PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002005{
2006 PyObject *okey, *r;
2007
2008 if (key == NULL)
2009 return null_error();
2010
Martin v. Löwis5b222132007-06-10 09:51:05 +00002011 okey = PyUnicode_FromString(key);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002012 if (okey == NULL)
2013 return NULL;
2014 r = PyObject_GetItem(o, okey);
2015 Py_DECREF(okey);
2016 return r;
2017}
2018
2019int
Fred Drake79912472000-07-09 04:06:11 +00002020PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002021{
2022 PyObject *okey;
2023 int r;
2024
2025 if (key == NULL) {
2026 null_error();
2027 return -1;
2028 }
2029
Martin v. Löwis5b222132007-06-10 09:51:05 +00002030 okey = PyUnicode_FromString(key);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002031 if (okey == NULL)
Guido van Rossumed6219b1998-05-29 02:59:33 +00002032 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002033 r = PyObject_SetItem(o, okey, value);
2034 Py_DECREF(okey);
2035 return r;
2036}
2037
2038int
Fred Drake79912472000-07-09 04:06:11 +00002039PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002040{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002041 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002042
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002043 v = PyMapping_GetItemString(o, key);
2044 if (v) {
2045 Py_DECREF(v);
2046 return 1;
2047 }
2048 PyErr_Clear();
2049 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002050}
2051
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002052int
Fred Drake79912472000-07-09 04:06:11 +00002053PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002054{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002055 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002056
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002057 v = PyObject_GetItem(o, key);
2058 if (v) {
2059 Py_DECREF(v);
2060 return 1;
2061 }
2062 PyErr_Clear();
2063 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002064}
2065
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002066PyObject *
2067PyMapping_Keys(PyObject *o)
2068{
2069 PyObject *keys;
2070 PyObject *fast;
2071
2072 if (PyDict_CheckExact(o))
2073 return PyDict_Keys(o);
2074 keys = PyObject_CallMethod(o, "keys", NULL);
2075 if (keys == NULL)
2076 return NULL;
2077 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2078 Py_DECREF(keys);
2079 return fast;
2080}
2081
2082PyObject *
2083PyMapping_Items(PyObject *o)
2084{
2085 PyObject *items;
2086 PyObject *fast;
2087
2088 if (PyDict_CheckExact(o))
2089 return PyDict_Items(o);
2090 items = PyObject_CallMethod(o, "items", NULL);
2091 if (items == NULL)
2092 return NULL;
2093 fast = PySequence_Fast(items, "o.items() are not iterable");
2094 Py_DECREF(items);
2095 return fast;
2096}
2097
2098PyObject *
2099PyMapping_Values(PyObject *o)
2100{
2101 PyObject *values;
2102 PyObject *fast;
2103
2104 if (PyDict_CheckExact(o))
2105 return PyDict_Values(o);
2106 values = PyObject_CallMethod(o, "values", NULL);
2107 if (values == NULL)
2108 return NULL;
2109 fast = PySequence_Fast(values, "o.values() are not iterable");
2110 Py_DECREF(values);
2111 return fast;
2112}
2113
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002114/* Operations on callable objects */
2115
2116/* XXX PyCallable_Check() is in object.c */
2117
Guido van Rossume15dee51995-07-18 14:12:02 +00002118PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002119PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002120{
Guido van Rossum5560b742001-09-14 16:47:50 +00002121 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002122}
Guido van Rossume15dee51995-07-18 14:12:02 +00002123
2124PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002125PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
2126{
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002127 ternaryfunc call;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128
2129 if ((call = func->ob_type->tp_call) != NULL) {
Thomas Wouters89d996e2007-09-08 17:39:28 +00002130 PyObject *result;
2131 if (Py_EnterRecursiveCall(" while calling a Python object"))
2132 return NULL;
2133 result = (*call)(func, arg, kw);
2134 Py_LeaveRecursiveCall();
Tim Peters6d6c1a32001-08-02 04:15:00 +00002135 if (result == NULL && !PyErr_Occurred())
2136 PyErr_SetString(
2137 PyExc_SystemError,
2138 "NULL result without error in PyObject_Call");
2139 return result;
2140 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002141 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
Fred Drake573395a2001-11-01 20:26:12 +00002142 func->ob_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143 return NULL;
2144}
2145
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002146static PyObject*
2147call_function_tail(PyObject *callable, PyObject *args)
2148{
2149 PyObject *retval;
2150
2151 if (args == NULL)
2152 return NULL;
2153
2154 if (!PyTuple_Check(args)) {
2155 PyObject *a;
2156
2157 a = PyTuple_New(1);
2158 if (a == NULL) {
2159 Py_DECREF(args);
2160 return NULL;
2161 }
2162 PyTuple_SET_ITEM(a, 0, args);
2163 args = a;
2164 }
2165 retval = PyObject_Call(callable, args, NULL);
2166
2167 Py_DECREF(args);
2168
2169 return retval;
2170}
2171
Tim Peters6d6c1a32001-08-02 04:15:00 +00002172PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002173PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002174{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002175 va_list va;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002176 PyObject *args;
Guido van Rossume15dee51995-07-18 14:12:02 +00002177
Fred Drakeb92cf062001-10-27 06:16:31 +00002178 if (callable == NULL)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002179 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002180
Fred Drakeb92cf062001-10-27 06:16:31 +00002181 if (format && *format) {
2182 va_start(va, format);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002183 args = Py_VaBuildValue(format, va);
Fred Drakeb92cf062001-10-27 06:16:31 +00002184 va_end(va);
2185 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002186 else
2187 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002188
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002189 return call_function_tail(callable, args);
2190}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002191
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002192PyObject *
2193_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
2194{
2195 va_list va;
2196 PyObject *args;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002198 if (callable == NULL)
2199 return null_error();
2200
2201 if (format && *format) {
2202 va_start(va, format);
2203 args = _Py_VaBuildValue_SizeT(format, va);
2204 va_end(va);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002205 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206 else
2207 args = PyTuple_New(0);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002208
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002209 return call_function_tail(callable, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002210}
2211
2212PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00002213PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002214{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002215 va_list va;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216 PyObject *args;
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002217 PyObject *func = NULL;
2218 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002219
Fred Drakeb92cf062001-10-27 06:16:31 +00002220 if (o == NULL || name == NULL)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002221 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002222
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002223 func = PyObject_GetAttrString(o, name);
2224 if (func == NULL) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002225 PyErr_SetString(PyExc_AttributeError, name);
2226 return 0;
2227 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002228
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002229 if (!PyCallable_Check(func)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002230 type_error("attribute of type '%.200s' is not callable", func);
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002231 goto exit;
2232 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002233
Fred Drakeb92cf062001-10-27 06:16:31 +00002234 if (format && *format) {
2235 va_start(va, format);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002236 args = Py_VaBuildValue(format, va);
Fred Drakeb92cf062001-10-27 06:16:31 +00002237 va_end(va);
2238 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002239 else
2240 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002241
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 retval = call_function_tail(func, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002243
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002244 exit:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002245 /* args gets consumed in call_function_tail */
2246 Py_XDECREF(func);
2247
2248 return retval;
2249}
2250
2251PyObject *
2252_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
2253{
2254 va_list va;
2255 PyObject *args;
2256 PyObject *func = NULL;
2257 PyObject *retval = NULL;
2258
2259 if (o == NULL || name == NULL)
2260 return null_error();
2261
2262 func = PyObject_GetAttrString(o, name);
2263 if (func == NULL) {
2264 PyErr_SetString(PyExc_AttributeError, name);
2265 return 0;
2266 }
2267
2268 if (!PyCallable_Check(func)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002269 type_error("attribute of type '%.200s' is not callable", func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270 goto exit;
2271 }
2272
2273 if (format && *format) {
2274 va_start(va, format);
2275 args = _Py_VaBuildValue_SizeT(format, va);
2276 va_end(va);
2277 }
2278 else
2279 args = PyTuple_New(0);
2280
2281 retval = call_function_tail(func, args);
2282
2283 exit:
2284 /* args gets consumed in call_function_tail */
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002285 Py_XDECREF(func);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002286
2287 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002288}
Guido van Rossum823649d2001-03-21 18:40:58 +00002289
2290
Fred Drakeb421b8c2001-10-26 16:21:32 +00002291static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002292objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002293{
2294 int i, n = 0;
2295 va_list countva;
2296 PyObject *result, *tmp;
2297
2298#ifdef VA_LIST_IS_ARRAY
2299 memcpy(countva, va, sizeof(va_list));
2300#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +00002301#ifdef __va_copy
2302 __va_copy(countva, va);
2303#else
Fred Drakeb421b8c2001-10-26 16:21:32 +00002304 countva = va;
2305#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +00002306#endif
Fred Drakeb421b8c2001-10-26 16:21:32 +00002307
2308 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
2309 ++n;
2310 result = PyTuple_New(n);
2311 if (result != NULL && n > 0) {
2312 for (i = 0; i < n; ++i) {
2313 tmp = (PyObject *)va_arg(va, PyObject *);
2314 PyTuple_SET_ITEM(result, i, tmp);
2315 Py_INCREF(tmp);
2316 }
2317 }
2318 return result;
2319}
2320
2321PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002322PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002323{
2324 PyObject *args, *tmp;
2325 va_list vargs;
2326
2327 if (callable == NULL || name == NULL)
2328 return null_error();
2329
2330 callable = PyObject_GetAttr(callable, name);
2331 if (callable == NULL)
2332 return NULL;
2333
2334 /* count the args */
2335 va_start(vargs, name);
Fred Drakeb0c079e2001-10-28 02:39:03 +00002336 args = objargs_mktuple(vargs);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002337 va_end(vargs);
2338 if (args == NULL) {
2339 Py_DECREF(callable);
2340 return NULL;
2341 }
2342 tmp = PyObject_Call(callable, args, NULL);
2343 Py_DECREF(args);
2344 Py_DECREF(callable);
2345
2346 return tmp;
2347}
2348
2349PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002350PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002351{
2352 PyObject *args, *tmp;
2353 va_list vargs;
2354
2355 if (callable == NULL)
2356 return null_error();
2357
2358 /* count the args */
2359 va_start(vargs, callable);
Fred Drakeb0c079e2001-10-28 02:39:03 +00002360 args = objargs_mktuple(vargs);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002361 va_end(vargs);
2362 if (args == NULL)
2363 return NULL;
2364 tmp = PyObject_Call(callable, args, NULL);
2365 Py_DECREF(args);
2366
2367 return tmp;
2368}
2369
2370
Guido van Rossum823649d2001-03-21 18:40:58 +00002371/* isinstance(), issubclass() */
2372
Barry Warsawf16951c2002-04-23 22:45:44 +00002373/* abstract_get_bases() has logically 4 return states, with a sort of 0th
2374 * state that will almost never happen.
2375 *
2376 * 0. creating the __bases__ static string could get a MemoryError
2377 * 1. getattr(cls, '__bases__') could raise an AttributeError
2378 * 2. getattr(cls, '__bases__') could raise some other exception
2379 * 3. getattr(cls, '__bases__') could return a tuple
2380 * 4. getattr(cls, '__bases__') could return something other than a tuple
2381 *
2382 * Only state #3 is a non-error state and only it returns a non-NULL object
2383 * (it returns the retrieved tuple).
2384 *
2385 * Any raised AttributeErrors are masked by clearing the exception and
2386 * returning NULL. If an object other than a tuple comes out of __bases__,
2387 * then again, the return value is NULL. So yes, these two situations
2388 * produce exactly the same results: NULL is returned and no error is set.
2389 *
2390 * If some exception other than AttributeError is raised, then NULL is also
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002391 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002392 * exception to be propagated along.
2393 *
2394 * Callers are expected to test for PyErr_Occurred() when the return value
2395 * is NULL to decide whether a valid exception should be propagated or not.
2396 * When there's no exception to propagate, it's customary for the caller to
2397 * set a TypeError.
2398 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002399static PyObject *
2400abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002401{
2402 static PyObject *__bases__ = NULL;
2403 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002404
2405 if (__bases__ == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +00002406 __bases__ = PyUnicode_InternFromString("__bases__");
Guido van Rossum823649d2001-03-21 18:40:58 +00002407 if (__bases__ == NULL)
Neil Schemenauer6b471292001-10-18 03:18:43 +00002408 return NULL;
Guido van Rossum823649d2001-03-21 18:40:58 +00002409 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002410 Py_ALLOW_RECURSION
Neil Schemenauer6b471292001-10-18 03:18:43 +00002411 bases = PyObject_GetAttr(cls, __bases__);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002412 Py_END_ALLOW_RECURSION
Barry Warsawf16951c2002-04-23 22:45:44 +00002413 if (bases == NULL) {
2414 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2415 PyErr_Clear();
Neil Schemenauer6b471292001-10-18 03:18:43 +00002416 return NULL;
Guido van Rossum823649d2001-03-21 18:40:58 +00002417 }
Barry Warsawf16951c2002-04-23 22:45:44 +00002418 if (!PyTuple_Check(bases)) {
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002419 Py_DECREF(bases);
Barry Warsawf16951c2002-04-23 22:45:44 +00002420 return NULL;
2421 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002422 return bases;
2423}
2424
2425
2426static int
2427abstract_issubclass(PyObject *derived, PyObject *cls)
2428{
2429 PyObject *bases;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002430 Py_ssize_t i, n;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002431 int r = 0;
2432
2433
Guido van Rossum823649d2001-03-21 18:40:58 +00002434 if (derived == cls)
2435 return 1;
2436
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002437 if (PyTuple_Check(cls)) {
2438 /* Not a general sequence -- that opens up the road to
2439 recursion and stack overflow. */
2440 n = PyTuple_GET_SIZE(cls);
2441 for (i = 0; i < n; i++) {
2442 if (derived == PyTuple_GET_ITEM(cls, i))
2443 return 1;
2444 }
2445 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002446 bases = abstract_get_bases(derived);
Barry Warsawf16951c2002-04-23 22:45:44 +00002447 if (bases == NULL) {
2448 if (PyErr_Occurred())
2449 return -1;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002450 return 0;
Barry Warsawf16951c2002-04-23 22:45:44 +00002451 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002452 n = PyTuple_GET_SIZE(bases);
2453 for (i = 0; i < n; i++) {
Neil Schemenauer6b471292001-10-18 03:18:43 +00002454 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002455 if (r != 0)
2456 break;
2457 }
2458
2459 Py_DECREF(bases);
2460
2461 return r;
2462}
2463
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002464static int
2465check_class(PyObject *cls, const char *error)
2466{
2467 PyObject *bases = abstract_get_bases(cls);
2468 if (bases == NULL) {
2469 /* Do not mask errors. */
2470 if (!PyErr_Occurred())
2471 PyErr_SetString(PyExc_TypeError, error);
2472 return 0;
2473 }
2474 Py_DECREF(bases);
2475 return -1;
2476}
2477
Brett Cannon4f653312004-03-20 22:52:14 +00002478static int
2479recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
Guido van Rossum823649d2001-03-21 18:40:58 +00002480{
2481 PyObject *icls;
2482 static PyObject *__class__ = NULL;
2483 int retval = 0;
2484
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002485 if (__class__ == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +00002486 __class__ = PyUnicode_InternFromString("__class__");
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002487 if (__class__ == NULL)
2488 return -1;
2489 }
2490
Guido van Rossum45aecf42006-03-15 04:58:47 +00002491 if (PyType_Check(cls)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002492 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002493 if (retval == 0) {
2494 PyObject *c = PyObject_GetAttr(inst, __class__);
2495 if (c == NULL) {
2496 PyErr_Clear();
2497 }
2498 else {
Guido van Rossume5b130b2003-02-12 03:36:05 +00002499 if (c != (PyObject *)(inst->ob_type) &&
2500 PyType_Check(c))
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002501 retval = PyType_IsSubtype(
2502 (PyTypeObject *)c,
2503 (PyTypeObject *)cls);
2504 Py_DECREF(c);
2505 }
2506 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002507 }
Guido van Rossum03290ec2001-10-07 20:54:12 +00002508 else if (PyTuple_Check(cls)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002509 Py_ssize_t i, n;
Guido van Rossum03290ec2001-10-07 20:54:12 +00002510
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002511 if (!recursion_depth) {
2512 PyErr_SetString(PyExc_RuntimeError,
2513 "nest level of tuple too deep");
2514 return -1;
2515 }
Brett Cannon4f653312004-03-20 22:52:14 +00002516
Guido van Rossum03290ec2001-10-07 20:54:12 +00002517 n = PyTuple_GET_SIZE(cls);
2518 for (i = 0; i < n; i++) {
Brett Cannon4f653312004-03-20 22:52:14 +00002519 retval = recursive_isinstance(
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002520 inst,
2521 PyTuple_GET_ITEM(cls, i),
2522 recursion_depth-1);
Guido van Rossum03290ec2001-10-07 20:54:12 +00002523 if (retval != 0)
2524 break;
2525 }
Guido van Rossum03290ec2001-10-07 20:54:12 +00002526 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002527 else {
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002528 if (!check_class(cls,
2529 "isinstance() arg 2 must be a class, type,"
2530 " or tuple of classes and types"))
Neil Schemenauer6b471292001-10-18 03:18:43 +00002531 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002532 icls = PyObject_GetAttr(inst, __class__);
Neil Schemenauer6b471292001-10-18 03:18:43 +00002533 if (icls == NULL) {
2534 PyErr_Clear();
2535 retval = 0;
Guido van Rossum823649d2001-03-21 18:40:58 +00002536 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002537 else {
2538 retval = abstract_issubclass(icls, cls);
2539 Py_DECREF(icls);
2540 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002541 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002542
Guido van Rossum823649d2001-03-21 18:40:58 +00002543 return retval;
2544}
2545
2546int
Brett Cannon4f653312004-03-20 22:52:14 +00002547PyObject_IsInstance(PyObject *inst, PyObject *cls)
2548{
Christian Heimesa156e092008-02-16 07:38:31 +00002549 static PyObject *name = NULL;
Guido van Rossumadee45e2007-05-25 17:55:52 +00002550 PyObject *t, *v, *tb;
Guido van Rossume7ba4952007-06-06 23:52:48 +00002551 PyObject *checker;
Guido van Rossumadee45e2007-05-25 17:55:52 +00002552 PyErr_Fetch(&t, &v, &tb);
Christian Heimesa156e092008-02-16 07:38:31 +00002553
2554 if (name == NULL) {
2555 name = PyUnicode_InternFromString("__instancecheck__");
2556 if (name == NULL)
2557 return -1;
2558 }
2559 checker = PyObject_GetAttr(cls, name);
Guido van Rossumadee45e2007-05-25 17:55:52 +00002560 PyErr_Restore(t, v, tb);
2561 if (checker != NULL) {
2562 PyObject *res;
2563 int ok = -1;
Guido van Rossum29478ef2007-05-29 00:39:44 +00002564 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2565 Py_DECREF(checker);
Guido van Rossumadee45e2007-05-25 17:55:52 +00002566 return ok;
Guido van Rossum29478ef2007-05-29 00:39:44 +00002567 }
Guido van Rossumadee45e2007-05-25 17:55:52 +00002568 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2569 Py_LeaveRecursiveCall();
2570 Py_DECREF(checker);
2571 if (res != NULL) {
2572 ok = PyObject_IsTrue(res);
2573 Py_DECREF(res);
2574 }
2575 return ok;
2576 }
2577 return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
Brett Cannon4f653312004-03-20 22:52:14 +00002578}
2579
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002580static int
Brett Cannon4f653312004-03-20 22:52:14 +00002581recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
Guido van Rossum823649d2001-03-21 18:40:58 +00002582{
2583 int retval;
2584
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002585 {
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002586 if (!check_class(derived,
2587 "issubclass() arg 1 must be a class"))
Neil Schemenauer6b471292001-10-18 03:18:43 +00002588 return -1;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002589
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002590 if (PyTuple_Check(cls)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002591 Py_ssize_t i;
2592 Py_ssize_t n = PyTuple_GET_SIZE(cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002593
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002594 if (!recursion_depth) {
2595 PyErr_SetString(PyExc_RuntimeError,
2596 "nest level of tuple too deep");
2597 return -1;
2598 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002599 for (i = 0; i < n; ++i) {
Brett Cannon4f653312004-03-20 22:52:14 +00002600 retval = recursive_issubclass(
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002601 derived,
2602 PyTuple_GET_ITEM(cls, i),
2603 recursion_depth-1);
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002604 if (retval != 0) {
2605 /* either found it, or got an error */
Walter Dörwald7e5c6a02002-12-12 19:14:08 +00002606 return retval;
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002607 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002608 }
Walter Dörwald7e5c6a02002-12-12 19:14:08 +00002609 return 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002610 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002611 else {
2612 if (!check_class(cls,
2613 "issubclass() arg 2 must be a class"
2614 " or tuple of classes"))
2615 return -1;
2616 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002617
2618 retval = abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002619 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002620
2621 return retval;
2622}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002623
Brett Cannon4f653312004-03-20 22:52:14 +00002624int
2625PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2626{
Christian Heimesa156e092008-02-16 07:38:31 +00002627 static PyObject *name = NULL;
Guido van Rossumadee45e2007-05-25 17:55:52 +00002628 PyObject *t, *v, *tb;
Guido van Rossume7ba4952007-06-06 23:52:48 +00002629 PyObject *checker;
Guido van Rossumadee45e2007-05-25 17:55:52 +00002630 PyErr_Fetch(&t, &v, &tb);
Christian Heimesa156e092008-02-16 07:38:31 +00002631
2632 if (name == NULL) {
2633 name = PyUnicode_InternFromString("__subclasscheck__");
2634 if (name == NULL)
2635 return -1;
2636 }
2637 checker = PyObject_GetAttr(cls, name);
Guido van Rossumadee45e2007-05-25 17:55:52 +00002638 PyErr_Restore(t, v, tb);
2639 if (checker != NULL) {
2640 PyObject *res;
2641 int ok = -1;
2642 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2643 return ok;
2644 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
2645 Py_LeaveRecursiveCall();
2646 Py_DECREF(checker);
2647 if (res != NULL) {
2648 ok = PyObject_IsTrue(res);
2649 Py_DECREF(res);
2650 }
2651 return ok;
2652 }
2653 return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
Brett Cannon4f653312004-03-20 22:52:14 +00002654}
2655
2656
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002657PyObject *
2658PyObject_GetIter(PyObject *o)
2659{
2660 PyTypeObject *t = o->ob_type;
2661 getiterfunc f = NULL;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002662 f = t->tp_iter;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002663 if (f == NULL) {
2664 if (PySequence_Check(o))
Guido van Rossum213c7a62001-04-23 14:08:49 +00002665 return PySeqIter_New(o);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666 return type_error("'%.200s' object is not iterable", o);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002667 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002668 else {
2669 PyObject *res = (*f)(o);
2670 if (res != NULL && !PyIter_Check(res)) {
2671 PyErr_Format(PyExc_TypeError,
2672 "iter() returned non-iterator "
2673 "of type '%.100s'",
2674 res->ob_type->tp_name);
2675 Py_DECREF(res);
2676 res = NULL;
2677 }
2678 return res;
2679 }
2680}
2681
Tim Petersf4848da2001-05-05 00:14:56 +00002682/* Return next item.
2683 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2684 * If the iteration terminates normally, return NULL and clear the
2685 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2686 * will be false.
Travis E. Oliphantfe9bed02007-10-12 23:27:53 +00002687 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002688 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002689PyObject *
2690PyIter_Next(PyObject *iter)
2691{
Tim Petersf4848da2001-05-05 00:14:56 +00002692 PyObject *result;
Raymond Hettinger8049dde2003-03-01 01:44:32 +00002693 assert(PyIter_Check(iter));
Tim Petersf4848da2001-05-05 00:14:56 +00002694 result = (*iter->ob_type->tp_iternext)(iter);
2695 if (result == NULL &&
2696 PyErr_Occurred() &&
2697 PyErr_ExceptionMatches(PyExc_StopIteration))
2698 PyErr_Clear();
2699 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002700}