blob: 783a83c3ed091e3e015e6d596e0412fce980b3f3 [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
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020067int
68_PyObject_HasLen(PyObject *o) {
69 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
70 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
71}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000072
Christian Heimes255f53b2007-12-08 15:33:56 +000073/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020074 or o.__length_hint__(). If those methods aren't found the defaultvalue is
75 returned. If one of the calls fails with an exception other than TypeError
76 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000077*/
78
79Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020080PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000081{
Christian Heimesb70e8a12012-10-06 17:16:39 +020082 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020083 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050084 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030085 if (_PyObject_HasLen(o)) {
86 res = PyObject_Length(o);
87 if (res < 0 && PyErr_Occurred()) {
88 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
89 return -1;
90 }
91 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020092 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +030093 else {
94 return res;
95 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 }
Christian Heimes6314d162012-10-06 17:13:29 +020097 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020098 if (hint == NULL) {
99 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return defaultvalue;
103 }
Christian Heimesb70e8a12012-10-06 17:16:39 +0200104 result = PyObject_CallFunctionObjArgs(hint, NULL);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200105 Py_DECREF(hint);
106 if (result == NULL) {
107 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
108 PyErr_Clear();
109 return defaultvalue;
110 }
111 return -1;
112 }
113 else if (result == Py_NotImplemented) {
114 Py_DECREF(result);
115 return defaultvalue;
116 }
117 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200118 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200119 Py_TYPE(result)->tp_name);
120 Py_DECREF(result);
121 return -1;
122 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200123 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200124 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200125 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200126 return -1;
127 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200128 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200129 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
130 return -1;
131 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200132 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000133}
134
Guido van Rossume15dee51995-07-18 14:12:02 +0000135PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000136PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (o == NULL || key == NULL)
141 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 m = o->ob_type->tp_as_mapping;
144 if (m && m->mp_subscript)
145 return m->mp_subscript(o, key);
Guido van Rossume15dee51995-07-18 14:12:02 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (o->ob_type->tp_as_sequence) {
148 if (PyIndex_Check(key)) {
149 Py_ssize_t key_value;
150 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
151 if (key_value == -1 && PyErr_Occurred())
152 return NULL;
153 return PySequence_GetItem(o, key_value);
154 }
155 else if (o->ob_type->tp_as_sequence->sq_item)
156 return type_error("sequence index must "
157 "be integer, not '%.200s'", key);
158 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000161}
162
163int
Fred Drake79912472000-07-09 04:06:11 +0000164PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (o == NULL || key == NULL || value == NULL) {
169 null_error();
170 return -1;
171 }
172 m = o->ob_type->tp_as_mapping;
173 if (m && m->mp_ass_subscript)
174 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (o->ob_type->tp_as_sequence) {
177 if (PyIndex_Check(key)) {
178 Py_ssize_t key_value;
179 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
180 if (key_value == -1 && PyErr_Occurred())
181 return -1;
182 return PySequence_SetItem(o, key_value, value);
183 }
184 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
185 type_error("sequence index must be "
186 "integer, not '%.200s'", key);
187 return -1;
188 }
189 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 type_error("'%.200s' object does not support item assignment", o);
192 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000193}
194
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000195int
Fred Drake79912472000-07-09 04:06:11 +0000196PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (o == NULL || key == NULL) {
201 null_error();
202 return -1;
203 }
204 m = o->ob_type->tp_as_mapping;
205 if (m && m->mp_ass_subscript)
206 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (o->ob_type->tp_as_sequence) {
209 if (PyIndex_Check(key)) {
210 Py_ssize_t key_value;
211 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
212 if (key_value == -1 && PyErr_Occurred())
213 return -1;
214 return PySequence_DelItem(o, key_value);
215 }
216 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
217 type_error("sequence index must be "
218 "integer, not '%.200s'", key);
219 return -1;
220 }
221 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 type_error("'%.200s' object does not support item deletion", o);
224 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000225}
226
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000227int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300228PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 PyObject *okey;
231 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (o == NULL || key == NULL) {
234 null_error();
235 return -1;
236 }
237 okey = PyUnicode_FromString(key);
238 if (okey == NULL)
239 return -1;
240 ret = PyObject_DelItem(o, okey);
241 Py_DECREF(okey);
242 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000243}
244
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000245/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000246 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000247 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000248int
249PyObject_AsCharBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 const char **buffer,
251 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyBufferProcs *pb;
254 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
257 null_error();
258 return -1;
259 }
260 pb = obj->ob_type->tp_as_buffer;
261 if (pb == NULL || pb->bf_getbuffer == NULL) {
262 PyErr_SetString(PyExc_TypeError,
Victor Stinnerf2a94212011-05-30 23:21:30 +0200263 "expected bytes, bytearray "
264 "or buffer compatible object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return -1;
266 }
267 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 *buffer = view.buf;
270 *buffer_len = view.len;
271 if (pb->bf_releasebuffer != NULL)
272 (*pb->bf_releasebuffer)(obj, &view);
273 Py_XDECREF(view.obj);
274 return 0;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000275}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000276
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000277int
278PyObject_CheckReadBuffer(PyObject *obj)
279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
281 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (pb == NULL ||
284 pb->bf_getbuffer == NULL)
285 return 0;
286 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
287 PyErr_Clear();
288 return 0;
289 }
290 PyBuffer_Release(&view);
291 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000292}
293
294int PyObject_AsReadBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 const void **buffer,
296 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyBufferProcs *pb;
299 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
302 null_error();
303 return -1;
304 }
305 pb = obj->ob_type->tp_as_buffer;
306 if (pb == NULL ||
307 pb->bf_getbuffer == NULL) {
308 PyErr_SetString(PyExc_TypeError,
309 "expected an object with a buffer interface");
310 return -1;
311 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 *buffer = view.buf;
316 *buffer_len = view.len;
317 if (pb->bf_releasebuffer != NULL)
318 (*pb->bf_releasebuffer)(obj, &view);
319 Py_XDECREF(view.obj);
320 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000321}
322
323int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 void **buffer,
325 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyBufferProcs *pb;
328 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
331 null_error();
332 return -1;
333 }
334 pb = obj->ob_type->tp_as_buffer;
335 if (pb == NULL ||
336 pb->bf_getbuffer == NULL ||
337 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
338 PyErr_SetString(PyExc_TypeError,
339 "expected an object with a writable buffer interface");
340 return -1;
341 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 *buffer = view.buf;
344 *buffer_len = view.len;
345 if (pb->bf_releasebuffer != NULL)
346 (*pb->bf_releasebuffer)(obj, &view);
347 Py_XDECREF(view.obj);
348 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000349}
350
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000351/* Buffer C-API for Python 3.0 */
352
353int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000354PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (!PyObject_CheckBuffer(obj)) {
357 PyErr_Format(PyExc_TypeError,
Victor Stinner0a5f65a2011-03-22 01:09:21 +0100358 "'%.100s' does not support the buffer interface",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 Py_TYPE(obj)->tp_name);
360 return -1;
361 }
362 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000363}
364
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000365static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100366_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 Py_ssize_t sd, dim;
369 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (view->ndim == 0) return 1;
372 if (view->strides == NULL) return (view->ndim == 1);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 sd = view->itemsize;
375 if (view->ndim == 1) return (view->shape[0] == 1 ||
376 sd == view->strides[0]);
377 for (i=0; i<view->ndim; i++) {
378 dim = view->shape[i];
379 if (dim == 0) return 1;
380 if (view->strides[i] != sd) return 0;
381 sd *= dim;
382 }
383 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000384}
385
386static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100387_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_ssize_t sd, dim;
390 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (view->ndim == 0) return 1;
393 if (view->strides == NULL) return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 sd = view->itemsize;
396 if (view->ndim == 1) return (view->shape[0] == 1 ||
397 sd == view->strides[0]);
398 for (i=view->ndim-1; i>=0; i--) {
399 dim = view->shape[i];
400 if (dim == 0) return 1;
401 if (view->strides[i] != sd) return 0;
402 sd *= dim;
403 }
404 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000405}
406
407int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100408PyBuffer_IsContiguous(const Py_buffer *view, char order)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000409{
410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000412
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100413 if (order == 'C')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return _IsCContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100415 else if (order == 'F')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 return _IsFortranContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100417 else if (order == 'A')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 return (_IsCContiguous(view) || _IsFortranContiguous(view));
419 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000420}
421
422
Guido van Rossum98297ee2007-11-06 21:34:58 +0000423void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000424PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 char* pointer;
427 int i;
428 pointer = (char *)view->buf;
429 for (i = 0; i < view->ndim; i++) {
430 pointer += view->strides[i]*indices[i];
431 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
432 pointer = *((char**)pointer) + view->suboffsets[i];
433 }
434 }
435 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000436}
437
438
Guido van Rossum98297ee2007-11-06 21:34:58 +0000439void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000440_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 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 for (k=0; k<nd; k++) {
445 if (index[k] < shape[k]-1) {
446 index[k]++;
447 break;
448 }
449 else {
450 index[k] = 0;
451 }
452 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000453}
454
Guido van Rossum98297ee2007-11-06 21:34:58 +0000455void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000456_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 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 for (k=nd-1; k>=0; k--) {
461 if (index[k] < shape[k]-1) {
462 index[k]++;
463 break;
464 }
465 else {
466 index[k] = 0;
467 }
468 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000469}
470
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000471int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000472PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000475 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 Py_ssize_t *indices, elements;
477 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (len > view->len) {
480 len = view->len;
481 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (PyBuffer_IsContiguous(view, fort)) {
484 /* simplest copy is all that is needed */
485 memcpy(view->buf, buf, len);
486 return 0;
487 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 /* XXX(nnorwitz): need to check for overflow! */
492 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
493 if (indices == NULL) {
494 PyErr_NoMemory();
495 return -1;
496 }
497 for (k=0; k<view->ndim;k++) {
498 indices[k] = 0;
499 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000502 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
504 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000505 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 }
507 src = buf;
508 /* XXX : This is not going to be the fastest code in the world
509 several optimizations are possible.
510 */
511 elements = len / view->itemsize;
512 while (elements--) {
513 addone(view->ndim, indices, view->shape);
514 ptr = PyBuffer_GetPointer(view, indices);
515 memcpy(ptr, src, view->itemsize);
516 src += view->itemsize;
517 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyMem_Free(indices);
520 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000521}
522
Guido van Rossum98297ee2007-11-06 21:34:58 +0000523int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_buffer view_dest, view_src;
526 int k;
527 Py_ssize_t *indices, elements;
528 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (!PyObject_CheckBuffer(dest) ||
531 !PyObject_CheckBuffer(src)) {
532 PyErr_SetString(PyExc_TypeError,
533 "both destination and source must have the "\
534 "buffer interface");
535 return -1;
536 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
539 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
540 PyBuffer_Release(&view_dest);
541 return -1;
542 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (view_dest.len < view_src.len) {
545 PyErr_SetString(PyExc_BufferError,
546 "destination is too small to receive data from source");
547 PyBuffer_Release(&view_dest);
548 PyBuffer_Release(&view_src);
549 return -1;
550 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
553 PyBuffer_IsContiguous(&view_src, 'C')) ||
554 (PyBuffer_IsContiguous(&view_dest, 'F') &&
555 PyBuffer_IsContiguous(&view_src, 'F'))) {
556 /* simplest copy is all that is needed */
557 memcpy(view_dest.buf, view_src.buf, view_src.len);
558 PyBuffer_Release(&view_dest);
559 PyBuffer_Release(&view_src);
560 return 0;
561 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* XXX(nnorwitz): need to check for overflow! */
566 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
567 if (indices == NULL) {
568 PyErr_NoMemory();
569 PyBuffer_Release(&view_dest);
570 PyBuffer_Release(&view_src);
571 return -1;
572 }
573 for (k=0; k<view_src.ndim;k++) {
574 indices[k] = 0;
575 }
576 elements = 1;
577 for (k=0; k<view_src.ndim; k++) {
578 /* XXX(nnorwitz): can this overflow? */
579 elements *= view_src.shape[k];
580 }
581 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000582 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 dptr = PyBuffer_GetPointer(&view_dest, indices);
584 sptr = PyBuffer_GetPointer(&view_src, indices);
585 memcpy(dptr, sptr, view_src.itemsize);
586 }
587 PyMem_Free(indices);
588 PyBuffer_Release(&view_dest);
589 PyBuffer_Release(&view_src);
590 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000591}
592
593void
594PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 Py_ssize_t *strides, int itemsize,
596 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 int k;
599 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 sd = itemsize;
602 if (fort == 'F') {
603 for (k=0; k<nd; k++) {
604 strides[k] = sd;
605 sd *= shape[k];
606 }
607 }
608 else {
609 for (k=nd-1; k>=0; k--) {
610 strides[k] = sd;
611 sd *= shape[k];
612 }
613 }
614 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000615}
616
617int
Martin v. Löwis423be952008-08-13 15:53:07 +0000618PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Stefan Krah4e141742012-03-06 15:27:31 +0100619 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000620{
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100621 if (view == NULL) return 0; /* XXX why not -1? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
623 (readonly == 1)) {
624 PyErr_SetString(PyExc_BufferError,
625 "Object is not writable.");
626 return -1;
627 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 view->obj = obj;
630 if (obj)
631 Py_INCREF(obj);
632 view->buf = buf;
633 view->len = len;
634 view->readonly = readonly;
635 view->itemsize = 1;
636 view->format = NULL;
637 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
638 view->format = "B";
639 view->ndim = 1;
640 view->shape = NULL;
641 if ((flags & PyBUF_ND) == PyBUF_ND)
642 view->shape = &(view->len);
643 view->strides = NULL;
644 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
645 view->strides = &(view->itemsize);
646 view->suboffsets = NULL;
647 view->internal = NULL;
648 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000649}
650
Martin v. Löwis423be952008-08-13 15:53:07 +0000651void
652PyBuffer_Release(Py_buffer *view)
653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyObject *obj = view->obj;
655 if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
656 Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
657 Py_XDECREF(obj);
658 view->obj = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000659}
660
Eric Smith8fd3eba2008-02-17 19:48:00 +0000661PyObject *
662PyObject_Format(PyObject *obj, PyObject *format_spec)
663{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000664 PyObject *meth;
665 PyObject *empty = NULL;
666 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500667 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000668
669 /* If no format_spec is provided, use an empty string */
670 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100671 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000672 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000673 }
674
Eric Smith8fd3eba2008-02-17 19:48:00 +0000675 /* Find the (unbound!) __format__ method (a borrowed reference) */
Benjamin Petersonce798522012-01-22 11:24:29 -0500676 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000677 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000678 if (!PyErr_Occurred())
679 PyErr_Format(PyExc_TypeError,
680 "Type %.100s doesn't define __format__",
681 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000683 }
684
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000685 /* And call it. */
686 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000687 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000688
689 if (result && !PyUnicode_Check(result)) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000690 PyErr_SetString(PyExc_TypeError,
691 "__format__ method did not return string");
692 Py_DECREF(result);
693 result = NULL;
694 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000695 }
696
697done:
698 Py_XDECREF(empty);
699 return result;
700}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000701/* Operations on numbers */
702
703int
Fred Drake79912472000-07-09 04:06:11 +0000704PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return o && o->ob_type->tp_as_number &&
707 (o->ob_type->tp_as_number->nb_int ||
708 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000709}
710
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000711/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000712
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000713#define NB_SLOT(x) offsetof(PyNumberMethods, x)
714#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000716#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000718
719/*
720 Calling scheme used for binary operations:
721
Neal Norwitz4886cc32006-08-21 17:06:07 +0000722 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000724
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000725 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
726 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000727 */
728
729static PyObject *
730binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyObject *x;
733 binaryfunc slotv = NULL;
734 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (v->ob_type->tp_as_number != NULL)
737 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
738 if (w->ob_type != v->ob_type &&
739 w->ob_type->tp_as_number != NULL) {
740 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
741 if (slotw == slotv)
742 slotw = NULL;
743 }
744 if (slotv) {
745 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
746 x = slotw(v, w);
747 if (x != Py_NotImplemented)
748 return x;
749 Py_DECREF(x); /* can't do it */
750 slotw = NULL;
751 }
752 x = slotv(v, w);
753 if (x != Py_NotImplemented)
754 return x;
755 Py_DECREF(x); /* can't do it */
756 }
757 if (slotw) {
758 x = slotw(v, w);
759 if (x != Py_NotImplemented)
760 return x;
761 Py_DECREF(x); /* can't do it */
762 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500763 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000764}
Guido van Rossum77660912002-04-16 16:32:50 +0000765
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000766static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000767binop_type_error(PyObject *v, PyObject *w, const char *op_name)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyErr_Format(PyExc_TypeError,
770 "unsupported operand type(s) for %.100s: "
771 "'%.100s' and '%.100s'",
772 op_name,
773 v->ob_type->tp_name,
774 w->ob_type->tp_name);
775 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000776}
777
778static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000779binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 PyObject *result = binary_op1(v, w, op_slot);
782 if (result == Py_NotImplemented) {
783 Py_DECREF(result);
784 return binop_type_error(v, w, op_name);
785 }
786 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000787}
788
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000789
790/*
791 Calling scheme used for ternary operations:
792
Neal Norwitz4886cc32006-08-21 17:06:07 +0000793 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000795 */
796
797static PyObject *
798ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyObject *w,
800 PyObject *z,
801 const int op_slot,
802 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyNumberMethods *mv, *mw, *mz;
805 PyObject *x = NULL;
806 ternaryfunc slotv = NULL;
807 ternaryfunc slotw = NULL;
808 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 mv = v->ob_type->tp_as_number;
811 mw = w->ob_type->tp_as_number;
812 if (mv != NULL)
813 slotv = NB_TERNOP(mv, op_slot);
814 if (w->ob_type != v->ob_type &&
815 mw != NULL) {
816 slotw = NB_TERNOP(mw, op_slot);
817 if (slotw == slotv)
818 slotw = NULL;
819 }
820 if (slotv) {
821 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
822 x = slotw(v, w, z);
823 if (x != Py_NotImplemented)
824 return x;
825 Py_DECREF(x); /* can't do it */
826 slotw = NULL;
827 }
828 x = slotv(v, w, z);
829 if (x != Py_NotImplemented)
830 return x;
831 Py_DECREF(x); /* can't do it */
832 }
833 if (slotw) {
834 x = slotw(v, w, z);
835 if (x != Py_NotImplemented)
836 return x;
837 Py_DECREF(x); /* can't do it */
838 }
839 mz = z->ob_type->tp_as_number;
840 if (mz != NULL) {
841 slotz = NB_TERNOP(mz, op_slot);
842 if (slotz == slotv || slotz == slotw)
843 slotz = NULL;
844 if (slotz) {
845 x = slotz(v, w, z);
846 if (x != Py_NotImplemented)
847 return x;
848 Py_DECREF(x); /* can't do it */
849 }
850 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (z == Py_None)
853 PyErr_Format(
854 PyExc_TypeError,
855 "unsupported operand type(s) for ** or pow(): "
856 "'%.100s' and '%.100s'",
857 v->ob_type->tp_name,
858 w->ob_type->tp_name);
859 else
860 PyErr_Format(
861 PyExc_TypeError,
862 "unsupported operand type(s) for pow(): "
863 "'%.100s', '%.100s', '%.100s'",
864 v->ob_type->tp_name,
865 w->ob_type->tp_name,
866 z->ob_type->tp_name);
867 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000868}
869
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000870#define BINARY_FUNC(func, op, op_name) \
871 PyObject * \
872 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000874 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000875
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000876BINARY_FUNC(PyNumber_Or, nb_or, "|")
877BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
878BINARY_FUNC(PyNumber_And, nb_and, "&")
879BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
880BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
881BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000882BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000883
884PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000885PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
888 if (result == Py_NotImplemented) {
889 PySequenceMethods *m = v->ob_type->tp_as_sequence;
890 Py_DECREF(result);
891 if (m && m->sq_concat) {
892 return (*m->sq_concat)(v, w);
893 }
894 result = binop_type_error(v, w, "+");
895 }
896 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000897}
898
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000899static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000900sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Py_ssize_t count;
903 if (PyIndex_Check(n)) {
904 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
905 if (count == -1 && PyErr_Occurred())
906 return NULL;
907 }
908 else {
909 return type_error("can't multiply sequence by "
910 "non-int of type '%.200s'", n);
911 }
912 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000913}
914
915PyObject *
916PyNumber_Multiply(PyObject *v, PyObject *w)
917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
919 if (result == Py_NotImplemented) {
920 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
921 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
922 Py_DECREF(result);
923 if (mv && mv->sq_repeat) {
924 return sequence_repeat(mv->sq_repeat, v, w);
925 }
926 else if (mw && mw->sq_repeat) {
927 return sequence_repeat(mw->sq_repeat, w, v);
928 }
929 result = binop_type_error(v, w, "*");
930 }
931 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000932}
933
Guido van Rossume15dee51995-07-18 14:12:02 +0000934PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -0400935PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
936{
937 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
938}
939
940PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000941PyNumber_FloorDivide(PyObject *v, PyObject *w)
942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000944}
945
946PyObject *
947PyNumber_TrueDivide(PyObject *v, PyObject *w)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +0000950}
951
952PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000953PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000956}
957
958PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000959PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000962}
963
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000964/* Binary in-place operators */
965
966/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000967 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000968
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000969 - If the left hand object has the appropriate struct members, and
970 they are filled, call the appropriate function and return the
971 result. No coercion is done on the arguments; the left-hand object
972 is the one the operation is performed on, and it's up to the
973 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +0000974
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000975 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000976 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000977
978 */
979
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000980static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000981binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyNumberMethods *mv = v->ob_type->tp_as_number;
984 if (mv != NULL) {
985 binaryfunc slot = NB_BINOP(mv, iop_slot);
986 if (slot) {
987 PyObject *x = (slot)(v, w);
988 if (x != Py_NotImplemented) {
989 return x;
990 }
991 Py_DECREF(x);
992 }
993 }
994 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000995}
996
997static PyObject *
998binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1002 if (result == Py_NotImplemented) {
1003 Py_DECREF(result);
1004 return binop_type_error(v, w, op_name);
1005 }
1006 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001007}
1008
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001009#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyObject * \
1011 func(PyObject *v, PyObject *w) { \
1012 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1013 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001014
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001015INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1016INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1017INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1018INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1019INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1020INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001021INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001022
1023PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001024PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1027 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001028}
1029
1030PyObject *
1031PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1034 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001035}
1036
1037PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001038PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1041 NB_SLOT(nb_add));
1042 if (result == Py_NotImplemented) {
1043 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1044 Py_DECREF(result);
1045 if (m != NULL) {
1046 binaryfunc f = NULL;
1047 f = m->sq_inplace_concat;
1048 if (f == NULL)
1049 f = m->sq_concat;
1050 if (f != NULL)
1051 return (*f)(v, w);
1052 }
1053 result = binop_type_error(v, w, "+=");
1054 }
1055 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001056}
1057
1058PyObject *
1059PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1062 NB_SLOT(nb_multiply));
1063 if (result == Py_NotImplemented) {
1064 ssizeargfunc f = NULL;
1065 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1066 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1067 Py_DECREF(result);
1068 if (mv != NULL) {
1069 f = mv->sq_inplace_repeat;
1070 if (f == NULL)
1071 f = mv->sq_repeat;
1072 if (f != NULL)
1073 return sequence_repeat(f, v, w);
1074 }
1075 else if (mw != NULL) {
1076 /* Note that the right hand operand should not be
1077 * mutated in this case so sq_inplace_repeat is not
1078 * used. */
1079 if (mw->sq_repeat)
1080 return sequence_repeat(mw->sq_repeat, w, v);
1081 }
1082 result = binop_type_error(v, w, "*=");
1083 }
1084 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001085}
1086
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001087PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001088PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1089{
1090 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1091 NB_SLOT(nb_matrix_multiply), "@=");
1092}
1093
1094PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001095PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1098 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001099}
1100
1101PyObject *
1102PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (v->ob_type->tp_as_number &&
1105 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1106 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1107 }
1108 else {
1109 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1110 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001111}
1112
1113
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001114/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001115
1116PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001117PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (o == NULL)
1122 return null_error();
1123 m = o->ob_type->tp_as_number;
1124 if (m && m->nb_negative)
1125 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001128}
1129
1130PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001131PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (o == NULL)
1136 return null_error();
1137 m = o->ob_type->tp_as_number;
1138 if (m && m->nb_positive)
1139 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001142}
1143
1144PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001145PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (o == NULL)
1150 return null_error();
1151 m = o->ob_type->tp_as_number;
1152 if (m && m->nb_invert)
1153 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001156}
1157
1158PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001159PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (o == NULL)
1164 return null_error();
1165 m = o->ob_type->tp_as_number;
1166 if (m && m->nb_absolute)
1167 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001170}
1171
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001172/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001173 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001174 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001175*/
1176PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001177PyNumber_Index(PyObject *item)
1178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 PyObject *result = NULL;
1180 if (item == NULL)
1181 return null_error();
1182 if (PyLong_Check(item)) {
1183 Py_INCREF(item);
1184 return item;
1185 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001186 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 PyErr_Format(PyExc_TypeError,
1188 "'%.200s' object cannot be interpreted "
1189 "as an integer", item->ob_type->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001190 return NULL;
1191 }
1192 result = item->ob_type->tp_as_number->nb_index(item);
1193 if (!result || PyLong_CheckExact(result))
1194 return result;
1195 if (!PyLong_Check(result)) {
1196 PyErr_Format(PyExc_TypeError,
1197 "__index__ returned non-int (type %.200s)",
1198 result->ob_type->tp_name);
1199 Py_DECREF(result);
1200 return NULL;
1201 }
1202 /* Issue #17576: warn if 'result' not of exact type int. */
1203 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1204 "__index__ returned non-int (type %.200s). "
1205 "The ability to return an instance of a strict subclass of int "
1206 "is deprecated, and may be removed in a future version of Python.",
1207 result->ob_type->tp_name)) {
1208 Py_DECREF(result);
1209 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
1211 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001212}
1213
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001214/* Return an error on Overflow only if err is not NULL*/
1215
1216Py_ssize_t
1217PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 Py_ssize_t result;
1220 PyObject *runerr;
1221 PyObject *value = PyNumber_Index(item);
1222 if (value == NULL)
1223 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 /* We're done if PyLong_AsSsize_t() returns without error. */
1226 result = PyLong_AsSsize_t(value);
1227 if (result != -1 || !(runerr = PyErr_Occurred()))
1228 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 /* Error handling code -- only manage OverflowError differently */
1231 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1232 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyErr_Clear();
1235 /* If no error-handling desired then the default clipping
1236 is sufficient.
1237 */
1238 if (!err) {
1239 assert(PyLong_Check(value));
1240 /* Whether or not it is less than or equal to
1241 zero is determined by the sign of ob_size
1242 */
1243 if (_PyLong_Sign(value) < 0)
1244 result = PY_SSIZE_T_MIN;
1245 else
1246 result = PY_SSIZE_T_MAX;
1247 }
1248 else {
1249 /* Otherwise replace the error with caller's error object. */
1250 PyErr_Format(err,
1251 "cannot fit '%.200s' into an index-sized integer",
1252 item->ob_type->tp_name);
1253 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001254
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001255 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 Py_DECREF(value);
1257 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001258}
1259
1260
Guido van Rossume15dee51995-07-18 14:12:02 +00001261PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001262PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 PyObject *trunc_func;
1266 const char *buffer;
1267 Py_ssize_t buffer_len;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001268 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (o == NULL)
1271 return null_error();
1272 if (PyLong_CheckExact(o)) {
1273 Py_INCREF(o);
1274 return o;
1275 }
1276 m = o->ob_type->tp_as_number;
1277 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001278 return (PyObject *)_PyLong_FromNbInt(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001280 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (trunc_func) {
1282 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
1283 PyObject *int_instance;
1284 Py_DECREF(trunc_func);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001285 if (truncated == NULL || PyLong_Check(truncated))
1286 return truncated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 /* __trunc__ is specified to return an Integral type,
Benjamin Peterson520e8502012-03-21 14:51:14 -04001288 but int() needs to return a int. */
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001289 m = truncated->ob_type->tp_as_number;
1290 if (m == NULL || m->nb_int == NULL) {
1291 PyErr_Format(
1292 PyExc_TypeError,
1293 "__trunc__ returned non-Integral (type %.200s)",
1294 truncated->ob_type->tp_name);
1295 Py_DECREF(truncated);
1296 return NULL;
1297 }
1298 int_instance = (PyObject *)_PyLong_FromNbInt(truncated);
1299 Py_DECREF(truncated);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 return int_instance;
1301 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001302 if (PyErr_Occurred())
1303 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (PyBytes_Check(o))
1306 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001307 * doesn't do. In particular int('9\x005') must raise an
1308 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 */
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001310 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1311 PyBytes_GET_SIZE(o), 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (PyUnicode_Check(o))
1313 /* The above check is done in PyLong_FromUnicode(). */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001314 return PyLong_FromUnicodeObject(o, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001316 return _PyLong_FromBytes(buffer, buffer_len, 10);
Guido van Rossume15dee51995-07-18 14:12:02 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 return type_error("int() argument must be a string or a "
1319 "number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001320}
1321
1322PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001323PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if (o == NULL)
1328 return null_error();
1329 m = o->ob_type->tp_as_number;
1330 if (m && m->nb_float) { /* This should include subclasses of float */
1331 PyObject *res = m->nb_float(o);
1332 if (res && !PyFloat_Check(res)) {
1333 PyErr_Format(PyExc_TypeError,
1334 "__float__ returned non-float (type %.200s)",
1335 res->ob_type->tp_name);
1336 Py_DECREF(res);
1337 return NULL;
1338 }
1339 return res;
1340 }
1341 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
1342 PyFloatObject *po = (PyFloatObject *)o;
1343 return PyFloat_FromDouble(po->ob_fval);
1344 }
1345 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001346}
1347
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001348
1349PyObject *
1350PyNumber_ToBase(PyObject *n, int base)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyObject *res = NULL;
1353 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (!index)
1356 return NULL;
1357 if (PyLong_Check(index))
1358 res = _PyLong_Format(index, base);
1359 else
1360 /* It should not be possible to get here, as
1361 PyNumber_Index already has a check for the same
1362 condition */
Serhiy Storchaka95949422013-08-27 19:40:23 +03001363 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 Py_DECREF(index);
1365 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001366}
1367
1368
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001369/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001370
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001371int
Fred Drake79912472000-07-09 04:06:11 +00001372PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (PyDict_Check(s))
1375 return 0;
1376 return s != NULL && s->ob_type->tp_as_sequence &&
1377 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001378}
1379
Martin v. Löwis18e16552006-02-15 17:27:45 +00001380Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001381PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (s == NULL) {
1386 null_error();
1387 return -1;
1388 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 m = s->ob_type->tp_as_sequence;
1391 if (m && m->sq_length)
1392 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 type_error("object of type '%.200s' has no len()", s);
1395 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001396}
1397
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001398#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001399Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001400PySequence_Length(PyObject *s)
1401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001403}
1404#define PySequence_Length PySequence_Size
1405
Guido van Rossume15dee51995-07-18 14:12:02 +00001406PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001407PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (s == NULL || o == NULL)
1412 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 m = s->ob_type->tp_as_sequence;
1415 if (m && m->sq_concat)
1416 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 /* Instances of user classes defining an __add__() method only
1419 have an nb_add slot, not an sq_concat slot. So we fall back
1420 to nb_add if both arguments appear to be sequences. */
1421 if (PySequence_Check(s) && PySequence_Check(o)) {
1422 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1423 if (result != Py_NotImplemented)
1424 return result;
1425 Py_DECREF(result);
1426 }
1427 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001428}
1429
1430PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001431PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (o == NULL)
1436 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 m = o->ob_type->tp_as_sequence;
1439 if (m && m->sq_repeat)
1440 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* Instances of user classes defining a __mul__() method only
1443 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1444 to nb_multiply if o appears to be a sequence. */
1445 if (PySequence_Check(o)) {
1446 PyObject *n, *result;
1447 n = PyLong_FromSsize_t(count);
1448 if (n == NULL)
1449 return NULL;
1450 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1451 Py_DECREF(n);
1452 if (result != Py_NotImplemented)
1453 return result;
1454 Py_DECREF(result);
1455 }
1456 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001457}
1458
1459PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001460PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (s == NULL || o == NULL)
1465 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 m = s->ob_type->tp_as_sequence;
1468 if (m && m->sq_inplace_concat)
1469 return m->sq_inplace_concat(s, o);
1470 if (m && m->sq_concat)
1471 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (PySequence_Check(s) && PySequence_Check(o)) {
1474 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1475 NB_SLOT(nb_add));
1476 if (result != Py_NotImplemented)
1477 return result;
1478 Py_DECREF(result);
1479 }
1480 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001481}
1482
1483PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001484PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 if (o == NULL)
1489 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 m = o->ob_type->tp_as_sequence;
1492 if (m && m->sq_inplace_repeat)
1493 return m->sq_inplace_repeat(o, count);
1494 if (m && m->sq_repeat)
1495 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (PySequence_Check(o)) {
1498 PyObject *n, *result;
1499 n = PyLong_FromSsize_t(count);
1500 if (n == NULL)
1501 return NULL;
1502 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1503 NB_SLOT(nb_multiply));
1504 Py_DECREF(n);
1505 if (result != Py_NotImplemented)
1506 return result;
1507 Py_DECREF(result);
1508 }
1509 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001510}
1511
1512PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001513PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (s == NULL)
1518 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 m = s->ob_type->tp_as_sequence;
1521 if (m && m->sq_item) {
1522 if (i < 0) {
1523 if (m->sq_length) {
1524 Py_ssize_t l = (*m->sq_length)(s);
1525 if (l < 0)
1526 return NULL;
1527 i += l;
1528 }
1529 }
1530 return m->sq_item(s, i);
1531 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001534}
1535
1536PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001537PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001544 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 PyObject *res;
1546 PyObject *slice = _PySlice_FromIndices(i1, i2);
1547 if (!slice)
1548 return NULL;
1549 res = mp->mp_subscript(s, slice);
1550 Py_DECREF(slice);
1551 return res;
1552 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001555}
1556
1557int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001558PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (s == NULL) {
1563 null_error();
1564 return -1;
1565 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 m = s->ob_type->tp_as_sequence;
1568 if (m && m->sq_ass_item) {
1569 if (i < 0) {
1570 if (m->sq_length) {
1571 Py_ssize_t l = (*m->sq_length)(s);
1572 if (l < 0)
1573 return -1;
1574 i += l;
1575 }
1576 }
1577 return m->sq_ass_item(s, i, o);
1578 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 type_error("'%.200s' object does not support item assignment", s);
1581 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001582}
1583
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001584int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001585PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (s == NULL) {
1590 null_error();
1591 return -1;
1592 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 m = s->ob_type->tp_as_sequence;
1595 if (m && m->sq_ass_item) {
1596 if (i < 0) {
1597 if (m->sq_length) {
1598 Py_ssize_t l = (*m->sq_length)(s);
1599 if (l < 0)
1600 return -1;
1601 i += l;
1602 }
1603 }
1604 return m->sq_ass_item(s, i, (PyObject *)NULL);
1605 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 type_error("'%.200s' object doesn't support item deletion", s);
1608 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001609}
1610
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001611int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001612PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (s == NULL) {
1617 null_error();
1618 return -1;
1619 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001622 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 int res;
1624 PyObject *slice = _PySlice_FromIndices(i1, i2);
1625 if (!slice)
1626 return -1;
1627 res = mp->mp_ass_subscript(s, slice, o);
1628 Py_DECREF(slice);
1629 return res;
1630 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 type_error("'%.200s' object doesn't support slice assignment", s);
1633 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001634}
1635
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001636int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001637PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (s == NULL) {
1642 null_error();
1643 return -1;
1644 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001647 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 int res;
1649 PyObject *slice = _PySlice_FromIndices(i1, i2);
1650 if (!slice)
1651 return -1;
1652 res = mp->mp_ass_subscript(s, slice, NULL);
1653 Py_DECREF(slice);
1654 return res;
1655 }
1656 type_error("'%.200s' object doesn't support slice deletion", s);
1657 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001658}
1659
Guido van Rossume15dee51995-07-18 14:12:02 +00001660PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001661PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PyObject *it; /* iter(v) */
1664 Py_ssize_t n; /* guess for result tuple size */
1665 PyObject *result = NULL;
1666 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (v == NULL)
1669 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 /* Special-case the common tuple and list cases, for efficiency. */
1672 if (PyTuple_CheckExact(v)) {
1673 /* Note that we can't know whether it's safe to return
1674 a tuple *subclass* instance as-is, hence the restriction
1675 to exact tuples here. In contrast, lists always make
1676 a copy, so there's no need for exactness below. */
1677 Py_INCREF(v);
1678 return v;
1679 }
1680 if (PyList_Check(v))
1681 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 /* Get iterator. */
1684 it = PyObject_GetIter(v);
1685 if (it == NULL)
1686 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001689 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (n == -1)
1691 goto Fail;
1692 result = PyTuple_New(n);
1693 if (result == NULL)
1694 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 /* Fill the tuple. */
1697 for (j = 0; ; ++j) {
1698 PyObject *item = PyIter_Next(it);
1699 if (item == NULL) {
1700 if (PyErr_Occurred())
1701 goto Fail;
1702 break;
1703 }
1704 if (j >= n) {
1705 Py_ssize_t oldn = n;
1706 /* The over-allocation strategy can grow a bit faster
1707 than for lists because unlike lists the
1708 over-allocation isn't permanent -- we reclaim
1709 the excess before the end of this routine.
1710 So, grow by ten and then add 25%.
1711 */
1712 n += 10;
1713 n += n >> 2;
1714 if (n < oldn) {
1715 /* Check for overflow */
1716 PyErr_NoMemory();
1717 Py_DECREF(item);
1718 goto Fail;
1719 }
1720 if (_PyTuple_Resize(&result, n) != 0) {
1721 Py_DECREF(item);
1722 goto Fail;
1723 }
1724 }
1725 PyTuple_SET_ITEM(result, j, item);
1726 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 /* Cut tuple back if guess was too large. */
1729 if (j < n &&
1730 _PyTuple_Resize(&result, j) != 0)
1731 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 Py_DECREF(it);
1734 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001735
1736Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 Py_XDECREF(result);
1738 Py_DECREF(it);
1739 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001740}
1741
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001742PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001743PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 PyObject *result; /* result list */
1746 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (v == NULL)
1749 return null_error();
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 result = PyList_New(0);
1752 if (result == NULL)
1753 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 rv = _PyList_Extend((PyListObject *)result, v);
1756 if (rv == NULL) {
1757 Py_DECREF(result);
1758 return NULL;
1759 }
1760 Py_DECREF(rv);
1761 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001762}
1763
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001764PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001765PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (v == NULL)
1770 return null_error();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1773 Py_INCREF(v);
1774 return v;
1775 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 it = PyObject_GetIter(v);
1778 if (it == NULL) {
1779 if (PyErr_ExceptionMatches(PyExc_TypeError))
1780 PyErr_SetString(PyExc_TypeError, m);
1781 return NULL;
1782 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 v = PySequence_List(it);
1785 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001788}
1789
Tim Peters16a77ad2001-09-08 04:00:12 +00001790/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1792 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1793 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001794 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1795*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001796Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001797_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 Py_ssize_t n;
1800 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1801 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 if (seq == NULL || obj == NULL) {
1804 null_error();
1805 return -1;
1806 }
Tim Peters75f8e352001-05-05 11:33:43 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 it = PyObject_GetIter(seq);
1809 if (it == NULL) {
1810 type_error("argument of type '%.200s' is not iterable", seq);
1811 return -1;
1812 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 n = wrapped = 0;
1815 for (;;) {
1816 int cmp;
1817 PyObject *item = PyIter_Next(it);
1818 if (item == NULL) {
1819 if (PyErr_Occurred())
1820 goto Fail;
1821 break;
1822 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1825 Py_DECREF(item);
1826 if (cmp < 0)
1827 goto Fail;
1828 if (cmp > 0) {
1829 switch (operation) {
1830 case PY_ITERSEARCH_COUNT:
1831 if (n == PY_SSIZE_T_MAX) {
1832 PyErr_SetString(PyExc_OverflowError,
1833 "count exceeds C integer size");
1834 goto Fail;
1835 }
1836 ++n;
1837 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 case PY_ITERSEARCH_INDEX:
1840 if (wrapped) {
1841 PyErr_SetString(PyExc_OverflowError,
1842 "index exceeds C integer size");
1843 goto Fail;
1844 }
1845 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 case PY_ITERSEARCH_CONTAINS:
1848 n = 1;
1849 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 default:
1852 assert(!"unknown operation");
1853 }
1854 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (operation == PY_ITERSEARCH_INDEX) {
1857 if (n == PY_SSIZE_T_MAX)
1858 wrapped = 1;
1859 ++n;
1860 }
1861 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (operation != PY_ITERSEARCH_INDEX)
1864 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyErr_SetString(PyExc_ValueError,
1867 "sequence.index(x): x not in sequence");
1868 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00001869Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 n = -1;
1871 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00001872Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 Py_DECREF(it);
1874 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001875
Guido van Rossume15dee51995-07-18 14:12:02 +00001876}
1877
Tim Peters16a77ad2001-09-08 04:00:12 +00001878/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001879Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001880PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001883}
1884
Tim Peterscb8d3682001-05-05 21:05:01 +00001885/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001886 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001887 */
1888int
1889PySequence_Contains(PyObject *seq, PyObject *ob)
1890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 Py_ssize_t result;
1892 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1893 if (sqm != NULL && sqm->sq_contains != NULL)
1894 return (*sqm->sq_contains)(seq, ob);
1895 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1896 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001897}
1898
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001899/* Backwards compatibility */
1900#undef PySequence_In
1901int
Fred Drake79912472000-07-09 04:06:11 +00001902PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001905}
1906
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001907Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001908PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001911}
1912
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001913/* Operations on mappings */
1914
1915int
Fred Drake79912472000-07-09 04:06:11 +00001916PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001917{
Benjamin Peterson21992272011-12-28 12:01:31 -06001918 return o && o->ob_type->tp_as_mapping &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00001920}
1921
Martin v. Löwis18e16552006-02-15 17:27:45 +00001922Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001923PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (o == NULL) {
1928 null_error();
1929 return -1;
1930 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 m = o->ob_type->tp_as_mapping;
1933 if (m && m->mp_length)
1934 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 type_error("object of type '%.200s' has no len()", o);
1937 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001938}
1939
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001940#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001941Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001942PyMapping_Length(PyObject *o)
1943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001945}
1946#define PyMapping_Length PyMapping_Size
1947
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001948PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001949PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 if (key == NULL)
1954 return null_error();
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 okey = PyUnicode_FromString(key);
1957 if (okey == NULL)
1958 return NULL;
1959 r = PyObject_GetItem(o, okey);
1960 Py_DECREF(okey);
1961 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001962}
1963
1964int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001965PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyObject *okey;
1968 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (key == NULL) {
1971 null_error();
1972 return -1;
1973 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 okey = PyUnicode_FromString(key);
1976 if (okey == NULL)
1977 return -1;
1978 r = PyObject_SetItem(o, okey, value);
1979 Py_DECREF(okey);
1980 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001981}
1982
1983int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001984PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 v = PyMapping_GetItemString(o, key);
1989 if (v) {
1990 Py_DECREF(v);
1991 return 1;
1992 }
1993 PyErr_Clear();
1994 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00001995}
1996
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001997int
Fred Drake79912472000-07-09 04:06:11 +00001998PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 v = PyObject_GetItem(o, key);
2003 if (v) {
2004 Py_DECREF(v);
2005 return 1;
2006 }
2007 PyErr_Clear();
2008 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002009}
2010
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002011PyObject *
2012PyMapping_Keys(PyObject *o)
2013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 PyObject *keys;
2015 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002016 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 if (PyDict_CheckExact(o))
2019 return PyDict_Keys(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002020 keys = _PyObject_CallMethodId(o, &PyId_keys, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (keys == NULL)
2022 return NULL;
2023 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2024 Py_DECREF(keys);
2025 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002026}
2027
2028PyObject *
2029PyMapping_Items(PyObject *o)
2030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 PyObject *items;
2032 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002033 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 if (PyDict_CheckExact(o))
2036 return PyDict_Items(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002037 items = _PyObject_CallMethodId(o, &PyId_items, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (items == NULL)
2039 return NULL;
2040 fast = PySequence_Fast(items, "o.items() are not iterable");
2041 Py_DECREF(items);
2042 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002043}
2044
2045PyObject *
2046PyMapping_Values(PyObject *o)
2047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 PyObject *values;
2049 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002050 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 if (PyDict_CheckExact(o))
2053 return PyDict_Values(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002054 values = _PyObject_CallMethodId(o, &PyId_values, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (values == NULL)
2056 return NULL;
2057 fast = PySequence_Fast(values, "o.values() are not iterable");
2058 Py_DECREF(values);
2059 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002060}
2061
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002062/* Operations on callable objects */
2063
2064/* XXX PyCallable_Check() is in object.c */
2065
Guido van Rossume15dee51995-07-18 14:12:02 +00002066PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002067PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002070}
Guido van Rossume15dee51995-07-18 14:12:02 +00002071
2072PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002073PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
2074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 ternaryfunc call;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if ((call = func->ob_type->tp_call) != NULL) {
2078 PyObject *result;
2079 if (Py_EnterRecursiveCall(" while calling a Python object"))
2080 return NULL;
2081 result = (*call)(func, arg, kw);
2082 Py_LeaveRecursiveCall();
Victor Stinner3de58692013-07-15 17:50:07 +02002083#ifdef NDEBUG
2084 if (result == NULL && !PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PyErr_SetString(
2086 PyExc_SystemError,
2087 "NULL result without error in PyObject_Call");
Victor Stinner3de58692013-07-15 17:50:07 +02002088 }
2089#else
Victor Stinner4ac9c002013-12-19 13:47:35 +01002090 assert((result != NULL && !PyErr_Occurred())
2091 || (result == NULL && PyErr_Occurred()));
Victor Stinner3de58692013-07-15 17:50:07 +02002092#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 return result;
2094 }
2095 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2096 func->ob_type->tp_name);
2097 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002098}
2099
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002100static PyObject*
2101call_function_tail(PyObject *callable, PyObject *args)
2102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (args == NULL)
2106 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (!PyTuple_Check(args)) {
2109 PyObject *a;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 a = PyTuple_New(1);
2112 if (a == NULL) {
2113 Py_DECREF(args);
2114 return NULL;
2115 }
2116 PyTuple_SET_ITEM(a, 0, args);
2117 args = a;
2118 }
2119 retval = PyObject_Call(callable, args, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002124}
2125
Tim Peters6d6c1a32001-08-02 04:15:00 +00002126PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002127PyObject_CallFunction(PyObject *callable, const char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 va_list va;
2130 PyObject *args;
Guido van Rossume15dee51995-07-18 14:12:02 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (callable == NULL)
2133 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 if (format && *format) {
2136 va_start(va, format);
2137 args = Py_VaBuildValue(format, va);
2138 va_end(va);
2139 }
2140 else
2141 args = PyTuple_New(0);
Victor Stinner0b0c8672013-10-29 19:29:52 +01002142 if (args == NULL)
2143 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 return call_function_tail(callable, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002146}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002148PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002149_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 va_list va;
2152 PyObject *args;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 if (callable == NULL)
2155 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (format && *format) {
2158 va_start(va, format);
2159 args = _Py_VaBuildValue_SizeT(format, va);
2160 va_end(va);
2161 }
2162 else
2163 args = PyTuple_New(0);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 return call_function_tail(callable, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002166}
2167
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002168static PyObject*
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002169callmethod(PyObject* func, const char *format, va_list va, int is_size_t)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002170{
2171 PyObject *retval = NULL;
2172 PyObject *args;
2173
2174 if (!PyCallable_Check(func)) {
2175 type_error("attribute of type '%.200s' is not callable", func);
2176 goto exit;
2177 }
2178
2179 if (format && *format) {
2180 if (is_size_t)
2181 args = _Py_VaBuildValue_SizeT(format, va);
2182 else
2183 args = Py_VaBuildValue(format, va);
2184 }
2185 else
2186 args = PyTuple_New(0);
2187
2188 retval = call_function_tail(func, args);
2189
2190 exit:
2191 /* args gets consumed in call_function_tail */
2192 Py_XDECREF(func);
2193
2194 return retval;
2195}
2196
Guido van Rossume15dee51995-07-18 14:12:02 +00002197PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002198PyObject_CallMethod(PyObject *o, const char *name, const char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 va_list va;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 PyObject *func = NULL;
2202 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (o == NULL || name == NULL)
2205 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 func = PyObject_GetAttrString(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002208 if (func == NULL)
2209 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002210
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002211 va_start(va, format);
2212 retval = callmethod(func, format, va, 0);
2213 va_end(va);
2214 return retval;
2215}
2216
2217PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002218_PyObject_CallMethodId(PyObject *o, _Py_Identifier *name,
2219 const char *format, ...)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002220{
2221 va_list va;
2222 PyObject *func = NULL;
2223 PyObject *retval = NULL;
2224
2225 if (o == NULL || name == NULL)
2226 return null_error();
2227
2228 func = _PyObject_GetAttrId(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002229 if (func == NULL)
2230 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002231
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002232 va_start(va, format);
2233 retval = callmethod(func, format, va, 0);
2234 va_end(va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002236}
2237
2238PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002239_PyObject_CallMethod_SizeT(PyObject *o, const char *name,
2240 const char *format, ...)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 va_list va;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 PyObject *func = NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002244 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 if (o == NULL || name == NULL)
2247 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 func = PyObject_GetAttrString(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002250 if (func == NULL)
2251 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002252 va_start(va, format);
2253 retval = callmethod(func, format, va, 1);
2254 va_end(va);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002256}
Guido van Rossum823649d2001-03-21 18:40:58 +00002257
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002258PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002259_PyObject_CallMethodId_SizeT(PyObject *o, _Py_Identifier *name,
2260 const char *format, ...)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002261{
2262 va_list va;
2263 PyObject *func = NULL;
2264 PyObject *retval;
2265
2266 if (o == NULL || name == NULL)
2267 return null_error();
2268
2269 func = _PyObject_GetAttrId(o, name);
2270 if (func == NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002271 return NULL;
2272 }
2273 va_start(va, format);
2274 retval = callmethod(func, format, va, 1);
2275 va_end(va);
2276 return retval;
2277}
Guido van Rossum823649d2001-03-21 18:40:58 +00002278
Fred Drakeb421b8c2001-10-26 16:21:32 +00002279static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002280objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 int i, n = 0;
2283 va_list countva;
2284 PyObject *result, *tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002285
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00002286 Py_VA_COPY(countva, va);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
2289 ++n;
2290 result = PyTuple_New(n);
2291 if (result != NULL && n > 0) {
2292 for (i = 0; i < n; ++i) {
2293 tmp = (PyObject *)va_arg(va, PyObject *);
2294 PyTuple_SET_ITEM(result, i, tmp);
2295 Py_INCREF(tmp);
2296 }
2297 }
2298 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002299}
2300
2301PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002302PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 PyObject *args, *tmp;
2305 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (callable == NULL || name == NULL)
2308 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 callable = PyObject_GetAttr(callable, name);
2311 if (callable == NULL)
2312 return NULL;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 /* count the args */
2315 va_start(vargs, name);
2316 args = objargs_mktuple(vargs);
2317 va_end(vargs);
2318 if (args == NULL) {
2319 Py_DECREF(callable);
2320 return NULL;
2321 }
2322 tmp = PyObject_Call(callable, args, NULL);
2323 Py_DECREF(args);
2324 Py_DECREF(callable);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002327}
2328
2329PyObject *
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07002330_PyObject_CallMethodIdObjArgs(PyObject *callable,
Brett Cannonfd074152012-04-14 14:10:13 -04002331 struct _Py_Identifier *name, ...)
2332{
2333 PyObject *args, *tmp;
2334 va_list vargs;
2335
2336 if (callable == NULL || name == NULL)
2337 return null_error();
2338
2339 callable = _PyObject_GetAttrId(callable, name);
2340 if (callable == NULL)
2341 return NULL;
2342
2343 /* count the args */
2344 va_start(vargs, name);
2345 args = objargs_mktuple(vargs);
2346 va_end(vargs);
2347 if (args == NULL) {
2348 Py_DECREF(callable);
2349 return NULL;
2350 }
2351 tmp = PyObject_Call(callable, args, NULL);
2352 Py_DECREF(args);
2353 Py_DECREF(callable);
2354
2355 return tmp;
2356}
2357
2358PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002359PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 PyObject *args, *tmp;
2362 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (callable == NULL)
2365 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* count the args */
2368 va_start(vargs, callable);
2369 args = objargs_mktuple(vargs);
2370 va_end(vargs);
2371 if (args == NULL)
2372 return NULL;
2373 tmp = PyObject_Call(callable, args, NULL);
2374 Py_DECREF(args);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002377}
2378
2379
Guido van Rossum823649d2001-03-21 18:40:58 +00002380/* isinstance(), issubclass() */
2381
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002382/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002383 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002384 * 1. getattr(cls, '__bases__') could raise an AttributeError
2385 * 2. getattr(cls, '__bases__') could raise some other exception
2386 * 3. getattr(cls, '__bases__') could return a tuple
2387 * 4. getattr(cls, '__bases__') could return something other than a tuple
2388 *
2389 * Only state #3 is a non-error state and only it returns a non-NULL object
2390 * (it returns the retrieved tuple).
2391 *
2392 * Any raised AttributeErrors are masked by clearing the exception and
2393 * returning NULL. If an object other than a tuple comes out of __bases__,
2394 * then again, the return value is NULL. So yes, these two situations
2395 * produce exactly the same results: NULL is returned and no error is set.
2396 *
2397 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002399 * exception to be propagated along.
2400 *
2401 * Callers are expected to test for PyErr_Occurred() when the return value
2402 * is NULL to decide whether a valid exception should be propagated or not.
2403 * When there's no exception to propagate, it's customary for the caller to
2404 * set a TypeError.
2405 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002406static PyObject *
2407abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002408{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002409 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 Py_ALLOW_RECURSION
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002413 bases = _PyObject_GetAttrId(cls, &PyId___bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_END_ALLOW_RECURSION
2415 if (bases == NULL) {
2416 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2417 PyErr_Clear();
2418 return NULL;
2419 }
2420 if (!PyTuple_Check(bases)) {
2421 Py_DECREF(bases);
2422 return NULL;
2423 }
2424 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002425}
2426
2427
2428static int
2429abstract_issubclass(PyObject *derived, PyObject *cls)
2430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 PyObject *bases = NULL;
2432 Py_ssize_t i, n;
2433 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 while (1) {
2436 if (derived == cls)
2437 return 1;
2438 bases = abstract_get_bases(derived);
2439 if (bases == NULL) {
2440 if (PyErr_Occurred())
2441 return -1;
2442 return 0;
2443 }
2444 n = PyTuple_GET_SIZE(bases);
2445 if (n == 0) {
2446 Py_DECREF(bases);
2447 return 0;
2448 }
2449 /* Avoid recursivity in the single inheritance case */
2450 if (n == 1) {
2451 derived = PyTuple_GET_ITEM(bases, 0);
2452 Py_DECREF(bases);
2453 continue;
2454 }
2455 for (i = 0; i < n; i++) {
2456 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2457 if (r != 0)
2458 break;
2459 }
2460 Py_DECREF(bases);
2461 return r;
2462 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002463}
2464
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002465static int
2466check_class(PyObject *cls, const char *error)
2467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 PyObject *bases = abstract_get_bases(cls);
2469 if (bases == NULL) {
2470 /* Do not mask errors. */
2471 if (!PyErr_Occurred())
2472 PyErr_SetString(PyExc_TypeError, error);
2473 return 0;
2474 }
2475 Py_DECREF(bases);
2476 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002477}
2478
Brett Cannon4f653312004-03-20 22:52:14 +00002479static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002480recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 PyObject *icls;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 int retval = 0;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002484 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (PyType_Check(cls)) {
2487 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2488 if (retval == 0) {
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002489 PyObject *c = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (c == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002491 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002492 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002493 else
R. David Murray6bb99892010-11-20 16:33:30 +00002494 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 }
2496 else {
2497 if (c != (PyObject *)(inst->ob_type) &&
2498 PyType_Check(c))
2499 retval = PyType_IsSubtype(
2500 (PyTypeObject *)c,
2501 (PyTypeObject *)cls);
2502 Py_DECREF(c);
2503 }
2504 }
2505 }
2506 else {
2507 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002508 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 return -1;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002510 icls = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 if (icls == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002512 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002513 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002514 else
R. David Murray6bb99892010-11-20 16:33:30 +00002515 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 }
2517 else {
2518 retval = abstract_issubclass(icls, cls);
2519 Py_DECREF(icls);
2520 }
2521 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002524}
2525
2526int
Brett Cannon4f653312004-03-20 22:52:14 +00002527PyObject_IsInstance(PyObject *inst, PyObject *cls)
2528{
Benjamin Petersonce798522012-01-22 11:24:29 -05002529 _Py_IDENTIFIER(__instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 /* Quick test for an exact match */
2533 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2534 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 if (PyTuple_Check(cls)) {
2537 Py_ssize_t i;
2538 Py_ssize_t n;
2539 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2542 return -1;
2543 n = PyTuple_GET_SIZE(cls);
2544 for (i = 0; i < n; ++i) {
2545 PyObject *item = PyTuple_GET_ITEM(cls, i);
2546 r = PyObject_IsInstance(inst, item);
2547 if (r != 0)
2548 /* either found it, or got an error */
2549 break;
2550 }
2551 Py_LeaveRecursiveCall();
2552 return r;
2553 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002554
Benjamin Petersonce798522012-01-22 11:24:29 -05002555 checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 if (checker != NULL) {
2557 PyObject *res;
2558 int ok = -1;
2559 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2560 Py_DECREF(checker);
2561 return ok;
2562 }
2563 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2564 Py_LeaveRecursiveCall();
2565 Py_DECREF(checker);
2566 if (res != NULL) {
2567 ok = PyObject_IsTrue(res);
2568 Py_DECREF(res);
2569 }
2570 return ok;
2571 }
2572 else if (PyErr_Occurred())
2573 return -1;
2574 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002575}
2576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002578recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 if (PyType_Check(cls) && PyType_Check(derived)) {
2581 /* Fast path (non-recursive) */
2582 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2583 }
2584 if (!check_class(derived,
2585 "issubclass() arg 1 must be a class"))
2586 return -1;
2587 if (!check_class(cls,
2588 "issubclass() arg 2 must be a class"
2589 " or tuple of classes"))
2590 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002593}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002594
Brett Cannon4f653312004-03-20 22:52:14 +00002595int
2596PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2597{
Benjamin Petersonce798522012-01-22 11:24:29 -05002598 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 if (PyTuple_Check(cls)) {
2602 Py_ssize_t i;
2603 Py_ssize_t n;
2604 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2607 return -1;
2608 n = PyTuple_GET_SIZE(cls);
2609 for (i = 0; i < n; ++i) {
2610 PyObject *item = PyTuple_GET_ITEM(cls, i);
2611 r = PyObject_IsSubclass(derived, item);
2612 if (r != 0)
2613 /* either found it, or got an error */
2614 break;
2615 }
2616 Py_LeaveRecursiveCall();
2617 return r;
2618 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002619
Benjamin Petersonce798522012-01-22 11:24:29 -05002620 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 if (checker != NULL) {
2622 PyObject *res;
2623 int ok = -1;
2624 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2625 Py_DECREF(checker);
2626 return ok;
2627 }
2628 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
2629 Py_LeaveRecursiveCall();
2630 Py_DECREF(checker);
2631 if (res != NULL) {
2632 ok = PyObject_IsTrue(res);
2633 Py_DECREF(res);
2634 }
2635 return ok;
2636 }
2637 else if (PyErr_Occurred())
2638 return -1;
2639 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002640}
2641
2642int
2643_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002646}
2647
2648int
2649_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002652}
2653
2654
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002655PyObject *
2656PyObject_GetIter(PyObject *o)
2657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 PyTypeObject *t = o->ob_type;
2659 getiterfunc f = NULL;
2660 f = t->tp_iter;
2661 if (f == NULL) {
2662 if (PySequence_Check(o))
2663 return PySeqIter_New(o);
2664 return type_error("'%.200s' object is not iterable", o);
2665 }
2666 else {
2667 PyObject *res = (*f)(o);
2668 if (res != NULL && !PyIter_Check(res)) {
2669 PyErr_Format(PyExc_TypeError,
2670 "iter() returned non-iterator "
2671 "of type '%.100s'",
2672 res->ob_type->tp_name);
2673 Py_DECREF(res);
2674 res = NULL;
2675 }
2676 return res;
2677 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002678}
2679
Tim Petersf4848da2001-05-05 00:14:56 +00002680/* Return next item.
2681 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2682 * If the iteration terminates normally, return NULL and clear the
2683 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2684 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002686 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002687PyObject *
2688PyIter_Next(PyObject *iter)
2689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 PyObject *result;
2691 result = (*iter->ob_type->tp_iternext)(iter);
2692 if (result == NULL &&
2693 PyErr_Occurred() &&
2694 PyErr_ExceptionMatches(PyExc_StopIteration))
2695 PyErr_Clear();
2696 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002697}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002698
2699
2700/*
2701 * Flatten a sequence of bytes() objects into a C array of
2702 * NULL terminated string pointers with a NULL char* terminating the array.
2703 * (ie: an argv or env list)
2704 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002705 * Memory allocated for the returned list is allocated using PyMem_Malloc()
2706 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002707 */
2708char *const *
2709_PySequence_BytesToCharpArray(PyObject* self)
2710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 char **array;
2712 Py_ssize_t i, argc;
2713 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002714 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 argc = PySequence_Size(self);
2717 if (argc == -1)
2718 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002719
Stefan Krah7cacd2e2012-08-21 08:16:09 +02002720 assert(argc >= 0);
2721
2722 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2723 PyErr_NoMemory();
2724 return NULL;
2725 }
2726
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002727 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (array == NULL) {
2729 PyErr_NoMemory();
2730 return NULL;
2731 }
2732 for (i = 0; i < argc; ++i) {
2733 char *data;
2734 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02002735 if (item == NULL) {
2736 /* NULL terminate before freeing. */
2737 array[i] = NULL;
2738 goto fail;
2739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 data = PyBytes_AsString(item);
2741 if (data == NULL) {
2742 /* NULL terminate before freeing. */
2743 array[i] = NULL;
2744 goto fail;
2745 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002746 size = PyBytes_GET_SIZE(item) + 1;
2747 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 if (!array[i]) {
2749 PyErr_NoMemory();
2750 goto fail;
2751 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002752 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 Py_DECREF(item);
2754 }
2755 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002758
2759fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 Py_XDECREF(item);
2761 _Py_FreeCharPArray(array);
2762 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002763}
2764
2765
2766/* Free's a NULL terminated char** array of C strings. */
2767void
2768_Py_FreeCharPArray(char *const array[])
2769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 Py_ssize_t i;
2771 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002772 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002774 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002775}