blob: 9e5405df0a9b18f7b03a219649197ac3d7275bb4 [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
Victor Stinner71aea8e2016-08-19 16:59:55 +020035 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +020037 }
38
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 v = (PyObject *)o->ob_type;
40 Py_INCREF(v);
41 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000042}
43
Martin v. Löwis18e16552006-02-15 17:27:45 +000044Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000045PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (o == NULL) {
50 null_error();
51 return -1;
52 }
Guido van Rossume15dee51995-07-18 14:12:02 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 m = o->ob_type->tp_as_sequence;
55 if (m && m->sq_length)
56 return m->sq_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000059}
60
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000061#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000062Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000063PyObject_Length(PyObject *o)
64{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000066}
67#define PyObject_Length PyObject_Size
68
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020069int
70_PyObject_HasLen(PyObject *o) {
71 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
72 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
73}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000074
Christian Heimes255f53b2007-12-08 15:33:56 +000075/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020076 or o.__length_hint__(). If those methods aren't found the defaultvalue is
77 returned. If one of the calls fails with an exception other than TypeError
78 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000079*/
80
81Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020082PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000083{
Christian Heimesb70e8a12012-10-06 17:16:39 +020084 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020085 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050086 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030087 if (_PyObject_HasLen(o)) {
88 res = PyObject_Length(o);
89 if (res < 0 && PyErr_Occurred()) {
90 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
91 return -1;
92 }
93 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020094 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +030095 else {
96 return res;
97 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 }
Christian Heimes6314d162012-10-06 17:13:29 +020099 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200100 if (hint == NULL) {
101 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return defaultvalue;
105 }
Christian Heimesb70e8a12012-10-06 17:16:39 +0200106 result = PyObject_CallFunctionObjArgs(hint, NULL);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200107 Py_DECREF(hint);
108 if (result == NULL) {
109 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
110 PyErr_Clear();
111 return defaultvalue;
112 }
113 return -1;
114 }
115 else if (result == Py_NotImplemented) {
116 Py_DECREF(result);
117 return defaultvalue;
118 }
119 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200120 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200121 Py_TYPE(result)->tp_name);
122 Py_DECREF(result);
123 return -1;
124 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200125 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200126 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200127 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200128 return -1;
129 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200130 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200131 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
132 return -1;
133 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200134 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000135}
136
Guido van Rossume15dee51995-07-18 14:12:02 +0000137PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000138PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000141
Victor Stinner71aea8e2016-08-19 16:59:55 +0200142 if (o == NULL || key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +0200144 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 m = o->ob_type->tp_as_mapping;
Victor Stinnere20310f2015-11-05 13:56:58 +0100147 if (m && m->mp_subscript) {
148 PyObject *item = m->mp_subscript(o, key);
149 assert((item != NULL) ^ (PyErr_Occurred() != NULL));
150 return item;
151 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (o->ob_type->tp_as_sequence) {
154 if (PyIndex_Check(key)) {
155 Py_ssize_t key_value;
156 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
157 if (key_value == -1 && PyErr_Occurred())
158 return NULL;
159 return PySequence_GetItem(o, key_value);
160 }
161 else if (o->ob_type->tp_as_sequence->sq_item)
162 return type_error("sequence index must "
163 "be integer, not '%.200s'", key);
164 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000167}
168
169int
Fred Drake79912472000-07-09 04:06:11 +0000170PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (o == NULL || key == NULL || value == NULL) {
175 null_error();
176 return -1;
177 }
178 m = o->ob_type->tp_as_mapping;
179 if (m && m->mp_ass_subscript)
180 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 if (o->ob_type->tp_as_sequence) {
183 if (PyIndex_Check(key)) {
184 Py_ssize_t key_value;
185 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
186 if (key_value == -1 && PyErr_Occurred())
187 return -1;
188 return PySequence_SetItem(o, key_value, value);
189 }
190 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
191 type_error("sequence index must be "
192 "integer, not '%.200s'", key);
193 return -1;
194 }
195 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 type_error("'%.200s' object does not support item assignment", o);
198 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000199}
200
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000201int
Fred Drake79912472000-07-09 04:06:11 +0000202PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (o == NULL || key == NULL) {
207 null_error();
208 return -1;
209 }
210 m = o->ob_type->tp_as_mapping;
211 if (m && m->mp_ass_subscript)
212 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (o->ob_type->tp_as_sequence) {
215 if (PyIndex_Check(key)) {
216 Py_ssize_t key_value;
217 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
218 if (key_value == -1 && PyErr_Occurred())
219 return -1;
220 return PySequence_DelItem(o, key_value);
221 }
222 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
223 type_error("sequence index must be "
224 "integer, not '%.200s'", key);
225 return -1;
226 }
227 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 type_error("'%.200s' object does not support item deletion", o);
230 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000231}
232
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000233int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300234PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 PyObject *okey;
237 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (o == NULL || key == NULL) {
240 null_error();
241 return -1;
242 }
243 okey = PyUnicode_FromString(key);
244 if (okey == NULL)
245 return -1;
246 ret = PyObject_DelItem(o, okey);
247 Py_DECREF(okey);
248 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000249}
250
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000251/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000252 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000253 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000254int
255PyObject_AsCharBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 const char **buffer,
257 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000258{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200259 return PyObject_AsReadBuffer(obj, (const void **)buffer, buffer_len);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000260}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000261
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000262int
263PyObject_CheckReadBuffer(PyObject *obj)
264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
266 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (pb == NULL ||
269 pb->bf_getbuffer == NULL)
270 return 0;
271 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
272 PyErr_Clear();
273 return 0;
274 }
275 PyBuffer_Release(&view);
276 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277}
278
279int PyObject_AsReadBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 const void **buffer,
281 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
286 null_error();
287 return -1;
288 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200289 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 *buffer = view.buf;
293 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200294 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000296}
297
298int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 void **buffer,
300 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyBufferProcs *pb;
303 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
306 null_error();
307 return -1;
308 }
309 pb = obj->ob_type->tp_as_buffer;
310 if (pb == NULL ||
311 pb->bf_getbuffer == NULL ||
312 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
313 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400314 "expected a writable bytes-like object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 return -1;
316 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 *buffer = view.buf;
319 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200320 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000322}
323
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000324/* Buffer C-API for Python 3.0 */
325
326int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000327PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000328{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200329 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
330
331 if (pb == NULL || pb->bf_getbuffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400333 "a bytes-like object is required, not '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 Py_TYPE(obj)->tp_name);
335 return -1;
336 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200337 return (*pb->bf_getbuffer)(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000338}
339
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000340static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100341_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_ssize_t sd, dim;
344 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000345
Stefan Krah363af442015-02-01 14:53:54 +0100346 /* 1) len = product(shape) * itemsize
347 2) itemsize > 0
348 3) len = 0 <==> exists i: shape[i] = 0 */
349 if (view->len == 0) return 1;
350 if (view->strides == NULL) { /* C-contiguous by definition */
351 /* Trivially F-contiguous */
352 if (view->ndim <= 1) return 1;
353
354 /* ndim > 1 implies shape != NULL */
355 assert(view->shape != NULL);
356
357 /* Effectively 1-d */
358 sd = 0;
359 for (i=0; i<view->ndim; i++) {
360 if (view->shape[i] > 1) sd += 1;
361 }
362 return sd <= 1;
363 }
364
365 /* strides != NULL implies both of these */
366 assert(view->ndim > 0);
367 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 for (i=0; i<view->ndim; i++) {
371 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100372 if (dim > 1 && view->strides[i] != sd) {
373 return 0;
374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 sd *= dim;
376 }
377 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000378}
379
380static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100381_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_ssize_t sd, dim;
384 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000385
Stefan Krah363af442015-02-01 14:53:54 +0100386 /* 1) len = product(shape) * itemsize
387 2) itemsize > 0
388 3) len = 0 <==> exists i: shape[i] = 0 */
389 if (view->len == 0) return 1;
390 if (view->strides == NULL) return 1; /* C-contiguous by definition */
391
392 /* strides != NULL implies both of these */
393 assert(view->ndim > 0);
394 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 for (i=view->ndim-1; i>=0; i--) {
398 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100399 if (dim > 1 && view->strides[i] != sd) {
400 return 0;
401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 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
Stefan Krah7213fcc2015-02-01 16:19:23 +0100491 /* view->ndim <= 64 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 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--) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 ptr = PyBuffer_GetPointer(view, indices);
514 memcpy(ptr, src, view->itemsize);
515 src += view->itemsize;
Stefan Krah7213fcc2015-02-01 16:19:23 +0100516 addone(view->ndim, indices, view->shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
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,
R David Murray861470c2014-10-05 11:47:01 -0400533 "both destination and source must be "\
534 "bytes-like objects");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 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 Krah5178d912015-02-03 16:57:21 +0100621 if (view == NULL) {
622 PyErr_SetString(PyExc_BufferError,
623 "PyBuffer_FillInfo: view==NULL argument is obsolete");
624 return -1;
625 }
626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
628 (readonly == 1)) {
629 PyErr_SetString(PyExc_BufferError,
630 "Object is not writable.");
631 return -1;
632 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 view->obj = obj;
635 if (obj)
636 Py_INCREF(obj);
637 view->buf = buf;
638 view->len = len;
639 view->readonly = readonly;
640 view->itemsize = 1;
641 view->format = NULL;
642 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
643 view->format = "B";
644 view->ndim = 1;
645 view->shape = NULL;
646 if ((flags & PyBUF_ND) == PyBUF_ND)
647 view->shape = &(view->len);
648 view->strides = NULL;
649 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
650 view->strides = &(view->itemsize);
651 view->suboffsets = NULL;
652 view->internal = NULL;
653 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000654}
655
Martin v. Löwis423be952008-08-13 15:53:07 +0000656void
657PyBuffer_Release(Py_buffer *view)
658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyObject *obj = view->obj;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200660 PyBufferProcs *pb;
661 if (obj == NULL)
662 return;
663 pb = Py_TYPE(obj)->tp_as_buffer;
664 if (pb && pb->bf_releasebuffer)
665 pb->bf_releasebuffer(obj, view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 view->obj = NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200667 Py_DECREF(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +0000668}
669
Eric Smith8fd3eba2008-02-17 19:48:00 +0000670PyObject *
671PyObject_Format(PyObject *obj, PyObject *format_spec)
672{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000673 PyObject *meth;
674 PyObject *empty = NULL;
675 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500676 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000677
678 /* If no format_spec is provided, use an empty string */
679 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100680 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000681 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000682 }
683
Eric Smith8fd3eba2008-02-17 19:48:00 +0000684 /* Find the (unbound!) __format__ method (a borrowed reference) */
Benjamin Petersonce798522012-01-22 11:24:29 -0500685 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000686 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000687 if (!PyErr_Occurred())
688 PyErr_Format(PyExc_TypeError,
689 "Type %.100s doesn't define __format__",
690 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000692 }
693
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000694 /* And call it. */
695 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000696 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000697
698 if (result && !PyUnicode_Check(result)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800699 PyErr_Format(PyExc_TypeError,
700 "__format__ must return a str, not %.200s",
701 Py_TYPE(result)->tp_name);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000702 Py_DECREF(result);
703 result = NULL;
704 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000705 }
706
707done:
708 Py_XDECREF(empty);
709 return result;
710}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000711/* Operations on numbers */
712
713int
Fred Drake79912472000-07-09 04:06:11 +0000714PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return o && o->ob_type->tp_as_number &&
717 (o->ob_type->tp_as_number->nb_int ||
718 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000719}
720
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000721/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000722
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000723#define NB_SLOT(x) offsetof(PyNumberMethods, x)
724#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000726#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000728
729/*
730 Calling scheme used for binary operations:
731
Neal Norwitz4886cc32006-08-21 17:06:07 +0000732 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000734
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000735 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
736 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000737 */
738
739static PyObject *
740binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyObject *x;
743 binaryfunc slotv = NULL;
744 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (v->ob_type->tp_as_number != NULL)
747 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
748 if (w->ob_type != v->ob_type &&
749 w->ob_type->tp_as_number != NULL) {
750 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
751 if (slotw == slotv)
752 slotw = NULL;
753 }
754 if (slotv) {
755 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
756 x = slotw(v, w);
757 if (x != Py_NotImplemented)
758 return x;
759 Py_DECREF(x); /* can't do it */
760 slotw = NULL;
761 }
762 x = slotv(v, w);
763 if (x != Py_NotImplemented)
764 return x;
765 Py_DECREF(x); /* can't do it */
766 }
767 if (slotw) {
768 x = slotw(v, w);
769 if (x != Py_NotImplemented)
770 return x;
771 Py_DECREF(x); /* can't do it */
772 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500773 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000774}
Guido van Rossum77660912002-04-16 16:32:50 +0000775
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000776static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000777binop_type_error(PyObject *v, PyObject *w, const char *op_name)
778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 PyErr_Format(PyExc_TypeError,
780 "unsupported operand type(s) for %.100s: "
781 "'%.100s' and '%.100s'",
782 op_name,
783 v->ob_type->tp_name,
784 w->ob_type->tp_name);
785 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000786}
787
788static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000789binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 PyObject *result = binary_op1(v, w, op_slot);
792 if (result == Py_NotImplemented) {
793 Py_DECREF(result);
794 return binop_type_error(v, w, op_name);
795 }
796 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000797}
798
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000799
800/*
801 Calling scheme used for ternary operations:
802
Neal Norwitz4886cc32006-08-21 17:06:07 +0000803 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000805 */
806
807static PyObject *
808ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject *w,
810 PyObject *z,
811 const int op_slot,
812 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyNumberMethods *mv, *mw, *mz;
815 PyObject *x = NULL;
816 ternaryfunc slotv = NULL;
817 ternaryfunc slotw = NULL;
818 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 mv = v->ob_type->tp_as_number;
821 mw = w->ob_type->tp_as_number;
822 if (mv != NULL)
823 slotv = NB_TERNOP(mv, op_slot);
824 if (w->ob_type != v->ob_type &&
825 mw != NULL) {
826 slotw = NB_TERNOP(mw, op_slot);
827 if (slotw == slotv)
828 slotw = NULL;
829 }
830 if (slotv) {
831 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
832 x = slotw(v, w, z);
833 if (x != Py_NotImplemented)
834 return x;
835 Py_DECREF(x); /* can't do it */
836 slotw = NULL;
837 }
838 x = slotv(v, w, z);
839 if (x != Py_NotImplemented)
840 return x;
841 Py_DECREF(x); /* can't do it */
842 }
843 if (slotw) {
844 x = slotw(v, w, z);
845 if (x != Py_NotImplemented)
846 return x;
847 Py_DECREF(x); /* can't do it */
848 }
849 mz = z->ob_type->tp_as_number;
850 if (mz != NULL) {
851 slotz = NB_TERNOP(mz, op_slot);
852 if (slotz == slotv || slotz == slotw)
853 slotz = NULL;
854 if (slotz) {
855 x = slotz(v, w, z);
856 if (x != Py_NotImplemented)
857 return x;
858 Py_DECREF(x); /* can't do it */
859 }
860 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (z == Py_None)
863 PyErr_Format(
864 PyExc_TypeError,
865 "unsupported operand type(s) for ** or pow(): "
866 "'%.100s' and '%.100s'",
867 v->ob_type->tp_name,
868 w->ob_type->tp_name);
869 else
870 PyErr_Format(
871 PyExc_TypeError,
872 "unsupported operand type(s) for pow(): "
873 "'%.100s', '%.100s', '%.100s'",
874 v->ob_type->tp_name,
875 w->ob_type->tp_name,
876 z->ob_type->tp_name);
877 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000878}
879
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000880#define BINARY_FUNC(func, op, op_name) \
881 PyObject * \
882 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000884 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000885
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000886BINARY_FUNC(PyNumber_Or, nb_or, "|")
887BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
888BINARY_FUNC(PyNumber_And, nb_and, "&")
889BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
890BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
891BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000892BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000893
894PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000895PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
898 if (result == Py_NotImplemented) {
899 PySequenceMethods *m = v->ob_type->tp_as_sequence;
900 Py_DECREF(result);
901 if (m && m->sq_concat) {
902 return (*m->sq_concat)(v, w);
903 }
904 result = binop_type_error(v, w, "+");
905 }
906 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000907}
908
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000909static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000910sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_ssize_t count;
913 if (PyIndex_Check(n)) {
914 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
915 if (count == -1 && PyErr_Occurred())
916 return NULL;
917 }
918 else {
919 return type_error("can't multiply sequence by "
920 "non-int of type '%.200s'", n);
921 }
922 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000923}
924
925PyObject *
926PyNumber_Multiply(PyObject *v, PyObject *w)
927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
929 if (result == Py_NotImplemented) {
930 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
931 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
932 Py_DECREF(result);
933 if (mv && mv->sq_repeat) {
934 return sequence_repeat(mv->sq_repeat, v, w);
935 }
936 else if (mw && mw->sq_repeat) {
937 return sequence_repeat(mw->sq_repeat, w, v);
938 }
939 result = binop_type_error(v, w, "*");
940 }
941 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000942}
943
Guido van Rossume15dee51995-07-18 14:12:02 +0000944PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -0400945PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
946{
947 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
948}
949
950PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000951PyNumber_FloorDivide(PyObject *v, PyObject *w)
952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000954}
955
956PyObject *
957PyNumber_TrueDivide(PyObject *v, PyObject *w)
958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +0000960}
961
962PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000963PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000966}
967
968PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000969PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000972}
973
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000974/* Binary in-place operators */
975
976/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000977 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000978
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000979 - If the left hand object has the appropriate struct members, and
980 they are filled, call the appropriate function and return the
981 result. No coercion is done on the arguments; the left-hand object
982 is the one the operation is performed on, and it's up to the
983 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +0000984
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000985 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000986 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000987
988 */
989
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000990static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000991binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyNumberMethods *mv = v->ob_type->tp_as_number;
994 if (mv != NULL) {
995 binaryfunc slot = NB_BINOP(mv, iop_slot);
996 if (slot) {
997 PyObject *x = (slot)(v, w);
998 if (x != Py_NotImplemented) {
999 return x;
1000 }
1001 Py_DECREF(x);
1002 }
1003 }
1004 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001005}
1006
1007static PyObject *
1008binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1012 if (result == Py_NotImplemented) {
1013 Py_DECREF(result);
1014 return binop_type_error(v, w, op_name);
1015 }
1016 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001017}
1018
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001019#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyObject * \
1021 func(PyObject *v, PyObject *w) { \
1022 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1023 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001024
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001025INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1026INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1027INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1028INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1029INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1030INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001031INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001032
1033PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001034PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1037 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001038}
1039
1040PyObject *
1041PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1044 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001045}
1046
1047PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001048PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1051 NB_SLOT(nb_add));
1052 if (result == Py_NotImplemented) {
1053 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1054 Py_DECREF(result);
1055 if (m != NULL) {
1056 binaryfunc f = NULL;
1057 f = m->sq_inplace_concat;
1058 if (f == NULL)
1059 f = m->sq_concat;
1060 if (f != NULL)
1061 return (*f)(v, w);
1062 }
1063 result = binop_type_error(v, w, "+=");
1064 }
1065 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001066}
1067
1068PyObject *
1069PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1072 NB_SLOT(nb_multiply));
1073 if (result == Py_NotImplemented) {
1074 ssizeargfunc f = NULL;
1075 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1076 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1077 Py_DECREF(result);
1078 if (mv != NULL) {
1079 f = mv->sq_inplace_repeat;
1080 if (f == NULL)
1081 f = mv->sq_repeat;
1082 if (f != NULL)
1083 return sequence_repeat(f, v, w);
1084 }
1085 else if (mw != NULL) {
1086 /* Note that the right hand operand should not be
1087 * mutated in this case so sq_inplace_repeat is not
1088 * used. */
1089 if (mw->sq_repeat)
1090 return sequence_repeat(mw->sq_repeat, w, v);
1091 }
1092 result = binop_type_error(v, w, "*=");
1093 }
1094 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001095}
1096
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001097PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001098PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1099{
1100 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1101 NB_SLOT(nb_matrix_multiply), "@=");
1102}
1103
1104PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001105PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1108 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001109}
1110
1111PyObject *
1112PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (v->ob_type->tp_as_number &&
1115 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1116 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1117 }
1118 else {
1119 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1120 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001121}
1122
1123
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001124/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001125
1126PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001127PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001130
Victor Stinner71aea8e2016-08-19 16:59:55 +02001131 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001133 }
1134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 m = o->ob_type->tp_as_number;
1136 if (m && m->nb_negative)
1137 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001140}
1141
1142PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001143PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001146
Victor Stinner71aea8e2016-08-19 16:59:55 +02001147 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001149 }
1150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 m = o->ob_type->tp_as_number;
1152 if (m && m->nb_positive)
1153 return (*m->nb_positive)(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_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001162
Victor Stinner71aea8e2016-08-19 16:59:55 +02001163 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001165 }
1166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 m = o->ob_type->tp_as_number;
1168 if (m && m->nb_invert)
1169 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001172}
1173
1174PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001175PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001178
Victor Stinner71aea8e2016-08-19 16:59:55 +02001179 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001181 }
1182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 m = o->ob_type->tp_as_number;
1184 if (m && m->nb_absolute)
1185 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001188}
1189
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001190/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001191 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001192 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001193*/
1194PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001195PyNumber_Index(PyObject *item)
1196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 PyObject *result = NULL;
Victor Stinner71aea8e2016-08-19 16:59:55 +02001198 if (item == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001200 }
1201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (PyLong_Check(item)) {
1203 Py_INCREF(item);
1204 return item;
1205 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001206 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 PyErr_Format(PyExc_TypeError,
1208 "'%.200s' object cannot be interpreted "
1209 "as an integer", item->ob_type->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001210 return NULL;
1211 }
1212 result = item->ob_type->tp_as_number->nb_index(item);
1213 if (!result || PyLong_CheckExact(result))
1214 return result;
1215 if (!PyLong_Check(result)) {
1216 PyErr_Format(PyExc_TypeError,
1217 "__index__ returned non-int (type %.200s)",
1218 result->ob_type->tp_name);
1219 Py_DECREF(result);
1220 return NULL;
1221 }
1222 /* Issue #17576: warn if 'result' not of exact type int. */
1223 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1224 "__index__ returned non-int (type %.200s). "
1225 "The ability to return an instance of a strict subclass of int "
1226 "is deprecated, and may be removed in a future version of Python.",
1227 result->ob_type->tp_name)) {
1228 Py_DECREF(result);
1229 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
1231 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001232}
1233
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001234/* Return an error on Overflow only if err is not NULL*/
1235
1236Py_ssize_t
1237PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 Py_ssize_t result;
1240 PyObject *runerr;
1241 PyObject *value = PyNumber_Index(item);
1242 if (value == NULL)
1243 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* We're done if PyLong_AsSsize_t() returns without error. */
1246 result = PyLong_AsSsize_t(value);
1247 if (result != -1 || !(runerr = PyErr_Occurred()))
1248 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 /* Error handling code -- only manage OverflowError differently */
1251 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1252 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 PyErr_Clear();
1255 /* If no error-handling desired then the default clipping
1256 is sufficient.
1257 */
1258 if (!err) {
1259 assert(PyLong_Check(value));
1260 /* Whether or not it is less than or equal to
1261 zero is determined by the sign of ob_size
1262 */
1263 if (_PyLong_Sign(value) < 0)
1264 result = PY_SSIZE_T_MIN;
1265 else
1266 result = PY_SSIZE_T_MAX;
1267 }
1268 else {
1269 /* Otherwise replace the error with caller's error object. */
1270 PyErr_Format(err,
1271 "cannot fit '%.200s' into an index-sized integer",
1272 item->ob_type->tp_name);
1273 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001274
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001275 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_DECREF(value);
1277 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001278}
1279
1280
Guido van Rossume15dee51995-07-18 14:12:02 +00001281PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001282PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001283{
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001284 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 PyObject *trunc_func;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001287 Py_buffer view;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001288 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001289
Victor Stinner71aea8e2016-08-19 16:59:55 +02001290 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001292 }
1293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (PyLong_CheckExact(o)) {
1295 Py_INCREF(o);
1296 return o;
1297 }
1298 m = o->ob_type->tp_as_number;
1299 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001300 result = (PyObject *)_PyLong_FromNbInt(o);
1301 if (result != NULL && !PyLong_CheckExact(result)) {
1302 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1303 }
1304 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001306 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (trunc_func) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001308 result = PyEval_CallObject(trunc_func, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 Py_DECREF(trunc_func);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001310 if (result == NULL || PyLong_CheckExact(result)) {
1311 return result;
1312 }
1313 if (PyLong_Check(result)) {
1314 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1315 return result;
1316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* __trunc__ is specified to return an Integral type,
Martin Panter7462b6492015-11-02 03:37:02 +00001318 but int() needs to return an int. */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001319 m = result->ob_type->tp_as_number;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001320 if (m == NULL || m->nb_int == NULL) {
1321 PyErr_Format(
1322 PyExc_TypeError,
1323 "__trunc__ returned non-Integral (type %.200s)",
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001324 result->ob_type->tp_name);
1325 Py_DECREF(result);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001326 return NULL;
1327 }
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001328 Py_SETREF(result, (PyObject *)_PyLong_FromNbInt(result));
1329 if (result != NULL && !PyLong_CheckExact(result)) {
1330 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1331 }
1332 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001334 if (PyErr_Occurred())
1335 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001336
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001337 if (PyUnicode_Check(o))
1338 /* The below check is done in PyLong_FromUnicode(). */
1339 return PyLong_FromUnicodeObject(o, 10);
1340
Martin Pantereeb896c2015-11-07 02:32:21 +00001341 if (PyBytes_Check(o))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001343 * doesn't do. In particular int('9\x005') must raise an
1344 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 */
Martin Pantereeb896c2015-11-07 02:32:21 +00001346 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1347 PyBytes_GET_SIZE(o), 10);
1348
1349 if (PyByteArray_Check(o))
1350 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1351 PyByteArray_GET_SIZE(o), 10);
1352
1353 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001354 PyObject *bytes;
Martin Pantereeb896c2015-11-07 02:32:21 +00001355
1356 /* Copy to NUL-terminated buffer. */
1357 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1358 if (bytes == NULL) {
1359 PyBuffer_Release(&view);
1360 return NULL;
1361 }
1362 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1363 PyBytes_GET_SIZE(bytes), 10);
1364 Py_DECREF(bytes);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001365 PyBuffer_Release(&view);
1366 return result;
1367 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001368
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001369 return type_error("int() argument must be a string, a bytes-like object "
1370 "or a number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001371}
1372
1373PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001374PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001377
Victor Stinner71aea8e2016-08-19 16:59:55 +02001378 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001380 }
1381
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001382 if (PyFloat_CheckExact(o)) {
1383 Py_INCREF(o);
1384 return o;
1385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 m = o->ob_type->tp_as_number;
1387 if (m && m->nb_float) { /* This should include subclasses of float */
1388 PyObject *res = m->nb_float(o);
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001389 double val;
1390 if (!res || PyFloat_CheckExact(res)) {
1391 return res;
1392 }
1393 if (!PyFloat_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001395 "%.50s.__float__ returned non-float (type %.50s)",
1396 o->ob_type->tp_name, res->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 Py_DECREF(res);
1398 return NULL;
1399 }
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001400 /* Issue #26983: warn if 'res' not of exact type float. */
1401 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1402 "%.50s.__float__ returned non-float (type %.50s). "
1403 "The ability to return an instance of a strict subclass of float "
1404 "is deprecated, and may be removed in a future version of Python.",
1405 o->ob_type->tp_name, res->ob_type->tp_name)) {
1406 Py_DECREF(res);
1407 return NULL;
1408 }
1409 val = PyFloat_AS_DOUBLE(res);
1410 Py_DECREF(res);
1411 return PyFloat_FromDouble(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 }
1413 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001414 return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 }
1416 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001417}
1418
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001419
1420PyObject *
1421PyNumber_ToBase(PyObject *n, int base)
1422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyObject *res = NULL;
1424 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (!index)
1427 return NULL;
1428 if (PyLong_Check(index))
1429 res = _PyLong_Format(index, base);
1430 else
1431 /* It should not be possible to get here, as
1432 PyNumber_Index already has a check for the same
1433 condition */
Serhiy Storchaka95949422013-08-27 19:40:23 +03001434 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 Py_DECREF(index);
1436 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001437}
1438
1439
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001440/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001441
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001442int
Fred Drake79912472000-07-09 04:06:11 +00001443PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (PyDict_Check(s))
1446 return 0;
1447 return s != NULL && s->ob_type->tp_as_sequence &&
1448 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001449}
1450
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001452PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (s == NULL) {
1457 null_error();
1458 return -1;
1459 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 m = s->ob_type->tp_as_sequence;
1462 if (m && m->sq_length)
1463 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 type_error("object of type '%.200s' has no len()", s);
1466 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001467}
1468
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001469#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001471PySequence_Length(PyObject *s)
1472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001474}
1475#define PySequence_Length PySequence_Size
1476
Guido van Rossume15dee51995-07-18 14:12:02 +00001477PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001478PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001481
Victor Stinner71aea8e2016-08-19 16:59:55 +02001482 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001484 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 m = s->ob_type->tp_as_sequence;
1487 if (m && m->sq_concat)
1488 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 /* Instances of user classes defining an __add__() method only
1491 have an nb_add slot, not an sq_concat slot. So we fall back
1492 to nb_add if both arguments appear to be sequences. */
1493 if (PySequence_Check(s) && PySequence_Check(o)) {
1494 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1495 if (result != Py_NotImplemented)
1496 return result;
1497 Py_DECREF(result);
1498 }
1499 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001500}
1501
1502PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001503PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001506
Victor Stinner71aea8e2016-08-19 16:59:55 +02001507 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001509 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 m = o->ob_type->tp_as_sequence;
1512 if (m && m->sq_repeat)
1513 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* Instances of user classes defining a __mul__() method only
1516 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1517 to nb_multiply if o appears to be a sequence. */
1518 if (PySequence_Check(o)) {
1519 PyObject *n, *result;
1520 n = PyLong_FromSsize_t(count);
1521 if (n == NULL)
1522 return NULL;
1523 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1524 Py_DECREF(n);
1525 if (result != Py_NotImplemented)
1526 return result;
1527 Py_DECREF(result);
1528 }
1529 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001530}
1531
1532PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001533PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001536
Victor Stinner71aea8e2016-08-19 16:59:55 +02001537 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001539 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 m = s->ob_type->tp_as_sequence;
1542 if (m && m->sq_inplace_concat)
1543 return m->sq_inplace_concat(s, o);
1544 if (m && m->sq_concat)
1545 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 if (PySequence_Check(s) && PySequence_Check(o)) {
1548 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1549 NB_SLOT(nb_add));
1550 if (result != Py_NotImplemented)
1551 return result;
1552 Py_DECREF(result);
1553 }
1554 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001555}
1556
1557PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001558PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001561
Victor Stinner71aea8e2016-08-19 16:59:55 +02001562 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001564 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 m = o->ob_type->tp_as_sequence;
1567 if (m && m->sq_inplace_repeat)
1568 return m->sq_inplace_repeat(o, count);
1569 if (m && m->sq_repeat)
1570 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (PySequence_Check(o)) {
1573 PyObject *n, *result;
1574 n = PyLong_FromSsize_t(count);
1575 if (n == NULL)
1576 return NULL;
1577 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1578 NB_SLOT(nb_multiply));
1579 Py_DECREF(n);
1580 if (result != Py_NotImplemented)
1581 return result;
1582 Py_DECREF(result);
1583 }
1584 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001585}
1586
1587PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001588PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001591
Victor Stinner71aea8e2016-08-19 16:59:55 +02001592 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001594 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 m = s->ob_type->tp_as_sequence;
1597 if (m && m->sq_item) {
1598 if (i < 0) {
1599 if (m->sq_length) {
1600 Py_ssize_t l = (*m->sq_length)(s);
Victor Stinnere20310f2015-11-05 13:56:58 +01001601 if (l < 0) {
1602 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return NULL;
Victor Stinnere20310f2015-11-05 13:56:58 +01001604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 i += l;
1606 }
1607 }
1608 return m->sq_item(s, i);
1609 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001612}
1613
1614PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001615PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001618
Victor Stinner71aea8e2016-08-19 16:59:55 +02001619 if (!s) {
1620 return null_error();
1621 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001624 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 PyObject *res;
1626 PyObject *slice = _PySlice_FromIndices(i1, i2);
1627 if (!slice)
1628 return NULL;
1629 res = mp->mp_subscript(s, slice);
1630 Py_DECREF(slice);
1631 return res;
1632 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001635}
1636
1637int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001638PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 if (s == NULL) {
1643 null_error();
1644 return -1;
1645 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 m = s->ob_type->tp_as_sequence;
1648 if (m && m->sq_ass_item) {
1649 if (i < 0) {
1650 if (m->sq_length) {
1651 Py_ssize_t l = (*m->sq_length)(s);
1652 if (l < 0)
1653 return -1;
1654 i += l;
1655 }
1656 }
1657 return m->sq_ass_item(s, i, o);
1658 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 type_error("'%.200s' object does not support item assignment", s);
1661 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001662}
1663
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001664int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001665PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (s == NULL) {
1670 null_error();
1671 return -1;
1672 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 m = s->ob_type->tp_as_sequence;
1675 if (m && m->sq_ass_item) {
1676 if (i < 0) {
1677 if (m->sq_length) {
1678 Py_ssize_t l = (*m->sq_length)(s);
1679 if (l < 0)
1680 return -1;
1681 i += l;
1682 }
1683 }
1684 return m->sq_ass_item(s, i, (PyObject *)NULL);
1685 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 type_error("'%.200s' object doesn't support item deletion", s);
1688 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001689}
1690
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001691int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001692PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (s == NULL) {
1697 null_error();
1698 return -1;
1699 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001702 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 int res;
1704 PyObject *slice = _PySlice_FromIndices(i1, i2);
1705 if (!slice)
1706 return -1;
1707 res = mp->mp_ass_subscript(s, slice, o);
1708 Py_DECREF(slice);
1709 return res;
1710 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 type_error("'%.200s' object doesn't support slice assignment", s);
1713 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001714}
1715
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001716int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001717PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (s == NULL) {
1722 null_error();
1723 return -1;
1724 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001727 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 int res;
1729 PyObject *slice = _PySlice_FromIndices(i1, i2);
1730 if (!slice)
1731 return -1;
1732 res = mp->mp_ass_subscript(s, slice, NULL);
1733 Py_DECREF(slice);
1734 return res;
1735 }
1736 type_error("'%.200s' object doesn't support slice deletion", s);
1737 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001738}
1739
Guido van Rossume15dee51995-07-18 14:12:02 +00001740PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001741PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 PyObject *it; /* iter(v) */
1744 Py_ssize_t n; /* guess for result tuple size */
1745 PyObject *result = NULL;
1746 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001747
Victor Stinner71aea8e2016-08-19 16:59:55 +02001748 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001750 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* Special-case the common tuple and list cases, for efficiency. */
1753 if (PyTuple_CheckExact(v)) {
1754 /* Note that we can't know whether it's safe to return
1755 a tuple *subclass* instance as-is, hence the restriction
1756 to exact tuples here. In contrast, lists always make
1757 a copy, so there's no need for exactness below. */
1758 Py_INCREF(v);
1759 return v;
1760 }
Raymond Hettinger610a51f2015-05-17 14:45:58 -07001761 if (PyList_CheckExact(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 /* Get iterator. */
1765 it = PyObject_GetIter(v);
1766 if (it == NULL)
1767 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001770 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (n == -1)
1772 goto Fail;
1773 result = PyTuple_New(n);
1774 if (result == NULL)
1775 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 /* Fill the tuple. */
1778 for (j = 0; ; ++j) {
1779 PyObject *item = PyIter_Next(it);
1780 if (item == NULL) {
1781 if (PyErr_Occurred())
1782 goto Fail;
1783 break;
1784 }
1785 if (j >= n) {
Martin Pantere8db8612016-07-25 02:30:05 +00001786 size_t newn = (size_t)n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 /* The over-allocation strategy can grow a bit faster
1788 than for lists because unlike lists the
1789 over-allocation isn't permanent -- we reclaim
1790 the excess before the end of this routine.
1791 So, grow by ten and then add 25%.
1792 */
Martin Pantere8db8612016-07-25 02:30:05 +00001793 newn += 10u;
1794 newn += newn >> 2;
1795 if (newn > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 /* Check for overflow */
1797 PyErr_NoMemory();
1798 Py_DECREF(item);
1799 goto Fail;
1800 }
Martin Pantere8db8612016-07-25 02:30:05 +00001801 n = (Py_ssize_t)newn;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (_PyTuple_Resize(&result, n) != 0) {
1803 Py_DECREF(item);
1804 goto Fail;
1805 }
1806 }
1807 PyTuple_SET_ITEM(result, j, item);
1808 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Cut tuple back if guess was too large. */
1811 if (j < n &&
1812 _PyTuple_Resize(&result, j) != 0)
1813 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 Py_DECREF(it);
1816 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001817
1818Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 Py_XDECREF(result);
1820 Py_DECREF(it);
1821 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001822}
1823
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001824PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001825PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 PyObject *result; /* result list */
1828 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001829
Victor Stinner71aea8e2016-08-19 16:59:55 +02001830 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001832 }
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 result = PyList_New(0);
1835 if (result == NULL)
1836 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 rv = _PyList_Extend((PyListObject *)result, v);
1839 if (rv == NULL) {
1840 Py_DECREF(result);
1841 return NULL;
1842 }
1843 Py_DECREF(rv);
1844 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001845}
1846
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001847PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001848PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001851
Victor Stinner71aea8e2016-08-19 16:59:55 +02001852 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001854 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1857 Py_INCREF(v);
1858 return v;
1859 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 it = PyObject_GetIter(v);
1862 if (it == NULL) {
1863 if (PyErr_ExceptionMatches(PyExc_TypeError))
1864 PyErr_SetString(PyExc_TypeError, m);
1865 return NULL;
1866 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 v = PySequence_List(it);
1869 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001872}
1873
Tim Peters16a77ad2001-09-08 04:00:12 +00001874/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1876 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1877 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001878 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1879*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001880Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001881_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 Py_ssize_t n;
1884 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1885 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 if (seq == NULL || obj == NULL) {
1888 null_error();
1889 return -1;
1890 }
Tim Peters75f8e352001-05-05 11:33:43 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 it = PyObject_GetIter(seq);
1893 if (it == NULL) {
1894 type_error("argument of type '%.200s' is not iterable", seq);
1895 return -1;
1896 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 n = wrapped = 0;
1899 for (;;) {
1900 int cmp;
1901 PyObject *item = PyIter_Next(it);
1902 if (item == NULL) {
1903 if (PyErr_Occurred())
1904 goto Fail;
1905 break;
1906 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1909 Py_DECREF(item);
1910 if (cmp < 0)
1911 goto Fail;
1912 if (cmp > 0) {
1913 switch (operation) {
1914 case PY_ITERSEARCH_COUNT:
1915 if (n == PY_SSIZE_T_MAX) {
1916 PyErr_SetString(PyExc_OverflowError,
1917 "count exceeds C integer size");
1918 goto Fail;
1919 }
1920 ++n;
1921 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 case PY_ITERSEARCH_INDEX:
1924 if (wrapped) {
1925 PyErr_SetString(PyExc_OverflowError,
1926 "index exceeds C integer size");
1927 goto Fail;
1928 }
1929 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 case PY_ITERSEARCH_CONTAINS:
1932 n = 1;
1933 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 default:
1936 assert(!"unknown operation");
1937 }
1938 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (operation == PY_ITERSEARCH_INDEX) {
1941 if (n == PY_SSIZE_T_MAX)
1942 wrapped = 1;
1943 ++n;
1944 }
1945 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (operation != PY_ITERSEARCH_INDEX)
1948 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 PyErr_SetString(PyExc_ValueError,
1951 "sequence.index(x): x not in sequence");
1952 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00001953Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 n = -1;
1955 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00001956Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 Py_DECREF(it);
1958 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001959
Guido van Rossume15dee51995-07-18 14:12:02 +00001960}
1961
Tim Peters16a77ad2001-09-08 04:00:12 +00001962/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001963Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001964PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001967}
1968
Tim Peterscb8d3682001-05-05 21:05:01 +00001969/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001970 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001971 */
1972int
1973PySequence_Contains(PyObject *seq, PyObject *ob)
1974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 Py_ssize_t result;
1976 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1977 if (sqm != NULL && sqm->sq_contains != NULL)
1978 return (*sqm->sq_contains)(seq, ob);
1979 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1980 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001981}
1982
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001983/* Backwards compatibility */
1984#undef PySequence_In
1985int
Fred Drake79912472000-07-09 04:06:11 +00001986PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001989}
1990
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001991Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001992PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001995}
1996
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001997/* Operations on mappings */
1998
1999int
Fred Drake79912472000-07-09 04:06:11 +00002000PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002001{
Benjamin Peterson21992272011-12-28 12:01:31 -06002002 return o && o->ob_type->tp_as_mapping &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002004}
2005
Martin v. Löwis18e16552006-02-15 17:27:45 +00002006Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002007PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 if (o == NULL) {
2012 null_error();
2013 return -1;
2014 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 m = o->ob_type->tp_as_mapping;
2017 if (m && m->mp_length)
2018 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 type_error("object of type '%.200s' has no len()", o);
2021 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002022}
2023
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002024#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002025Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002026PyMapping_Length(PyObject *o)
2027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002029}
2030#define PyMapping_Length PyMapping_Size
2031
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002032PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002033PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002036
Victor Stinner71aea8e2016-08-19 16:59:55 +02002037 if (key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002039 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 okey = PyUnicode_FromString(key);
2042 if (okey == NULL)
2043 return NULL;
2044 r = PyObject_GetItem(o, okey);
2045 Py_DECREF(okey);
2046 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002047}
2048
2049int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002050PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 PyObject *okey;
2053 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (key == NULL) {
2056 null_error();
2057 return -1;
2058 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 okey = PyUnicode_FromString(key);
2061 if (okey == NULL)
2062 return -1;
2063 r = PyObject_SetItem(o, okey, value);
2064 Py_DECREF(okey);
2065 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002066}
2067
2068int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002069PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 v = PyMapping_GetItemString(o, key);
2074 if (v) {
2075 Py_DECREF(v);
2076 return 1;
2077 }
2078 PyErr_Clear();
2079 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002080}
2081
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002082int
Fred Drake79912472000-07-09 04:06:11 +00002083PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 v = PyObject_GetItem(o, key);
2088 if (v) {
2089 Py_DECREF(v);
2090 return 1;
2091 }
2092 PyErr_Clear();
2093 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002094}
2095
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002096PyObject *
2097PyMapping_Keys(PyObject *o)
2098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 PyObject *keys;
2100 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002101 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (PyDict_CheckExact(o))
2104 return PyDict_Keys(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002105 keys = _PyObject_CallMethodId(o, &PyId_keys, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 if (keys == NULL)
2107 return NULL;
2108 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2109 Py_DECREF(keys);
2110 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002111}
2112
2113PyObject *
2114PyMapping_Items(PyObject *o)
2115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 PyObject *items;
2117 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002118 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 if (PyDict_CheckExact(o))
2121 return PyDict_Items(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002122 items = _PyObject_CallMethodId(o, &PyId_items, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 if (items == NULL)
2124 return NULL;
2125 fast = PySequence_Fast(items, "o.items() are not iterable");
2126 Py_DECREF(items);
2127 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002128}
2129
2130PyObject *
2131PyMapping_Values(PyObject *o)
2132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 PyObject *values;
2134 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002135 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 if (PyDict_CheckExact(o))
2138 return PyDict_Values(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002139 values = _PyObject_CallMethodId(o, &PyId_values, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 if (values == NULL)
2141 return NULL;
2142 fast = PySequence_Fast(values, "o.values() are not iterable");
2143 Py_DECREF(values);
2144 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002145}
2146
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002147/* Operations on callable objects */
2148
2149/* XXX PyCallable_Check() is in object.c */
2150
Guido van Rossume15dee51995-07-18 14:12:02 +00002151PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002152PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002155}
Guido van Rossume15dee51995-07-18 14:12:02 +00002156
Victor Stinner4a7cc882015-03-06 23:35:27 +01002157PyObject*
Victor Stinnerefde1462015-03-21 15:04:43 +01002158_Py_CheckFunctionResult(PyObject *func, PyObject *result, const char *where)
Victor Stinner4a7cc882015-03-06 23:35:27 +01002159{
2160 int err_occurred = (PyErr_Occurred() != NULL);
2161
Victor Stinnerefde1462015-03-21 15:04:43 +01002162 assert((func != NULL) ^ (where != NULL));
2163
Victor Stinner4a7cc882015-03-06 23:35:27 +01002164 if (result == NULL) {
2165 if (!err_occurred) {
Victor Stinnerefde1462015-03-21 15:04:43 +01002166 if (func)
2167 PyErr_Format(PyExc_SystemError,
2168 "%R returned NULL without setting an error",
2169 func);
2170 else
2171 PyErr_Format(PyExc_SystemError,
2172 "%s returned NULL without setting an error",
2173 where);
Victor Stinner944fbcc2015-03-24 16:28:52 +01002174#ifdef Py_DEBUG
Serhiy Storchaka5697c4b2016-06-12 17:02:10 +03002175 /* Ensure that the bug is caught in debug mode */
Victor Stinner944fbcc2015-03-24 16:28:52 +01002176 Py_FatalError("a function returned NULL without setting an error");
2177#endif
2178 return NULL;
Victor Stinner4a7cc882015-03-06 23:35:27 +01002179 }
2180 }
2181 else {
2182 if (err_occurred) {
2183 PyObject *exc, *val, *tb;
2184 PyErr_Fetch(&exc, &val, &tb);
2185
2186 Py_DECREF(result);
2187
Victor Stinnerefde1462015-03-21 15:04:43 +01002188 if (func)
2189 PyErr_Format(PyExc_SystemError,
2190 "%R returned a result with an error set",
2191 func);
2192 else
2193 PyErr_Format(PyExc_SystemError,
2194 "%s returned a result with an error set",
2195 where);
Victor Stinner4a7cc882015-03-06 23:35:27 +01002196 _PyErr_ChainExceptions(exc, val, tb);
Victor Stinner944fbcc2015-03-24 16:28:52 +01002197#ifdef Py_DEBUG
Martin Panter2275e622016-06-20 07:52:50 +00002198 /* Ensure that the bug is caught in debug mode */
Victor Stinner944fbcc2015-03-24 16:28:52 +01002199 Py_FatalError("a function returned a result with an error set");
2200#endif
2201 return NULL;
Victor Stinner4a7cc882015-03-06 23:35:27 +01002202 }
2203 }
2204 return result;
2205}
2206
Guido van Rossume15dee51995-07-18 14:12:02 +00002207PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02002208PyObject_Call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 ternaryfunc call;
Victor Stinner4a7cc882015-03-06 23:35:27 +01002211 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212
Victor Stinner8f4ec8d2014-09-05 01:10:29 +02002213 /* PyObject_Call() must not be called with an exception set,
2214 because it may clear it (directly or indirectly) and so the
Martin Panterec1aa5c2015-10-07 11:03:53 +00002215 caller loses its exception */
Victor Stinner8f4ec8d2014-09-05 01:10:29 +02002216 assert(!PyErr_Occurred());
Victor Stinner8a31c822016-08-19 17:12:23 +02002217 assert(PyTuple_Check(args));
2218 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner8f4ec8d2014-09-05 01:10:29 +02002219
Victor Stinner4a7cc882015-03-06 23:35:27 +01002220 call = func->ob_type->tp_call;
2221 if (call == NULL) {
2222 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2223 func->ob_type->tp_name);
2224 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01002226
2227 if (Py_EnterRecursiveCall(" while calling a Python object"))
2228 return NULL;
2229
Victor Stinner8a31c822016-08-19 17:12:23 +02002230 result = (*call)(func, args, kwargs);
Victor Stinner4a7cc882015-03-06 23:35:27 +01002231
2232 Py_LeaveRecursiveCall();
2233
Victor Stinnerefde1462015-03-21 15:04:43 +01002234 return _Py_CheckFunctionResult(func, result, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002235}
2236
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002237PyObject*
2238_PyStack_AsTuple(PyObject **stack, Py_ssize_t nargs)
2239{
2240 PyObject *args;
2241 Py_ssize_t i;
2242
2243 args = PyTuple_New(nargs);
2244 if (args == NULL) {
2245 return NULL;
2246 }
2247
2248 for (i=0; i < nargs; i++) {
2249 PyObject *item = stack[i];
2250 Py_INCREF(item);
2251 PyTuple_SET_ITEM(args, i, item);
2252 }
2253
2254 return args;
2255}
2256
2257PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02002258_PyObject_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02002259 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002260{
2261 ternaryfunc call;
2262 PyObject *result = NULL;
2263
Victor Stinner559bb6a2016-08-22 22:48:54 +02002264 /* _PyObject_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002265 because it may clear it (directly or indirectly) and so the
2266 caller loses its exception */
2267 assert(!PyErr_Occurred());
2268
2269 assert(func != NULL);
2270 assert(nargs >= 0);
2271 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02002272 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002273
2274 if (Py_EnterRecursiveCall(" while calling a Python object")) {
2275 return NULL;
2276 }
2277
2278 if (PyFunction_Check(func)) {
Victor Stinnerb9009392016-08-22 23:15:44 +02002279 result = _PyFunction_FastCallDict(func, args, nargs, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002280 }
2281 else if (PyCFunction_Check(func)) {
Victor Stinnerb9009392016-08-22 23:15:44 +02002282 result = _PyCFunction_FastCallDict(func, args, nargs, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002283 }
2284 else {
2285 PyObject *tuple;
2286
2287 /* Slow-path: build a temporary tuple */
2288 call = func->ob_type->tp_call;
2289 if (call == NULL) {
2290 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2291 func->ob_type->tp_name);
2292 goto exit;
2293 }
2294
2295 tuple = _PyStack_AsTuple(args, nargs);
2296 if (tuple == NULL) {
2297 goto exit;
2298 }
2299
2300 result = (*call)(func, tuple, kwargs);
2301 Py_DECREF(tuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002302
Victor Stinner53cb4892016-08-25 00:39:34 +02002303 result = _Py_CheckFunctionResult(func, result, NULL);
2304 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02002305
2306exit:
2307 Py_LeaveRecursiveCall();
2308
2309 return result;
2310}
2311
Victor Stinner577e1f82016-08-25 00:29:32 +02002312static PyObject *
2313_PyStack_AsDict(PyObject **stack, Py_ssize_t nkwargs, PyObject *func)
2314{
2315 PyObject *kwdict;
2316
2317 kwdict = PyDict_New();
2318 if (kwdict == NULL) {
2319 return NULL;
2320 }
2321
2322 while (--nkwargs >= 0) {
2323 int err;
2324 PyObject *key = *stack++;
2325 PyObject *value = *stack++;
2326 if (PyDict_GetItem(kwdict, key) != NULL) {
2327 PyErr_Format(PyExc_TypeError,
2328 "%.200s%s got multiple values "
2329 "for keyword argument '%U'",
2330 PyEval_GetFuncName(func),
2331 PyEval_GetFuncDesc(func),
2332 key);
2333 Py_DECREF(kwdict);
2334 return NULL;
2335 }
2336
2337 err = PyDict_SetItem(kwdict, key, value);
2338 if (err) {
2339 Py_DECREF(kwdict);
2340 return NULL;
2341 }
2342 }
2343 return kwdict;
2344}
2345
2346PyObject *
2347_PyObject_FastCallKeywords(PyObject *func, PyObject **stack, Py_ssize_t nargs,
2348 Py_ssize_t nkwargs)
2349{
2350 PyObject *args, *kwdict, *result;
2351
2352 /* _PyObject_FastCallKeywords() must not be called with an exception set,
2353 because it may clear it (directly or indirectly) and so the
2354 caller loses its exception */
2355 assert(!PyErr_Occurred());
2356
2357 assert(func != NULL);
2358 assert(nargs >= 0);
2359 assert(nkwargs >= 0);
2360 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
2361
2362 if (PyFunction_Check(func)) {
2363 /* Fast-path: avoid temporary tuple or dict */
2364 return _PyFunction_FastCallKeywords(func, stack, nargs, nkwargs);
2365 }
2366
2367 if (PyCFunction_Check(func) && nkwargs == 0) {
Victor Stinnerf7507dd2016-08-25 01:00:31 +02002368 return _PyCFunction_FastCallDict(func, stack, nargs, NULL);
Victor Stinner577e1f82016-08-25 00:29:32 +02002369 }
2370
2371 /* Slow-path: build temporary tuple and/or dict */
2372 args = _PyStack_AsTuple(stack, nargs);
2373
2374 if (nkwargs > 0) {
2375 kwdict = _PyStack_AsDict(stack + nargs, nkwargs, func);
2376 if (kwdict == NULL) {
2377 Py_DECREF(args);
2378 return NULL;
2379 }
2380 }
2381 else {
2382 kwdict = NULL;
2383 }
2384
2385 result = PyObject_Call(func, args, kwdict);
2386 Py_DECREF(args);
2387 Py_XDECREF(kwdict);
2388 return result;
2389}
2390
Victor Stinner3f1057a2016-08-25 01:04:14 +02002391/* Positional arguments are obj followed args. */
2392PyObject *
2393_PyObject_Call_Prepend(PyObject *func,
2394 PyObject *obj, PyObject *args, PyObject *kwargs)
2395{
2396 PyObject *small_stack[8];
2397 PyObject **stack;
2398 Py_ssize_t argcount;
2399 PyObject *result;
2400
2401 assert(PyTuple_Check(args));
2402
2403 argcount = PyTuple_GET_SIZE(args);
2404 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
2405 stack = small_stack;
2406 }
2407 else {
2408 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
2409 if (stack == NULL) {
2410 PyErr_NoMemory();
2411 return NULL;
2412 }
2413 }
2414
2415 /* use borrowed references */
2416 stack[0] = obj;
2417 Py_MEMCPY(&stack[1],
2418 &PyTuple_GET_ITEM(args, 0),
2419 argcount * sizeof(PyObject *));
2420
2421 result = _PyObject_FastCallDict(func,
2422 stack, argcount + 1,
2423 kwargs);
2424 if (stack != small_stack) {
2425 PyMem_Free(stack);
2426 }
2427 return result;
2428}
2429
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002430static PyObject*
2431call_function_tail(PyObject *callable, PyObject *args)
2432{
Victor Stinner88807082016-08-19 16:44:19 +02002433 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002434
Victor Stinner64faad62016-08-19 16:50:49 +02002435 assert(args != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 if (!PyTuple_Check(args)) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02002438 result = _PyObject_CallArg1(callable, args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
Victor Stinner88807082016-08-19 16:44:19 +02002440 else {
2441 result = PyObject_Call(callable, args, NULL);
2442 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002443
Victor Stinner88807082016-08-19 16:44:19 +02002444 return result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002445}
2446
Tim Peters6d6c1a32001-08-02 04:15:00 +00002447PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002448PyObject_CallFunction(PyObject *callable, const char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 va_list va;
Victor Stinner64faad62016-08-19 16:50:49 +02002451 PyObject *args, *result;
Guido van Rossume15dee51995-07-18 14:12:02 +00002452
Victor Stinner71aea8e2016-08-19 16:59:55 +02002453 if (callable == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002455 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002456
Victor Stinner0d1a7992016-08-19 17:04:54 +02002457 if (!format || !*format) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02002458 return _PyObject_CallNoArg(callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 }
Victor Stinner0d1a7992016-08-19 17:04:54 +02002460
2461 va_start(va, format);
2462 args = Py_VaBuildValue(format, va);
2463 va_end(va);
Victor Stinner64faad62016-08-19 16:50:49 +02002464 if (args == NULL) {
Victor Stinner0b0c8672013-10-29 19:29:52 +01002465 return NULL;
Victor Stinner64faad62016-08-19 16:50:49 +02002466 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002467
Victor Stinner64faad62016-08-19 16:50:49 +02002468 result = call_function_tail(callable, args);
2469 Py_DECREF(args);
2470 return result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002472
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002473PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002474_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 va_list va;
Victor Stinner64faad62016-08-19 16:50:49 +02002477 PyObject *args, *result;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002478
Victor Stinner71aea8e2016-08-19 16:59:55 +02002479 if (callable == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002481 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002482
Victor Stinner0d1a7992016-08-19 17:04:54 +02002483 if (!format || !*format) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02002484 return _PyObject_CallNoArg(callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 }
Victor Stinner0d1a7992016-08-19 17:04:54 +02002486
2487 va_start(va, format);
2488 args = _Py_VaBuildValue_SizeT(format, va);
2489 va_end(va);
Victor Stinner64faad62016-08-19 16:50:49 +02002490 if (args == NULL) {
2491 return NULL;
2492 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002493
Victor Stinner64faad62016-08-19 16:50:49 +02002494 result = call_function_tail(callable, args);
2495 Py_DECREF(args);
2496 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +00002497}
2498
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002499static PyObject*
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002500callmethod(PyObject* func, const char *format, va_list va, int is_size_t)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002501{
Victor Stinner64faad62016-08-19 16:50:49 +02002502 PyObject *args, *result;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002503
Victor Stinnerd042f1f2016-08-19 16:56:49 +02002504 assert(func != NULL);
2505
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002506 if (!PyCallable_Check(func)) {
2507 type_error("attribute of type '%.200s' is not callable", func);
Victor Stinner64faad62016-08-19 16:50:49 +02002508 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002509 }
2510
Victor Stinner0d1a7992016-08-19 17:04:54 +02002511 if (!format || !*format) {
Victor Stinner559bb6a2016-08-22 22:48:54 +02002512 return _PyObject_CallNoArg(func);
Victor Stinner0d1a7992016-08-19 17:04:54 +02002513 }
2514
2515 if (is_size_t) {
2516 args = _Py_VaBuildValue_SizeT(format, va);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002517 }
Victor Stinner64faad62016-08-19 16:50:49 +02002518 else {
Victor Stinner0d1a7992016-08-19 17:04:54 +02002519 args = Py_VaBuildValue(format, va);
Victor Stinner64faad62016-08-19 16:50:49 +02002520 }
2521 if (args == NULL) {
2522 return NULL;
2523 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002524
Victor Stinner64faad62016-08-19 16:50:49 +02002525 result = call_function_tail(func, args);
Victor Stinner64faad62016-08-19 16:50:49 +02002526 Py_DECREF(args);
2527 return result;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002528}
2529
Guido van Rossume15dee51995-07-18 14:12:02 +00002530PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002531PyObject_CallMethod(PyObject *o, const char *name, const char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 va_list va;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 PyObject *func = NULL;
2535 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002536
Victor Stinner71aea8e2016-08-19 16:59:55 +02002537 if (o == NULL || name == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002539 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 func = PyObject_GetAttrString(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002542 if (func == NULL)
2543 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002544
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002545 va_start(va, format);
2546 retval = callmethod(func, format, va, 0);
2547 va_end(va);
Victor Stinnerd042f1f2016-08-19 16:56:49 +02002548 Py_DECREF(func);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002549 return retval;
2550}
2551
2552PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002553_PyObject_CallMethodId(PyObject *o, _Py_Identifier *name,
2554 const char *format, ...)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002555{
2556 va_list va;
2557 PyObject *func = NULL;
2558 PyObject *retval = NULL;
2559
Victor Stinner71aea8e2016-08-19 16:59:55 +02002560 if (o == NULL || name == NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002561 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002562 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002563
2564 func = _PyObject_GetAttrId(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002565 if (func == NULL)
2566 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002567
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002568 va_start(va, format);
2569 retval = callmethod(func, format, va, 0);
2570 va_end(va);
Victor Stinnerd042f1f2016-08-19 16:56:49 +02002571 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002573}
2574
2575PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002576_PyObject_CallMethod_SizeT(PyObject *o, const char *name,
2577 const char *format, ...)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 va_list va;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 PyObject *func = NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002581 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002582
Victor Stinner71aea8e2016-08-19 16:59:55 +02002583 if (o == NULL || name == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002585 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 func = PyObject_GetAttrString(o, name);
Benjamin Peterson1791c222014-06-26 23:29:13 -07002588 if (func == NULL)
2589 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002590 va_start(va, format);
2591 retval = callmethod(func, format, va, 1);
2592 va_end(va);
Victor Stinnerd042f1f2016-08-19 16:56:49 +02002593 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002595}
Guido van Rossum823649d2001-03-21 18:40:58 +00002596
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002597PyObject *
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +03002598_PyObject_CallMethodId_SizeT(PyObject *o, _Py_Identifier *name,
2599 const char *format, ...)
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002600{
2601 va_list va;
2602 PyObject *func = NULL;
2603 PyObject *retval;
2604
Victor Stinner71aea8e2016-08-19 16:59:55 +02002605 if (o == NULL || name == NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002606 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002607 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002608
2609 func = _PyObject_GetAttrId(o, name);
2610 if (func == NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002611 return NULL;
2612 }
2613 va_start(va, format);
2614 retval = callmethod(func, format, va, 1);
2615 va_end(va);
Victor Stinnerd042f1f2016-08-19 16:56:49 +02002616 Py_DECREF(func);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002617 return retval;
2618}
Guido van Rossum823649d2001-03-21 18:40:58 +00002619
Victor Stinnerdd695642016-08-24 01:14:54 +02002620static PyObject **
2621objargs_mkstack(PyObject **small_stack, Py_ssize_t small_stack_size,
2622 va_list va, Py_ssize_t *p_nargs)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002623{
Victor Stinnerdd695642016-08-24 01:14:54 +02002624 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 va_list countva;
Victor Stinnerdd695642016-08-24 01:14:54 +02002626 PyObject **stack;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002627
Victor Stinnerdd695642016-08-24 01:14:54 +02002628 /* Count the number of arguments */
2629 Py_VA_COPY(countva, va);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002630
Victor Stinnerdd695642016-08-24 01:14:54 +02002631 n = 0;
2632 while (1) {
2633 PyObject *arg = (PyObject *)va_arg(countva, PyObject *);
2634 if (arg == NULL) {
2635 break;
2636 }
2637 n++;
2638 }
2639 *p_nargs = n;
2640
2641 /* Copy arguments */
2642 if (n <= small_stack_size) {
2643 stack = small_stack;
2644 }
2645 else {
2646 stack = PyMem_Malloc(n * sizeof(stack[0]));
2647 if (stack == NULL) {
2648 PyErr_NoMemory();
2649 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
2651 }
Victor Stinnerdd695642016-08-24 01:14:54 +02002652
2653 for (i = 0; i < n; ++i) {
2654 stack[i] = va_arg(va, PyObject *);
2655 }
2656 return stack;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002657}
2658
2659PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002660PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002661{
Victor Stinnerdd695642016-08-24 01:14:54 +02002662 PyObject *small_stack[5];
2663 PyObject **stack;
2664 Py_ssize_t nargs;
2665 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002667
Victor Stinner71aea8e2016-08-19 16:59:55 +02002668 if (callable == NULL || name == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002670 }
Fred Drakeb421b8c2001-10-26 16:21:32 +00002671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 callable = PyObject_GetAttr(callable, name);
2673 if (callable == NULL)
2674 return NULL;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 /* count the args */
2677 va_start(vargs, name);
Victor Stinnerdd695642016-08-24 01:14:54 +02002678 stack = objargs_mkstack(small_stack, Py_ARRAY_LENGTH(small_stack),
2679 vargs, &nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 va_end(vargs);
Victor Stinnerdd695642016-08-24 01:14:54 +02002681 if (stack == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 Py_DECREF(callable);
2683 return NULL;
2684 }
Victor Stinnere62b8522016-08-24 00:01:56 +02002685
Victor Stinnerdd695642016-08-24 01:14:54 +02002686 result = _PyObject_FastCall(callable, stack, nargs);
2687 Py_DECREF(callable);
2688 if (stack != small_stack) {
2689 PyMem_Free(stack);
2690 }
2691
2692 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002693}
2694
2695PyObject *
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07002696_PyObject_CallMethodIdObjArgs(PyObject *callable,
Brett Cannonfd074152012-04-14 14:10:13 -04002697 struct _Py_Identifier *name, ...)
2698{
Victor Stinnerdd695642016-08-24 01:14:54 +02002699 PyObject *small_stack[5];
2700 PyObject **stack;
2701 Py_ssize_t nargs;
2702 PyObject *result;
Brett Cannonfd074152012-04-14 14:10:13 -04002703 va_list vargs;
2704
Victor Stinner71aea8e2016-08-19 16:59:55 +02002705 if (callable == NULL || name == NULL) {
Brett Cannonfd074152012-04-14 14:10:13 -04002706 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002707 }
Brett Cannonfd074152012-04-14 14:10:13 -04002708
2709 callable = _PyObject_GetAttrId(callable, name);
2710 if (callable == NULL)
2711 return NULL;
2712
2713 /* count the args */
2714 va_start(vargs, name);
Victor Stinnerdd695642016-08-24 01:14:54 +02002715 stack = objargs_mkstack(small_stack, Py_ARRAY_LENGTH(small_stack),
2716 vargs, &nargs);
Brett Cannonfd074152012-04-14 14:10:13 -04002717 va_end(vargs);
Victor Stinnerdd695642016-08-24 01:14:54 +02002718 if (stack == NULL) {
Brett Cannonfd074152012-04-14 14:10:13 -04002719 Py_DECREF(callable);
2720 return NULL;
2721 }
Victor Stinnere62b8522016-08-24 00:01:56 +02002722
Victor Stinnerdd695642016-08-24 01:14:54 +02002723 result = _PyObject_FastCall(callable, stack, nargs);
2724 Py_DECREF(callable);
2725 if (stack != small_stack) {
2726 PyMem_Free(stack);
2727 }
2728
2729 return result;
Brett Cannonfd074152012-04-14 14:10:13 -04002730}
2731
2732PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002733PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002734{
Victor Stinnerdd695642016-08-24 01:14:54 +02002735 PyObject *small_stack[5];
2736 PyObject **stack;
2737 Py_ssize_t nargs;
2738 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002740
Victor Stinner71aea8e2016-08-19 16:59:55 +02002741 if (callable == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002743 }
Fred Drakeb421b8c2001-10-26 16:21:32 +00002744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 /* count the args */
2746 va_start(vargs, callable);
Victor Stinnerdd695642016-08-24 01:14:54 +02002747 stack = objargs_mkstack(small_stack, Py_ARRAY_LENGTH(small_stack),
2748 vargs, &nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 va_end(vargs);
Victor Stinnerdd695642016-08-24 01:14:54 +02002750 if (stack == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 return NULL;
Victor Stinnerdd695642016-08-24 01:14:54 +02002752 }
Fred Drakeb421b8c2001-10-26 16:21:32 +00002753
Victor Stinnerdd695642016-08-24 01:14:54 +02002754 result = _PyObject_FastCall(callable, stack, nargs);
2755 if (stack != small_stack) {
2756 PyMem_Free(stack);
2757 }
2758
2759 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002760}
2761
2762
Guido van Rossum823649d2001-03-21 18:40:58 +00002763/* isinstance(), issubclass() */
2764
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002765/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002766 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002767 * 1. getattr(cls, '__bases__') could raise an AttributeError
2768 * 2. getattr(cls, '__bases__') could raise some other exception
2769 * 3. getattr(cls, '__bases__') could return a tuple
2770 * 4. getattr(cls, '__bases__') could return something other than a tuple
2771 *
2772 * Only state #3 is a non-error state and only it returns a non-NULL object
2773 * (it returns the retrieved tuple).
2774 *
2775 * Any raised AttributeErrors are masked by clearing the exception and
2776 * returning NULL. If an object other than a tuple comes out of __bases__,
2777 * then again, the return value is NULL. So yes, these two situations
2778 * produce exactly the same results: NULL is returned and no error is set.
2779 *
2780 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002782 * exception to be propagated along.
2783 *
2784 * Callers are expected to test for PyErr_Occurred() when the return value
2785 * is NULL to decide whether a valid exception should be propagated or not.
2786 * When there's no exception to propagate, it's customary for the caller to
2787 * set a TypeError.
2788 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002789static PyObject *
2790abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002791{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002792 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 Py_ALLOW_RECURSION
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002796 bases = _PyObject_GetAttrId(cls, &PyId___bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 Py_END_ALLOW_RECURSION
2798 if (bases == NULL) {
2799 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2800 PyErr_Clear();
2801 return NULL;
2802 }
2803 if (!PyTuple_Check(bases)) {
2804 Py_DECREF(bases);
2805 return NULL;
2806 }
2807 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002808}
2809
2810
2811static int
2812abstract_issubclass(PyObject *derived, PyObject *cls)
2813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 PyObject *bases = NULL;
2815 Py_ssize_t i, n;
2816 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 while (1) {
2819 if (derived == cls)
2820 return 1;
2821 bases = abstract_get_bases(derived);
2822 if (bases == NULL) {
2823 if (PyErr_Occurred())
2824 return -1;
2825 return 0;
2826 }
2827 n = PyTuple_GET_SIZE(bases);
2828 if (n == 0) {
2829 Py_DECREF(bases);
2830 return 0;
2831 }
2832 /* Avoid recursivity in the single inheritance case */
2833 if (n == 1) {
2834 derived = PyTuple_GET_ITEM(bases, 0);
2835 Py_DECREF(bases);
2836 continue;
2837 }
2838 for (i = 0; i < n; i++) {
2839 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2840 if (r != 0)
2841 break;
2842 }
2843 Py_DECREF(bases);
2844 return r;
2845 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002846}
2847
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002848static int
2849check_class(PyObject *cls, const char *error)
2850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 PyObject *bases = abstract_get_bases(cls);
2852 if (bases == NULL) {
2853 /* Do not mask errors. */
2854 if (!PyErr_Occurred())
2855 PyErr_SetString(PyExc_TypeError, error);
2856 return 0;
2857 }
2858 Py_DECREF(bases);
2859 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002860}
2861
Brett Cannon4f653312004-03-20 22:52:14 +00002862static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002863recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PyObject *icls;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 int retval = 0;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002867 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 if (PyType_Check(cls)) {
2870 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2871 if (retval == 0) {
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002872 PyObject *c = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (c == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002874 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002875 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002876 else
R. David Murray6bb99892010-11-20 16:33:30 +00002877 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 }
2879 else {
2880 if (c != (PyObject *)(inst->ob_type) &&
2881 PyType_Check(c))
2882 retval = PyType_IsSubtype(
2883 (PyTypeObject *)c,
2884 (PyTypeObject *)cls);
2885 Py_DECREF(c);
2886 }
2887 }
2888 }
2889 else {
2890 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002891 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 return -1;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002893 icls = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 if (icls == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002895 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002896 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002897 else
R. David Murray6bb99892010-11-20 16:33:30 +00002898 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 }
2900 else {
2901 retval = abstract_issubclass(icls, cls);
2902 Py_DECREF(icls);
2903 }
2904 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002907}
2908
2909int
Brett Cannon4f653312004-03-20 22:52:14 +00002910PyObject_IsInstance(PyObject *inst, PyObject *cls)
2911{
Benjamin Petersonce798522012-01-22 11:24:29 -05002912 _Py_IDENTIFIER(__instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* Quick test for an exact match */
2916 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2917 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002918
Georg Brandl72b8a802014-10-03 09:26:37 +02002919 /* We know what type's __instancecheck__ does. */
2920 if (PyType_CheckExact(cls)) {
2921 return recursive_isinstance(inst, cls);
2922 }
2923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (PyTuple_Check(cls)) {
2925 Py_ssize_t i;
2926 Py_ssize_t n;
2927 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2930 return -1;
2931 n = PyTuple_GET_SIZE(cls);
2932 for (i = 0; i < n; ++i) {
2933 PyObject *item = PyTuple_GET_ITEM(cls, i);
2934 r = PyObject_IsInstance(inst, item);
2935 if (r != 0)
2936 /* either found it, or got an error */
2937 break;
2938 }
2939 Py_LeaveRecursiveCall();
2940 return r;
2941 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002942
Benjamin Petersonce798522012-01-22 11:24:29 -05002943 checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 if (checker != NULL) {
2945 PyObject *res;
2946 int ok = -1;
2947 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2948 Py_DECREF(checker);
2949 return ok;
2950 }
2951 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2952 Py_LeaveRecursiveCall();
2953 Py_DECREF(checker);
2954 if (res != NULL) {
2955 ok = PyObject_IsTrue(res);
2956 Py_DECREF(res);
2957 }
2958 return ok;
2959 }
2960 else if (PyErr_Occurred())
2961 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02002962 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002964}
2965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002967recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 if (PyType_Check(cls) && PyType_Check(derived)) {
2970 /* Fast path (non-recursive) */
2971 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2972 }
2973 if (!check_class(derived,
2974 "issubclass() arg 1 must be a class"))
2975 return -1;
2976 if (!check_class(cls,
2977 "issubclass() arg 2 must be a class"
2978 " or tuple of classes"))
2979 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002982}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002983
Brett Cannon4f653312004-03-20 22:52:14 +00002984int
2985PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2986{
Benjamin Petersonce798522012-01-22 11:24:29 -05002987 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002989
Georg Brandl72b8a802014-10-03 09:26:37 +02002990 /* We know what type's __subclasscheck__ does. */
2991 if (PyType_CheckExact(cls)) {
2992 /* Quick test for an exact match */
2993 if (derived == cls)
2994 return 1;
2995 return recursive_issubclass(derived, cls);
2996 }
2997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 if (PyTuple_Check(cls)) {
2999 Py_ssize_t i;
3000 Py_ssize_t n;
3001 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
3004 return -1;
3005 n = PyTuple_GET_SIZE(cls);
3006 for (i = 0; i < n; ++i) {
3007 PyObject *item = PyTuple_GET_ITEM(cls, i);
3008 r = PyObject_IsSubclass(derived, item);
3009 if (r != 0)
3010 /* either found it, or got an error */
3011 break;
3012 }
3013 Py_LeaveRecursiveCall();
3014 return r;
3015 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00003016
Benjamin Petersonce798522012-01-22 11:24:29 -05003017 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 if (checker != NULL) {
3019 PyObject *res;
3020 int ok = -1;
3021 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
3022 Py_DECREF(checker);
3023 return ok;
3024 }
3025 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
3026 Py_LeaveRecursiveCall();
3027 Py_DECREF(checker);
3028 if (res != NULL) {
3029 ok = PyObject_IsTrue(res);
3030 Py_DECREF(res);
3031 }
3032 return ok;
3033 }
3034 else if (PyErr_Occurred())
3035 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02003036 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00003038}
3039
3040int
3041_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
3042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00003044}
3045
3046int
3047_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
3048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00003050}
3051
3052
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003053PyObject *
3054PyObject_GetIter(PyObject *o)
3055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 PyTypeObject *t = o->ob_type;
3057 getiterfunc f = NULL;
3058 f = t->tp_iter;
3059 if (f == NULL) {
3060 if (PySequence_Check(o))
3061 return PySeqIter_New(o);
3062 return type_error("'%.200s' object is not iterable", o);
3063 }
3064 else {
3065 PyObject *res = (*f)(o);
3066 if (res != NULL && !PyIter_Check(res)) {
3067 PyErr_Format(PyExc_TypeError,
3068 "iter() returned non-iterator "
3069 "of type '%.100s'",
3070 res->ob_type->tp_name);
3071 Py_DECREF(res);
3072 res = NULL;
3073 }
3074 return res;
3075 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00003076}
3077
Tim Petersf4848da2001-05-05 00:14:56 +00003078/* Return next item.
3079 * If an error occurs, return NULL. PyErr_Occurred() will be true.
3080 * If the iteration terminates normally, return NULL and clear the
3081 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
3082 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00003084 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00003085PyObject *
3086PyIter_Next(PyObject *iter)
3087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 PyObject *result;
3089 result = (*iter->ob_type->tp_iternext)(iter);
3090 if (result == NULL &&
3091 PyErr_Occurred() &&
3092 PyErr_ExceptionMatches(PyExc_StopIteration))
3093 PyErr_Clear();
3094 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003095}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003096
3097
3098/*
3099 * Flatten a sequence of bytes() objects into a C array of
3100 * NULL terminated string pointers with a NULL char* terminating the array.
3101 * (ie: an argv or env list)
3102 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003103 * Memory allocated for the returned list is allocated using PyMem_Malloc()
3104 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003105 */
3106char *const *
3107_PySequence_BytesToCharpArray(PyObject* self)
3108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 char **array;
3110 Py_ssize_t i, argc;
3111 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003112 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 argc = PySequence_Size(self);
3115 if (argc == -1)
3116 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003117
Stefan Krah7cacd2e2012-08-21 08:16:09 +02003118 assert(argc >= 0);
3119
3120 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
3121 PyErr_NoMemory();
3122 return NULL;
3123 }
3124
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003125 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 if (array == NULL) {
3127 PyErr_NoMemory();
3128 return NULL;
3129 }
3130 for (i = 0; i < argc; ++i) {
3131 char *data;
3132 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02003133 if (item == NULL) {
3134 /* NULL terminate before freeing. */
3135 array[i] = NULL;
3136 goto fail;
3137 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 data = PyBytes_AsString(item);
3139 if (data == NULL) {
3140 /* NULL terminate before freeing. */
3141 array[i] = NULL;
3142 goto fail;
3143 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003144 size = PyBytes_GET_SIZE(item) + 1;
3145 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 if (!array[i]) {
3147 PyErr_NoMemory();
3148 goto fail;
3149 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003150 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 Py_DECREF(item);
3152 }
3153 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003156
3157fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 Py_XDECREF(item);
3159 _Py_FreeCharPArray(array);
3160 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003161}
3162
3163
3164/* Free's a NULL terminated char** array of C strings. */
3165void
3166_Py_FreeCharPArray(char *const array[])
3167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 Py_ssize_t i;
3169 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003170 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02003172 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00003173}