blob: 2f887aa0563796bb9422f2c05b6697687508db4b [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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000033 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000045 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 if (o == NULL) {
48 null_error();
49 return -1;
50 }
Guido van Rossume15dee51995-07-18 14:12:02 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000077 static PyObject *hintstrobj = NULL;
78 PyObject *ro, *hintmeth;
79 Py_ssize_t rv;
Christian Heimes255f53b2007-12-08 15:33:56 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (o == NULL || key == NULL)
118 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000143 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000207 PyObject *okey;
208 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000227 const char **buffer,
228 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 PyBufferProcs *pb;
231 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
257 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000271 const void **buffer,
272 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 PyBufferProcs *pb;
275 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000289 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000300 void **buffer,
301 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 PyBufferProcs *pb;
304 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_ssize_t sd, dim;
345 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000365 Py_ssize_t sd, dim;
366 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (view->ndim == 0) return 1;
369 if (view->strides == NULL) return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf68c2a72010-09-01 12:58:21 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000418 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf68c2a72010-09-01 12:58:21 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000434 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000455 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000456 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_ssize_t *indices, elements;
458 char *dest, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (len > view->len) {
461 len = view->len;
462 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000470 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000483 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
485 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000486 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000506 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000507 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 Py_ssize_t *indices, elements;
509 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (len > view->len) {
512 len = view->len;
513 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000534 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 }
536 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000537 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf68c2a72010-09-01 12:58:21 +0000614 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000627 Py_ssize_t *strides, int itemsize,
628 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 int k;
631 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000651 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000696 PyObject *meth;
697 PyObject *empty = NULL;
698 PyObject *result = NULL;
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000699 static PyObject *format_cache = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000700
701 /* If no format_spec is provided, use an empty string */
702 if (format_spec == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000703 empty = PyUnicode_FromUnicode(NULL, 0);
704 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000705 }
706
Eric Smith8fd3eba2008-02-17 19:48:00 +0000707 /* Find the (unbound!) __format__ method (a borrowed reference) */
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000708 meth = _PyObject_LookupSpecial(obj, "__format__", &format_cache);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000709 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000710 if (!PyErr_Occurred())
711 PyErr_Format(PyExc_TypeError,
712 "Type %.100s doesn't define __format__",
713 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000715 }
716
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000717 /* And call it. */
718 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000719 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000720
721 if (result && !PyUnicode_Check(result)) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000722 PyErr_SetString(PyExc_TypeError,
723 "__format__ method did not return string");
724 Py_DECREF(result);
725 result = NULL;
726 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000727 }
728
729done:
730 Py_XDECREF(empty);
731 return result;
732}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000733/* Operations on numbers */
734
735int
Fred Drake79912472000-07-09 04:06:11 +0000736PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 return o && o->ob_type->tp_as_number &&
739 (o->ob_type->tp_as_number->nb_int ||
740 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000741}
742
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000743/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000744
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000745#define NB_SLOT(x) offsetof(PyNumberMethods, x)
746#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000748#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000750
751/*
752 Calling scheme used for binary operations:
753
Neal Norwitz4886cc32006-08-21 17:06:07 +0000754 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000756
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000757 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
758 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000759 */
760
761static PyObject *
762binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyObject *x;
765 binaryfunc slotv = NULL;
766 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (v->ob_type->tp_as_number != NULL)
769 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
770 if (w->ob_type != v->ob_type &&
771 w->ob_type->tp_as_number != NULL) {
772 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
773 if (slotw == slotv)
774 slotw = NULL;
775 }
776 if (slotv) {
777 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
778 x = slotw(v, w);
779 if (x != Py_NotImplemented)
780 return x;
781 Py_DECREF(x); /* can't do it */
782 slotw = NULL;
783 }
784 x = slotv(v, w);
785 if (x != Py_NotImplemented)
786 return x;
787 Py_DECREF(x); /* can't do it */
788 }
789 if (slotw) {
790 x = slotw(v, w);
791 if (x != Py_NotImplemented)
792 return x;
793 Py_DECREF(x); /* can't do it */
794 }
795 Py_INCREF(Py_NotImplemented);
796 return Py_NotImplemented;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000797}
Guido van Rossum77660912002-04-16 16:32:50 +0000798
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000799static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000800binop_type_error(PyObject *v, PyObject *w, const char *op_name)
801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 PyErr_Format(PyExc_TypeError,
803 "unsupported operand type(s) for %.100s: "
804 "'%.100s' and '%.100s'",
805 op_name,
806 v->ob_type->tp_name,
807 w->ob_type->tp_name);
808 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000809}
810
811static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000812binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject *result = binary_op1(v, w, op_slot);
815 if (result == Py_NotImplemented) {
816 Py_DECREF(result);
817 return binop_type_error(v, w, op_name);
818 }
819 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000820}
821
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000822
823/*
824 Calling scheme used for ternary operations:
825
Neal Norwitz4886cc32006-08-21 17:06:07 +0000826 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000828 */
829
830static PyObject *
831ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyObject *w,
833 PyObject *z,
834 const int op_slot,
835 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 PyNumberMethods *mv, *mw, *mz;
838 PyObject *x = NULL;
839 ternaryfunc slotv = NULL;
840 ternaryfunc slotw = NULL;
841 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 mv = v->ob_type->tp_as_number;
844 mw = w->ob_type->tp_as_number;
845 if (mv != NULL)
846 slotv = NB_TERNOP(mv, op_slot);
847 if (w->ob_type != v->ob_type &&
848 mw != NULL) {
849 slotw = NB_TERNOP(mw, op_slot);
850 if (slotw == slotv)
851 slotw = NULL;
852 }
853 if (slotv) {
854 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
855 x = slotw(v, w, z);
856 if (x != Py_NotImplemented)
857 return x;
858 Py_DECREF(x); /* can't do it */
859 slotw = NULL;
860 }
861 x = slotv(v, w, z);
862 if (x != Py_NotImplemented)
863 return x;
864 Py_DECREF(x); /* can't do it */
865 }
866 if (slotw) {
867 x = slotw(v, w, z);
868 if (x != Py_NotImplemented)
869 return x;
870 Py_DECREF(x); /* can't do it */
871 }
872 mz = z->ob_type->tp_as_number;
873 if (mz != NULL) {
874 slotz = NB_TERNOP(mz, op_slot);
875 if (slotz == slotv || slotz == slotw)
876 slotz = NULL;
877 if (slotz) {
878 x = slotz(v, w, z);
879 if (x != Py_NotImplemented)
880 return x;
881 Py_DECREF(x); /* can't do it */
882 }
883 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (z == Py_None)
886 PyErr_Format(
887 PyExc_TypeError,
888 "unsupported operand type(s) for ** or pow(): "
889 "'%.100s' and '%.100s'",
890 v->ob_type->tp_name,
891 w->ob_type->tp_name);
892 else
893 PyErr_Format(
894 PyExc_TypeError,
895 "unsupported operand type(s) for pow(): "
896 "'%.100s', '%.100s', '%.100s'",
897 v->ob_type->tp_name,
898 w->ob_type->tp_name,
899 z->ob_type->tp_name);
900 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000901}
902
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000903#define BINARY_FUNC(func, op, op_name) \
904 PyObject * \
905 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000907 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000908
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000909BINARY_FUNC(PyNumber_Or, nb_or, "|")
910BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
911BINARY_FUNC(PyNumber_And, nb_and, "&")
912BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
913BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
914BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000915BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000916
917PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000918PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
921 if (result == Py_NotImplemented) {
922 PySequenceMethods *m = v->ob_type->tp_as_sequence;
923 Py_DECREF(result);
924 if (m && m->sq_concat) {
925 return (*m->sq_concat)(v, w);
926 }
927 result = binop_type_error(v, w, "+");
928 }
929 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000930}
931
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000932static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000933sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 Py_ssize_t count;
936 if (PyIndex_Check(n)) {
937 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
938 if (count == -1 && PyErr_Occurred())
939 return NULL;
940 }
941 else {
942 return type_error("can't multiply sequence by "
943 "non-int of type '%.200s'", n);
944 }
945 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000946}
947
948PyObject *
949PyNumber_Multiply(PyObject *v, PyObject *w)
950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
952 if (result == Py_NotImplemented) {
953 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
954 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
955 Py_DECREF(result);
956 if (mv && mv->sq_repeat) {
957 return sequence_repeat(mv->sq_repeat, v, w);
958 }
959 else if (mw && mw->sq_repeat) {
960 return sequence_repeat(mw->sq_repeat, w, v);
961 }
962 result = binop_type_error(v, w, "*");
963 }
964 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000965}
966
Guido van Rossume15dee51995-07-18 14:12:02 +0000967PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000968PyNumber_FloorDivide(PyObject *v, PyObject *w)
969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000971}
972
973PyObject *
974PyNumber_TrueDivide(PyObject *v, PyObject *w)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +0000977}
978
979PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000980PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000983}
984
985PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000986PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000989}
990
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000991/* Binary in-place operators */
992
993/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000994 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000995
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000996 - If the left hand object has the appropriate struct members, and
997 they are filled, call the appropriate function and return the
998 result. No coercion is done on the arguments; the left-hand object
999 is the one the operation is performed on, and it's up to the
1000 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001001
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001002 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001003 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001004
1005 */
1006
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001007static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001008binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyNumberMethods *mv = v->ob_type->tp_as_number;
1011 if (mv != NULL) {
1012 binaryfunc slot = NB_BINOP(mv, iop_slot);
1013 if (slot) {
1014 PyObject *x = (slot)(v, w);
1015 if (x != Py_NotImplemented) {
1016 return x;
1017 }
1018 Py_DECREF(x);
1019 }
1020 }
1021 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001022}
1023
1024static PyObject *
1025binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1029 if (result == Py_NotImplemented) {
1030 Py_DECREF(result);
1031 return binop_type_error(v, w, op_name);
1032 }
1033 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001034}
1035
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001036#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 PyObject * \
1038 func(PyObject *v, PyObject *w) { \
1039 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1040 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001041
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001042INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1043INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1044INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1045INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1046INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1047INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001048
1049PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001050PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1053 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001054}
1055
1056PyObject *
1057PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1060 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001061}
1062
1063PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001064PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1067 NB_SLOT(nb_add));
1068 if (result == Py_NotImplemented) {
1069 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1070 Py_DECREF(result);
1071 if (m != NULL) {
1072 binaryfunc f = NULL;
1073 f = m->sq_inplace_concat;
1074 if (f == NULL)
1075 f = m->sq_concat;
1076 if (f != NULL)
1077 return (*f)(v, w);
1078 }
1079 result = binop_type_error(v, w, "+=");
1080 }
1081 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001082}
1083
1084PyObject *
1085PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1088 NB_SLOT(nb_multiply));
1089 if (result == Py_NotImplemented) {
1090 ssizeargfunc f = NULL;
1091 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1092 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1093 Py_DECREF(result);
1094 if (mv != NULL) {
1095 f = mv->sq_inplace_repeat;
1096 if (f == NULL)
1097 f = mv->sq_repeat;
1098 if (f != NULL)
1099 return sequence_repeat(f, v, w);
1100 }
1101 else if (mw != NULL) {
1102 /* Note that the right hand operand should not be
1103 * mutated in this case so sq_inplace_repeat is not
1104 * used. */
1105 if (mw->sq_repeat)
1106 return sequence_repeat(mw->sq_repeat, w, v);
1107 }
1108 result = binop_type_error(v, w, "*=");
1109 }
1110 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001111}
1112
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001113PyObject *
1114PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1117 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001118}
1119
1120PyObject *
1121PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (v->ob_type->tp_as_number &&
1124 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1125 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1126 }
1127 else {
1128 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1129 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001130}
1131
1132
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001133/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001134
1135PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001136PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (o == NULL)
1141 return null_error();
1142 m = o->ob_type->tp_as_number;
1143 if (m && m->nb_negative)
1144 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001147}
1148
1149PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001150PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (o == NULL)
1155 return null_error();
1156 m = o->ob_type->tp_as_number;
1157 if (m && m->nb_positive)
1158 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001161}
1162
1163PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001164PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (o == NULL)
1169 return null_error();
1170 m = o->ob_type->tp_as_number;
1171 if (m && m->nb_invert)
1172 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001175}
1176
1177PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001178PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (o == NULL)
1183 return null_error();
1184 m = o->ob_type->tp_as_number;
1185 if (m && m->nb_absolute)
1186 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001189}
1190
Guido van Rossum98297ee2007-11-06 21:34:58 +00001191/* Return a Python Int or Long from the object item
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001192 Raise TypeError if the result is not an int-or-long
Guido van Rossum98297ee2007-11-06 21:34:58 +00001193 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001194*/
1195PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001196PyNumber_Index(PyObject *item)
1197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 PyObject *result = NULL;
1199 if (item == NULL)
1200 return null_error();
1201 if (PyLong_Check(item)) {
1202 Py_INCREF(item);
1203 return item;
1204 }
1205 if (PyIndex_Check(item)) {
1206 result = item->ob_type->tp_as_number->nb_index(item);
1207 if (result && !PyLong_Check(result)) {
1208 PyErr_Format(PyExc_TypeError,
1209 "__index__ returned non-int "
1210 "(type %.200s)",
1211 result->ob_type->tp_name);
1212 Py_DECREF(result);
1213 return NULL;
1214 }
1215 }
1216 else {
1217 PyErr_Format(PyExc_TypeError,
1218 "'%.200s' object cannot be interpreted "
1219 "as an integer", item->ob_type->tp_name);
1220 }
1221 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001222}
1223
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001224/* Return an error on Overflow only if err is not NULL*/
1225
1226Py_ssize_t
1227PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 Py_ssize_t result;
1230 PyObject *runerr;
1231 PyObject *value = PyNumber_Index(item);
1232 if (value == NULL)
1233 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* We're done if PyLong_AsSsize_t() returns without error. */
1236 result = PyLong_AsSsize_t(value);
1237 if (result != -1 || !(runerr = PyErr_Occurred()))
1238 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 /* Error handling code -- only manage OverflowError differently */
1241 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1242 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 PyErr_Clear();
1245 /* If no error-handling desired then the default clipping
1246 is sufficient.
1247 */
1248 if (!err) {
1249 assert(PyLong_Check(value));
1250 /* Whether or not it is less than or equal to
1251 zero is determined by the sign of ob_size
1252 */
1253 if (_PyLong_Sign(value) < 0)
1254 result = PY_SSIZE_T_MIN;
1255 else
1256 result = PY_SSIZE_T_MAX;
1257 }
1258 else {
1259 /* Otherwise replace the error with caller's error object. */
1260 PyErr_Format(err,
1261 "cannot fit '%.200s' into an index-sized integer",
1262 item->ob_type->tp_name);
1263 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001264
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001265 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 Py_DECREF(value);
1267 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001268}
1269
1270
Christian Heimes15ebc882008-02-04 18:48:49 +00001271PyObject *
1272_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
1273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 static PyObject *int_name = NULL;
1275 if (int_name == NULL) {
1276 int_name = PyUnicode_InternFromString("__int__");
1277 if (int_name == NULL)
1278 return NULL;
1279 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (integral && !PyLong_Check(integral)) {
1282 /* Don't go through tp_as_number->nb_int to avoid
1283 hitting the classic class fallback to __trunc__. */
1284 PyObject *int_func = PyObject_GetAttr(integral, int_name);
1285 if (int_func == NULL) {
1286 PyErr_Clear(); /* Raise a different error. */
1287 goto non_integral_error;
1288 }
1289 Py_DECREF(integral);
1290 integral = PyEval_CallObject(int_func, NULL);
1291 Py_DECREF(int_func);
1292 if (integral && !PyLong_Check(integral)) {
1293 goto non_integral_error;
1294 }
1295 }
1296 return integral;
Christian Heimes15ebc882008-02-04 18:48:49 +00001297
1298non_integral_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
1300 Py_DECREF(integral);
1301 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001302}
1303
1304
Guido van Rossum9e896b32000-04-05 20:11:21 +00001305/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001306static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307long_from_string(const char *s, Py_ssize_t len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 char *end;
1310 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 x = PyLong_FromString((char*)s, &end, 10);
1313 if (x == NULL)
1314 return NULL;
1315 if (end != s + len) {
1316 PyErr_SetString(PyExc_ValueError,
1317 "null byte in argument for int()");
1318 Py_DECREF(x);
1319 return NULL;
1320 }
1321 return x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001322}
1323
Guido van Rossume15dee51995-07-18 14:12:02 +00001324PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001325PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 PyNumberMethods *m;
1328 static PyObject *trunc_name = NULL;
1329 PyObject *trunc_func;
1330 const char *buffer;
1331 Py_ssize_t buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (trunc_name == NULL) {
1334 trunc_name = PyUnicode_InternFromString("__trunc__");
1335 if (trunc_name == NULL)
1336 return NULL;
1337 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (o == NULL)
1340 return null_error();
1341 if (PyLong_CheckExact(o)) {
1342 Py_INCREF(o);
1343 return o;
1344 }
1345 m = o->ob_type->tp_as_number;
1346 if (m && m->nb_int) { /* This should include subclasses of int */
1347 PyObject *res = m->nb_int(o);
1348 if (res && !PyLong_Check(res)) {
1349 PyErr_Format(PyExc_TypeError,
1350 "__int__ returned non-int (type %.200s)",
1351 res->ob_type->tp_name);
1352 Py_DECREF(res);
1353 return NULL;
1354 }
1355 return res;
1356 }
1357 if (PyLong_Check(o)) /* An int subclass without nb_int */
1358 return _PyLong_Copy((PyLongObject *)o);
1359 trunc_func = PyObject_GetAttr(o, trunc_name);
1360 if (trunc_func) {
1361 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
1362 PyObject *int_instance;
1363 Py_DECREF(trunc_func);
1364 /* __trunc__ is specified to return an Integral type,
1365 but long() needs to return a long. */
1366 int_instance = _PyNumber_ConvertIntegralToInt(
1367 truncated,
1368 "__trunc__ returned non-Integral (type %.200s)");
1369 return int_instance;
1370 }
1371 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
Christian Heimes15ebc882008-02-04 18:48:49 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (PyBytes_Check(o))
1374 /* need to do extra error checking that PyLong_FromString()
1375 * doesn't do. In particular long('9.5') must raise an
1376 * exception, not truncate the float.
1377 */
1378 return long_from_string(PyBytes_AS_STRING(o),
1379 PyBytes_GET_SIZE(o));
1380 if (PyUnicode_Check(o))
1381 /* The above check is done in PyLong_FromUnicode(). */
1382 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
1383 PyUnicode_GET_SIZE(o),
1384 10);
1385 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
1386 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 return type_error("int() argument must be a string or a "
1389 "number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001390}
1391
1392PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001393PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (o == NULL)
1398 return null_error();
1399 m = o->ob_type->tp_as_number;
1400 if (m && m->nb_float) { /* This should include subclasses of float */
1401 PyObject *res = m->nb_float(o);
1402 if (res && !PyFloat_Check(res)) {
1403 PyErr_Format(PyExc_TypeError,
1404 "__float__ returned non-float (type %.200s)",
1405 res->ob_type->tp_name);
1406 Py_DECREF(res);
1407 return NULL;
1408 }
1409 return res;
1410 }
1411 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
1412 PyFloatObject *po = (PyFloatObject *)o;
1413 return PyFloat_FromDouble(po->ob_fval);
1414 }
1415 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001416}
1417
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001418
1419PyObject *
1420PyNumber_ToBase(PyObject *n, int base)
1421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 PyObject *res = NULL;
1423 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (!index)
1426 return NULL;
1427 if (PyLong_Check(index))
1428 res = _PyLong_Format(index, base);
1429 else
1430 /* It should not be possible to get here, as
1431 PyNumber_Index already has a check for the same
1432 condition */
1433 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
1434 "int or long");
1435 Py_DECREF(index);
1436 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001437}
1438
1439
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001440/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001441
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001442int
Fred Drake79912472000-07-09 04:06:11 +00001443PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (PyDict_Check(s))
1446 return 0;
1447 return s != NULL && s->ob_type->tp_as_sequence &&
1448 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001449}
1450
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001452PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (s == NULL) {
1457 null_error();
1458 return -1;
1459 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 m = s->ob_type->tp_as_sequence;
1462 if (m && m->sq_length)
1463 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 type_error("object of type '%.200s' has no len()", s);
1466 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001467}
1468
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001469#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001471PySequence_Length(PyObject *s)
1472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001474}
1475#define PySequence_Length PySequence_Size
1476
Guido van Rossume15dee51995-07-18 14:12:02 +00001477PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001478PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (s == NULL || o == NULL)
1483 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 m = s->ob_type->tp_as_sequence;
1486 if (m && m->sq_concat)
1487 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 /* Instances of user classes defining an __add__() method only
1490 have an nb_add slot, not an sq_concat slot. So we fall back
1491 to nb_add if both arguments appear to be sequences. */
1492 if (PySequence_Check(s) && PySequence_Check(o)) {
1493 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1494 if (result != Py_NotImplemented)
1495 return result;
1496 Py_DECREF(result);
1497 }
1498 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001499}
1500
1501PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001502PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (o == NULL)
1507 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 m = o->ob_type->tp_as_sequence;
1510 if (m && m->sq_repeat)
1511 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* Instances of user classes defining a __mul__() method only
1514 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1515 to nb_multiply if o appears to be a sequence. */
1516 if (PySequence_Check(o)) {
1517 PyObject *n, *result;
1518 n = PyLong_FromSsize_t(count);
1519 if (n == NULL)
1520 return NULL;
1521 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1522 Py_DECREF(n);
1523 if (result != Py_NotImplemented)
1524 return result;
1525 Py_DECREF(result);
1526 }
1527 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001528}
1529
1530PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001531PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (s == NULL || o == NULL)
1536 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 m = s->ob_type->tp_as_sequence;
1539 if (m && m->sq_inplace_concat)
1540 return m->sq_inplace_concat(s, o);
1541 if (m && m->sq_concat)
1542 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (PySequence_Check(s) && PySequence_Check(o)) {
1545 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1546 NB_SLOT(nb_add));
1547 if (result != Py_NotImplemented)
1548 return result;
1549 Py_DECREF(result);
1550 }
1551 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001552}
1553
1554PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001555PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (o == NULL)
1560 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 m = o->ob_type->tp_as_sequence;
1563 if (m && m->sq_inplace_repeat)
1564 return m->sq_inplace_repeat(o, count);
1565 if (m && m->sq_repeat)
1566 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (PySequence_Check(o)) {
1569 PyObject *n, *result;
1570 n = PyLong_FromSsize_t(count);
1571 if (n == NULL)
1572 return NULL;
1573 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1574 NB_SLOT(nb_multiply));
1575 Py_DECREF(n);
1576 if (result != Py_NotImplemented)
1577 return result;
1578 Py_DECREF(result);
1579 }
1580 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001581}
1582
1583PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001584PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 if (s == NULL)
1589 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 m = s->ob_type->tp_as_sequence;
1592 if (m && m->sq_item) {
1593 if (i < 0) {
1594 if (m->sq_length) {
1595 Py_ssize_t l = (*m->sq_length)(s);
1596 if (l < 0)
1597 return NULL;
1598 i += l;
1599 }
1600 }
1601 return m->sq_item(s, i);
1602 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001605}
1606
1607PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001608PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001615 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 PyObject *res;
1617 PyObject *slice = _PySlice_FromIndices(i1, i2);
1618 if (!slice)
1619 return NULL;
1620 res = mp->mp_subscript(s, slice);
1621 Py_DECREF(slice);
1622 return res;
1623 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001626}
1627
1628int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001629PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (s == NULL) {
1634 null_error();
1635 return -1;
1636 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 m = s->ob_type->tp_as_sequence;
1639 if (m && m->sq_ass_item) {
1640 if (i < 0) {
1641 if (m->sq_length) {
1642 Py_ssize_t l = (*m->sq_length)(s);
1643 if (l < 0)
1644 return -1;
1645 i += l;
1646 }
1647 }
1648 return m->sq_ass_item(s, i, o);
1649 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 type_error("'%.200s' object does not support item assignment", s);
1652 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001653}
1654
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001655int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001656PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (s == NULL) {
1661 null_error();
1662 return -1;
1663 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 m = s->ob_type->tp_as_sequence;
1666 if (m && m->sq_ass_item) {
1667 if (i < 0) {
1668 if (m->sq_length) {
1669 Py_ssize_t l = (*m->sq_length)(s);
1670 if (l < 0)
1671 return -1;
1672 i += l;
1673 }
1674 }
1675 return m->sq_ass_item(s, i, (PyObject *)NULL);
1676 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 type_error("'%.200s' object doesn't support item deletion", s);
1679 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001680}
1681
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001682int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001683PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 if (s == NULL) {
1688 null_error();
1689 return -1;
1690 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001693 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 int res;
1695 PyObject *slice = _PySlice_FromIndices(i1, i2);
1696 if (!slice)
1697 return -1;
1698 res = mp->mp_ass_subscript(s, slice, o);
1699 Py_DECREF(slice);
1700 return res;
1701 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 type_error("'%.200s' object doesn't support slice assignment", s);
1704 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001705}
1706
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001707int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001708PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (s == NULL) {
1713 null_error();
1714 return -1;
1715 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001718 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 int res;
1720 PyObject *slice = _PySlice_FromIndices(i1, i2);
1721 if (!slice)
1722 return -1;
1723 res = mp->mp_ass_subscript(s, slice, NULL);
1724 Py_DECREF(slice);
1725 return res;
1726 }
1727 type_error("'%.200s' object doesn't support slice deletion", s);
1728 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001729}
1730
Guido van Rossume15dee51995-07-18 14:12:02 +00001731PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001732PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 PyObject *it; /* iter(v) */
1735 Py_ssize_t n; /* guess for result tuple size */
1736 PyObject *result = NULL;
1737 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 if (v == NULL)
1740 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 /* Special-case the common tuple and list cases, for efficiency. */
1743 if (PyTuple_CheckExact(v)) {
1744 /* Note that we can't know whether it's safe to return
1745 a tuple *subclass* instance as-is, hence the restriction
1746 to exact tuples here. In contrast, lists always make
1747 a copy, so there's no need for exactness below. */
1748 Py_INCREF(v);
1749 return v;
1750 }
1751 if (PyList_Check(v))
1752 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 /* Get iterator. */
1755 it = PyObject_GetIter(v);
1756 if (it == NULL)
1757 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 /* Guess result size and allocate space. */
1760 n = _PyObject_LengthHint(v, 10);
1761 if (n == -1)
1762 goto Fail;
1763 result = PyTuple_New(n);
1764 if (result == NULL)
1765 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 /* Fill the tuple. */
1768 for (j = 0; ; ++j) {
1769 PyObject *item = PyIter_Next(it);
1770 if (item == NULL) {
1771 if (PyErr_Occurred())
1772 goto Fail;
1773 break;
1774 }
1775 if (j >= n) {
1776 Py_ssize_t oldn = n;
1777 /* The over-allocation strategy can grow a bit faster
1778 than for lists because unlike lists the
1779 over-allocation isn't permanent -- we reclaim
1780 the excess before the end of this routine.
1781 So, grow by ten and then add 25%.
1782 */
1783 n += 10;
1784 n += n >> 2;
1785 if (n < oldn) {
1786 /* Check for overflow */
1787 PyErr_NoMemory();
1788 Py_DECREF(item);
1789 goto Fail;
1790 }
1791 if (_PyTuple_Resize(&result, n) != 0) {
1792 Py_DECREF(item);
1793 goto Fail;
1794 }
1795 }
1796 PyTuple_SET_ITEM(result, j, item);
1797 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 /* Cut tuple back if guess was too large. */
1800 if (j < n &&
1801 _PyTuple_Resize(&result, j) != 0)
1802 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 Py_DECREF(it);
1805 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001806
1807Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 Py_XDECREF(result);
1809 Py_DECREF(it);
1810 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001811}
1812
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001813PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001814PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 PyObject *result; /* result list */
1817 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (v == NULL)
1820 return null_error();
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 result = PyList_New(0);
1823 if (result == NULL)
1824 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 rv = _PyList_Extend((PyListObject *)result, v);
1827 if (rv == NULL) {
1828 Py_DECREF(result);
1829 return NULL;
1830 }
1831 Py_DECREF(rv);
1832 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001833}
1834
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001835PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001836PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (v == NULL)
1841 return null_error();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1844 Py_INCREF(v);
1845 return v;
1846 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 it = PyObject_GetIter(v);
1849 if (it == NULL) {
1850 if (PyErr_ExceptionMatches(PyExc_TypeError))
1851 PyErr_SetString(PyExc_TypeError, m);
1852 return NULL;
1853 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 v = PySequence_List(it);
1856 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001859}
1860
Tim Peters16a77ad2001-09-08 04:00:12 +00001861/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1863 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1864 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001865 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1866*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001867Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001868_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 Py_ssize_t n;
1871 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1872 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (seq == NULL || obj == NULL) {
1875 null_error();
1876 return -1;
1877 }
Tim Peters75f8e352001-05-05 11:33:43 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 it = PyObject_GetIter(seq);
1880 if (it == NULL) {
1881 type_error("argument of type '%.200s' is not iterable", seq);
1882 return -1;
1883 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 n = wrapped = 0;
1886 for (;;) {
1887 int cmp;
1888 PyObject *item = PyIter_Next(it);
1889 if (item == NULL) {
1890 if (PyErr_Occurred())
1891 goto Fail;
1892 break;
1893 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1896 Py_DECREF(item);
1897 if (cmp < 0)
1898 goto Fail;
1899 if (cmp > 0) {
1900 switch (operation) {
1901 case PY_ITERSEARCH_COUNT:
1902 if (n == PY_SSIZE_T_MAX) {
1903 PyErr_SetString(PyExc_OverflowError,
1904 "count exceeds C integer size");
1905 goto Fail;
1906 }
1907 ++n;
1908 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 case PY_ITERSEARCH_INDEX:
1911 if (wrapped) {
1912 PyErr_SetString(PyExc_OverflowError,
1913 "index exceeds C integer size");
1914 goto Fail;
1915 }
1916 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 case PY_ITERSEARCH_CONTAINS:
1919 n = 1;
1920 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 default:
1923 assert(!"unknown operation");
1924 }
1925 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (operation == PY_ITERSEARCH_INDEX) {
1928 if (n == PY_SSIZE_T_MAX)
1929 wrapped = 1;
1930 ++n;
1931 }
1932 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 if (operation != PY_ITERSEARCH_INDEX)
1935 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyErr_SetString(PyExc_ValueError,
1938 "sequence.index(x): x not in sequence");
1939 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00001940Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 n = -1;
1942 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00001943Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 Py_DECREF(it);
1945 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001946
Guido van Rossume15dee51995-07-18 14:12:02 +00001947}
1948
Tim Peters16a77ad2001-09-08 04:00:12 +00001949/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001950Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001951PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001954}
1955
Tim Peterscb8d3682001-05-05 21:05:01 +00001956/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001957 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001958 */
1959int
1960PySequence_Contains(PyObject *seq, PyObject *ob)
1961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 Py_ssize_t result;
1963 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1964 if (sqm != NULL && sqm->sq_contains != NULL)
1965 return (*sqm->sq_contains)(seq, ob);
1966 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1967 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001968}
1969
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001970/* Backwards compatibility */
1971#undef PySequence_In
1972int
Fred Drake79912472000-07-09 04:06:11 +00001973PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001976}
1977
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001978Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001979PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001982}
1983
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001984/* Operations on mappings */
1985
1986int
Fred Drake79912472000-07-09 04:06:11 +00001987PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 return o && o->ob_type->tp_as_mapping &&
1990 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00001991}
1992
Martin v. Löwis18e16552006-02-15 17:27:45 +00001993Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001994PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (o == NULL) {
1999 null_error();
2000 return -1;
2001 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 m = o->ob_type->tp_as_mapping;
2004 if (m && m->mp_length)
2005 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 type_error("object of type '%.200s' has no len()", o);
2008 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002009}
2010
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002011#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002012Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002013PyMapping_Length(PyObject *o)
2014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002016}
2017#define PyMapping_Length PyMapping_Size
2018
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002019PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002020PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 if (key == NULL)
2025 return null_error();
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 okey = PyUnicode_FromString(key);
2028 if (okey == NULL)
2029 return NULL;
2030 r = PyObject_GetItem(o, okey);
2031 Py_DECREF(okey);
2032 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002033}
2034
2035int
Fred Drake79912472000-07-09 04:06:11 +00002036PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 PyObject *okey;
2039 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (key == NULL) {
2042 null_error();
2043 return -1;
2044 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 okey = PyUnicode_FromString(key);
2047 if (okey == NULL)
2048 return -1;
2049 r = PyObject_SetItem(o, okey, value);
2050 Py_DECREF(okey);
2051 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002052}
2053
2054int
Fred Drake79912472000-07-09 04:06:11 +00002055PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 v = PyMapping_GetItemString(o, key);
2060 if (v) {
2061 Py_DECREF(v);
2062 return 1;
2063 }
2064 PyErr_Clear();
2065 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002066}
2067
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002068int
Fred Drake79912472000-07-09 04:06:11 +00002069PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 v = PyObject_GetItem(o, key);
2074 if (v) {
2075 Py_DECREF(v);
2076 return 1;
2077 }
2078 PyErr_Clear();
2079 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002080}
2081
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002082PyObject *
2083PyMapping_Keys(PyObject *o)
2084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PyObject *keys;
2086 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (PyDict_CheckExact(o))
2089 return PyDict_Keys(o);
2090 keys = PyObject_CallMethod(o, "keys", NULL);
2091 if (keys == NULL)
2092 return NULL;
2093 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2094 Py_DECREF(keys);
2095 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002096}
2097
2098PyObject *
2099PyMapping_Items(PyObject *o)
2100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 PyObject *items;
2102 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 if (PyDict_CheckExact(o))
2105 return PyDict_Items(o);
2106 items = PyObject_CallMethod(o, "items", NULL);
2107 if (items == NULL)
2108 return NULL;
2109 fast = PySequence_Fast(items, "o.items() are not iterable");
2110 Py_DECREF(items);
2111 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002112}
2113
2114PyObject *
2115PyMapping_Values(PyObject *o)
2116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyObject *values;
2118 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 if (PyDict_CheckExact(o))
2121 return PyDict_Values(o);
2122 values = PyObject_CallMethod(o, "values", NULL);
2123 if (values == NULL)
2124 return NULL;
2125 fast = PySequence_Fast(values, "o.values() are not iterable");
2126 Py_DECREF(values);
2127 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002128}
2129
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002130/* Operations on callable objects */
2131
2132/* XXX PyCallable_Check() is in object.c */
2133
Guido van Rossume15dee51995-07-18 14:12:02 +00002134PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002135PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002138}
Guido van Rossume15dee51995-07-18 14:12:02 +00002139
2140PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002141PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
2142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 ternaryfunc call;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 if ((call = func->ob_type->tp_call) != NULL) {
2146 PyObject *result;
2147 if (Py_EnterRecursiveCall(" while calling a Python object"))
2148 return NULL;
2149 result = (*call)(func, arg, kw);
2150 Py_LeaveRecursiveCall();
2151 if (result == NULL && !PyErr_Occurred())
2152 PyErr_SetString(
2153 PyExc_SystemError,
2154 "NULL result without error in PyObject_Call");
2155 return result;
2156 }
2157 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2158 func->ob_type->tp_name);
2159 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002160}
2161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002162static PyObject*
2163call_function_tail(PyObject *callable, PyObject *args)
2164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 if (args == NULL)
2168 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 if (!PyTuple_Check(args)) {
2171 PyObject *a;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 a = PyTuple_New(1);
2174 if (a == NULL) {
2175 Py_DECREF(args);
2176 return NULL;
2177 }
2178 PyTuple_SET_ITEM(a, 0, args);
2179 args = a;
2180 }
2181 retval = PyObject_Call(callable, args, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002186}
2187
Tim Peters6d6c1a32001-08-02 04:15:00 +00002188PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002189PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 va_list va;
2192 PyObject *args;
Guido van Rossume15dee51995-07-18 14:12:02 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 if (callable == NULL)
2195 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 if (format && *format) {
2198 va_start(va, format);
2199 args = Py_VaBuildValue(format, va);
2200 va_end(va);
2201 }
2202 else
2203 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 return call_function_tail(callable, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208PyObject *
2209_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
2210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 va_list va;
2212 PyObject *args;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (callable == NULL)
2215 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 if (format && *format) {
2218 va_start(va, format);
2219 args = _Py_VaBuildValue_SizeT(format, va);
2220 va_end(va);
2221 }
2222 else
2223 args = PyTuple_New(0);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 return call_function_tail(callable, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002226}
2227
2228PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00002229PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 va_list va;
2232 PyObject *args;
2233 PyObject *func = NULL;
2234 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (o == NULL || name == NULL)
2237 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 func = PyObject_GetAttrString(o, name);
2240 if (func == NULL) {
2241 PyErr_SetString(PyExc_AttributeError, name);
2242 return 0;
2243 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (!PyCallable_Check(func)) {
2246 type_error("attribute of type '%.200s' is not callable", func);
2247 goto exit;
2248 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (format && *format) {
2251 va_start(va, format);
2252 args = Py_VaBuildValue(format, va);
2253 va_end(va);
2254 }
2255 else
2256 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 retval = call_function_tail(func, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002259
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002260 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 /* args gets consumed in call_function_tail */
2262 Py_XDECREF(func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002265}
2266
2267PyObject *
2268_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
2269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 va_list va;
2271 PyObject *args;
2272 PyObject *func = NULL;
2273 PyObject *retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (o == NULL || name == NULL)
2276 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 func = PyObject_GetAttrString(o, name);
2279 if (func == NULL) {
2280 PyErr_SetString(PyExc_AttributeError, name);
2281 return 0;
2282 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (!PyCallable_Check(func)) {
2285 type_error("attribute of type '%.200s' is not callable", func);
2286 goto exit;
2287 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 if (format && *format) {
2290 va_start(va, format);
2291 args = _Py_VaBuildValue_SizeT(format, va);
2292 va_end(va);
2293 }
2294 else
2295 args = PyTuple_New(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 retval = call_function_tail(func, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002298
2299 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 /* args gets consumed in call_function_tail */
2301 Py_XDECREF(func);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002304}
Guido van Rossum823649d2001-03-21 18:40:58 +00002305
2306
Fred Drakeb421b8c2001-10-26 16:21:32 +00002307static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002308objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 int i, n = 0;
2311 va_list countva;
2312 PyObject *result, *tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002313
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00002314 Py_VA_COPY(countva, va);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
2317 ++n;
2318 result = PyTuple_New(n);
2319 if (result != NULL && n > 0) {
2320 for (i = 0; i < n; ++i) {
2321 tmp = (PyObject *)va_arg(va, PyObject *);
2322 PyTuple_SET_ITEM(result, i, tmp);
2323 Py_INCREF(tmp);
2324 }
2325 }
2326 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002327}
2328
2329PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002330PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 PyObject *args, *tmp;
2333 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 if (callable == NULL || name == NULL)
2336 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 callable = PyObject_GetAttr(callable, name);
2339 if (callable == NULL)
2340 return NULL;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 /* count the args */
2343 va_start(vargs, name);
2344 args = objargs_mktuple(vargs);
2345 va_end(vargs);
2346 if (args == NULL) {
2347 Py_DECREF(callable);
2348 return NULL;
2349 }
2350 tmp = PyObject_Call(callable, args, NULL);
2351 Py_DECREF(args);
2352 Py_DECREF(callable);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002355}
2356
2357PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002358PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 PyObject *args, *tmp;
2361 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 if (callable == NULL)
2364 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* count the args */
2367 va_start(vargs, callable);
2368 args = objargs_mktuple(vargs);
2369 va_end(vargs);
2370 if (args == NULL)
2371 return NULL;
2372 tmp = PyObject_Call(callable, args, NULL);
2373 Py_DECREF(args);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002376}
2377
2378
Guido van Rossum823649d2001-03-21 18:40:58 +00002379/* isinstance(), issubclass() */
2380
Barry Warsawf16951c2002-04-23 22:45:44 +00002381/* abstract_get_bases() has logically 4 return states, with a sort of 0th
2382 * state that will almost never happen.
2383 *
2384 * 0. creating the __bases__ static string could get a MemoryError
2385 * 1. getattr(cls, '__bases__') could raise an AttributeError
2386 * 2. getattr(cls, '__bases__') could raise some other exception
2387 * 3. getattr(cls, '__bases__') could return a tuple
2388 * 4. getattr(cls, '__bases__') could return something other than a tuple
2389 *
2390 * Only state #3 is a non-error state and only it returns a non-NULL object
2391 * (it returns the retrieved tuple).
2392 *
2393 * Any raised AttributeErrors are masked by clearing the exception and
2394 * returning NULL. If an object other than a tuple comes out of __bases__,
2395 * then again, the return value is NULL. So yes, these two situations
2396 * produce exactly the same results: NULL is returned and no error is set.
2397 *
2398 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002400 * exception to be propagated along.
2401 *
2402 * Callers are expected to test for PyErr_Occurred() when the return value
2403 * is NULL to decide whether a valid exception should be propagated or not.
2404 * When there's no exception to propagate, it's customary for the caller to
2405 * set a TypeError.
2406 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002407static PyObject *
2408abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 static PyObject *__bases__ = NULL;
2411 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 if (__bases__ == NULL) {
2414 __bases__ = PyUnicode_InternFromString("__bases__");
2415 if (__bases__ == NULL)
2416 return NULL;
2417 }
2418 Py_ALLOW_RECURSION
2419 bases = PyObject_GetAttr(cls, __bases__);
2420 Py_END_ALLOW_RECURSION
2421 if (bases == NULL) {
2422 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2423 PyErr_Clear();
2424 return NULL;
2425 }
2426 if (!PyTuple_Check(bases)) {
2427 Py_DECREF(bases);
2428 return NULL;
2429 }
2430 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002431}
2432
2433
2434static int
2435abstract_issubclass(PyObject *derived, PyObject *cls)
2436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 PyObject *bases = NULL;
2438 Py_ssize_t i, n;
2439 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 while (1) {
2442 if (derived == cls)
2443 return 1;
2444 bases = abstract_get_bases(derived);
2445 if (bases == NULL) {
2446 if (PyErr_Occurred())
2447 return -1;
2448 return 0;
2449 }
2450 n = PyTuple_GET_SIZE(bases);
2451 if (n == 0) {
2452 Py_DECREF(bases);
2453 return 0;
2454 }
2455 /* Avoid recursivity in the single inheritance case */
2456 if (n == 1) {
2457 derived = PyTuple_GET_ITEM(bases, 0);
2458 Py_DECREF(bases);
2459 continue;
2460 }
2461 for (i = 0; i < n; i++) {
2462 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2463 if (r != 0)
2464 break;
2465 }
2466 Py_DECREF(bases);
2467 return r;
2468 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002469}
2470
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002471static int
2472check_class(PyObject *cls, const char *error)
2473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 PyObject *bases = abstract_get_bases(cls);
2475 if (bases == NULL) {
2476 /* Do not mask errors. */
2477 if (!PyErr_Occurred())
2478 PyErr_SetString(PyExc_TypeError, error);
2479 return 0;
2480 }
2481 Py_DECREF(bases);
2482 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002483}
2484
Brett Cannon4f653312004-03-20 22:52:14 +00002485static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002486recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 PyObject *icls;
2489 static PyObject *__class__ = NULL;
2490 int retval = 0;
Guido van Rossum823649d2001-03-21 18:40:58 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (__class__ == NULL) {
2493 __class__ = PyUnicode_InternFromString("__class__");
2494 if (__class__ == NULL)
2495 return -1;
2496 }
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 if (PyType_Check(cls)) {
2499 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2500 if (retval == 0) {
2501 PyObject *c = PyObject_GetAttr(inst, __class__);
2502 if (c == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002503 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002504 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002505 else
R. David Murray6bb99892010-11-20 16:33:30 +00002506 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
2508 else {
2509 if (c != (PyObject *)(inst->ob_type) &&
2510 PyType_Check(c))
2511 retval = PyType_IsSubtype(
2512 (PyTypeObject *)c,
2513 (PyTypeObject *)cls);
2514 Py_DECREF(c);
2515 }
2516 }
2517 }
2518 else {
2519 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002520 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 return -1;
2522 icls = PyObject_GetAttr(inst, __class__);
2523 if (icls == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002524 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002525 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002526 else
R. David Murray6bb99892010-11-20 16:33:30 +00002527 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 }
2529 else {
2530 retval = abstract_issubclass(icls, cls);
2531 Py_DECREF(icls);
2532 }
2533 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002536}
2537
2538int
Brett Cannon4f653312004-03-20 22:52:14 +00002539PyObject_IsInstance(PyObject *inst, PyObject *cls)
2540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 static PyObject *name = NULL;
2542 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 /* Quick test for an exact match */
2545 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2546 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 if (PyTuple_Check(cls)) {
2549 Py_ssize_t i;
2550 Py_ssize_t n;
2551 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2554 return -1;
2555 n = PyTuple_GET_SIZE(cls);
2556 for (i = 0; i < n; ++i) {
2557 PyObject *item = PyTuple_GET_ITEM(cls, i);
2558 r = PyObject_IsInstance(inst, item);
2559 if (r != 0)
2560 /* either found it, or got an error */
2561 break;
2562 }
2563 Py_LeaveRecursiveCall();
2564 return r;
2565 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
2568 if (checker != NULL) {
2569 PyObject *res;
2570 int ok = -1;
2571 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2572 Py_DECREF(checker);
2573 return ok;
2574 }
2575 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2576 Py_LeaveRecursiveCall();
2577 Py_DECREF(checker);
2578 if (res != NULL) {
2579 ok = PyObject_IsTrue(res);
2580 Py_DECREF(res);
2581 }
2582 return ok;
2583 }
2584 else if (PyErr_Occurred())
2585 return -1;
2586 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002587}
2588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002590recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 if (PyType_Check(cls) && PyType_Check(derived)) {
2593 /* Fast path (non-recursive) */
2594 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2595 }
2596 if (!check_class(derived,
2597 "issubclass() arg 1 must be a class"))
2598 return -1;
2599 if (!check_class(cls,
2600 "issubclass() arg 2 must be a class"
2601 " or tuple of classes"))
2602 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002605}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002606
Brett Cannon4f653312004-03-20 22:52:14 +00002607int
2608PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 static PyObject *name = NULL;
2611 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (PyTuple_Check(cls)) {
2614 Py_ssize_t i;
2615 Py_ssize_t n;
2616 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2619 return -1;
2620 n = PyTuple_GET_SIZE(cls);
2621 for (i = 0; i < n; ++i) {
2622 PyObject *item = PyTuple_GET_ITEM(cls, i);
2623 r = PyObject_IsSubclass(derived, item);
2624 if (r != 0)
2625 /* either found it, or got an error */
2626 break;
2627 }
2628 Py_LeaveRecursiveCall();
2629 return r;
2630 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
2633 if (checker != NULL) {
2634 PyObject *res;
2635 int ok = -1;
2636 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2637 Py_DECREF(checker);
2638 return ok;
2639 }
2640 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
2641 Py_LeaveRecursiveCall();
2642 Py_DECREF(checker);
2643 if (res != NULL) {
2644 ok = PyObject_IsTrue(res);
2645 Py_DECREF(res);
2646 }
2647 return ok;
2648 }
2649 else if (PyErr_Occurred())
2650 return -1;
2651 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002652}
2653
2654int
2655_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002658}
2659
2660int
2661_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002664}
2665
2666
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002667PyObject *
2668PyObject_GetIter(PyObject *o)
2669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 PyTypeObject *t = o->ob_type;
2671 getiterfunc f = NULL;
2672 f = t->tp_iter;
2673 if (f == NULL) {
2674 if (PySequence_Check(o))
2675 return PySeqIter_New(o);
2676 return type_error("'%.200s' object is not iterable", o);
2677 }
2678 else {
2679 PyObject *res = (*f)(o);
2680 if (res != NULL && !PyIter_Check(res)) {
2681 PyErr_Format(PyExc_TypeError,
2682 "iter() returned non-iterator "
2683 "of type '%.100s'",
2684 res->ob_type->tp_name);
2685 Py_DECREF(res);
2686 res = NULL;
2687 }
2688 return res;
2689 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002690}
2691
Tim Petersf4848da2001-05-05 00:14:56 +00002692/* Return next item.
2693 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2694 * If the iteration terminates normally, return NULL and clear the
2695 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2696 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002698 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002699PyObject *
2700PyIter_Next(PyObject *iter)
2701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 PyObject *result;
2703 result = (*iter->ob_type->tp_iternext)(iter);
2704 if (result == NULL &&
2705 PyErr_Occurred() &&
2706 PyErr_ExceptionMatches(PyExc_StopIteration))
2707 PyErr_Clear();
2708 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002709}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002710
2711
2712/*
2713 * Flatten a sequence of bytes() objects into a C array of
2714 * NULL terminated string pointers with a NULL char* terminating the array.
2715 * (ie: an argv or env list)
2716 *
2717 * Memory allocated for the returned list is allocated using malloc() and MUST
2718 * be freed by the caller using a free() loop or _Py_FreeCharPArray().
2719 */
2720char *const *
2721_PySequence_BytesToCharpArray(PyObject* self)
2722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 char **array;
2724 Py_ssize_t i, argc;
2725 PyObject *item = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 argc = PySequence_Size(self);
2728 if (argc == -1)
2729 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 array = malloc((argc + 1) * sizeof(char *));
2732 if (array == NULL) {
2733 PyErr_NoMemory();
2734 return NULL;
2735 }
2736 for (i = 0; i < argc; ++i) {
2737 char *data;
2738 item = PySequence_GetItem(self, i);
2739 data = PyBytes_AsString(item);
2740 if (data == NULL) {
2741 /* NULL terminate before freeing. */
2742 array[i] = NULL;
2743 goto fail;
2744 }
2745 array[i] = strdup(data);
2746 if (!array[i]) {
2747 PyErr_NoMemory();
2748 goto fail;
2749 }
2750 Py_DECREF(item);
2751 }
2752 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002755
2756fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_XDECREF(item);
2758 _Py_FreeCharPArray(array);
2759 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002760}
2761
2762
2763/* Free's a NULL terminated char** array of C strings. */
2764void
2765_Py_FreeCharPArray(char *const array[])
2766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 Py_ssize_t i;
2768 for (i = 0; array[i] != NULL; ++i) {
2769 free(array[i]);
2770 }
2771 free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002772}