blob: 2d4f2e1772d9a735465ec8110bb2fe8634b1d484 [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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000015 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
16 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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
Guido van Rossume15dee51995-07-18 14:12:02 +000030PyObject *
Fred Drake79912472000-07-09 04:06:11 +000031PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000032{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000033 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000034
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000035 if (o == NULL)
36 return null_error();
37 v = (PyObject *)o->ob_type;
38 Py_INCREF(v);
39 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000040}
41
Martin v. Löwis18e16552006-02-15 17:27:45 +000042Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000043PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000044{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000045 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 if (o == NULL) {
48 null_error();
49 return -1;
50 }
Guido van Rossume15dee51995-07-18 14:12:02 +000051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000052 m = o->ob_type->tp_as_sequence;
53 if (m && m->sq_length)
54 return m->sq_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000055
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000056 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000057}
58
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000059#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000060Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000061PyObject_Length(PyObject *o)
62{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000064}
65#define PyObject_Length PyObject_Size
66
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000067
Christian Heimes255f53b2007-12-08 15:33:56 +000068/* The length hint function returns a non-negative value from o.__len__()
69 or o.__length_hint__(). If those methods aren't found or return a negative
Raymond Hettingere8364232009-02-02 22:55:09 +000070 value, then the defaultvalue is returned. If one of the calls fails,
71 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000072*/
73
74Py_ssize_t
75_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
76{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000077 static PyObject *hintstrobj = NULL;
78 PyObject *ro, *hintmeth;
79 Py_ssize_t rv;
Christian Heimes255f53b2007-12-08 15:33:56 +000080
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000081 /* try o.__len__() */
82 rv = PyObject_Size(o);
83 if (rv >= 0)
84 return rv;
85 if (PyErr_Occurred()) {
86 if (!PyErr_ExceptionMatches(PyExc_TypeError))
87 return -1;
88 PyErr_Clear();
89 }
Christian Heimes255f53b2007-12-08 15:33:56 +000090
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 /* try o.__length_hint__() */
92 hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
93 if (hintmeth == NULL) {
94 if (PyErr_Occurred())
95 return -1;
96 else
97 return defaultvalue;
98 }
99 ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
100 Py_DECREF(hintmeth);
101 if (ro == NULL) {
102 if (!PyErr_ExceptionMatches(PyExc_TypeError))
103 return -1;
104 PyErr_Clear();
105 return defaultvalue;
106 }
107 rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
108 Py_DECREF(ro);
109 return rv;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000110}
111
Guido van Rossume15dee51995-07-18 14:12:02 +0000112PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000113PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000114{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000115 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000116
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 if (o == NULL || key == NULL)
118 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000119
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000120 m = o->ob_type->tp_as_mapping;
121 if (m && m->mp_subscript)
122 return m->mp_subscript(o, key);
Guido van Rossume15dee51995-07-18 14:12:02 +0000123
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000124 if (o->ob_type->tp_as_sequence) {
125 if (PyIndex_Check(key)) {
126 Py_ssize_t key_value;
127 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
128 if (key_value == -1 && PyErr_Occurred())
129 return NULL;
130 return PySequence_GetItem(o, key_value);
131 }
132 else if (o->ob_type->tp_as_sequence->sq_item)
133 return type_error("sequence index must "
134 "be integer, not '%.200s'", key);
135 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000136
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000137 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000138}
139
140int
Fred Drake79912472000-07-09 04:06:11 +0000141PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000142{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000144
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 if (o == NULL || key == NULL || value == NULL) {
146 null_error();
147 return -1;
148 }
149 m = o->ob_type->tp_as_mapping;
150 if (m && m->mp_ass_subscript)
151 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000152
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000153 if (o->ob_type->tp_as_sequence) {
154 if (PyIndex_Check(key)) {
155 Py_ssize_t key_value;
156 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
157 if (key_value == -1 && PyErr_Occurred())
158 return -1;
159 return PySequence_SetItem(o, key_value, value);
160 }
161 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
162 type_error("sequence index must be "
163 "integer, not '%.200s'", key);
164 return -1;
165 }
166 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 type_error("'%.200s' object does not support item assignment", o);
169 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000170}
171
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000172int
Fred Drake79912472000-07-09 04:06:11 +0000173PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000176
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 if (o == NULL || key == NULL) {
178 null_error();
179 return -1;
180 }
181 m = o->ob_type->tp_as_mapping;
182 if (m && m->mp_ass_subscript)
183 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000184
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000185 if (o->ob_type->tp_as_sequence) {
186 if (PyIndex_Check(key)) {
187 Py_ssize_t key_value;
188 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
189 if (key_value == -1 && PyErr_Occurred())
190 return -1;
191 return PySequence_DelItem(o, key_value);
192 }
193 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
194 type_error("sequence index must be "
195 "integer, not '%.200s'", key);
196 return -1;
197 }
198 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000199
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 type_error("'%.200s' object does not support item deletion", o);
201 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000202}
203
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000204int
205PyObject_DelItemString(PyObject *o, char *key)
206{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000207 PyObject *okey;
208 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 if (o == NULL || key == NULL) {
211 null_error();
212 return -1;
213 }
214 okey = PyUnicode_FromString(key);
215 if (okey == NULL)
216 return -1;
217 ret = PyObject_DelItem(o, okey);
218 Py_DECREF(okey);
219 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000220}
221
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000222/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000223 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000224 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000225int
226PyObject_AsCharBuffer(PyObject *obj,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000227 const char **buffer,
228 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000229{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 PyBufferProcs *pb;
231 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000232
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
234 null_error();
235 return -1;
236 }
237 pb = obj->ob_type->tp_as_buffer;
238 if (pb == NULL || pb->bf_getbuffer == NULL) {
239 PyErr_SetString(PyExc_TypeError,
240 "expected an object with the buffer interface");
241 return -1;
242 }
243 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000244
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 *buffer = view.buf;
246 *buffer_len = view.len;
247 if (pb->bf_releasebuffer != NULL)
248 (*pb->bf_releasebuffer)(obj, &view);
249 Py_XDECREF(view.obj);
250 return 0;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000251}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000252
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000253int
254PyObject_CheckReadBuffer(PyObject *obj)
255{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
257 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000258
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 if (pb == NULL ||
260 pb->bf_getbuffer == NULL)
261 return 0;
262 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
263 PyErr_Clear();
264 return 0;
265 }
266 PyBuffer_Release(&view);
267 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268}
269
270int PyObject_AsReadBuffer(PyObject *obj,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000271 const void **buffer,
272 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 PyBufferProcs *pb;
275 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000276
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000277 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
278 null_error();
279 return -1;
280 }
281 pb = obj->ob_type->tp_as_buffer;
282 if (pb == NULL ||
283 pb->bf_getbuffer == NULL) {
284 PyErr_SetString(PyExc_TypeError,
285 "expected an object with a buffer interface");
286 return -1;
287 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000288
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000289 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 *buffer = view.buf;
292 *buffer_len = view.len;
293 if (pb->bf_releasebuffer != NULL)
294 (*pb->bf_releasebuffer)(obj, &view);
295 Py_XDECREF(view.obj);
296 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297}
298
299int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000300 void **buffer,
301 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 PyBufferProcs *pb;
304 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000306 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
307 null_error();
308 return -1;
309 }
310 pb = obj->ob_type->tp_as_buffer;
311 if (pb == NULL ||
312 pb->bf_getbuffer == NULL ||
313 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
314 PyErr_SetString(PyExc_TypeError,
315 "expected an object with a writable buffer interface");
316 return -1;
317 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000318
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000319 *buffer = view.buf;
320 *buffer_len = view.len;
321 if (pb->bf_releasebuffer != NULL)
322 (*pb->bf_releasebuffer)(obj, &view);
323 Py_XDECREF(view.obj);
324 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000325}
326
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000327/* Buffer C-API for Python 3.0 */
328
329int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000330PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000331{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 if (!PyObject_CheckBuffer(obj)) {
333 PyErr_Format(PyExc_TypeError,
334 "'%100s' does not support the buffer interface",
335 Py_TYPE(obj)->tp_name);
336 return -1;
337 }
338 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000339}
340
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000341static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000342_IsFortranContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000343{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000344 Py_ssize_t sd, dim;
345 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 if (view->ndim == 0) return 1;
348 if (view->strides == NULL) return (view->ndim == 1);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350 sd = view->itemsize;
351 if (view->ndim == 1) return (view->shape[0] == 1 ||
352 sd == view->strides[0]);
353 for (i=0; i<view->ndim; i++) {
354 dim = view->shape[i];
355 if (dim == 0) return 1;
356 if (view->strides[i] != sd) return 0;
357 sd *= dim;
358 }
359 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000360}
361
362static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000363_IsCContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000364{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000365 Py_ssize_t sd, dim;
366 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000367
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 if (view->ndim == 0) return 1;
369 if (view->strides == NULL) return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000370
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 sd = view->itemsize;
372 if (view->ndim == 1) return (view->shape[0] == 1 ||
373 sd == view->strides[0]);
374 for (i=view->ndim-1; i>=0; 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
383int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000384PyBuffer_IsContiguous(Py_buffer *view, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000385{
386
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000388
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000389 if (fort == 'C')
390 return _IsCContiguous(view);
391 else if (fort == 'F')
392 return _IsFortranContiguous(view);
393 else if (fort == 'A')
394 return (_IsCContiguous(view) || _IsFortranContiguous(view));
395 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000396}
397
398
Guido van Rossum98297ee2007-11-06 21:34:58 +0000399void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000400PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000401{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000402 char* pointer;
403 int i;
404 pointer = (char *)view->buf;
405 for (i = 0; i < view->ndim; i++) {
406 pointer += view->strides[i]*indices[i];
407 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
408 pointer = *((char**)pointer) + view->suboffsets[i];
409 }
410 }
411 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000412}
413
414
Guido van Rossum98297ee2007-11-06 21:34:58 +0000415void
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000416_Py_add_one_to_index_F(int nd, Py_ssize_t *index, const Py_ssize_t *shape)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000417{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000420 for (k=0; k<nd; k++) {
421 if (index[k] < shape[k]-1) {
422 index[k]++;
423 break;
424 }
425 else {
426 index[k] = 0;
427 }
428 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000429}
430
Guido van Rossum98297ee2007-11-06 21:34:58 +0000431void
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000432_Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000433{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 for (k=nd-1; k>=0; k--) {
437 if (index[k] < shape[k]-1) {
438 index[k]++;
439 break;
440 }
441 else {
442 index[k] = 0;
443 }
444 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000445}
446
447 /* view is not checked for consistency in either of these. It is
Guido van Rossum98297ee2007-11-06 21:34:58 +0000448 assumed that the size of the buffer is view->len in
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000449 view->len / view->itemsize elements.
450 */
451
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000453PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 int k;
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000456 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000457 Py_ssize_t *indices, elements;
458 char *dest, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000459
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 if (len > view->len) {
461 len = view->len;
462 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 if (PyBuffer_IsContiguous(view, fort)) {
465 /* simplest copy is all that is needed */
466 memcpy(buf, view->buf, len);
467 return 0;
468 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000469
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 /* XXX(nnorwitz): need to check for overflow! */
473 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
474 if (indices == NULL) {
475 PyErr_NoMemory();
476 return -1;
477 }
478 for (k=0; k<view->ndim;k++) {
479 indices[k] = 0;
480 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000481
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 if (fort == 'F') {
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000483 addone = _Py_add_one_to_index_F;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000484 }
485 else {
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000486 addone = _Py_add_one_to_index_C;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 }
488 dest = buf;
489 /* XXX : This is not going to be the fastest code in the world
490 several optimizations are possible.
491 */
492 elements = len / view->itemsize;
493 while (elements--) {
494 addone(view->ndim, indices, view->shape);
495 ptr = PyBuffer_GetPointer(view, indices);
496 memcpy(dest, ptr, view->itemsize);
497 dest += view->itemsize;
498 }
499 PyMem_Free(indices);
500 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000501}
502
503int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000504PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000505{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000506 int k;
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000507 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000508 Py_ssize_t *indices, elements;
509 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000510
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 if (len > view->len) {
512 len = view->len;
513 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000514
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 if (PyBuffer_IsContiguous(view, fort)) {
516 /* simplest copy is all that is needed */
517 memcpy(view->buf, buf, len);
518 return 0;
519 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000520
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000522
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 /* XXX(nnorwitz): need to check for overflow! */
524 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
525 if (indices == NULL) {
526 PyErr_NoMemory();
527 return -1;
528 }
529 for (k=0; k<view->ndim;k++) {
530 indices[k] = 0;
531 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000532
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000533 if (fort == 'F') {
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000534 addone = _Py_add_one_to_index_F;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000535 }
536 else {
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000537 addone = _Py_add_one_to_index_C;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000538 }
539 src = buf;
540 /* XXX : This is not going to be the fastest code in the world
541 several optimizations are possible.
542 */
543 elements = len / view->itemsize;
544 while (elements--) {
545 addone(view->ndim, indices, view->shape);
546 ptr = PyBuffer_GetPointer(view, indices);
547 memcpy(ptr, src, view->itemsize);
548 src += view->itemsize;
549 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000550
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 PyMem_Free(indices);
552 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000553}
554
Guido van Rossum98297ee2007-11-06 21:34:58 +0000555int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000556{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000557 Py_buffer view_dest, view_src;
558 int k;
559 Py_ssize_t *indices, elements;
560 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 if (!PyObject_CheckBuffer(dest) ||
563 !PyObject_CheckBuffer(src)) {
564 PyErr_SetString(PyExc_TypeError,
565 "both destination and source must have the "\
566 "buffer interface");
567 return -1;
568 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
571 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
572 PyBuffer_Release(&view_dest);
573 return -1;
574 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000575
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 if (view_dest.len < view_src.len) {
577 PyErr_SetString(PyExc_BufferError,
578 "destination is too small to receive data from source");
579 PyBuffer_Release(&view_dest);
580 PyBuffer_Release(&view_src);
581 return -1;
582 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000583
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000584 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
585 PyBuffer_IsContiguous(&view_src, 'C')) ||
586 (PyBuffer_IsContiguous(&view_dest, 'F') &&
587 PyBuffer_IsContiguous(&view_src, 'F'))) {
588 /* simplest copy is all that is needed */
589 memcpy(view_dest.buf, view_src.buf, view_src.len);
590 PyBuffer_Release(&view_dest);
591 PyBuffer_Release(&view_src);
592 return 0;
593 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 /* XXX(nnorwitz): need to check for overflow! */
598 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
599 if (indices == NULL) {
600 PyErr_NoMemory();
601 PyBuffer_Release(&view_dest);
602 PyBuffer_Release(&view_src);
603 return -1;
604 }
605 for (k=0; k<view_src.ndim;k++) {
606 indices[k] = 0;
607 }
608 elements = 1;
609 for (k=0; k<view_src.ndim; k++) {
610 /* XXX(nnorwitz): can this overflow? */
611 elements *= view_src.shape[k];
612 }
613 while (elements--) {
Antoine Pitroub83df8f2010-09-01 13:01:35 +0000614 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000615 dptr = PyBuffer_GetPointer(&view_dest, indices);
616 sptr = PyBuffer_GetPointer(&view_src, indices);
617 memcpy(dptr, sptr, view_src.itemsize);
618 }
619 PyMem_Free(indices);
620 PyBuffer_Release(&view_dest);
621 PyBuffer_Release(&view_src);
622 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000623}
624
625void
626PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 Py_ssize_t *strides, int itemsize,
628 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000629{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 int k;
631 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000632
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000633 sd = itemsize;
634 if (fort == 'F') {
635 for (k=0; k<nd; k++) {
636 strides[k] = sd;
637 sd *= shape[k];
638 }
639 }
640 else {
641 for (k=nd-1; k>=0; k--) {
642 strides[k] = sd;
643 sd *= shape[k];
644 }
645 }
646 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000647}
648
649int
Martin v. Löwis423be952008-08-13 15:53:07 +0000650PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000651 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000652{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000653 if (view == NULL) return 0;
654 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
655 (readonly == 1)) {
656 PyErr_SetString(PyExc_BufferError,
657 "Object is not writable.");
658 return -1;
659 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000660
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000661 view->obj = obj;
662 if (obj)
663 Py_INCREF(obj);
664 view->buf = buf;
665 view->len = len;
666 view->readonly = readonly;
667 view->itemsize = 1;
668 view->format = NULL;
669 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
670 view->format = "B";
671 view->ndim = 1;
672 view->shape = NULL;
673 if ((flags & PyBUF_ND) == PyBUF_ND)
674 view->shape = &(view->len);
675 view->strides = NULL;
676 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
677 view->strides = &(view->itemsize);
678 view->suboffsets = NULL;
679 view->internal = NULL;
680 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000681}
682
Martin v. Löwis423be952008-08-13 15:53:07 +0000683void
684PyBuffer_Release(Py_buffer *view)
685{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000686 PyObject *obj = view->obj;
687 if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
688 Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
689 Py_XDECREF(obj);
690 view->obj = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000691}
692
Eric Smith8fd3eba2008-02-17 19:48:00 +0000693PyObject *
694PyObject_Format(PyObject *obj, PyObject *format_spec)
695{
696 static PyObject * str__format__ = NULL;
697 PyObject *meth;
698 PyObject *empty = NULL;
699 PyObject *result = NULL;
700
701 /* Initialize cached value */
702 if (str__format__ == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000703 /* Initialize static variable needed by _PyType_Lookup */
704 str__format__ = PyUnicode_FromString("__format__");
705 if (str__format__ == NULL)
706 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000707 }
708
709 /* If no format_spec is provided, use an empty string */
710 if (format_spec == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 empty = PyUnicode_FromUnicode(NULL, 0);
712 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000713 }
714
715 /* Make sure the type is initialized. float gets initialized late */
716 if (Py_TYPE(obj)->tp_dict == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000717 if (PyType_Ready(Py_TYPE(obj)) < 0)
718 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000719
720 /* Find the (unbound!) __format__ method (a borrowed reference) */
721 meth = _PyType_Lookup(Py_TYPE(obj), str__format__);
722 if (meth == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000723 PyErr_Format(PyExc_TypeError,
724 "Type %.100s doesn't define __format__",
725 Py_TYPE(obj)->tp_name);
726 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000727 }
728
729 /* And call it, binding it to the value */
730 result = PyObject_CallFunctionObjArgs(meth, obj, format_spec, NULL);
731
732 if (result && !PyUnicode_Check(result)) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000733 PyErr_SetString(PyExc_TypeError,
734 "__format__ method did not return string");
735 Py_DECREF(result);
736 result = NULL;
737 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000738 }
739
740done:
741 Py_XDECREF(empty);
742 return result;
743}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000744/* Operations on numbers */
745
746int
Fred Drake79912472000-07-09 04:06:11 +0000747PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000748{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000749 return o && o->ob_type->tp_as_number &&
750 (o->ob_type->tp_as_number->nb_int ||
751 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000752}
753
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000754/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000755
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000756#define NB_SLOT(x) offsetof(PyNumberMethods, x)
757#define NB_BINOP(nb_methods, slot) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000758 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000759#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000760 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000761
762/*
763 Calling scheme used for binary operations:
764
Neal Norwitz4886cc32006-08-21 17:06:07 +0000765 Order operations are tried until either a valid result or error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000767
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000768 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
769 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000770 */
771
772static PyObject *
773binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000774{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 PyObject *x;
776 binaryfunc slotv = NULL;
777 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000778
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000779 if (v->ob_type->tp_as_number != NULL)
780 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
781 if (w->ob_type != v->ob_type &&
782 w->ob_type->tp_as_number != NULL) {
783 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
784 if (slotw == slotv)
785 slotw = NULL;
786 }
787 if (slotv) {
788 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
789 x = slotw(v, w);
790 if (x != Py_NotImplemented)
791 return x;
792 Py_DECREF(x); /* can't do it */
793 slotw = NULL;
794 }
795 x = slotv(v, w);
796 if (x != Py_NotImplemented)
797 return x;
798 Py_DECREF(x); /* can't do it */
799 }
800 if (slotw) {
801 x = slotw(v, w);
802 if (x != Py_NotImplemented)
803 return x;
804 Py_DECREF(x); /* can't do it */
805 }
806 Py_INCREF(Py_NotImplemented);
807 return Py_NotImplemented;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000808}
Guido van Rossum77660912002-04-16 16:32:50 +0000809
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000810static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000811binop_type_error(PyObject *v, PyObject *w, const char *op_name)
812{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 PyErr_Format(PyExc_TypeError,
814 "unsupported operand type(s) for %.100s: "
815 "'%.100s' and '%.100s'",
816 op_name,
817 v->ob_type->tp_name,
818 w->ob_type->tp_name);
819 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000820}
821
822static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000823binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
824{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 PyObject *result = binary_op1(v, w, op_slot);
826 if (result == Py_NotImplemented) {
827 Py_DECREF(result);
828 return binop_type_error(v, w, op_name);
829 }
830 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000831}
832
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000833
834/*
835 Calling scheme used for ternary operations:
836
Neal Norwitz4886cc32006-08-21 17:06:07 +0000837 Order operations are tried until either a valid result or error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000839 */
840
841static PyObject *
842ternary_op(PyObject *v,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 PyObject *w,
844 PyObject *z,
845 const int op_slot,
846 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000847{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000848 PyNumberMethods *mv, *mw, *mz;
849 PyObject *x = NULL;
850 ternaryfunc slotv = NULL;
851 ternaryfunc slotw = NULL;
852 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 mv = v->ob_type->tp_as_number;
855 mw = w->ob_type->tp_as_number;
856 if (mv != NULL)
857 slotv = NB_TERNOP(mv, op_slot);
858 if (w->ob_type != v->ob_type &&
859 mw != NULL) {
860 slotw = NB_TERNOP(mw, op_slot);
861 if (slotw == slotv)
862 slotw = NULL;
863 }
864 if (slotv) {
865 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
866 x = slotw(v, w, z);
867 if (x != Py_NotImplemented)
868 return x;
869 Py_DECREF(x); /* can't do it */
870 slotw = NULL;
871 }
872 x = slotv(v, w, z);
873 if (x != Py_NotImplemented)
874 return x;
875 Py_DECREF(x); /* can't do it */
876 }
877 if (slotw) {
878 x = slotw(v, w, z);
879 if (x != Py_NotImplemented)
880 return x;
881 Py_DECREF(x); /* can't do it */
882 }
883 mz = z->ob_type->tp_as_number;
884 if (mz != NULL) {
885 slotz = NB_TERNOP(mz, op_slot);
886 if (slotz == slotv || slotz == slotw)
887 slotz = NULL;
888 if (slotz) {
889 x = slotz(v, w, z);
890 if (x != Py_NotImplemented)
891 return x;
892 Py_DECREF(x); /* can't do it */
893 }
894 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 if (z == Py_None)
897 PyErr_Format(
898 PyExc_TypeError,
899 "unsupported operand type(s) for ** or pow(): "
900 "'%.100s' and '%.100s'",
901 v->ob_type->tp_name,
902 w->ob_type->tp_name);
903 else
904 PyErr_Format(
905 PyExc_TypeError,
906 "unsupported operand type(s) for pow(): "
907 "'%.100s', '%.100s', '%.100s'",
908 v->ob_type->tp_name,
909 w->ob_type->tp_name,
910 z->ob_type->tp_name);
911 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000912}
913
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000914#define BINARY_FUNC(func, op, op_name) \
915 PyObject * \
916 func(PyObject *v, PyObject *w) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000918 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000919
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000920BINARY_FUNC(PyNumber_Or, nb_or, "|")
921BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
922BINARY_FUNC(PyNumber_And, nb_and, "&")
923BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
924BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
925BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000926BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000927
928PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000929PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000930{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000931 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
932 if (result == Py_NotImplemented) {
933 PySequenceMethods *m = v->ob_type->tp_as_sequence;
934 Py_DECREF(result);
935 if (m && m->sq_concat) {
936 return (*m->sq_concat)(v, w);
937 }
938 result = binop_type_error(v, w, "+");
939 }
940 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000941}
942
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000943static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000944sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000945{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 Py_ssize_t count;
947 if (PyIndex_Check(n)) {
948 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
949 if (count == -1 && PyErr_Occurred())
950 return NULL;
951 }
952 else {
953 return type_error("can't multiply sequence by "
954 "non-int of type '%.200s'", n);
955 }
956 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000957}
958
959PyObject *
960PyNumber_Multiply(PyObject *v, PyObject *w)
961{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000962 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
963 if (result == Py_NotImplemented) {
964 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
965 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
966 Py_DECREF(result);
967 if (mv && mv->sq_repeat) {
968 return sequence_repeat(mv->sq_repeat, v, w);
969 }
970 else if (mw && mw->sq_repeat) {
971 return sequence_repeat(mw->sq_repeat, w, v);
972 }
973 result = binop_type_error(v, w, "*");
974 }
975 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000976}
977
Guido van Rossume15dee51995-07-18 14:12:02 +0000978PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000979PyNumber_FloorDivide(PyObject *v, PyObject *w)
980{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000981 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000982}
983
984PyObject *
985PyNumber_TrueDivide(PyObject *v, PyObject *w)
986{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000987 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +0000988}
989
990PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000991PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000992{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000993 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000994}
995
996PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000997PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000998{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000999 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +00001000}
1001
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001002/* Binary in-place operators */
1003
1004/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001005 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001006
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001007 - If the left hand object has the appropriate struct members, and
1008 they are filled, call the appropriate function and return the
1009 result. No coercion is done on the arguments; the left-hand object
1010 is the one the operation is performed on, and it's up to the
1011 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001012
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001013 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001014 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001015
1016 */
1017
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001018static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001019binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001020{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001021 PyNumberMethods *mv = v->ob_type->tp_as_number;
1022 if (mv != NULL) {
1023 binaryfunc slot = NB_BINOP(mv, iop_slot);
1024 if (slot) {
1025 PyObject *x = (slot)(v, w);
1026 if (x != Py_NotImplemented) {
1027 return x;
1028 }
1029 Py_DECREF(x);
1030 }
1031 }
1032 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001033}
1034
1035static PyObject *
1036binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001037 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001038{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1040 if (result == Py_NotImplemented) {
1041 Py_DECREF(result);
1042 return binop_type_error(v, w, op_name);
1043 }
1044 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001045}
1046
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001047#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001048 PyObject * \
1049 func(PyObject *v, PyObject *w) { \
1050 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1051 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001052
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001053INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1054INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1055INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1056INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1057INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1058INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001059
1060PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001061PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1062{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001063 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1064 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001065}
1066
1067PyObject *
1068PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1069{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001070 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1071 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001072}
1073
1074PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001075PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1076{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001077 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1078 NB_SLOT(nb_add));
1079 if (result == Py_NotImplemented) {
1080 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1081 Py_DECREF(result);
1082 if (m != NULL) {
1083 binaryfunc f = NULL;
1084 f = m->sq_inplace_concat;
1085 if (f == NULL)
1086 f = m->sq_concat;
1087 if (f != NULL)
1088 return (*f)(v, w);
1089 }
1090 result = binop_type_error(v, w, "+=");
1091 }
1092 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001093}
1094
1095PyObject *
1096PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1097{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001098 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1099 NB_SLOT(nb_multiply));
1100 if (result == Py_NotImplemented) {
1101 ssizeargfunc f = NULL;
1102 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1103 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1104 Py_DECREF(result);
1105 if (mv != NULL) {
1106 f = mv->sq_inplace_repeat;
1107 if (f == NULL)
1108 f = mv->sq_repeat;
1109 if (f != NULL)
1110 return sequence_repeat(f, v, w);
1111 }
1112 else if (mw != NULL) {
1113 /* Note that the right hand operand should not be
1114 * mutated in this case so sq_inplace_repeat is not
1115 * used. */
1116 if (mw->sq_repeat)
1117 return sequence_repeat(mw->sq_repeat, w, v);
1118 }
1119 result = binop_type_error(v, w, "*=");
1120 }
1121 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001122}
1123
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001124PyObject *
1125PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1126{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001127 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1128 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001129}
1130
1131PyObject *
1132PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1133{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001134 if (v->ob_type->tp_as_number &&
1135 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1136 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1137 }
1138 else {
1139 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1140 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001141}
1142
1143
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001144/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001145
1146PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001147PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001148{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001149 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001150
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001151 if (o == NULL)
1152 return null_error();
1153 m = o->ob_type->tp_as_number;
1154 if (m && m->nb_negative)
1155 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001156
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001157 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001158}
1159
1160PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001161PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001163 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001164
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 if (o == NULL)
1166 return null_error();
1167 m = o->ob_type->tp_as_number;
1168 if (m && m->nb_positive)
1169 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001170
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001171 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001172}
1173
1174PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001175PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001176{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001177 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001178
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001179 if (o == NULL)
1180 return null_error();
1181 m = o->ob_type->tp_as_number;
1182 if (m && m->nb_invert)
1183 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001184
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001185 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001186}
1187
1188PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001189PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001190{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001191 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001193 if (o == NULL)
1194 return null_error();
1195 m = o->ob_type->tp_as_number;
1196 if (m && m->nb_absolute)
1197 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001199 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001200}
1201
Guido van Rossum98297ee2007-11-06 21:34:58 +00001202/* Return a Python Int or Long from the object item
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001203 Raise TypeError if the result is not an int-or-long
Guido van Rossum98297ee2007-11-06 21:34:58 +00001204 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001205*/
1206PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001207PyNumber_Index(PyObject *item)
1208{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001209 PyObject *result = NULL;
1210 if (item == NULL)
1211 return null_error();
1212 if (PyLong_Check(item)) {
1213 Py_INCREF(item);
1214 return item;
1215 }
1216 if (PyIndex_Check(item)) {
1217 result = item->ob_type->tp_as_number->nb_index(item);
1218 if (result && !PyLong_Check(result)) {
1219 PyErr_Format(PyExc_TypeError,
1220 "__index__ returned non-int "
1221 "(type %.200s)",
1222 result->ob_type->tp_name);
1223 Py_DECREF(result);
1224 return NULL;
1225 }
1226 }
1227 else {
1228 PyErr_Format(PyExc_TypeError,
1229 "'%.200s' object cannot be interpreted "
1230 "as an integer", item->ob_type->tp_name);
1231 }
1232 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001233}
1234
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001235/* Return an error on Overflow only if err is not NULL*/
1236
1237Py_ssize_t
1238PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001240 Py_ssize_t result;
1241 PyObject *runerr;
1242 PyObject *value = PyNumber_Index(item);
1243 if (value == NULL)
1244 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001246 /* We're done if PyLong_AsSsize_t() returns without error. */
1247 result = PyLong_AsSsize_t(value);
1248 if (result != -1 || !(runerr = PyErr_Occurred()))
1249 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001250
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001251 /* Error handling code -- only manage OverflowError differently */
1252 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1253 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001255 PyErr_Clear();
1256 /* If no error-handling desired then the default clipping
1257 is sufficient.
1258 */
1259 if (!err) {
1260 assert(PyLong_Check(value));
1261 /* Whether or not it is less than or equal to
1262 zero is determined by the sign of ob_size
1263 */
1264 if (_PyLong_Sign(value) < 0)
1265 result = PY_SSIZE_T_MIN;
1266 else
1267 result = PY_SSIZE_T_MAX;
1268 }
1269 else {
1270 /* Otherwise replace the error with caller's error object. */
1271 PyErr_Format(err,
1272 "cannot fit '%.200s' into an index-sized integer",
1273 item->ob_type->tp_name);
1274 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001275
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001276 finish:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001277 Py_DECREF(value);
1278 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001279}
1280
1281
Christian Heimes15ebc882008-02-04 18:48:49 +00001282PyObject *
1283_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
1284{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001285 static PyObject *int_name = NULL;
1286 if (int_name == NULL) {
1287 int_name = PyUnicode_InternFromString("__int__");
1288 if (int_name == NULL)
1289 return NULL;
1290 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001291
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001292 if (integral && !PyLong_Check(integral)) {
1293 /* Don't go through tp_as_number->nb_int to avoid
1294 hitting the classic class fallback to __trunc__. */
1295 PyObject *int_func = PyObject_GetAttr(integral, int_name);
1296 if (int_func == NULL) {
1297 PyErr_Clear(); /* Raise a different error. */
1298 goto non_integral_error;
1299 }
1300 Py_DECREF(integral);
1301 integral = PyEval_CallObject(int_func, NULL);
1302 Py_DECREF(int_func);
1303 if (integral && !PyLong_Check(integral)) {
1304 goto non_integral_error;
1305 }
1306 }
1307 return integral;
Christian Heimes15ebc882008-02-04 18:48:49 +00001308
1309non_integral_error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001310 PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
1311 Py_DECREF(integral);
1312 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001313}
1314
1315
Guido van Rossum9e896b32000-04-05 20:11:21 +00001316/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001317static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001318long_from_string(const char *s, Py_ssize_t len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001319{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001320 char *end;
1321 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001322
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001323 x = PyLong_FromString((char*)s, &end, 10);
1324 if (x == NULL)
1325 return NULL;
1326 if (end != s + len) {
1327 PyErr_SetString(PyExc_ValueError,
1328 "null byte in argument for int()");
1329 Py_DECREF(x);
1330 return NULL;
1331 }
1332 return x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001333}
1334
Guido van Rossume15dee51995-07-18 14:12:02 +00001335PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001336PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001337{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001338 PyNumberMethods *m;
1339 static PyObject *trunc_name = NULL;
1340 PyObject *trunc_func;
1341 const char *buffer;
1342 Py_ssize_t buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +00001343
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001344 if (trunc_name == NULL) {
1345 trunc_name = PyUnicode_InternFromString("__trunc__");
1346 if (trunc_name == NULL)
1347 return NULL;
1348 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001350 if (o == NULL)
1351 return null_error();
1352 if (PyLong_CheckExact(o)) {
1353 Py_INCREF(o);
1354 return o;
1355 }
1356 m = o->ob_type->tp_as_number;
1357 if (m && m->nb_int) { /* This should include subclasses of int */
1358 PyObject *res = m->nb_int(o);
1359 if (res && !PyLong_Check(res)) {
1360 PyErr_Format(PyExc_TypeError,
1361 "__int__ returned non-int (type %.200s)",
1362 res->ob_type->tp_name);
1363 Py_DECREF(res);
1364 return NULL;
1365 }
1366 return res;
1367 }
1368 if (PyLong_Check(o)) /* An int subclass without nb_int */
1369 return _PyLong_Copy((PyLongObject *)o);
1370 trunc_func = PyObject_GetAttr(o, trunc_name);
1371 if (trunc_func) {
1372 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
1373 PyObject *int_instance;
1374 Py_DECREF(trunc_func);
1375 /* __trunc__ is specified to return an Integral type,
1376 but long() needs to return a long. */
1377 int_instance = _PyNumber_ConvertIntegralToInt(
1378 truncated,
1379 "__trunc__ returned non-Integral (type %.200s)");
1380 return int_instance;
1381 }
1382 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
Christian Heimes15ebc882008-02-04 18:48:49 +00001383
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001384 if (PyBytes_Check(o))
1385 /* need to do extra error checking that PyLong_FromString()
1386 * doesn't do. In particular long('9.5') must raise an
1387 * exception, not truncate the float.
1388 */
1389 return long_from_string(PyBytes_AS_STRING(o),
1390 PyBytes_GET_SIZE(o));
1391 if (PyUnicode_Check(o))
1392 /* The above check is done in PyLong_FromUnicode(). */
1393 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
1394 PyUnicode_GET_SIZE(o),
1395 10);
1396 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
1397 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +00001398
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001399 return type_error("int() argument must be a string or a "
1400 "number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001401}
1402
1403PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001404PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001405{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001406 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001407
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001408 if (o == NULL)
1409 return null_error();
1410 m = o->ob_type->tp_as_number;
1411 if (m && m->nb_float) { /* This should include subclasses of float */
1412 PyObject *res = m->nb_float(o);
1413 if (res && !PyFloat_Check(res)) {
1414 PyErr_Format(PyExc_TypeError,
1415 "__float__ returned non-float (type %.200s)",
1416 res->ob_type->tp_name);
1417 Py_DECREF(res);
1418 return NULL;
1419 }
1420 return res;
1421 }
1422 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
1423 PyFloatObject *po = (PyFloatObject *)o;
1424 return PyFloat_FromDouble(po->ob_fval);
1425 }
1426 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001427}
1428
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001429
1430PyObject *
1431PyNumber_ToBase(PyObject *n, int base)
1432{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001433 PyObject *res = NULL;
1434 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001436 if (!index)
1437 return NULL;
1438 if (PyLong_Check(index))
1439 res = _PyLong_Format(index, base);
1440 else
1441 /* It should not be possible to get here, as
1442 PyNumber_Index already has a check for the same
1443 condition */
1444 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
1445 "int or long");
1446 Py_DECREF(index);
1447 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001448}
1449
1450
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001451/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001452
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001453int
Fred Drake79912472000-07-09 04:06:11 +00001454PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001455{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001456 if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
1457 return 0;
1458 return s != NULL && s->ob_type->tp_as_sequence &&
1459 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001460}
1461
Martin v. Löwis18e16552006-02-15 17:27:45 +00001462Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001463PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001464{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001465 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001466
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001467 if (s == NULL) {
1468 null_error();
1469 return -1;
1470 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001472 m = s->ob_type->tp_as_sequence;
1473 if (m && m->sq_length)
1474 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001476 type_error("object of type '%.200s' has no len()", s);
1477 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001478}
1479
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001480#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001481Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001482PySequence_Length(PyObject *s)
1483{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001484 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001485}
1486#define PySequence_Length PySequence_Size
1487
Guido van Rossume15dee51995-07-18 14:12:02 +00001488PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001489PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001490{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001491 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001492
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001493 if (s == NULL || o == NULL)
1494 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001495
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001496 m = s->ob_type->tp_as_sequence;
1497 if (m && m->sq_concat)
1498 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001499
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001500 /* Instances of user classes defining an __add__() method only
1501 have an nb_add slot, not an sq_concat slot. So we fall back
1502 to nb_add if both arguments appear to be sequences. */
1503 if (PySequence_Check(s) && PySequence_Check(o)) {
1504 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1505 if (result != Py_NotImplemented)
1506 return result;
1507 Py_DECREF(result);
1508 }
1509 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001510}
1511
1512PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001513PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001514{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001515 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001516
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001517 if (o == NULL)
1518 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001520 m = o->ob_type->tp_as_sequence;
1521 if (m && m->sq_repeat)
1522 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001523
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001524 /* Instances of user classes defining a __mul__() method only
1525 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1526 to nb_multiply if o appears to be a sequence. */
1527 if (PySequence_Check(o)) {
1528 PyObject *n, *result;
1529 n = PyLong_FromSsize_t(count);
1530 if (n == NULL)
1531 return NULL;
1532 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1533 Py_DECREF(n);
1534 if (result != Py_NotImplemented)
1535 return result;
1536 Py_DECREF(result);
1537 }
1538 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001539}
1540
1541PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001542PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1543{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001544 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001545
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001546 if (s == NULL || o == NULL)
1547 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001549 m = s->ob_type->tp_as_sequence;
1550 if (m && m->sq_inplace_concat)
1551 return m->sq_inplace_concat(s, o);
1552 if (m && m->sq_concat)
1553 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001554
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001555 if (PySequence_Check(s) && PySequence_Check(o)) {
1556 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1557 NB_SLOT(nb_add));
1558 if (result != Py_NotImplemented)
1559 return result;
1560 Py_DECREF(result);
1561 }
1562 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001563}
1564
1565PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001566PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001567{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001568 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001570 if (o == NULL)
1571 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001572
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001573 m = o->ob_type->tp_as_sequence;
1574 if (m && m->sq_inplace_repeat)
1575 return m->sq_inplace_repeat(o, count);
1576 if (m && m->sq_repeat)
1577 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001578
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001579 if (PySequence_Check(o)) {
1580 PyObject *n, *result;
1581 n = PyLong_FromSsize_t(count);
1582 if (n == NULL)
1583 return NULL;
1584 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1585 NB_SLOT(nb_multiply));
1586 Py_DECREF(n);
1587 if (result != Py_NotImplemented)
1588 return result;
1589 Py_DECREF(result);
1590 }
1591 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001592}
1593
1594PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001595PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001596{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001597 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001599 if (s == NULL)
1600 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001601
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001602 m = s->ob_type->tp_as_sequence;
1603 if (m && m->sq_item) {
1604 if (i < 0) {
1605 if (m->sq_length) {
1606 Py_ssize_t l = (*m->sq_length)(s);
1607 if (l < 0)
1608 return NULL;
1609 i += l;
1610 }
1611 }
1612 return m->sq_item(s, i);
1613 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001614
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001615 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001616}
1617
1618PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001619PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001620{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001621 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001622
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001623 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001624
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001625 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson8042f4a2010-09-11 16:03:33 +00001626 if (mp && mp->mp_subscript) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001627 PyObject *res;
1628 PyObject *slice = _PySlice_FromIndices(i1, i2);
1629 if (!slice)
1630 return NULL;
1631 res = mp->mp_subscript(s, slice);
1632 Py_DECREF(slice);
1633 return res;
1634 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001635
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001636 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001637}
1638
1639int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001640PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001641{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001642 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001643
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001644 if (s == NULL) {
1645 null_error();
1646 return -1;
1647 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001648
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001649 m = s->ob_type->tp_as_sequence;
1650 if (m && m->sq_ass_item) {
1651 if (i < 0) {
1652 if (m->sq_length) {
1653 Py_ssize_t l = (*m->sq_length)(s);
1654 if (l < 0)
1655 return -1;
1656 i += l;
1657 }
1658 }
1659 return m->sq_ass_item(s, i, o);
1660 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001662 type_error("'%.200s' object does not support item assignment", s);
1663 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001664}
1665
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001666int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001667PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001668{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001669 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001670
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001671 if (s == NULL) {
1672 null_error();
1673 return -1;
1674 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001675
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001676 m = s->ob_type->tp_as_sequence;
1677 if (m && m->sq_ass_item) {
1678 if (i < 0) {
1679 if (m->sq_length) {
1680 Py_ssize_t l = (*m->sq_length)(s);
1681 if (l < 0)
1682 return -1;
1683 i += l;
1684 }
1685 }
1686 return m->sq_ass_item(s, i, (PyObject *)NULL);
1687 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001688
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001689 type_error("'%.200s' object doesn't support item deletion", s);
1690 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001691}
1692
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001693int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001694PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001695{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001696 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001697
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001698 if (s == NULL) {
1699 null_error();
1700 return -1;
1701 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001702
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001703 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson8042f4a2010-09-11 16:03:33 +00001704 if (mp && mp->mp_ass_subscript) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001705 int res;
1706 PyObject *slice = _PySlice_FromIndices(i1, i2);
1707 if (!slice)
1708 return -1;
1709 res = mp->mp_ass_subscript(s, slice, o);
1710 Py_DECREF(slice);
1711 return res;
1712 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001713
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001714 type_error("'%.200s' object doesn't support slice assignment", s);
1715 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001716}
1717
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001718int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001719PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001720{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001721 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001722
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001723 if (s == NULL) {
1724 null_error();
1725 return -1;
1726 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001727
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001728 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson8042f4a2010-09-11 16:03:33 +00001729 if (mp && mp->mp_ass_subscript) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001730 int res;
1731 PyObject *slice = _PySlice_FromIndices(i1, i2);
1732 if (!slice)
1733 return -1;
1734 res = mp->mp_ass_subscript(s, slice, NULL);
1735 Py_DECREF(slice);
1736 return res;
1737 }
1738 type_error("'%.200s' object doesn't support slice deletion", s);
1739 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001740}
1741
Guido van Rossume15dee51995-07-18 14:12:02 +00001742PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001743PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001744{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001745 PyObject *it; /* iter(v) */
1746 Py_ssize_t n; /* guess for result tuple size */
1747 PyObject *result = NULL;
1748 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001749
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001750 if (v == NULL)
1751 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001752
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001753 /* Special-case the common tuple and list cases, for efficiency. */
1754 if (PyTuple_CheckExact(v)) {
1755 /* Note that we can't know whether it's safe to return
1756 a tuple *subclass* instance as-is, hence the restriction
1757 to exact tuples here. In contrast, lists always make
1758 a copy, so there's no need for exactness below. */
1759 Py_INCREF(v);
1760 return v;
1761 }
1762 if (PyList_Check(v))
1763 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001764
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001765 /* Get iterator. */
1766 it = PyObject_GetIter(v);
1767 if (it == NULL)
1768 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001769
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001770 /* Guess result size and allocate space. */
1771 n = _PyObject_LengthHint(v, 10);
1772 if (n == -1)
1773 goto Fail;
1774 result = PyTuple_New(n);
1775 if (result == NULL)
1776 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001777
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001778 /* Fill the tuple. */
1779 for (j = 0; ; ++j) {
1780 PyObject *item = PyIter_Next(it);
1781 if (item == NULL) {
1782 if (PyErr_Occurred())
1783 goto Fail;
1784 break;
1785 }
1786 if (j >= n) {
1787 Py_ssize_t oldn = n;
1788 /* The over-allocation strategy can grow a bit faster
1789 than for lists because unlike lists the
1790 over-allocation isn't permanent -- we reclaim
1791 the excess before the end of this routine.
1792 So, grow by ten and then add 25%.
1793 */
1794 n += 10;
1795 n += n >> 2;
1796 if (n < oldn) {
1797 /* Check for overflow */
1798 PyErr_NoMemory();
1799 Py_DECREF(item);
1800 goto Fail;
1801 }
1802 if (_PyTuple_Resize(&result, n) != 0) {
1803 Py_DECREF(item);
1804 goto Fail;
1805 }
1806 }
1807 PyTuple_SET_ITEM(result, j, item);
1808 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001809
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001810 /* Cut tuple back if guess was too large. */
1811 if (j < n &&
1812 _PyTuple_Resize(&result, j) != 0)
1813 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001815 Py_DECREF(it);
1816 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001817
1818Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001819 Py_XDECREF(result);
1820 Py_DECREF(it);
1821 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001822}
1823
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001824PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001825PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001826{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001827 PyObject *result; /* result list */
1828 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001829
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001830 if (v == NULL)
1831 return null_error();
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001832
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001833 result = PyList_New(0);
1834 if (result == NULL)
1835 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001836
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001837 rv = _PyList_Extend((PyListObject *)result, v);
1838 if (rv == NULL) {
1839 Py_DECREF(result);
1840 return NULL;
1841 }
1842 Py_DECREF(rv);
1843 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001844}
1845
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001846PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001847PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001848{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001849 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001851 if (v == NULL)
1852 return null_error();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001854 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1855 Py_INCREF(v);
1856 return v;
1857 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001859 it = PyObject_GetIter(v);
1860 if (it == NULL) {
1861 if (PyErr_ExceptionMatches(PyExc_TypeError))
1862 PyErr_SetString(PyExc_TypeError, m);
1863 return NULL;
1864 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001865
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001866 v = PySequence_List(it);
1867 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001868
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001869 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001870}
1871
Tim Peters16a77ad2001-09-08 04:00:12 +00001872/* Iterate over seq. Result depends on the operation:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001873 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1874 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1875 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001876 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1877*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001878Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001879_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001880{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001881 Py_ssize_t n;
1882 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1883 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001885 if (seq == NULL || obj == NULL) {
1886 null_error();
1887 return -1;
1888 }
Tim Peters75f8e352001-05-05 11:33:43 +00001889
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001890 it = PyObject_GetIter(seq);
1891 if (it == NULL) {
1892 type_error("argument of type '%.200s' is not iterable", seq);
1893 return -1;
1894 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001896 n = wrapped = 0;
1897 for (;;) {
1898 int cmp;
1899 PyObject *item = PyIter_Next(it);
1900 if (item == NULL) {
1901 if (PyErr_Occurred())
1902 goto Fail;
1903 break;
1904 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001905
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001906 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1907 Py_DECREF(item);
1908 if (cmp < 0)
1909 goto Fail;
1910 if (cmp > 0) {
1911 switch (operation) {
1912 case PY_ITERSEARCH_COUNT:
1913 if (n == PY_SSIZE_T_MAX) {
1914 PyErr_SetString(PyExc_OverflowError,
1915 "count exceeds C integer size");
1916 goto Fail;
1917 }
1918 ++n;
1919 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001920
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001921 case PY_ITERSEARCH_INDEX:
1922 if (wrapped) {
1923 PyErr_SetString(PyExc_OverflowError,
1924 "index exceeds C integer size");
1925 goto Fail;
1926 }
1927 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001929 case PY_ITERSEARCH_CONTAINS:
1930 n = 1;
1931 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001932
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001933 default:
1934 assert(!"unknown operation");
1935 }
1936 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001937
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001938 if (operation == PY_ITERSEARCH_INDEX) {
1939 if (n == PY_SSIZE_T_MAX)
1940 wrapped = 1;
1941 ++n;
1942 }
1943 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001944
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001945 if (operation != PY_ITERSEARCH_INDEX)
1946 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001947
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001948 PyErr_SetString(PyExc_ValueError,
1949 "sequence.index(x): x not in sequence");
1950 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00001951Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001952 n = -1;
1953 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00001954Done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001955 Py_DECREF(it);
1956 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001957
Guido van Rossume15dee51995-07-18 14:12:02 +00001958}
1959
Tim Peters16a77ad2001-09-08 04:00:12 +00001960/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001961Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001962PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001963{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001964 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001965}
1966
Tim Peterscb8d3682001-05-05 21:05:01 +00001967/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001968 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001969 */
1970int
1971PySequence_Contains(PyObject *seq, PyObject *ob)
1972{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001973 Py_ssize_t result;
1974 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1975 if (sqm != NULL && sqm->sq_contains != NULL)
1976 return (*sqm->sq_contains)(seq, ob);
1977 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1978 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001979}
1980
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001981/* Backwards compatibility */
1982#undef PySequence_In
1983int
Fred Drake79912472000-07-09 04:06:11 +00001984PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001985{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001986 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001987}
1988
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001989Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001990PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001991{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001992 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001993}
1994
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001995/* Operations on mappings */
1996
1997int
Fred Drake79912472000-07-09 04:06:11 +00001998PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001999{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002000 return o && o->ob_type->tp_as_mapping &&
2001 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002002}
2003
Martin v. Löwis18e16552006-02-15 17:27:45 +00002004Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002005PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002006{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002007 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002008
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002009 if (o == NULL) {
2010 null_error();
2011 return -1;
2012 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002013
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002014 m = o->ob_type->tp_as_mapping;
2015 if (m && m->mp_length)
2016 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00002017
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002018 type_error("object of type '%.200s' has no len()", o);
2019 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002020}
2021
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002022#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002023Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002024PyMapping_Length(PyObject *o)
2025{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002026 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002027}
2028#define PyMapping_Length PyMapping_Size
2029
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002030PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002031PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002032{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002033 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002034
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002035 if (key == NULL)
2036 return null_error();
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002038 okey = PyUnicode_FromString(key);
2039 if (okey == NULL)
2040 return NULL;
2041 r = PyObject_GetItem(o, okey);
2042 Py_DECREF(okey);
2043 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002044}
2045
2046int
Fred Drake79912472000-07-09 04:06:11 +00002047PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002048{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002049 PyObject *okey;
2050 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002052 if (key == NULL) {
2053 null_error();
2054 return -1;
2055 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002056
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002057 okey = PyUnicode_FromString(key);
2058 if (okey == NULL)
2059 return -1;
2060 r = PyObject_SetItem(o, okey, value);
2061 Py_DECREF(okey);
2062 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002063}
2064
2065int
Fred Drake79912472000-07-09 04:06:11 +00002066PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002067{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002068 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002069
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002070 v = PyMapping_GetItemString(o, key);
2071 if (v) {
2072 Py_DECREF(v);
2073 return 1;
2074 }
2075 PyErr_Clear();
2076 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002077}
2078
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002079int
Fred Drake79912472000-07-09 04:06:11 +00002080PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002081{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002082 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002083
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002084 v = PyObject_GetItem(o, key);
2085 if (v) {
2086 Py_DECREF(v);
2087 return 1;
2088 }
2089 PyErr_Clear();
2090 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002091}
2092
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002093PyObject *
2094PyMapping_Keys(PyObject *o)
2095{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002096 PyObject *keys;
2097 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002098
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002099 if (PyDict_CheckExact(o))
2100 return PyDict_Keys(o);
2101 keys = PyObject_CallMethod(o, "keys", NULL);
2102 if (keys == NULL)
2103 return NULL;
2104 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2105 Py_DECREF(keys);
2106 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002107}
2108
2109PyObject *
2110PyMapping_Items(PyObject *o)
2111{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002112 PyObject *items;
2113 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002114
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002115 if (PyDict_CheckExact(o))
2116 return PyDict_Items(o);
2117 items = PyObject_CallMethod(o, "items", NULL);
2118 if (items == NULL)
2119 return NULL;
2120 fast = PySequence_Fast(items, "o.items() are not iterable");
2121 Py_DECREF(items);
2122 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002123}
2124
2125PyObject *
2126PyMapping_Values(PyObject *o)
2127{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002128 PyObject *values;
2129 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002130
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002131 if (PyDict_CheckExact(o))
2132 return PyDict_Values(o);
2133 values = PyObject_CallMethod(o, "values", NULL);
2134 if (values == NULL)
2135 return NULL;
2136 fast = PySequence_Fast(values, "o.values() are not iterable");
2137 Py_DECREF(values);
2138 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002139}
2140
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002141/* Operations on callable objects */
2142
2143/* XXX PyCallable_Check() is in object.c */
2144
Guido van Rossume15dee51995-07-18 14:12:02 +00002145PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002146PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002147{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002148 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002149}
Guido van Rossume15dee51995-07-18 14:12:02 +00002150
2151PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002152PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
2153{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002154 ternaryfunc call;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002155
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002156 if ((call = func->ob_type->tp_call) != NULL) {
2157 PyObject *result;
2158 if (Py_EnterRecursiveCall(" while calling a Python object"))
2159 return NULL;
2160 result = (*call)(func, arg, kw);
2161 Py_LeaveRecursiveCall();
2162 if (result == NULL && !PyErr_Occurred())
2163 PyErr_SetString(
2164 PyExc_SystemError,
2165 "NULL result without error in PyObject_Call");
2166 return result;
2167 }
2168 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2169 func->ob_type->tp_name);
2170 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002171}
2172
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002173static PyObject*
2174call_function_tail(PyObject *callable, PyObject *args)
2175{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002176 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002177
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002178 if (args == NULL)
2179 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002180
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002181 if (!PyTuple_Check(args)) {
2182 PyObject *a;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002183
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002184 a = PyTuple_New(1);
2185 if (a == NULL) {
2186 Py_DECREF(args);
2187 return NULL;
2188 }
2189 PyTuple_SET_ITEM(a, 0, args);
2190 args = a;
2191 }
2192 retval = PyObject_Call(callable, args, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002193
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002194 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002196 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002197}
2198
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002200PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002201{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002202 va_list va;
2203 PyObject *args;
Guido van Rossume15dee51995-07-18 14:12:02 +00002204
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002205 if (callable == NULL)
2206 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002208 if (format && *format) {
2209 va_start(va, format);
2210 args = Py_VaBuildValue(format, va);
2211 va_end(va);
2212 }
2213 else
2214 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002216 return call_function_tail(callable, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002217}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002218
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002219PyObject *
2220_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
2221{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002222 va_list va;
2223 PyObject *args;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002224
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002225 if (callable == NULL)
2226 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002227
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002228 if (format && *format) {
2229 va_start(va, format);
2230 args = _Py_VaBuildValue_SizeT(format, va);
2231 va_end(va);
2232 }
2233 else
2234 args = PyTuple_New(0);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002235
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002236 return call_function_tail(callable, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002237}
2238
2239PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00002240PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002241{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002242 va_list va;
2243 PyObject *args;
2244 PyObject *func = NULL;
2245 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002246
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002247 if (o == NULL || name == NULL)
2248 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002249
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002250 func = PyObject_GetAttrString(o, name);
2251 if (func == NULL) {
2252 PyErr_SetString(PyExc_AttributeError, name);
2253 return 0;
2254 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002256 if (!PyCallable_Check(func)) {
2257 type_error("attribute of type '%.200s' is not callable", func);
2258 goto exit;
2259 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002260
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002261 if (format && *format) {
2262 va_start(va, format);
2263 args = Py_VaBuildValue(format, va);
2264 va_end(va);
2265 }
2266 else
2267 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002268
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002269 retval = call_function_tail(func, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002270
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002271 exit:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002272 /* args gets consumed in call_function_tail */
2273 Py_XDECREF(func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002275 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002276}
2277
2278PyObject *
2279_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
2280{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002281 va_list va;
2282 PyObject *args;
2283 PyObject *func = NULL;
2284 PyObject *retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002285
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002286 if (o == NULL || name == NULL)
2287 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002288
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002289 func = PyObject_GetAttrString(o, name);
2290 if (func == NULL) {
2291 PyErr_SetString(PyExc_AttributeError, name);
2292 return 0;
2293 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002295 if (!PyCallable_Check(func)) {
2296 type_error("attribute of type '%.200s' is not callable", func);
2297 goto exit;
2298 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002299
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002300 if (format && *format) {
2301 va_start(va, format);
2302 args = _Py_VaBuildValue_SizeT(format, va);
2303 va_end(va);
2304 }
2305 else
2306 args = PyTuple_New(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002308 retval = call_function_tail(func, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309
2310 exit:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002311 /* args gets consumed in call_function_tail */
2312 Py_XDECREF(func);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002313
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002314 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002315}
Guido van Rossum823649d2001-03-21 18:40:58 +00002316
2317
Fred Drakeb421b8c2001-10-26 16:21:32 +00002318static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002319objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002320{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002321 int i, n = 0;
2322 va_list countva;
2323 PyObject *result, *tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002324
2325#ifdef VA_LIST_IS_ARRAY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002326 memcpy(countva, va, sizeof(va_list));
Fred Drakeb421b8c2001-10-26 16:21:32 +00002327#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +00002328#ifdef __va_copy
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002329 __va_copy(countva, va);
Martin v. Löwis75d2d942002-07-28 10:23:27 +00002330#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002331 countva = va;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002332#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +00002333#endif
Fred Drakeb421b8c2001-10-26 16:21:32 +00002334
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002335 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
2336 ++n;
2337 result = PyTuple_New(n);
2338 if (result != NULL && n > 0) {
2339 for (i = 0; i < n; ++i) {
2340 tmp = (PyObject *)va_arg(va, PyObject *);
2341 PyTuple_SET_ITEM(result, i, tmp);
2342 Py_INCREF(tmp);
2343 }
2344 }
2345 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002346}
2347
2348PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002349PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002351 PyObject *args, *tmp;
2352 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002353
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002354 if (callable == NULL || name == NULL)
2355 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002356
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002357 callable = PyObject_GetAttr(callable, name);
2358 if (callable == NULL)
2359 return NULL;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002360
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002361 /* count the args */
2362 va_start(vargs, name);
2363 args = objargs_mktuple(vargs);
2364 va_end(vargs);
2365 if (args == NULL) {
2366 Py_DECREF(callable);
2367 return NULL;
2368 }
2369 tmp = PyObject_Call(callable, args, NULL);
2370 Py_DECREF(args);
2371 Py_DECREF(callable);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002372
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002373 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002374}
2375
2376PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002377PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002378{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002379 PyObject *args, *tmp;
2380 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002381
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002382 if (callable == NULL)
2383 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002384
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002385 /* count the args */
2386 va_start(vargs, callable);
2387 args = objargs_mktuple(vargs);
2388 va_end(vargs);
2389 if (args == NULL)
2390 return NULL;
2391 tmp = PyObject_Call(callable, args, NULL);
2392 Py_DECREF(args);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002393
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002394 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002395}
2396
2397
Guido van Rossum823649d2001-03-21 18:40:58 +00002398/* isinstance(), issubclass() */
2399
Barry Warsawf16951c2002-04-23 22:45:44 +00002400/* abstract_get_bases() has logically 4 return states, with a sort of 0th
2401 * state that will almost never happen.
2402 *
2403 * 0. creating the __bases__ static string could get a MemoryError
2404 * 1. getattr(cls, '__bases__') could raise an AttributeError
2405 * 2. getattr(cls, '__bases__') could raise some other exception
2406 * 3. getattr(cls, '__bases__') could return a tuple
2407 * 4. getattr(cls, '__bases__') could return something other than a tuple
2408 *
2409 * Only state #3 is a non-error state and only it returns a non-NULL object
2410 * (it returns the retrieved tuple).
2411 *
2412 * Any raised AttributeErrors are masked by clearing the exception and
2413 * returning NULL. If an object other than a tuple comes out of __bases__,
2414 * then again, the return value is NULL. So yes, these two situations
2415 * produce exactly the same results: NULL is returned and no error is set.
2416 *
2417 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002418 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002419 * exception to be propagated along.
2420 *
2421 * Callers are expected to test for PyErr_Occurred() when the return value
2422 * is NULL to decide whether a valid exception should be propagated or not.
2423 * When there's no exception to propagate, it's customary for the caller to
2424 * set a TypeError.
2425 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002426static PyObject *
2427abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002428{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002429 static PyObject *__bases__ = NULL;
2430 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002431
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002432 if (__bases__ == NULL) {
2433 __bases__ = PyUnicode_InternFromString("__bases__");
2434 if (__bases__ == NULL)
2435 return NULL;
2436 }
2437 Py_ALLOW_RECURSION
2438 bases = PyObject_GetAttr(cls, __bases__);
2439 Py_END_ALLOW_RECURSION
2440 if (bases == NULL) {
2441 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2442 PyErr_Clear();
2443 return NULL;
2444 }
2445 if (!PyTuple_Check(bases)) {
2446 Py_DECREF(bases);
2447 return NULL;
2448 }
2449 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002450}
2451
2452
2453static int
2454abstract_issubclass(PyObject *derived, PyObject *cls)
2455{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002456 PyObject *bases = NULL;
2457 Py_ssize_t i, n;
2458 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002459
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002460 while (1) {
2461 if (derived == cls)
2462 return 1;
2463 bases = abstract_get_bases(derived);
2464 if (bases == NULL) {
2465 if (PyErr_Occurred())
2466 return -1;
2467 return 0;
2468 }
2469 n = PyTuple_GET_SIZE(bases);
2470 if (n == 0) {
2471 Py_DECREF(bases);
2472 return 0;
2473 }
2474 /* Avoid recursivity in the single inheritance case */
2475 if (n == 1) {
2476 derived = PyTuple_GET_ITEM(bases, 0);
2477 Py_DECREF(bases);
2478 continue;
2479 }
2480 for (i = 0; i < n; i++) {
2481 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2482 if (r != 0)
2483 break;
2484 }
2485 Py_DECREF(bases);
2486 return r;
2487 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002488}
2489
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002490static int
2491check_class(PyObject *cls, const char *error)
2492{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002493 PyObject *bases = abstract_get_bases(cls);
2494 if (bases == NULL) {
2495 /* Do not mask errors. */
2496 if (!PyErr_Occurred())
2497 PyErr_SetString(PyExc_TypeError, error);
2498 return 0;
2499 }
2500 Py_DECREF(bases);
2501 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002502}
2503
Brett Cannon4f653312004-03-20 22:52:14 +00002504static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002505recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002506{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002507 PyObject *icls;
2508 static PyObject *__class__ = NULL;
2509 int retval = 0;
Guido van Rossum823649d2001-03-21 18:40:58 +00002510
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002511 if (__class__ == NULL) {
2512 __class__ = PyUnicode_InternFromString("__class__");
2513 if (__class__ == NULL)
2514 return -1;
2515 }
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002516
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002517 if (PyType_Check(cls)) {
2518 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2519 if (retval == 0) {
2520 PyObject *c = PyObject_GetAttr(inst, __class__);
2521 if (c == NULL) {
2522 PyErr_Clear();
2523 }
2524 else {
2525 if (c != (PyObject *)(inst->ob_type) &&
2526 PyType_Check(c))
2527 retval = PyType_IsSubtype(
2528 (PyTypeObject *)c,
2529 (PyTypeObject *)cls);
2530 Py_DECREF(c);
2531 }
2532 }
2533 }
2534 else {
2535 if (!check_class(cls,
Benjamin Peterson26eb9d02010-06-28 19:46:35 +00002536 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002537 return -1;
2538 icls = PyObject_GetAttr(inst, __class__);
2539 if (icls == NULL) {
2540 PyErr_Clear();
2541 retval = 0;
2542 }
2543 else {
2544 retval = abstract_issubclass(icls, cls);
2545 Py_DECREF(icls);
2546 }
2547 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002549 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002550}
2551
2552int
Brett Cannon4f653312004-03-20 22:52:14 +00002553PyObject_IsInstance(PyObject *inst, PyObject *cls)
2554{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002555 static PyObject *name = NULL;
2556 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002557
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002558 /* Quick test for an exact match */
2559 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2560 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002562 if (PyTuple_Check(cls)) {
2563 Py_ssize_t i;
2564 Py_ssize_t n;
2565 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002566
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002567 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2568 return -1;
2569 n = PyTuple_GET_SIZE(cls);
2570 for (i = 0; i < n; ++i) {
2571 PyObject *item = PyTuple_GET_ITEM(cls, i);
2572 r = PyObject_IsInstance(inst, item);
2573 if (r != 0)
2574 /* either found it, or got an error */
2575 break;
2576 }
2577 Py_LeaveRecursiveCall();
2578 return r;
2579 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002580
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002581 checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
2582 if (checker != NULL) {
2583 PyObject *res;
2584 int ok = -1;
2585 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2586 Py_DECREF(checker);
2587 return ok;
2588 }
2589 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2590 Py_LeaveRecursiveCall();
2591 Py_DECREF(checker);
2592 if (res != NULL) {
2593 ok = PyObject_IsTrue(res);
2594 Py_DECREF(res);
2595 }
2596 return ok;
2597 }
2598 else if (PyErr_Occurred())
2599 return -1;
2600 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002601}
2602
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002603static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002604recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002605{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002606 if (PyType_Check(cls) && PyType_Check(derived)) {
2607 /* Fast path (non-recursive) */
2608 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2609 }
2610 if (!check_class(derived,
2611 "issubclass() arg 1 must be a class"))
2612 return -1;
2613 if (!check_class(cls,
2614 "issubclass() arg 2 must be a class"
2615 " or tuple of classes"))
2616 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002617
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002618 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002619}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002620
Brett Cannon4f653312004-03-20 22:52:14 +00002621int
2622PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2623{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002624 static PyObject *name = NULL;
2625 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002626
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002627 if (PyTuple_Check(cls)) {
2628 Py_ssize_t i;
2629 Py_ssize_t n;
2630 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002631
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002632 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2633 return -1;
2634 n = PyTuple_GET_SIZE(cls);
2635 for (i = 0; i < n; ++i) {
2636 PyObject *item = PyTuple_GET_ITEM(cls, i);
2637 r = PyObject_IsSubclass(derived, item);
2638 if (r != 0)
2639 /* either found it, or got an error */
2640 break;
2641 }
2642 Py_LeaveRecursiveCall();
2643 return r;
2644 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002645
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002646 checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
2647 if (checker != NULL) {
2648 PyObject *res;
2649 int ok = -1;
2650 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2651 Py_DECREF(checker);
2652 return ok;
2653 }
2654 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
2655 Py_LeaveRecursiveCall();
2656 Py_DECREF(checker);
2657 if (res != NULL) {
2658 ok = PyObject_IsTrue(res);
2659 Py_DECREF(res);
2660 }
2661 return ok;
2662 }
2663 else if (PyErr_Occurred())
2664 return -1;
2665 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002666}
2667
2668int
2669_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2670{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002671 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002672}
2673
2674int
2675_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2676{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002677 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002678}
2679
2680
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002681PyObject *
2682PyObject_GetIter(PyObject *o)
2683{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002684 PyTypeObject *t = o->ob_type;
2685 getiterfunc f = NULL;
2686 f = t->tp_iter;
2687 if (f == NULL) {
2688 if (PySequence_Check(o))
2689 return PySeqIter_New(o);
2690 return type_error("'%.200s' object is not iterable", o);
2691 }
2692 else {
2693 PyObject *res = (*f)(o);
2694 if (res != NULL && !PyIter_Check(res)) {
2695 PyErr_Format(PyExc_TypeError,
2696 "iter() returned non-iterator "
2697 "of type '%.100s'",
2698 res->ob_type->tp_name);
2699 Py_DECREF(res);
2700 res = NULL;
2701 }
2702 return res;
2703 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002704}
2705
Tim Petersf4848da2001-05-05 00:14:56 +00002706/* Return next item.
2707 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2708 * If the iteration terminates normally, return NULL and clear the
2709 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2710 * will be false.
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002711 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002712 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002713PyObject *
2714PyIter_Next(PyObject *iter)
2715{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002716 PyObject *result;
2717 result = (*iter->ob_type->tp_iternext)(iter);
2718 if (result == NULL &&
2719 PyErr_Occurred() &&
2720 PyErr_ExceptionMatches(PyExc_StopIteration))
2721 PyErr_Clear();
2722 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002723}