blob: 305910c3584ca0e507ccb86cd72f071b23932600 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00005#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00006#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00007#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000010
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000011/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000012
13static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000014type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
17 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000018}
19
Guido van Rossum052b7e11996-11-11 15:08:19 +000020static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000021null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 if (!PyErr_Occurred())
24 PyErr_SetString(PyExc_SystemError,
25 "null argument to internal routine");
26 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000027}
28
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000029/* Operations on any object */
30
Guido van Rossume15dee51995-07-18 14:12:02 +000031PyObject *
Fred Drake79912472000-07-09 04:06:11 +000032PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000035
Victor Stinner71aea8e2016-08-19 16:59:55 +020036 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +020038 }
39
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 v = (PyObject *)o->ob_type;
41 Py_INCREF(v);
42 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000043}
44
Martin v. Löwis18e16552006-02-15 17:27:45 +000045Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000046PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 if (o == NULL) {
51 null_error();
52 return -1;
53 }
Guido van Rossume15dee51995-07-18 14:12:02 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 m = o->ob_type->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +030056 if (m && m->sq_length) {
57 Py_ssize_t len = m->sq_length(o);
58 assert(len >= 0 || PyErr_Occurred());
59 return len;
60 }
Guido van Rossume15dee51995-07-18 14:12:02 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000063}
64
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000065#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000066Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000067PyObject_Length(PyObject *o)
68{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000070}
71#define PyObject_Length PyObject_Size
72
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020073int
74_PyObject_HasLen(PyObject *o) {
75 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
76 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
77}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000078
Christian Heimes255f53b2007-12-08 15:33:56 +000079/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020080 or o.__length_hint__(). If those methods aren't found the defaultvalue is
81 returned. If one of the calls fails with an exception other than TypeError
82 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000083*/
84
85Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020086PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000087{
Christian Heimesb70e8a12012-10-06 17:16:39 +020088 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020089 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050090 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030091 if (_PyObject_HasLen(o)) {
92 res = PyObject_Length(o);
Serhiy Storchaka813f9432017-04-16 09:21:44 +030093 if (res < 0) {
94 assert(PyErr_Occurred());
Serhiy Storchakaf740d462013-10-24 23:19:51 +030095 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
96 return -1;
97 }
98 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020099 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +0300100 else {
101 return res;
102 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 }
Christian Heimes6314d162012-10-06 17:13:29 +0200104 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200105 if (hint == NULL) {
106 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 return defaultvalue;
110 }
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100111 result = _PyObject_CallNoArg(hint);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200112 Py_DECREF(hint);
113 if (result == NULL) {
114 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
115 PyErr_Clear();
116 return defaultvalue;
117 }
118 return -1;
119 }
120 else if (result == Py_NotImplemented) {
121 Py_DECREF(result);
122 return defaultvalue;
123 }
124 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200125 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200126 Py_TYPE(result)->tp_name);
127 Py_DECREF(result);
128 return -1;
129 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200130 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200131 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200132 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200133 return -1;
134 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200135 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200136 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
137 return -1;
138 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200139 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000140}
141
Guido van Rossume15dee51995-07-18 14:12:02 +0000142PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000143PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000146
Victor Stinner71aea8e2016-08-19 16:59:55 +0200147 if (o == NULL || key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +0200149 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 m = o->ob_type->tp_as_mapping;
Victor Stinnere20310f2015-11-05 13:56:58 +0100152 if (m && m->mp_subscript) {
153 PyObject *item = m->mp_subscript(o, key);
154 assert((item != NULL) ^ (PyErr_Occurred() != NULL));
155 return item;
156 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 if (o->ob_type->tp_as_sequence) {
159 if (PyIndex_Check(key)) {
160 Py_ssize_t key_value;
161 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
162 if (key_value == -1 && PyErr_Occurred())
163 return NULL;
164 return PySequence_GetItem(o, key_value);
165 }
166 else if (o->ob_type->tp_as_sequence->sq_item)
167 return type_error("sequence index must "
168 "be integer, not '%.200s'", key);
169 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000170
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100171 if (PyType_Check(o)) {
Serhiy Storchakace5b0e92018-01-05 00:21:41 +0200172 PyObject *meth, *result, *stack[1] = {key};
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100173 _Py_IDENTIFIER(__class_getitem__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200174 if (_PyObject_LookupAttrId(o, &PyId___class_getitem__, &meth) < 0) {
175 return NULL;
176 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100177 if (meth) {
Serhiy Storchakace5b0e92018-01-05 00:21:41 +0200178 result = _PyObject_FastCall(meth, stack, 1);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100179 Py_DECREF(meth);
180 return result;
181 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100182 }
183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000185}
186
187int
Fred Drake79912472000-07-09 04:06:11 +0000188PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (o == NULL || key == NULL || value == NULL) {
193 null_error();
194 return -1;
195 }
196 m = o->ob_type->tp_as_mapping;
197 if (m && m->mp_ass_subscript)
198 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (o->ob_type->tp_as_sequence) {
201 if (PyIndex_Check(key)) {
202 Py_ssize_t key_value;
203 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
204 if (key_value == -1 && PyErr_Occurred())
205 return -1;
206 return PySequence_SetItem(o, key_value, value);
207 }
208 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
209 type_error("sequence index must be "
210 "integer, not '%.200s'", key);
211 return -1;
212 }
213 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 type_error("'%.200s' object does not support item assignment", o);
216 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000217}
218
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000219int
Fred Drake79912472000-07-09 04:06:11 +0000220PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (o == NULL || key == NULL) {
225 null_error();
226 return -1;
227 }
228 m = o->ob_type->tp_as_mapping;
229 if (m && m->mp_ass_subscript)
230 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (o->ob_type->tp_as_sequence) {
233 if (PyIndex_Check(key)) {
234 Py_ssize_t key_value;
235 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
236 if (key_value == -1 && PyErr_Occurred())
237 return -1;
238 return PySequence_DelItem(o, key_value);
239 }
240 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
241 type_error("sequence index must be "
242 "integer, not '%.200s'", key);
243 return -1;
244 }
245 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 type_error("'%.200s' object does not support item deletion", o);
248 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000249}
250
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000251int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300252PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyObject *okey;
255 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (o == NULL || key == NULL) {
258 null_error();
259 return -1;
260 }
261 okey = PyUnicode_FromString(key);
262 if (okey == NULL)
263 return -1;
264 ret = PyObject_DelItem(o, okey);
265 Py_DECREF(okey);
266 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000267}
268
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000269/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000270 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000271 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000272int
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000273PyObject_CheckReadBuffer(PyObject *obj)
274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
276 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (pb == NULL ||
279 pb->bf_getbuffer == NULL)
280 return 0;
281 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
282 PyErr_Clear();
283 return 0;
284 }
285 PyBuffer_Release(&view);
286 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000287}
288
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200289static int
290as_read_buffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
295 null_error();
296 return -1;
297 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200298 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 *buffer = view.buf;
302 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200303 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305}
306
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200307int
308PyObject_AsCharBuffer(PyObject *obj,
309 const char **buffer,
310 Py_ssize_t *buffer_len)
311{
312 return as_read_buffer(obj, (const void **)buffer, buffer_len);
313}
314
315int PyObject_AsReadBuffer(PyObject *obj,
316 const void **buffer,
317 Py_ssize_t *buffer_len)
318{
319 return as_read_buffer(obj, buffer, buffer_len);
320}
321
Guido van Rossum4c08d552000-03-10 22:55:18 +0000322int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 void **buffer,
324 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyBufferProcs *pb;
327 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
330 null_error();
331 return -1;
332 }
333 pb = obj->ob_type->tp_as_buffer;
334 if (pb == NULL ||
335 pb->bf_getbuffer == NULL ||
336 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
337 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400338 "expected a writable bytes-like object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return -1;
340 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 *buffer = view.buf;
343 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200344 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000346}
347
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000348/* Buffer C-API for Python 3.0 */
349
350int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000351PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000352{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200353 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
354
355 if (pb == NULL || pb->bf_getbuffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400357 "a bytes-like object is required, not '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 Py_TYPE(obj)->tp_name);
359 return -1;
360 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200361 return (*pb->bf_getbuffer)(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000362}
363
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000364static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100365_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_ssize_t sd, dim;
368 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000369
Stefan Krah363af442015-02-01 14:53:54 +0100370 /* 1) len = product(shape) * itemsize
371 2) itemsize > 0
372 3) len = 0 <==> exists i: shape[i] = 0 */
373 if (view->len == 0) return 1;
374 if (view->strides == NULL) { /* C-contiguous by definition */
375 /* Trivially F-contiguous */
376 if (view->ndim <= 1) return 1;
377
378 /* ndim > 1 implies shape != NULL */
379 assert(view->shape != NULL);
380
381 /* Effectively 1-d */
382 sd = 0;
383 for (i=0; i<view->ndim; i++) {
384 if (view->shape[i] > 1) sd += 1;
385 }
386 return sd <= 1;
387 }
388
389 /* strides != NULL implies both of these */
390 assert(view->ndim > 0);
391 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 for (i=0; i<view->ndim; i++) {
395 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100396 if (dim > 1 && view->strides[i] != sd) {
397 return 0;
398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 sd *= dim;
400 }
401 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000402}
403
404static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100405_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 Py_ssize_t sd, dim;
408 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000409
Stefan Krah363af442015-02-01 14:53:54 +0100410 /* 1) len = product(shape) * itemsize
411 2) itemsize > 0
412 3) len = 0 <==> exists i: shape[i] = 0 */
413 if (view->len == 0) return 1;
414 if (view->strides == NULL) return 1; /* C-contiguous by definition */
415
416 /* strides != NULL implies both of these */
417 assert(view->ndim > 0);
418 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 for (i=view->ndim-1; i>=0; i--) {
422 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100423 if (dim > 1 && view->strides[i] != sd) {
424 return 0;
425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 sd *= dim;
427 }
428 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000429}
430
431int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100432PyBuffer_IsContiguous(const Py_buffer *view, char order)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000433{
434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000436
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100437 if (order == 'C')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 return _IsCContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100439 else if (order == 'F')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return _IsFortranContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100441 else if (order == 'A')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return (_IsCContiguous(view) || _IsFortranContiguous(view));
443 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000444}
445
446
Guido van Rossum98297ee2007-11-06 21:34:58 +0000447void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000448PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 char* pointer;
451 int i;
452 pointer = (char *)view->buf;
453 for (i = 0; i < view->ndim; i++) {
454 pointer += view->strides[i]*indices[i];
455 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
456 pointer = *((char**)pointer) + view->suboffsets[i];
457 }
458 }
459 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000460}
461
462
Guido van Rossum98297ee2007-11-06 21:34:58 +0000463void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000464_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 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 for (k=0; k<nd; k++) {
469 if (index[k] < shape[k]-1) {
470 index[k]++;
471 break;
472 }
473 else {
474 index[k] = 0;
475 }
476 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000477}
478
Guido van Rossum98297ee2007-11-06 21:34:58 +0000479void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000480_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 +0000481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 for (k=nd-1; k>=0; k--) {
485 if (index[k] < shape[k]-1) {
486 index[k]++;
487 break;
488 }
489 else {
490 index[k] = 0;
491 }
492 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000493}
494
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000495int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000496PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000499 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 Py_ssize_t *indices, elements;
501 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (len > view->len) {
504 len = view->len;
505 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (PyBuffer_IsContiguous(view, fort)) {
508 /* simplest copy is all that is needed */
509 memcpy(view->buf, buf, len);
510 return 0;
511 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000514
Stefan Krah7213fcc2015-02-01 16:19:23 +0100515 /* view->ndim <= 64 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
517 if (indices == NULL) {
518 PyErr_NoMemory();
519 return -1;
520 }
521 for (k=0; k<view->ndim;k++) {
522 indices[k] = 0;
523 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000526 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
528 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000529 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 }
531 src = buf;
532 /* XXX : This is not going to be the fastest code in the world
533 several optimizations are possible.
534 */
535 elements = len / view->itemsize;
536 while (elements--) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 ptr = PyBuffer_GetPointer(view, indices);
538 memcpy(ptr, src, view->itemsize);
539 src += view->itemsize;
Stefan Krah7213fcc2015-02-01 16:19:23 +0100540 addone(view->ndim, indices, view->shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 PyMem_Free(indices);
544 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000545}
546
Guido van Rossum98297ee2007-11-06 21:34:58 +0000547int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_buffer view_dest, view_src;
550 int k;
551 Py_ssize_t *indices, elements;
552 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (!PyObject_CheckBuffer(dest) ||
555 !PyObject_CheckBuffer(src)) {
556 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400557 "both destination and source must be "\
558 "bytes-like objects");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return -1;
560 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
563 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
564 PyBuffer_Release(&view_dest);
565 return -1;
566 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (view_dest.len < view_src.len) {
569 PyErr_SetString(PyExc_BufferError,
570 "destination is too small to receive data from source");
571 PyBuffer_Release(&view_dest);
572 PyBuffer_Release(&view_src);
573 return -1;
574 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
577 PyBuffer_IsContiguous(&view_src, 'C')) ||
578 (PyBuffer_IsContiguous(&view_dest, 'F') &&
579 PyBuffer_IsContiguous(&view_src, 'F'))) {
580 /* simplest copy is all that is needed */
581 memcpy(view_dest.buf, view_src.buf, view_src.len);
582 PyBuffer_Release(&view_dest);
583 PyBuffer_Release(&view_src);
584 return 0;
585 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* XXX(nnorwitz): need to check for overflow! */
590 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
591 if (indices == NULL) {
592 PyErr_NoMemory();
593 PyBuffer_Release(&view_dest);
594 PyBuffer_Release(&view_src);
595 return -1;
596 }
597 for (k=0; k<view_src.ndim;k++) {
598 indices[k] = 0;
599 }
600 elements = 1;
601 for (k=0; k<view_src.ndim; k++) {
602 /* XXX(nnorwitz): can this overflow? */
603 elements *= view_src.shape[k];
604 }
605 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000606 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 dptr = PyBuffer_GetPointer(&view_dest, indices);
608 sptr = PyBuffer_GetPointer(&view_src, indices);
609 memcpy(dptr, sptr, view_src.itemsize);
610 }
611 PyMem_Free(indices);
612 PyBuffer_Release(&view_dest);
613 PyBuffer_Release(&view_src);
614 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000615}
616
617void
618PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_ssize_t *strides, int itemsize,
620 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 int k;
623 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 sd = itemsize;
626 if (fort == 'F') {
627 for (k=0; k<nd; k++) {
628 strides[k] = sd;
629 sd *= shape[k];
630 }
631 }
632 else {
633 for (k=nd-1; k>=0; k--) {
634 strides[k] = sd;
635 sd *= shape[k];
636 }
637 }
638 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000639}
640
641int
Martin v. Löwis423be952008-08-13 15:53:07 +0000642PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Stefan Krah4e141742012-03-06 15:27:31 +0100643 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000644{
Stefan Krah5178d912015-02-03 16:57:21 +0100645 if (view == NULL) {
646 PyErr_SetString(PyExc_BufferError,
647 "PyBuffer_FillInfo: view==NULL argument is obsolete");
648 return -1;
649 }
650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
652 (readonly == 1)) {
653 PyErr_SetString(PyExc_BufferError,
654 "Object is not writable.");
655 return -1;
656 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 view->obj = obj;
659 if (obj)
660 Py_INCREF(obj);
661 view->buf = buf;
662 view->len = len;
663 view->readonly = readonly;
664 view->itemsize = 1;
665 view->format = NULL;
666 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
667 view->format = "B";
668 view->ndim = 1;
669 view->shape = NULL;
670 if ((flags & PyBUF_ND) == PyBUF_ND)
671 view->shape = &(view->len);
672 view->strides = NULL;
673 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
674 view->strides = &(view->itemsize);
675 view->suboffsets = NULL;
676 view->internal = NULL;
677 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000678}
679
Martin v. Löwis423be952008-08-13 15:53:07 +0000680void
681PyBuffer_Release(Py_buffer *view)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject *obj = view->obj;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200684 PyBufferProcs *pb;
685 if (obj == NULL)
686 return;
687 pb = Py_TYPE(obj)->tp_as_buffer;
688 if (pb && pb->bf_releasebuffer)
689 pb->bf_releasebuffer(obj, view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 view->obj = NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200691 Py_DECREF(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +0000692}
693
Eric Smith8fd3eba2008-02-17 19:48:00 +0000694PyObject *
695PyObject_Format(PyObject *obj, PyObject *format_spec)
696{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000697 PyObject *meth;
698 PyObject *empty = NULL;
699 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500700 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000701
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300702 if (format_spec != NULL && !PyUnicode_Check(format_spec)) {
703 PyErr_Format(PyExc_SystemError,
704 "Format specifier must be a string, not %.200s",
705 Py_TYPE(format_spec)->tp_name);
706 return NULL;
707 }
708
709 /* Fast path for common types. */
710 if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
711 if (PyUnicode_CheckExact(obj)) {
712 Py_INCREF(obj);
713 return obj;
714 }
715 if (PyLong_CheckExact(obj)) {
716 return PyObject_Str(obj);
717 }
718 }
719
Eric Smith8fd3eba2008-02-17 19:48:00 +0000720 /* If no format_spec is provided, use an empty string */
721 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100722 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000723 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000724 }
725
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300726 /* Find the (unbound!) __format__ method */
Benjamin Petersonce798522012-01-22 11:24:29 -0500727 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000728 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000729 if (!PyErr_Occurred())
730 PyErr_Format(PyExc_TypeError,
731 "Type %.100s doesn't define __format__",
732 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000734 }
735
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000736 /* And call it. */
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100737 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000738 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000739
740 if (result && !PyUnicode_Check(result)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800741 PyErr_Format(PyExc_TypeError,
742 "__format__ must return a str, not %.200s",
743 Py_TYPE(result)->tp_name);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000744 Py_DECREF(result);
745 result = NULL;
746 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000747 }
748
749done:
750 Py_XDECREF(empty);
751 return result;
752}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000753/* Operations on numbers */
754
755int
Fred Drake79912472000-07-09 04:06:11 +0000756PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 return o && o->ob_type->tp_as_number &&
759 (o->ob_type->tp_as_number->nb_int ||
760 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000761}
762
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000763/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000764
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000765#define NB_SLOT(x) offsetof(PyNumberMethods, x)
766#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000768#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000770
771/*
772 Calling scheme used for binary operations:
773
Neal Norwitz4886cc32006-08-21 17:06:07 +0000774 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000776
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000777 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
778 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000779 */
780
781static PyObject *
782binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 PyObject *x;
785 binaryfunc slotv = NULL;
786 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (v->ob_type->tp_as_number != NULL)
789 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
790 if (w->ob_type != v->ob_type &&
791 w->ob_type->tp_as_number != NULL) {
792 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
793 if (slotw == slotv)
794 slotw = NULL;
795 }
796 if (slotv) {
797 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
798 x = slotw(v, w);
799 if (x != Py_NotImplemented)
800 return x;
801 Py_DECREF(x); /* can't do it */
802 slotw = NULL;
803 }
804 x = slotv(v, w);
805 if (x != Py_NotImplemented)
806 return x;
807 Py_DECREF(x); /* can't do it */
808 }
809 if (slotw) {
810 x = slotw(v, w);
811 if (x != Py_NotImplemented)
812 return x;
813 Py_DECREF(x); /* can't do it */
814 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500815 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000816}
Guido van Rossum77660912002-04-16 16:32:50 +0000817
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000818static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000819binop_type_error(PyObject *v, PyObject *w, const char *op_name)
820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 PyErr_Format(PyExc_TypeError,
822 "unsupported operand type(s) for %.100s: "
823 "'%.100s' and '%.100s'",
824 op_name,
825 v->ob_type->tp_name,
826 w->ob_type->tp_name);
827 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000828}
829
830static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000831binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyObject *result = binary_op1(v, w, op_slot);
834 if (result == Py_NotImplemented) {
835 Py_DECREF(result);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530836
837 if (op_slot == NB_SLOT(nb_rshift) &&
838 PyCFunction_Check(v) &&
839 strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
840 {
841 PyErr_Format(PyExc_TypeError,
842 "unsupported operand type(s) for %.100s: "
843 "'%.100s' and '%.100s'. Did you mean \"print(<message>, "
Sanyam Khuranaa7c449b2017-08-18 17:48:14 +0530844 "file=<output_stream>)\"?",
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530845 op_name,
846 v->ob_type->tp_name,
847 w->ob_type->tp_name);
848 return NULL;
849 }
850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 return binop_type_error(v, w, op_name);
852 }
853 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000854}
855
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000856
857/*
858 Calling scheme used for ternary operations:
859
Neal Norwitz4886cc32006-08-21 17:06:07 +0000860 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000862 */
863
864static PyObject *
865ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyObject *w,
867 PyObject *z,
868 const int op_slot,
869 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyNumberMethods *mv, *mw, *mz;
872 PyObject *x = NULL;
873 ternaryfunc slotv = NULL;
874 ternaryfunc slotw = NULL;
875 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 mv = v->ob_type->tp_as_number;
878 mw = w->ob_type->tp_as_number;
879 if (mv != NULL)
880 slotv = NB_TERNOP(mv, op_slot);
881 if (w->ob_type != v->ob_type &&
882 mw != NULL) {
883 slotw = NB_TERNOP(mw, op_slot);
884 if (slotw == slotv)
885 slotw = NULL;
886 }
887 if (slotv) {
888 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
889 x = slotw(v, w, z);
890 if (x != Py_NotImplemented)
891 return x;
892 Py_DECREF(x); /* can't do it */
893 slotw = NULL;
894 }
895 x = slotv(v, w, z);
896 if (x != Py_NotImplemented)
897 return x;
898 Py_DECREF(x); /* can't do it */
899 }
900 if (slotw) {
901 x = slotw(v, w, z);
902 if (x != Py_NotImplemented)
903 return x;
904 Py_DECREF(x); /* can't do it */
905 }
906 mz = z->ob_type->tp_as_number;
907 if (mz != NULL) {
908 slotz = NB_TERNOP(mz, op_slot);
909 if (slotz == slotv || slotz == slotw)
910 slotz = NULL;
911 if (slotz) {
912 x = slotz(v, w, z);
913 if (x != Py_NotImplemented)
914 return x;
915 Py_DECREF(x); /* can't do it */
916 }
917 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (z == Py_None)
920 PyErr_Format(
921 PyExc_TypeError,
922 "unsupported operand type(s) for ** or pow(): "
923 "'%.100s' and '%.100s'",
924 v->ob_type->tp_name,
925 w->ob_type->tp_name);
926 else
927 PyErr_Format(
928 PyExc_TypeError,
929 "unsupported operand type(s) for pow(): "
930 "'%.100s', '%.100s', '%.100s'",
931 v->ob_type->tp_name,
932 w->ob_type->tp_name,
933 z->ob_type->tp_name);
934 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000935}
936
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000937#define BINARY_FUNC(func, op, op_name) \
938 PyObject * \
939 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000941 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000942
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000943BINARY_FUNC(PyNumber_Or, nb_or, "|")
944BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
945BINARY_FUNC(PyNumber_And, nb_and, "&")
946BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
947BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
948BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000949BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000950
951PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000952PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
955 if (result == Py_NotImplemented) {
956 PySequenceMethods *m = v->ob_type->tp_as_sequence;
957 Py_DECREF(result);
958 if (m && m->sq_concat) {
959 return (*m->sq_concat)(v, w);
960 }
961 result = binop_type_error(v, w, "+");
962 }
963 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000964}
965
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000966static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000967sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 Py_ssize_t count;
970 if (PyIndex_Check(n)) {
971 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
972 if (count == -1 && PyErr_Occurred())
973 return NULL;
974 }
975 else {
976 return type_error("can't multiply sequence by "
977 "non-int of type '%.200s'", n);
978 }
979 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000980}
981
982PyObject *
983PyNumber_Multiply(PyObject *v, PyObject *w)
984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
986 if (result == Py_NotImplemented) {
987 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
988 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
989 Py_DECREF(result);
990 if (mv && mv->sq_repeat) {
991 return sequence_repeat(mv->sq_repeat, v, w);
992 }
993 else if (mw && mw->sq_repeat) {
994 return sequence_repeat(mw->sq_repeat, w, v);
995 }
996 result = binop_type_error(v, w, "*");
997 }
998 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000999}
1000
Guido van Rossume15dee51995-07-18 14:12:02 +00001001PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001002PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
1003{
1004 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
1005}
1006
1007PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001008PyNumber_FloorDivide(PyObject *v, PyObject *w)
1009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +00001011}
1012
1013PyObject *
1014PyNumber_TrueDivide(PyObject *v, PyObject *w)
1015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +00001017}
1018
1019PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001020PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +00001021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +00001023}
1024
1025PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001026PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +00001029}
1030
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001031/* Binary in-place operators */
1032
1033/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001034 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001035
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001036 - If the left hand object has the appropriate struct members, and
1037 they are filled, call the appropriate function and return the
1038 result. No coercion is done on the arguments; the left-hand object
1039 is the one the operation is performed on, and it's up to the
1040 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001041
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001042 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001043 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001044
1045 */
1046
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001047static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001048binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 PyNumberMethods *mv = v->ob_type->tp_as_number;
1051 if (mv != NULL) {
1052 binaryfunc slot = NB_BINOP(mv, iop_slot);
1053 if (slot) {
1054 PyObject *x = (slot)(v, w);
1055 if (x != Py_NotImplemented) {
1056 return x;
1057 }
1058 Py_DECREF(x);
1059 }
1060 }
1061 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001062}
1063
1064static PyObject *
1065binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1069 if (result == Py_NotImplemented) {
1070 Py_DECREF(result);
1071 return binop_type_error(v, w, op_name);
1072 }
1073 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001074}
1075
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001076#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 PyObject * \
1078 func(PyObject *v, PyObject *w) { \
1079 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1080 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001081
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001082INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1083INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1084INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1085INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1086INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1087INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001088INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001089
1090PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001091PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1094 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001095}
1096
1097PyObject *
1098PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1101 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001102}
1103
1104PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001105PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1108 NB_SLOT(nb_add));
1109 if (result == Py_NotImplemented) {
1110 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1111 Py_DECREF(result);
1112 if (m != NULL) {
1113 binaryfunc f = NULL;
1114 f = m->sq_inplace_concat;
1115 if (f == NULL)
1116 f = m->sq_concat;
1117 if (f != NULL)
1118 return (*f)(v, w);
1119 }
1120 result = binop_type_error(v, w, "+=");
1121 }
1122 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001123}
1124
1125PyObject *
1126PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1129 NB_SLOT(nb_multiply));
1130 if (result == Py_NotImplemented) {
1131 ssizeargfunc f = NULL;
1132 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1133 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1134 Py_DECREF(result);
1135 if (mv != NULL) {
1136 f = mv->sq_inplace_repeat;
1137 if (f == NULL)
1138 f = mv->sq_repeat;
1139 if (f != NULL)
1140 return sequence_repeat(f, v, w);
1141 }
1142 else if (mw != NULL) {
1143 /* Note that the right hand operand should not be
1144 * mutated in this case so sq_inplace_repeat is not
1145 * used. */
1146 if (mw->sq_repeat)
1147 return sequence_repeat(mw->sq_repeat, w, v);
1148 }
1149 result = binop_type_error(v, w, "*=");
1150 }
1151 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001152}
1153
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001154PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001155PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1156{
1157 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1158 NB_SLOT(nb_matrix_multiply), "@=");
1159}
1160
1161PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001162PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1165 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001166}
1167
1168PyObject *
1169PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (v->ob_type->tp_as_number &&
1172 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1173 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1174 }
1175 else {
1176 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1177 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001178}
1179
1180
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001181/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001182
1183PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001184PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001187
Victor Stinner71aea8e2016-08-19 16:59:55 +02001188 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001190 }
1191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 m = o->ob_type->tp_as_number;
1193 if (m && m->nb_negative)
1194 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001197}
1198
1199PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001200PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001203
Victor Stinner71aea8e2016-08-19 16:59:55 +02001204 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001206 }
1207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 m = o->ob_type->tp_as_number;
1209 if (m && m->nb_positive)
1210 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001213}
1214
1215PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001216PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001219
Victor Stinner71aea8e2016-08-19 16:59:55 +02001220 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001222 }
1223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 m = o->ob_type->tp_as_number;
1225 if (m && m->nb_invert)
1226 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001229}
1230
1231PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001232PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001235
Victor Stinner71aea8e2016-08-19 16:59:55 +02001236 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001238 }
1239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 m = o->ob_type->tp_as_number;
1241 if (m && m->nb_absolute)
1242 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001245}
1246
Christian Tismerea62ce72018-06-09 20:32:25 +02001247#undef PyIndex_Check
Christian Tismer83987132018-06-11 00:48:28 +02001248
Christian Tismerea62ce72018-06-09 20:32:25 +02001249int
1250PyIndex_Check(PyObject *obj)
1251{
1252 return obj->ob_type->tp_as_number != NULL &&
1253 obj->ob_type->tp_as_number->nb_index != NULL;
1254}
1255
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001256/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001257 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001258 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001259*/
1260PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001261PyNumber_Index(PyObject *item)
1262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 PyObject *result = NULL;
Victor Stinner71aea8e2016-08-19 16:59:55 +02001264 if (item == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001266 }
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (PyLong_Check(item)) {
1269 Py_INCREF(item);
1270 return item;
1271 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001272 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 PyErr_Format(PyExc_TypeError,
1274 "'%.200s' object cannot be interpreted "
1275 "as an integer", item->ob_type->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001276 return NULL;
1277 }
1278 result = item->ob_type->tp_as_number->nb_index(item);
1279 if (!result || PyLong_CheckExact(result))
1280 return result;
1281 if (!PyLong_Check(result)) {
1282 PyErr_Format(PyExc_TypeError,
1283 "__index__ returned non-int (type %.200s)",
1284 result->ob_type->tp_name);
1285 Py_DECREF(result);
1286 return NULL;
1287 }
1288 /* Issue #17576: warn if 'result' not of exact type int. */
1289 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1290 "__index__ returned non-int (type %.200s). "
1291 "The ability to return an instance of a strict subclass of int "
1292 "is deprecated, and may be removed in a future version of Python.",
1293 result->ob_type->tp_name)) {
1294 Py_DECREF(result);
1295 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
1297 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001298}
1299
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001300/* Return an error on Overflow only if err is not NULL*/
1301
1302Py_ssize_t
1303PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 Py_ssize_t result;
1306 PyObject *runerr;
1307 PyObject *value = PyNumber_Index(item);
1308 if (value == NULL)
1309 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* We're done if PyLong_AsSsize_t() returns without error. */
1312 result = PyLong_AsSsize_t(value);
1313 if (result != -1 || !(runerr = PyErr_Occurred()))
1314 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Error handling code -- only manage OverflowError differently */
1317 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1318 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyErr_Clear();
1321 /* If no error-handling desired then the default clipping
1322 is sufficient.
1323 */
1324 if (!err) {
1325 assert(PyLong_Check(value));
1326 /* Whether or not it is less than or equal to
1327 zero is determined by the sign of ob_size
1328 */
1329 if (_PyLong_Sign(value) < 0)
1330 result = PY_SSIZE_T_MIN;
1331 else
1332 result = PY_SSIZE_T_MAX;
1333 }
1334 else {
1335 /* Otherwise replace the error with caller's error object. */
1336 PyErr_Format(err,
1337 "cannot fit '%.200s' into an index-sized integer",
1338 item->ob_type->tp_name);
1339 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001340
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001341 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 Py_DECREF(value);
1343 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001344}
1345
1346
Guido van Rossume15dee51995-07-18 14:12:02 +00001347PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001348PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001349{
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001350 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyObject *trunc_func;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001353 Py_buffer view;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001354 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001355
Victor Stinner71aea8e2016-08-19 16:59:55 +02001356 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001358 }
1359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (PyLong_CheckExact(o)) {
1361 Py_INCREF(o);
1362 return o;
1363 }
1364 m = o->ob_type->tp_as_number;
1365 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001366 result = (PyObject *)_PyLong_FromNbInt(o);
1367 if (result != NULL && !PyLong_CheckExact(result)) {
1368 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1369 }
1370 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001372 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (trunc_func) {
INADA Naoki72dccde2017-02-16 09:26:01 +09001374 result = _PyObject_CallNoArg(trunc_func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_DECREF(trunc_func);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001376 if (result == NULL || PyLong_CheckExact(result)) {
1377 return result;
1378 }
1379 if (PyLong_Check(result)) {
1380 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1381 return result;
1382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* __trunc__ is specified to return an Integral type,
Martin Panter7462b6492015-11-02 03:37:02 +00001384 but int() needs to return an int. */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001385 m = result->ob_type->tp_as_number;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001386 if (m == NULL || m->nb_int == NULL) {
1387 PyErr_Format(
1388 PyExc_TypeError,
1389 "__trunc__ returned non-Integral (type %.200s)",
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001390 result->ob_type->tp_name);
1391 Py_DECREF(result);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001392 return NULL;
1393 }
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001394 Py_SETREF(result, (PyObject *)_PyLong_FromNbInt(result));
1395 if (result != NULL && !PyLong_CheckExact(result)) {
1396 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1397 }
1398 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001400 if (PyErr_Occurred())
1401 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001402
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001403 if (PyUnicode_Check(o))
1404 /* The below check is done in PyLong_FromUnicode(). */
1405 return PyLong_FromUnicodeObject(o, 10);
1406
Martin Pantereeb896c2015-11-07 02:32:21 +00001407 if (PyBytes_Check(o))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001409 * doesn't do. In particular int('9\x005') must raise an
1410 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 */
Martin Pantereeb896c2015-11-07 02:32:21 +00001412 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1413 PyBytes_GET_SIZE(o), 10);
1414
1415 if (PyByteArray_Check(o))
1416 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1417 PyByteArray_GET_SIZE(o), 10);
1418
1419 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001420 PyObject *bytes;
Martin Pantereeb896c2015-11-07 02:32:21 +00001421
1422 /* Copy to NUL-terminated buffer. */
1423 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1424 if (bytes == NULL) {
1425 PyBuffer_Release(&view);
1426 return NULL;
1427 }
1428 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1429 PyBytes_GET_SIZE(bytes), 10);
1430 Py_DECREF(bytes);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001431 PyBuffer_Release(&view);
1432 return result;
1433 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001434
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001435 return type_error("int() argument must be a string, a bytes-like object "
1436 "or a number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001437}
1438
1439PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001440PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001443
Victor Stinner71aea8e2016-08-19 16:59:55 +02001444 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001446 }
1447
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001448 if (PyFloat_CheckExact(o)) {
1449 Py_INCREF(o);
1450 return o;
1451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 m = o->ob_type->tp_as_number;
1453 if (m && m->nb_float) { /* This should include subclasses of float */
1454 PyObject *res = m->nb_float(o);
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001455 double val;
1456 if (!res || PyFloat_CheckExact(res)) {
1457 return res;
1458 }
1459 if (!PyFloat_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001461 "%.50s.__float__ returned non-float (type %.50s)",
1462 o->ob_type->tp_name, res->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 Py_DECREF(res);
1464 return NULL;
1465 }
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001466 /* Issue #26983: warn if 'res' not of exact type float. */
1467 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1468 "%.50s.__float__ returned non-float (type %.50s). "
1469 "The ability to return an instance of a strict subclass of float "
1470 "is deprecated, and may be removed in a future version of Python.",
1471 o->ob_type->tp_name, res->ob_type->tp_name)) {
1472 Py_DECREF(res);
1473 return NULL;
1474 }
1475 val = PyFloat_AS_DOUBLE(res);
1476 Py_DECREF(res);
1477 return PyFloat_FromDouble(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 }
1479 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001480 return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 }
1482 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001483}
1484
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001485
1486PyObject *
1487PyNumber_ToBase(PyObject *n, int base)
1488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 PyObject *res = NULL;
1490 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (!index)
1493 return NULL;
1494 if (PyLong_Check(index))
1495 res = _PyLong_Format(index, base);
1496 else
1497 /* It should not be possible to get here, as
1498 PyNumber_Index already has a check for the same
1499 condition */
Serhiy Storchaka95949422013-08-27 19:40:23 +03001500 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 Py_DECREF(index);
1502 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001503}
1504
1505
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001506/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001507
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001508int
Fred Drake79912472000-07-09 04:06:11 +00001509PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (PyDict_Check(s))
1512 return 0;
Alexey Izbyshevb57b4ac2018-08-26 02:52:27 +03001513 return s->ob_type->tp_as_sequence &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001515}
1516
Martin v. Löwis18e16552006-02-15 17:27:45 +00001517Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001518PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if (s == NULL) {
1523 null_error();
1524 return -1;
1525 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 m = s->ob_type->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001528 if (m && m->sq_length) {
1529 Py_ssize_t len = m->sq_length(s);
1530 assert(len >= 0 || PyErr_Occurred());
1531 return len;
1532 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001533
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001534 if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_length) {
1535 type_error("%.200s is not a sequence", s);
1536 return -1;
1537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 type_error("object of type '%.200s' has no len()", s);
1539 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001540}
1541
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001542#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001543Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001544PySequence_Length(PyObject *s)
1545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001547}
1548#define PySequence_Length PySequence_Size
1549
Guido van Rossume15dee51995-07-18 14:12:02 +00001550PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001551PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001554
Victor Stinner71aea8e2016-08-19 16:59:55 +02001555 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001557 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 m = s->ob_type->tp_as_sequence;
1560 if (m && m->sq_concat)
1561 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 /* Instances of user classes defining an __add__() method only
1564 have an nb_add slot, not an sq_concat slot. So we fall back
1565 to nb_add if both arguments appear to be sequences. */
1566 if (PySequence_Check(s) && PySequence_Check(o)) {
1567 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1568 if (result != Py_NotImplemented)
1569 return result;
1570 Py_DECREF(result);
1571 }
1572 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001573}
1574
1575PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001579
Victor Stinner71aea8e2016-08-19 16:59:55 +02001580 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001582 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 m = o->ob_type->tp_as_sequence;
1585 if (m && m->sq_repeat)
1586 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 /* Instances of user classes defining a __mul__() method only
1589 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1590 to nb_multiply if o appears to be a sequence. */
1591 if (PySequence_Check(o)) {
1592 PyObject *n, *result;
1593 n = PyLong_FromSsize_t(count);
1594 if (n == NULL)
1595 return NULL;
1596 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1597 Py_DECREF(n);
1598 if (result != Py_NotImplemented)
1599 return result;
1600 Py_DECREF(result);
1601 }
1602 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001603}
1604
1605PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001606PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001609
Victor Stinner71aea8e2016-08-19 16:59:55 +02001610 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001612 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 m = s->ob_type->tp_as_sequence;
1615 if (m && m->sq_inplace_concat)
1616 return m->sq_inplace_concat(s, o);
1617 if (m && m->sq_concat)
1618 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (PySequence_Check(s) && PySequence_Check(o)) {
1621 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1622 NB_SLOT(nb_add));
1623 if (result != Py_NotImplemented)
1624 return result;
1625 Py_DECREF(result);
1626 }
1627 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001628}
1629
1630PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001631PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001634
Victor Stinner71aea8e2016-08-19 16:59:55 +02001635 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001637 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 m = o->ob_type->tp_as_sequence;
1640 if (m && m->sq_inplace_repeat)
1641 return m->sq_inplace_repeat(o, count);
1642 if (m && m->sq_repeat)
1643 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (PySequence_Check(o)) {
1646 PyObject *n, *result;
1647 n = PyLong_FromSsize_t(count);
1648 if (n == NULL)
1649 return NULL;
1650 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1651 NB_SLOT(nb_multiply));
1652 Py_DECREF(n);
1653 if (result != Py_NotImplemented)
1654 return result;
1655 Py_DECREF(result);
1656 }
1657 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001658}
1659
1660PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001661PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001664
Victor Stinner71aea8e2016-08-19 16:59:55 +02001665 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001667 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 m = s->ob_type->tp_as_sequence;
1670 if (m && m->sq_item) {
1671 if (i < 0) {
1672 if (m->sq_length) {
1673 Py_ssize_t l = (*m->sq_length)(s);
Victor Stinnere20310f2015-11-05 13:56:58 +01001674 if (l < 0) {
1675 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return NULL;
Victor Stinnere20310f2015-11-05 13:56:58 +01001677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 i += l;
1679 }
1680 }
1681 return m->sq_item(s, i);
1682 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001683
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001684 if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_subscript) {
1685 return type_error("%.200s is not a sequence", s);
1686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001688}
1689
1690PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001691PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001694
Victor Stinner71aea8e2016-08-19 16:59:55 +02001695 if (!s) {
1696 return null_error();
1697 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001700 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 PyObject *res;
1702 PyObject *slice = _PySlice_FromIndices(i1, i2);
1703 if (!slice)
1704 return NULL;
1705 res = mp->mp_subscript(s, slice);
1706 Py_DECREF(slice);
1707 return res;
1708 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001711}
1712
1713int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001714PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (s == NULL) {
1719 null_error();
1720 return -1;
1721 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 m = s->ob_type->tp_as_sequence;
1724 if (m && m->sq_ass_item) {
1725 if (i < 0) {
1726 if (m->sq_length) {
1727 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001728 if (l < 0) {
1729 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 i += l;
1733 }
1734 }
1735 return m->sq_ass_item(s, i, o);
1736 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001737
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001738 if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_ass_subscript) {
1739 type_error("%.200s is not a sequence", s);
1740 return -1;
1741 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 type_error("'%.200s' object does not support item assignment", s);
1743 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001744}
1745
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001746int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001747PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 if (s == NULL) {
1752 null_error();
1753 return -1;
1754 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 m = s->ob_type->tp_as_sequence;
1757 if (m && m->sq_ass_item) {
1758 if (i < 0) {
1759 if (m->sq_length) {
1760 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001761 if (l < 0) {
1762 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 i += l;
1766 }
1767 }
1768 return m->sq_ass_item(s, i, (PyObject *)NULL);
1769 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001770
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001771 if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_ass_subscript) {
1772 type_error("%.200s is not a sequence", s);
1773 return -1;
1774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 type_error("'%.200s' object doesn't support item deletion", s);
1776 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001777}
1778
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001779int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001780PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 if (s == NULL) {
1785 null_error();
1786 return -1;
1787 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001790 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 int res;
1792 PyObject *slice = _PySlice_FromIndices(i1, i2);
1793 if (!slice)
1794 return -1;
1795 res = mp->mp_ass_subscript(s, slice, o);
1796 Py_DECREF(slice);
1797 return res;
1798 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 type_error("'%.200s' object doesn't support slice assignment", s);
1801 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001802}
1803
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001804int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001805PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 if (s == NULL) {
1810 null_error();
1811 return -1;
1812 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001815 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 int res;
1817 PyObject *slice = _PySlice_FromIndices(i1, i2);
1818 if (!slice)
1819 return -1;
1820 res = mp->mp_ass_subscript(s, slice, NULL);
1821 Py_DECREF(slice);
1822 return res;
1823 }
1824 type_error("'%.200s' object doesn't support slice deletion", s);
1825 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001826}
1827
Guido van Rossume15dee51995-07-18 14:12:02 +00001828PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001829PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 PyObject *it; /* iter(v) */
1832 Py_ssize_t n; /* guess for result tuple size */
1833 PyObject *result = NULL;
1834 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001835
Victor Stinner71aea8e2016-08-19 16:59:55 +02001836 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001838 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 /* Special-case the common tuple and list cases, for efficiency. */
1841 if (PyTuple_CheckExact(v)) {
1842 /* Note that we can't know whether it's safe to return
1843 a tuple *subclass* instance as-is, hence the restriction
1844 to exact tuples here. In contrast, lists always make
1845 a copy, so there's no need for exactness below. */
1846 Py_INCREF(v);
1847 return v;
1848 }
Raymond Hettinger610a51f2015-05-17 14:45:58 -07001849 if (PyList_CheckExact(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 /* Get iterator. */
1853 it = PyObject_GetIter(v);
1854 if (it == NULL)
1855 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001858 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (n == -1)
1860 goto Fail;
1861 result = PyTuple_New(n);
1862 if (result == NULL)
1863 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 /* Fill the tuple. */
1866 for (j = 0; ; ++j) {
1867 PyObject *item = PyIter_Next(it);
1868 if (item == NULL) {
1869 if (PyErr_Occurred())
1870 goto Fail;
1871 break;
1872 }
1873 if (j >= n) {
Martin Pantere8db8612016-07-25 02:30:05 +00001874 size_t newn = (size_t)n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 /* The over-allocation strategy can grow a bit faster
1876 than for lists because unlike lists the
1877 over-allocation isn't permanent -- we reclaim
1878 the excess before the end of this routine.
1879 So, grow by ten and then add 25%.
1880 */
Martin Pantere8db8612016-07-25 02:30:05 +00001881 newn += 10u;
1882 newn += newn >> 2;
1883 if (newn > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 /* Check for overflow */
1885 PyErr_NoMemory();
1886 Py_DECREF(item);
1887 goto Fail;
1888 }
Martin Pantere8db8612016-07-25 02:30:05 +00001889 n = (Py_ssize_t)newn;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 if (_PyTuple_Resize(&result, n) != 0) {
1891 Py_DECREF(item);
1892 goto Fail;
1893 }
1894 }
1895 PyTuple_SET_ITEM(result, j, item);
1896 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 /* Cut tuple back if guess was too large. */
1899 if (j < n &&
1900 _PyTuple_Resize(&result, j) != 0)
1901 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 Py_DECREF(it);
1904 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001905
1906Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_XDECREF(result);
1908 Py_DECREF(it);
1909 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001910}
1911
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001912PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001913PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 PyObject *result; /* result list */
1916 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001917
Victor Stinner71aea8e2016-08-19 16:59:55 +02001918 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001920 }
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 result = PyList_New(0);
1923 if (result == NULL)
1924 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 rv = _PyList_Extend((PyListObject *)result, v);
1927 if (rv == NULL) {
1928 Py_DECREF(result);
1929 return NULL;
1930 }
1931 Py_DECREF(rv);
1932 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001933}
1934
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001935PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001936PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001939
Victor Stinner71aea8e2016-08-19 16:59:55 +02001940 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001942 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1945 Py_INCREF(v);
1946 return v;
1947 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 it = PyObject_GetIter(v);
1950 if (it == NULL) {
1951 if (PyErr_ExceptionMatches(PyExc_TypeError))
1952 PyErr_SetString(PyExc_TypeError, m);
1953 return NULL;
1954 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 v = PySequence_List(it);
1957 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001960}
1961
Tim Peters16a77ad2001-09-08 04:00:12 +00001962/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1964 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1965 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001966 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1967*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001968Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001969_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 Py_ssize_t n;
1972 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1973 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (seq == NULL || obj == NULL) {
1976 null_error();
1977 return -1;
1978 }
Tim Peters75f8e352001-05-05 11:33:43 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 it = PyObject_GetIter(seq);
1981 if (it == NULL) {
1982 type_error("argument of type '%.200s' is not iterable", seq);
1983 return -1;
1984 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 n = wrapped = 0;
1987 for (;;) {
1988 int cmp;
1989 PyObject *item = PyIter_Next(it);
1990 if (item == NULL) {
1991 if (PyErr_Occurred())
1992 goto Fail;
1993 break;
1994 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1997 Py_DECREF(item);
1998 if (cmp < 0)
1999 goto Fail;
2000 if (cmp > 0) {
2001 switch (operation) {
2002 case PY_ITERSEARCH_COUNT:
2003 if (n == PY_SSIZE_T_MAX) {
2004 PyErr_SetString(PyExc_OverflowError,
2005 "count exceeds C integer size");
2006 goto Fail;
2007 }
2008 ++n;
2009 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 case PY_ITERSEARCH_INDEX:
2012 if (wrapped) {
2013 PyErr_SetString(PyExc_OverflowError,
2014 "index exceeds C integer size");
2015 goto Fail;
2016 }
2017 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 case PY_ITERSEARCH_CONTAINS:
2020 n = 1;
2021 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07002024 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
2026 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (operation == PY_ITERSEARCH_INDEX) {
2029 if (n == PY_SSIZE_T_MAX)
2030 wrapped = 1;
2031 ++n;
2032 }
2033 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 if (operation != PY_ITERSEARCH_INDEX)
2036 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 PyErr_SetString(PyExc_ValueError,
2039 "sequence.index(x): x not in sequence");
2040 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00002041Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 n = -1;
2043 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00002044Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 Py_DECREF(it);
2046 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00002047
Guido van Rossume15dee51995-07-18 14:12:02 +00002048}
2049
Tim Peters16a77ad2001-09-08 04:00:12 +00002050/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002051Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002052PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00002055}
2056
Tim Peterscb8d3682001-05-05 21:05:01 +00002057/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00002058 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00002059 */
2060int
2061PySequence_Contains(PyObject *seq, PyObject *ob)
2062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 Py_ssize_t result;
2064 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
2065 if (sqm != NULL && sqm->sq_contains != NULL)
2066 return (*sqm->sq_contains)(seq, ob);
2067 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
2068 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00002069}
2070
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002071/* Backwards compatibility */
2072#undef PySequence_In
2073int
Fred Drake79912472000-07-09 04:06:11 +00002074PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002077}
2078
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002079Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00002080PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00002083}
2084
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002085/* Operations on mappings */
2086
2087int
Fred Drake79912472000-07-09 04:06:11 +00002088PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002089{
Benjamin Peterson21992272011-12-28 12:01:31 -06002090 return o && o->ob_type->tp_as_mapping &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002092}
2093
Martin v. Löwis18e16552006-02-15 17:27:45 +00002094Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002095PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (o == NULL) {
2100 null_error();
2101 return -1;
2102 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 m = o->ob_type->tp_as_mapping;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03002105 if (m && m->mp_length) {
2106 Py_ssize_t len = m->mp_length(o);
2107 assert(len >= 0 || PyErr_Occurred());
2108 return len;
2109 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002110
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03002111 if (o->ob_type->tp_as_sequence && o->ob_type->tp_as_sequence->sq_length) {
2112 type_error("%.200s is not a mapping", o);
2113 return -1;
2114 }
2115 /* PyMapping_Size() can be called from PyObject_Size(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 type_error("object of type '%.200s' has no len()", o);
2117 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002118}
2119
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002120#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002121Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002122PyMapping_Length(PyObject *o)
2123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002125}
2126#define PyMapping_Length PyMapping_Size
2127
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002128PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002129PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002132
Victor Stinner71aea8e2016-08-19 16:59:55 +02002133 if (key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002135 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 okey = PyUnicode_FromString(key);
2138 if (okey == NULL)
2139 return NULL;
2140 r = PyObject_GetItem(o, okey);
2141 Py_DECREF(okey);
2142 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002143}
2144
2145int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002146PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 PyObject *okey;
2149 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (key == NULL) {
2152 null_error();
2153 return -1;
2154 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 okey = PyUnicode_FromString(key);
2157 if (okey == NULL)
2158 return -1;
2159 r = PyObject_SetItem(o, okey, value);
2160 Py_DECREF(okey);
2161 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002162}
2163
2164int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002165PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 v = PyMapping_GetItemString(o, key);
2170 if (v) {
2171 Py_DECREF(v);
2172 return 1;
2173 }
2174 PyErr_Clear();
2175 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002176}
2177
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002178int
Fred Drake79912472000-07-09 04:06:11 +00002179PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 v = PyObject_GetItem(o, key);
2184 if (v) {
2185 Py_DECREF(v);
2186 return 1;
2187 }
2188 PyErr_Clear();
2189 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002190}
2191
Oren Milman0ccc0f62017-10-08 11:17:46 +03002192/* This function is quite similar to PySequence_Fast(), but specialized to be
2193 a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values().
2194 */
2195static PyObject *
2196method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
2197{
2198 PyObject *it, *result, *meth_output;
2199
2200 assert(o != NULL);
2201 meth_output = _PyObject_CallMethodId(o, meth_id, NULL);
2202 if (meth_output == NULL || PyList_CheckExact(meth_output)) {
2203 return meth_output;
2204 }
2205 it = PyObject_GetIter(meth_output);
2206 if (it == NULL) {
2207 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
2208 PyErr_Format(PyExc_TypeError,
2209 "%.200s.%U() returned a non-iterable (type %.200s)",
2210 Py_TYPE(o)->tp_name,
2211 meth_id->object,
2212 Py_TYPE(meth_output)->tp_name);
2213 }
2214 Py_DECREF(meth_output);
2215 return NULL;
2216 }
2217 Py_DECREF(meth_output);
2218 result = PySequence_List(it);
2219 Py_DECREF(it);
2220 return result;
2221}
2222
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002223PyObject *
2224PyMapping_Keys(PyObject *o)
2225{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002226 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002227
Oren Milman0ccc0f62017-10-08 11:17:46 +03002228 if (o == NULL) {
2229 return null_error();
2230 }
2231 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 return PyDict_Keys(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002233 }
2234 return method_output_as_list(o, &PyId_keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002235}
2236
2237PyObject *
2238PyMapping_Items(PyObject *o)
2239{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002240 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002241
Oren Milman0ccc0f62017-10-08 11:17:46 +03002242 if (o == NULL) {
2243 return null_error();
2244 }
2245 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 return PyDict_Items(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002247 }
2248 return method_output_as_list(o, &PyId_items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002249}
2250
2251PyObject *
2252PyMapping_Values(PyObject *o)
2253{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002254 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002255
Oren Milman0ccc0f62017-10-08 11:17:46 +03002256 if (o == NULL) {
2257 return null_error();
2258 }
2259 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 return PyDict_Values(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002261 }
2262 return method_output_as_list(o, &PyId_values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002263}
2264
Guido van Rossum823649d2001-03-21 18:40:58 +00002265/* isinstance(), issubclass() */
2266
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002267/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002268 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002269 * 1. getattr(cls, '__bases__') could raise an AttributeError
2270 * 2. getattr(cls, '__bases__') could raise some other exception
2271 * 3. getattr(cls, '__bases__') could return a tuple
2272 * 4. getattr(cls, '__bases__') could return something other than a tuple
2273 *
2274 * Only state #3 is a non-error state and only it returns a non-NULL object
2275 * (it returns the retrieved tuple).
2276 *
2277 * Any raised AttributeErrors are masked by clearing the exception and
2278 * returning NULL. If an object other than a tuple comes out of __bases__,
2279 * then again, the return value is NULL. So yes, these two situations
2280 * produce exactly the same results: NULL is returned and no error is set.
2281 *
2282 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002284 * exception to be propagated along.
2285 *
2286 * Callers are expected to test for PyErr_Occurred() when the return value
2287 * is NULL to decide whether a valid exception should be propagated or not.
2288 * When there's no exception to propagate, it's customary for the caller to
2289 * set a TypeError.
2290 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002291static PyObject *
2292abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002293{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002294 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 Py_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002298 (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 Py_END_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002300 if (bases != NULL && !PyTuple_Check(bases)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 Py_DECREF(bases);
2302 return NULL;
2303 }
2304 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002305}
2306
2307
2308static int
2309abstract_issubclass(PyObject *derived, PyObject *cls)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyObject *bases = NULL;
2312 Py_ssize_t i, n;
2313 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 while (1) {
2316 if (derived == cls)
2317 return 1;
2318 bases = abstract_get_bases(derived);
2319 if (bases == NULL) {
2320 if (PyErr_Occurred())
2321 return -1;
2322 return 0;
2323 }
2324 n = PyTuple_GET_SIZE(bases);
2325 if (n == 0) {
2326 Py_DECREF(bases);
2327 return 0;
2328 }
2329 /* Avoid recursivity in the single inheritance case */
2330 if (n == 1) {
2331 derived = PyTuple_GET_ITEM(bases, 0);
2332 Py_DECREF(bases);
2333 continue;
2334 }
2335 for (i = 0; i < n; i++) {
2336 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2337 if (r != 0)
2338 break;
2339 }
2340 Py_DECREF(bases);
2341 return r;
2342 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002343}
2344
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002345static int
2346check_class(PyObject *cls, const char *error)
2347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 PyObject *bases = abstract_get_bases(cls);
2349 if (bases == NULL) {
2350 /* Do not mask errors. */
2351 if (!PyErr_Occurred())
2352 PyErr_SetString(PyExc_TypeError, error);
2353 return 0;
2354 }
2355 Py_DECREF(bases);
2356 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002357}
2358
Brett Cannon4f653312004-03-20 22:52:14 +00002359static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002360recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 PyObject *icls;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002363 int retval;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002364 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 if (PyType_Check(cls)) {
2367 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2368 if (retval == 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002369 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2370 if (icls != NULL) {
2371 if (icls != (PyObject *)(inst->ob_type) && PyType_Check(icls)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 retval = PyType_IsSubtype(
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002373 (PyTypeObject *)icls,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 (PyTypeObject *)cls);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002375 }
2376 else {
2377 retval = 0;
2378 }
2379 Py_DECREF(icls);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
2381 }
2382 }
2383 else {
2384 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002385 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 return -1;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002387 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2388 if (icls != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 retval = abstract_issubclass(icls, cls);
2390 Py_DECREF(icls);
2391 }
2392 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002395}
2396
2397int
Brett Cannon4f653312004-03-20 22:52:14 +00002398PyObject_IsInstance(PyObject *inst, PyObject *cls)
2399{
Benjamin Petersonce798522012-01-22 11:24:29 -05002400 _Py_IDENTIFIER(__instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Quick test for an exact match */
2404 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2405 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002406
Georg Brandl72b8a802014-10-03 09:26:37 +02002407 /* We know what type's __instancecheck__ does. */
2408 if (PyType_CheckExact(cls)) {
2409 return recursive_isinstance(inst, cls);
2410 }
2411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (PyTuple_Check(cls)) {
2413 Py_ssize_t i;
2414 Py_ssize_t n;
2415 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2418 return -1;
2419 n = PyTuple_GET_SIZE(cls);
2420 for (i = 0; i < n; ++i) {
2421 PyObject *item = PyTuple_GET_ITEM(cls, i);
2422 r = PyObject_IsInstance(inst, item);
2423 if (r != 0)
2424 /* either found it, or got an error */
2425 break;
2426 }
2427 Py_LeaveRecursiveCall();
2428 return r;
2429 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002430
Benjamin Petersonce798522012-01-22 11:24:29 -05002431 checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (checker != NULL) {
2433 PyObject *res;
2434 int ok = -1;
2435 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2436 Py_DECREF(checker);
2437 return ok;
2438 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002439 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 Py_LeaveRecursiveCall();
2441 Py_DECREF(checker);
2442 if (res != NULL) {
2443 ok = PyObject_IsTrue(res);
2444 Py_DECREF(res);
2445 }
2446 return ok;
2447 }
2448 else if (PyErr_Occurred())
2449 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02002450 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002452}
2453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002455recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 if (PyType_Check(cls) && PyType_Check(derived)) {
2458 /* Fast path (non-recursive) */
2459 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2460 }
2461 if (!check_class(derived,
2462 "issubclass() arg 1 must be a class"))
2463 return -1;
2464 if (!check_class(cls,
2465 "issubclass() arg 2 must be a class"
2466 " or tuple of classes"))
2467 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002470}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002471
Brett Cannon4f653312004-03-20 22:52:14 +00002472int
2473PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2474{
Benjamin Petersonce798522012-01-22 11:24:29 -05002475 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002477
Georg Brandl72b8a802014-10-03 09:26:37 +02002478 /* We know what type's __subclasscheck__ does. */
2479 if (PyType_CheckExact(cls)) {
2480 /* Quick test for an exact match */
2481 if (derived == cls)
2482 return 1;
2483 return recursive_issubclass(derived, cls);
2484 }
2485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (PyTuple_Check(cls)) {
2487 Py_ssize_t i;
2488 Py_ssize_t n;
2489 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2492 return -1;
2493 n = PyTuple_GET_SIZE(cls);
2494 for (i = 0; i < n; ++i) {
2495 PyObject *item = PyTuple_GET_ITEM(cls, i);
2496 r = PyObject_IsSubclass(derived, item);
2497 if (r != 0)
2498 /* either found it, or got an error */
2499 break;
2500 }
2501 Py_LeaveRecursiveCall();
2502 return r;
2503 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002504
Benjamin Petersonce798522012-01-22 11:24:29 -05002505 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 if (checker != NULL) {
2507 PyObject *res;
2508 int ok = -1;
2509 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2510 Py_DECREF(checker);
2511 return ok;
2512 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002513 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 Py_LeaveRecursiveCall();
2515 Py_DECREF(checker);
2516 if (res != NULL) {
2517 ok = PyObject_IsTrue(res);
2518 Py_DECREF(res);
2519 }
2520 return ok;
2521 }
2522 else if (PyErr_Occurred())
2523 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02002524 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002526}
2527
2528int
2529_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002532}
2533
2534int
2535_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002538}
2539
2540
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002541PyObject *
2542PyObject_GetIter(PyObject *o)
2543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 PyTypeObject *t = o->ob_type;
Victor Stinner14e6d092016-12-09 17:08:59 +01002545 getiterfunc f;
2546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 f = t->tp_iter;
2548 if (f == NULL) {
2549 if (PySequence_Check(o))
2550 return PySeqIter_New(o);
2551 return type_error("'%.200s' object is not iterable", o);
2552 }
2553 else {
2554 PyObject *res = (*f)(o);
2555 if (res != NULL && !PyIter_Check(res)) {
2556 PyErr_Format(PyExc_TypeError,
2557 "iter() returned non-iterator "
2558 "of type '%.100s'",
2559 res->ob_type->tp_name);
2560 Py_DECREF(res);
2561 res = NULL;
2562 }
2563 return res;
2564 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002565}
2566
Christian Tismerea62ce72018-06-09 20:32:25 +02002567#undef PyIter_Check
Christian Tismer83987132018-06-11 00:48:28 +02002568
Christian Tismerea62ce72018-06-09 20:32:25 +02002569int PyIter_Check(PyObject *obj)
2570{
2571 return obj->ob_type->tp_iternext != NULL &&
2572 obj->ob_type->tp_iternext != &_PyObject_NextNotImplemented;
2573}
2574
Tim Petersf4848da2001-05-05 00:14:56 +00002575/* Return next item.
2576 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2577 * If the iteration terminates normally, return NULL and clear the
2578 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2579 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002581 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002582PyObject *
2583PyIter_Next(PyObject *iter)
2584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 PyObject *result;
2586 result = (*iter->ob_type->tp_iternext)(iter);
2587 if (result == NULL &&
2588 PyErr_Occurred() &&
2589 PyErr_ExceptionMatches(PyExc_StopIteration))
2590 PyErr_Clear();
2591 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002592}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002593
2594
2595/*
2596 * Flatten a sequence of bytes() objects into a C array of
2597 * NULL terminated string pointers with a NULL char* terminating the array.
2598 * (ie: an argv or env list)
2599 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002600 * Memory allocated for the returned list is allocated using PyMem_Malloc()
2601 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002602 */
2603char *const *
2604_PySequence_BytesToCharpArray(PyObject* self)
2605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 char **array;
2607 Py_ssize_t i, argc;
2608 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002609 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 argc = PySequence_Size(self);
2612 if (argc == -1)
2613 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002614
Stefan Krah7cacd2e2012-08-21 08:16:09 +02002615 assert(argc >= 0);
2616
2617 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2618 PyErr_NoMemory();
2619 return NULL;
2620 }
2621
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002622 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 if (array == NULL) {
2624 PyErr_NoMemory();
2625 return NULL;
2626 }
2627 for (i = 0; i < argc; ++i) {
2628 char *data;
2629 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02002630 if (item == NULL) {
2631 /* NULL terminate before freeing. */
2632 array[i] = NULL;
2633 goto fail;
2634 }
Serhiy Storchakad174d242017-06-23 19:39:27 +03002635 /* check for embedded null bytes */
2636 if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 /* NULL terminate before freeing. */
2638 array[i] = NULL;
2639 goto fail;
2640 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002641 size = PyBytes_GET_SIZE(item) + 1;
2642 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 if (!array[i]) {
2644 PyErr_NoMemory();
2645 goto fail;
2646 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002647 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 Py_DECREF(item);
2649 }
2650 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002653
2654fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 Py_XDECREF(item);
2656 _Py_FreeCharPArray(array);
2657 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002658}
2659
2660
2661/* Free's a NULL terminated char** array of C strings. */
2662void
2663_Py_FreeCharPArray(char *const array[])
2664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 Py_ssize_t i;
2666 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002667 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002669 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002670}