blob: 66ac0e3ea73760f4d61b35ca4c34426946873134 [file] [log] [blame]
Guido van Rossume15dee51995-07-18 14:12:02 +00001/* Abstract Object Interface (many thanks to Jim Fulton) */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002
Guido van Rossume15dee51995-07-18 14:12:02 +00003#include "Python.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00004#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00005#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00006#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00009
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000010/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000011
12static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
16 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000017}
18
Guido van Rossum052b7e11996-11-11 15:08:19 +000019static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000020null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (!PyErr_Occurred())
23 PyErr_SetString(PyExc_SystemError,
24 "null argument to internal routine");
25 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000026}
27
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000028/* Operations on any object */
29
Guido van Rossume15dee51995-07-18 14:12:02 +000030PyObject *
Fred Drake79912472000-07-09 04:06:11 +000031PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000034
Victor Stinner71aea8e2016-08-19 16:59:55 +020035 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +020037 }
38
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 v = (PyObject *)o->ob_type;
40 Py_INCREF(v);
41 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000042}
43
Martin v. Löwis18e16552006-02-15 17:27:45 +000044Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000045PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (o == NULL) {
50 null_error();
51 return -1;
52 }
Guido van Rossume15dee51995-07-18 14:12:02 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 m = o->ob_type->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +030055 if (m && m->sq_length) {
56 Py_ssize_t len = m->sq_length(o);
57 assert(len >= 0 || PyErr_Occurred());
58 return len;
59 }
Guido van Rossume15dee51995-07-18 14:12:02 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000062}
63
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000064#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000065Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000066PyObject_Length(PyObject *o)
67{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000069}
70#define PyObject_Length PyObject_Size
71
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020072int
73_PyObject_HasLen(PyObject *o) {
74 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
75 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
76}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000077
Christian Heimes255f53b2007-12-08 15:33:56 +000078/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020079 or o.__length_hint__(). If those methods aren't found the defaultvalue is
80 returned. If one of the calls fails with an exception other than TypeError
81 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000082*/
83
84Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020085PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000086{
Christian Heimesb70e8a12012-10-06 17:16:39 +020087 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020088 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050089 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030090 if (_PyObject_HasLen(o)) {
91 res = PyObject_Length(o);
Serhiy Storchaka813f9432017-04-16 09:21:44 +030092 if (res < 0) {
93 assert(PyErr_Occurred());
Serhiy Storchakaf740d462013-10-24 23:19:51 +030094 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
95 return -1;
96 }
97 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020098 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +030099 else {
100 return res;
101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 }
Christian Heimes6314d162012-10-06 17:13:29 +0200103 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200104 if (hint == NULL) {
105 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200107 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return defaultvalue;
109 }
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100110 result = _PyObject_CallNoArg(hint);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200111 Py_DECREF(hint);
112 if (result == NULL) {
113 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
114 PyErr_Clear();
115 return defaultvalue;
116 }
117 return -1;
118 }
119 else if (result == Py_NotImplemented) {
120 Py_DECREF(result);
121 return defaultvalue;
122 }
123 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200124 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200125 Py_TYPE(result)->tp_name);
126 Py_DECREF(result);
127 return -1;
128 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200129 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200130 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200131 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200132 return -1;
133 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200134 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200135 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
136 return -1;
137 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200138 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000139}
140
Guido van Rossume15dee51995-07-18 14:12:02 +0000141PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000142PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000145
Victor Stinner71aea8e2016-08-19 16:59:55 +0200146 if (o == NULL || key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +0200148 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 m = o->ob_type->tp_as_mapping;
Victor Stinnere20310f2015-11-05 13:56:58 +0100151 if (m && m->mp_subscript) {
152 PyObject *item = m->mp_subscript(o, key);
153 assert((item != NULL) ^ (PyErr_Occurred() != NULL));
154 return item;
155 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 if (o->ob_type->tp_as_sequence) {
158 if (PyIndex_Check(key)) {
159 Py_ssize_t key_value;
160 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
161 if (key_value == -1 && PyErr_Occurred())
162 return NULL;
163 return PySequence_GetItem(o, key_value);
164 }
165 else if (o->ob_type->tp_as_sequence->sq_item)
166 return type_error("sequence index must "
167 "be integer, not '%.200s'", key);
168 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000171}
172
173int
Fred Drake79912472000-07-09 04:06:11 +0000174PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (o == NULL || key == NULL || value == NULL) {
179 null_error();
180 return -1;
181 }
182 m = o->ob_type->tp_as_mapping;
183 if (m && m->mp_ass_subscript)
184 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (o->ob_type->tp_as_sequence) {
187 if (PyIndex_Check(key)) {
188 Py_ssize_t key_value;
189 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
190 if (key_value == -1 && PyErr_Occurred())
191 return -1;
192 return PySequence_SetItem(o, key_value, value);
193 }
194 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
195 type_error("sequence index must be "
196 "integer, not '%.200s'", key);
197 return -1;
198 }
199 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 type_error("'%.200s' object does not support item assignment", o);
202 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000203}
204
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000205int
Fred Drake79912472000-07-09 04:06:11 +0000206PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (o == NULL || key == NULL) {
211 null_error();
212 return -1;
213 }
214 m = o->ob_type->tp_as_mapping;
215 if (m && m->mp_ass_subscript)
216 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (o->ob_type->tp_as_sequence) {
219 if (PyIndex_Check(key)) {
220 Py_ssize_t key_value;
221 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
222 if (key_value == -1 && PyErr_Occurred())
223 return -1;
224 return PySequence_DelItem(o, key_value);
225 }
226 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
227 type_error("sequence index must be "
228 "integer, not '%.200s'", key);
229 return -1;
230 }
231 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 type_error("'%.200s' object does not support item deletion", o);
234 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000235}
236
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000237int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300238PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyObject *okey;
241 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (o == NULL || key == NULL) {
244 null_error();
245 return -1;
246 }
247 okey = PyUnicode_FromString(key);
248 if (okey == NULL)
249 return -1;
250 ret = PyObject_DelItem(o, okey);
251 Py_DECREF(okey);
252 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000253}
254
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000255/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000256 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000257 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000258int
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000259PyObject_CheckReadBuffer(PyObject *obj)
260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
262 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (pb == NULL ||
265 pb->bf_getbuffer == NULL)
266 return 0;
267 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
268 PyErr_Clear();
269 return 0;
270 }
271 PyBuffer_Release(&view);
272 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273}
274
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200275static int
276as_read_buffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
281 null_error();
282 return -1;
283 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200284 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 *buffer = view.buf;
288 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200289 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000291}
292
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200293int
294PyObject_AsCharBuffer(PyObject *obj,
295 const char **buffer,
296 Py_ssize_t *buffer_len)
297{
298 return as_read_buffer(obj, (const void **)buffer, buffer_len);
299}
300
301int PyObject_AsReadBuffer(PyObject *obj,
302 const void **buffer,
303 Py_ssize_t *buffer_len)
304{
305 return as_read_buffer(obj, buffer, buffer_len);
306}
307
Guido van Rossum4c08d552000-03-10 22:55:18 +0000308int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 void **buffer,
310 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyBufferProcs *pb;
313 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
316 null_error();
317 return -1;
318 }
319 pb = obj->ob_type->tp_as_buffer;
320 if (pb == NULL ||
321 pb->bf_getbuffer == NULL ||
322 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
323 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400324 "expected a writable bytes-like object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 return -1;
326 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 *buffer = view.buf;
329 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200330 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000332}
333
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000334/* Buffer C-API for Python 3.0 */
335
336int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000337PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000338{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200339 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
340
341 if (pb == NULL || pb->bf_getbuffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400343 "a bytes-like object is required, not '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_TYPE(obj)->tp_name);
345 return -1;
346 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200347 return (*pb->bf_getbuffer)(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000348}
349
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000350static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100351_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_ssize_t sd, dim;
354 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000355
Stefan Krah363af442015-02-01 14:53:54 +0100356 /* 1) len = product(shape) * itemsize
357 2) itemsize > 0
358 3) len = 0 <==> exists i: shape[i] = 0 */
359 if (view->len == 0) return 1;
360 if (view->strides == NULL) { /* C-contiguous by definition */
361 /* Trivially F-contiguous */
362 if (view->ndim <= 1) return 1;
363
364 /* ndim > 1 implies shape != NULL */
365 assert(view->shape != NULL);
366
367 /* Effectively 1-d */
368 sd = 0;
369 for (i=0; i<view->ndim; i++) {
370 if (view->shape[i] > 1) sd += 1;
371 }
372 return sd <= 1;
373 }
374
375 /* strides != NULL implies both of these */
376 assert(view->ndim > 0);
377 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 for (i=0; i<view->ndim; i++) {
381 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100382 if (dim > 1 && view->strides[i] != sd) {
383 return 0;
384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 sd *= dim;
386 }
387 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000388}
389
390static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100391_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_ssize_t sd, dim;
394 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000395
Stefan Krah363af442015-02-01 14:53:54 +0100396 /* 1) len = product(shape) * itemsize
397 2) itemsize > 0
398 3) len = 0 <==> exists i: shape[i] = 0 */
399 if (view->len == 0) return 1;
400 if (view->strides == NULL) return 1; /* C-contiguous by definition */
401
402 /* strides != NULL implies both of these */
403 assert(view->ndim > 0);
404 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 for (i=view->ndim-1; i>=0; i--) {
408 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100409 if (dim > 1 && view->strides[i] != sd) {
410 return 0;
411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 sd *= dim;
413 }
414 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000415}
416
417int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100418PyBuffer_IsContiguous(const Py_buffer *view, char order)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000419{
420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000422
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100423 if (order == 'C')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return _IsCContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100425 else if (order == 'F')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return _IsFortranContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100427 else if (order == 'A')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 return (_IsCContiguous(view) || _IsFortranContiguous(view));
429 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000430}
431
432
Guido van Rossum98297ee2007-11-06 21:34:58 +0000433void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000434PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 char* pointer;
437 int i;
438 pointer = (char *)view->buf;
439 for (i = 0; i < view->ndim; i++) {
440 pointer += view->strides[i]*indices[i];
441 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
442 pointer = *((char**)pointer) + view->suboffsets[i];
443 }
444 }
445 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000446}
447
448
Guido van Rossum98297ee2007-11-06 21:34:58 +0000449void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000450_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 +0000451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 for (k=0; k<nd; k++) {
455 if (index[k] < shape[k]-1) {
456 index[k]++;
457 break;
458 }
459 else {
460 index[k] = 0;
461 }
462 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000463}
464
Guido van Rossum98297ee2007-11-06 21:34:58 +0000465void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000466_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 +0000467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 for (k=nd-1; k>=0; k--) {
471 if (index[k] < shape[k]-1) {
472 index[k]++;
473 break;
474 }
475 else {
476 index[k] = 0;
477 }
478 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000479}
480
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000481int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000482PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000485 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_ssize_t *indices, elements;
487 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 if (len > view->len) {
490 len = view->len;
491 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (PyBuffer_IsContiguous(view, fort)) {
494 /* simplest copy is all that is needed */
495 memcpy(view->buf, buf, len);
496 return 0;
497 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000500
Stefan Krah7213fcc2015-02-01 16:19:23 +0100501 /* view->ndim <= 64 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
503 if (indices == NULL) {
504 PyErr_NoMemory();
505 return -1;
506 }
507 for (k=0; k<view->ndim;k++) {
508 indices[k] = 0;
509 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000512 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
514 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000515 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 }
517 src = buf;
518 /* XXX : This is not going to be the fastest code in the world
519 several optimizations are possible.
520 */
521 elements = len / view->itemsize;
522 while (elements--) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 ptr = PyBuffer_GetPointer(view, indices);
524 memcpy(ptr, src, view->itemsize);
525 src += view->itemsize;
Stefan Krah7213fcc2015-02-01 16:19:23 +0100526 addone(view->ndim, indices, view->shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyMem_Free(indices);
530 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000531}
532
Guido van Rossum98297ee2007-11-06 21:34:58 +0000533int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_buffer view_dest, view_src;
536 int k;
537 Py_ssize_t *indices, elements;
538 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (!PyObject_CheckBuffer(dest) ||
541 !PyObject_CheckBuffer(src)) {
542 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400543 "both destination and source must be "\
544 "bytes-like objects");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return -1;
546 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
549 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
550 PyBuffer_Release(&view_dest);
551 return -1;
552 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (view_dest.len < view_src.len) {
555 PyErr_SetString(PyExc_BufferError,
556 "destination is too small to receive data from source");
557 PyBuffer_Release(&view_dest);
558 PyBuffer_Release(&view_src);
559 return -1;
560 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
563 PyBuffer_IsContiguous(&view_src, 'C')) ||
564 (PyBuffer_IsContiguous(&view_dest, 'F') &&
565 PyBuffer_IsContiguous(&view_src, 'F'))) {
566 /* simplest copy is all that is needed */
567 memcpy(view_dest.buf, view_src.buf, view_src.len);
568 PyBuffer_Release(&view_dest);
569 PyBuffer_Release(&view_src);
570 return 0;
571 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* XXX(nnorwitz): need to check for overflow! */
576 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
577 if (indices == NULL) {
578 PyErr_NoMemory();
579 PyBuffer_Release(&view_dest);
580 PyBuffer_Release(&view_src);
581 return -1;
582 }
583 for (k=0; k<view_src.ndim;k++) {
584 indices[k] = 0;
585 }
586 elements = 1;
587 for (k=0; k<view_src.ndim; k++) {
588 /* XXX(nnorwitz): can this overflow? */
589 elements *= view_src.shape[k];
590 }
591 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000592 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 dptr = PyBuffer_GetPointer(&view_dest, indices);
594 sptr = PyBuffer_GetPointer(&view_src, indices);
595 memcpy(dptr, sptr, view_src.itemsize);
596 }
597 PyMem_Free(indices);
598 PyBuffer_Release(&view_dest);
599 PyBuffer_Release(&view_src);
600 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000601}
602
603void
604PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 Py_ssize_t *strides, int itemsize,
606 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 int k;
609 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 sd = itemsize;
612 if (fort == 'F') {
613 for (k=0; k<nd; k++) {
614 strides[k] = sd;
615 sd *= shape[k];
616 }
617 }
618 else {
619 for (k=nd-1; k>=0; k--) {
620 strides[k] = sd;
621 sd *= shape[k];
622 }
623 }
624 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000625}
626
627int
Martin v. Löwis423be952008-08-13 15:53:07 +0000628PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Stefan Krah4e141742012-03-06 15:27:31 +0100629 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000630{
Stefan Krah5178d912015-02-03 16:57:21 +0100631 if (view == NULL) {
632 PyErr_SetString(PyExc_BufferError,
633 "PyBuffer_FillInfo: view==NULL argument is obsolete");
634 return -1;
635 }
636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
638 (readonly == 1)) {
639 PyErr_SetString(PyExc_BufferError,
640 "Object is not writable.");
641 return -1;
642 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 view->obj = obj;
645 if (obj)
646 Py_INCREF(obj);
647 view->buf = buf;
648 view->len = len;
649 view->readonly = readonly;
650 view->itemsize = 1;
651 view->format = NULL;
652 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
653 view->format = "B";
654 view->ndim = 1;
655 view->shape = NULL;
656 if ((flags & PyBUF_ND) == PyBUF_ND)
657 view->shape = &(view->len);
658 view->strides = NULL;
659 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
660 view->strides = &(view->itemsize);
661 view->suboffsets = NULL;
662 view->internal = NULL;
663 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000664}
665
Martin v. Löwis423be952008-08-13 15:53:07 +0000666void
667PyBuffer_Release(Py_buffer *view)
668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject *obj = view->obj;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200670 PyBufferProcs *pb;
671 if (obj == NULL)
672 return;
673 pb = Py_TYPE(obj)->tp_as_buffer;
674 if (pb && pb->bf_releasebuffer)
675 pb->bf_releasebuffer(obj, view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 view->obj = NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200677 Py_DECREF(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +0000678}
679
Eric Smith8fd3eba2008-02-17 19:48:00 +0000680PyObject *
681PyObject_Format(PyObject *obj, PyObject *format_spec)
682{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000683 PyObject *meth;
684 PyObject *empty = NULL;
685 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500686 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000687
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300688 if (format_spec != NULL && !PyUnicode_Check(format_spec)) {
689 PyErr_Format(PyExc_SystemError,
690 "Format specifier must be a string, not %.200s",
691 Py_TYPE(format_spec)->tp_name);
692 return NULL;
693 }
694
695 /* Fast path for common types. */
696 if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
697 if (PyUnicode_CheckExact(obj)) {
698 Py_INCREF(obj);
699 return obj;
700 }
701 if (PyLong_CheckExact(obj)) {
702 return PyObject_Str(obj);
703 }
704 }
705
Eric Smith8fd3eba2008-02-17 19:48:00 +0000706 /* If no format_spec is provided, use an empty string */
707 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100708 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000709 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000710 }
711
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300712 /* Find the (unbound!) __format__ method */
Benjamin Petersonce798522012-01-22 11:24:29 -0500713 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000714 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000715 if (!PyErr_Occurred())
716 PyErr_Format(PyExc_TypeError,
717 "Type %.100s doesn't define __format__",
718 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000720 }
721
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000722 /* And call it. */
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100723 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000724 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000725
726 if (result && !PyUnicode_Check(result)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800727 PyErr_Format(PyExc_TypeError,
728 "__format__ must return a str, not %.200s",
729 Py_TYPE(result)->tp_name);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000730 Py_DECREF(result);
731 result = NULL;
732 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000733 }
734
735done:
736 Py_XDECREF(empty);
737 return result;
738}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000739/* Operations on numbers */
740
741int
Fred Drake79912472000-07-09 04:06:11 +0000742PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return o && o->ob_type->tp_as_number &&
745 (o->ob_type->tp_as_number->nb_int ||
746 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000747}
748
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000749/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000750
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000751#define NB_SLOT(x) offsetof(PyNumberMethods, x)
752#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000754#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000756
757/*
758 Calling scheme used for binary operations:
759
Neal Norwitz4886cc32006-08-21 17:06:07 +0000760 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000762
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000763 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
764 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000765 */
766
767static PyObject *
768binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *x;
771 binaryfunc slotv = NULL;
772 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 if (v->ob_type->tp_as_number != NULL)
775 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
776 if (w->ob_type != v->ob_type &&
777 w->ob_type->tp_as_number != NULL) {
778 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
779 if (slotw == slotv)
780 slotw = NULL;
781 }
782 if (slotv) {
783 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
784 x = slotw(v, w);
785 if (x != Py_NotImplemented)
786 return x;
787 Py_DECREF(x); /* can't do it */
788 slotw = NULL;
789 }
790 x = slotv(v, w);
791 if (x != Py_NotImplemented)
792 return x;
793 Py_DECREF(x); /* can't do it */
794 }
795 if (slotw) {
796 x = slotw(v, w);
797 if (x != Py_NotImplemented)
798 return x;
799 Py_DECREF(x); /* can't do it */
800 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500801 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000802}
Guido van Rossum77660912002-04-16 16:32:50 +0000803
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000804static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000805binop_type_error(PyObject *v, PyObject *w, const char *op_name)
806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyErr_Format(PyExc_TypeError,
808 "unsupported operand type(s) for %.100s: "
809 "'%.100s' and '%.100s'",
810 op_name,
811 v->ob_type->tp_name,
812 w->ob_type->tp_name);
813 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000814}
815
816static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000817binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 PyObject *result = binary_op1(v, w, op_slot);
820 if (result == Py_NotImplemented) {
821 Py_DECREF(result);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530822
823 if (op_slot == NB_SLOT(nb_rshift) &&
824 PyCFunction_Check(v) &&
825 strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
826 {
827 PyErr_Format(PyExc_TypeError,
828 "unsupported operand type(s) for %.100s: "
829 "'%.100s' and '%.100s'. Did you mean \"print(<message>, "
Sanyam Khuranaa7c449b2017-08-18 17:48:14 +0530830 "file=<output_stream>)\"?",
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530831 op_name,
832 v->ob_type->tp_name,
833 w->ob_type->tp_name);
834 return NULL;
835 }
836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 return binop_type_error(v, w, op_name);
838 }
839 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000840}
841
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000842
843/*
844 Calling scheme used for ternary operations:
845
Neal Norwitz4886cc32006-08-21 17:06:07 +0000846 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000848 */
849
850static PyObject *
851ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyObject *w,
853 PyObject *z,
854 const int op_slot,
855 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyNumberMethods *mv, *mw, *mz;
858 PyObject *x = NULL;
859 ternaryfunc slotv = NULL;
860 ternaryfunc slotw = NULL;
861 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 mv = v->ob_type->tp_as_number;
864 mw = w->ob_type->tp_as_number;
865 if (mv != NULL)
866 slotv = NB_TERNOP(mv, op_slot);
867 if (w->ob_type != v->ob_type &&
868 mw != NULL) {
869 slotw = NB_TERNOP(mw, op_slot);
870 if (slotw == slotv)
871 slotw = NULL;
872 }
873 if (slotv) {
874 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
875 x = slotw(v, w, z);
876 if (x != Py_NotImplemented)
877 return x;
878 Py_DECREF(x); /* can't do it */
879 slotw = NULL;
880 }
881 x = slotv(v, w, z);
882 if (x != Py_NotImplemented)
883 return x;
884 Py_DECREF(x); /* can't do it */
885 }
886 if (slotw) {
887 x = slotw(v, w, z);
888 if (x != Py_NotImplemented)
889 return x;
890 Py_DECREF(x); /* can't do it */
891 }
892 mz = z->ob_type->tp_as_number;
893 if (mz != NULL) {
894 slotz = NB_TERNOP(mz, op_slot);
895 if (slotz == slotv || slotz == slotw)
896 slotz = NULL;
897 if (slotz) {
898 x = slotz(v, w, z);
899 if (x != Py_NotImplemented)
900 return x;
901 Py_DECREF(x); /* can't do it */
902 }
903 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 if (z == Py_None)
906 PyErr_Format(
907 PyExc_TypeError,
908 "unsupported operand type(s) for ** or pow(): "
909 "'%.100s' and '%.100s'",
910 v->ob_type->tp_name,
911 w->ob_type->tp_name);
912 else
913 PyErr_Format(
914 PyExc_TypeError,
915 "unsupported operand type(s) for pow(): "
916 "'%.100s', '%.100s', '%.100s'",
917 v->ob_type->tp_name,
918 w->ob_type->tp_name,
919 z->ob_type->tp_name);
920 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000921}
922
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000923#define BINARY_FUNC(func, op, op_name) \
924 PyObject * \
925 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000927 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000928
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000929BINARY_FUNC(PyNumber_Or, nb_or, "|")
930BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
931BINARY_FUNC(PyNumber_And, nb_and, "&")
932BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
933BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
934BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000935BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000936
937PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000938PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
941 if (result == Py_NotImplemented) {
942 PySequenceMethods *m = v->ob_type->tp_as_sequence;
943 Py_DECREF(result);
944 if (m && m->sq_concat) {
945 return (*m->sq_concat)(v, w);
946 }
947 result = binop_type_error(v, w, "+");
948 }
949 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000950}
951
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000952static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000953sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 Py_ssize_t count;
956 if (PyIndex_Check(n)) {
957 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
958 if (count == -1 && PyErr_Occurred())
959 return NULL;
960 }
961 else {
962 return type_error("can't multiply sequence by "
963 "non-int of type '%.200s'", n);
964 }
965 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000966}
967
968PyObject *
969PyNumber_Multiply(PyObject *v, PyObject *w)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
972 if (result == Py_NotImplemented) {
973 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
974 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
975 Py_DECREF(result);
976 if (mv && mv->sq_repeat) {
977 return sequence_repeat(mv->sq_repeat, v, w);
978 }
979 else if (mw && mw->sq_repeat) {
980 return sequence_repeat(mw->sq_repeat, w, v);
981 }
982 result = binop_type_error(v, w, "*");
983 }
984 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000985}
986
Guido van Rossume15dee51995-07-18 14:12:02 +0000987PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -0400988PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
989{
990 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
991}
992
993PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000994PyNumber_FloorDivide(PyObject *v, PyObject *w)
995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000997}
998
999PyObject *
1000PyNumber_TrueDivide(PyObject *v, PyObject *w)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +00001003}
1004
1005PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001006PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +00001007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +00001009}
1010
1011PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001012PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +00001013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +00001015}
1016
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001017/* Binary in-place operators */
1018
1019/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001020 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001021
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001022 - If the left hand object has the appropriate struct members, and
1023 they are filled, call the appropriate function and return the
1024 result. No coercion is done on the arguments; the left-hand object
1025 is the one the operation is performed on, and it's up to the
1026 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001027
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001028 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001029 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001030
1031 */
1032
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001033static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001034binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 PyNumberMethods *mv = v->ob_type->tp_as_number;
1037 if (mv != NULL) {
1038 binaryfunc slot = NB_BINOP(mv, iop_slot);
1039 if (slot) {
1040 PyObject *x = (slot)(v, w);
1041 if (x != Py_NotImplemented) {
1042 return x;
1043 }
1044 Py_DECREF(x);
1045 }
1046 }
1047 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001048}
1049
1050static PyObject *
1051binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1055 if (result == Py_NotImplemented) {
1056 Py_DECREF(result);
1057 return binop_type_error(v, w, op_name);
1058 }
1059 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001060}
1061
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001062#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyObject * \
1064 func(PyObject *v, PyObject *w) { \
1065 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1066 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001067
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001068INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1069INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1070INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1071INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1072INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1073INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001074INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001075
1076PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001077PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1080 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001081}
1082
1083PyObject *
1084PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1087 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001088}
1089
1090PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001091PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1094 NB_SLOT(nb_add));
1095 if (result == Py_NotImplemented) {
1096 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1097 Py_DECREF(result);
1098 if (m != NULL) {
1099 binaryfunc f = NULL;
1100 f = m->sq_inplace_concat;
1101 if (f == NULL)
1102 f = m->sq_concat;
1103 if (f != NULL)
1104 return (*f)(v, w);
1105 }
1106 result = binop_type_error(v, w, "+=");
1107 }
1108 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001109}
1110
1111PyObject *
1112PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1115 NB_SLOT(nb_multiply));
1116 if (result == Py_NotImplemented) {
1117 ssizeargfunc f = NULL;
1118 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1119 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1120 Py_DECREF(result);
1121 if (mv != NULL) {
1122 f = mv->sq_inplace_repeat;
1123 if (f == NULL)
1124 f = mv->sq_repeat;
1125 if (f != NULL)
1126 return sequence_repeat(f, v, w);
1127 }
1128 else if (mw != NULL) {
1129 /* Note that the right hand operand should not be
1130 * mutated in this case so sq_inplace_repeat is not
1131 * used. */
1132 if (mw->sq_repeat)
1133 return sequence_repeat(mw->sq_repeat, w, v);
1134 }
1135 result = binop_type_error(v, w, "*=");
1136 }
1137 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001138}
1139
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001140PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001141PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1142{
1143 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1144 NB_SLOT(nb_matrix_multiply), "@=");
1145}
1146
1147PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001148PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1151 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001152}
1153
1154PyObject *
1155PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (v->ob_type->tp_as_number &&
1158 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1159 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1160 }
1161 else {
1162 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1163 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001164}
1165
1166
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001167/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001168
1169PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001170PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001173
Victor Stinner71aea8e2016-08-19 16:59:55 +02001174 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001176 }
1177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 m = o->ob_type->tp_as_number;
1179 if (m && m->nb_negative)
1180 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001183}
1184
1185PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001186PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001189
Victor Stinner71aea8e2016-08-19 16:59:55 +02001190 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001192 }
1193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 m = o->ob_type->tp_as_number;
1195 if (m && m->nb_positive)
1196 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001199}
1200
1201PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001202PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001205
Victor Stinner71aea8e2016-08-19 16:59:55 +02001206 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001208 }
1209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 m = o->ob_type->tp_as_number;
1211 if (m && m->nb_invert)
1212 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001215}
1216
1217PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001218PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001221
Victor Stinner71aea8e2016-08-19 16:59:55 +02001222 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001224 }
1225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 m = o->ob_type->tp_as_number;
1227 if (m && m->nb_absolute)
1228 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001231}
1232
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001233/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001234 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001235 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001236*/
1237PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001238PyNumber_Index(PyObject *item)
1239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PyObject *result = NULL;
Victor Stinner71aea8e2016-08-19 16:59:55 +02001241 if (item == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001243 }
1244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (PyLong_Check(item)) {
1246 Py_INCREF(item);
1247 return item;
1248 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001249 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 PyErr_Format(PyExc_TypeError,
1251 "'%.200s' object cannot be interpreted "
1252 "as an integer", item->ob_type->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001253 return NULL;
1254 }
1255 result = item->ob_type->tp_as_number->nb_index(item);
1256 if (!result || PyLong_CheckExact(result))
1257 return result;
1258 if (!PyLong_Check(result)) {
1259 PyErr_Format(PyExc_TypeError,
1260 "__index__ returned non-int (type %.200s)",
1261 result->ob_type->tp_name);
1262 Py_DECREF(result);
1263 return NULL;
1264 }
1265 /* Issue #17576: warn if 'result' not of exact type int. */
1266 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1267 "__index__ returned non-int (type %.200s). "
1268 "The ability to return an instance of a strict subclass of int "
1269 "is deprecated, and may be removed in a future version of Python.",
1270 result->ob_type->tp_name)) {
1271 Py_DECREF(result);
1272 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 }
1274 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001275}
1276
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001277/* Return an error on Overflow only if err is not NULL*/
1278
1279Py_ssize_t
1280PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 Py_ssize_t result;
1283 PyObject *runerr;
1284 PyObject *value = PyNumber_Index(item);
1285 if (value == NULL)
1286 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* We're done if PyLong_AsSsize_t() returns without error. */
1289 result = PyLong_AsSsize_t(value);
1290 if (result != -1 || !(runerr = PyErr_Occurred()))
1291 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* Error handling code -- only manage OverflowError differently */
1294 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1295 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 PyErr_Clear();
1298 /* If no error-handling desired then the default clipping
1299 is sufficient.
1300 */
1301 if (!err) {
1302 assert(PyLong_Check(value));
1303 /* Whether or not it is less than or equal to
1304 zero is determined by the sign of ob_size
1305 */
1306 if (_PyLong_Sign(value) < 0)
1307 result = PY_SSIZE_T_MIN;
1308 else
1309 result = PY_SSIZE_T_MAX;
1310 }
1311 else {
1312 /* Otherwise replace the error with caller's error object. */
1313 PyErr_Format(err,
1314 "cannot fit '%.200s' into an index-sized integer",
1315 item->ob_type->tp_name);
1316 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001317
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001318 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 Py_DECREF(value);
1320 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001321}
1322
1323
Guido van Rossume15dee51995-07-18 14:12:02 +00001324PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001325PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001326{
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001327 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *trunc_func;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001330 Py_buffer view;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001331 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001332
Victor Stinner71aea8e2016-08-19 16:59:55 +02001333 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001335 }
1336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (PyLong_CheckExact(o)) {
1338 Py_INCREF(o);
1339 return o;
1340 }
1341 m = o->ob_type->tp_as_number;
1342 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001343 result = (PyObject *)_PyLong_FromNbInt(o);
1344 if (result != NULL && !PyLong_CheckExact(result)) {
1345 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1346 }
1347 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001349 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (trunc_func) {
INADA Naoki72dccde2017-02-16 09:26:01 +09001351 result = _PyObject_CallNoArg(trunc_func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_DECREF(trunc_func);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001353 if (result == NULL || PyLong_CheckExact(result)) {
1354 return result;
1355 }
1356 if (PyLong_Check(result)) {
1357 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1358 return result;
1359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 /* __trunc__ is specified to return an Integral type,
Martin Panter7462b6492015-11-02 03:37:02 +00001361 but int() needs to return an int. */
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001362 m = result->ob_type->tp_as_number;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001363 if (m == NULL || m->nb_int == NULL) {
1364 PyErr_Format(
1365 PyExc_TypeError,
1366 "__trunc__ returned non-Integral (type %.200s)",
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001367 result->ob_type->tp_name);
1368 Py_DECREF(result);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001369 return NULL;
1370 }
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001371 Py_SETREF(result, (PyObject *)_PyLong_FromNbInt(result));
1372 if (result != NULL && !PyLong_CheckExact(result)) {
1373 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1374 }
1375 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001377 if (PyErr_Occurred())
1378 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001379
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001380 if (PyUnicode_Check(o))
1381 /* The below check is done in PyLong_FromUnicode(). */
1382 return PyLong_FromUnicodeObject(o, 10);
1383
Martin Pantereeb896c2015-11-07 02:32:21 +00001384 if (PyBytes_Check(o))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001386 * doesn't do. In particular int('9\x005') must raise an
1387 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 */
Martin Pantereeb896c2015-11-07 02:32:21 +00001389 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1390 PyBytes_GET_SIZE(o), 10);
1391
1392 if (PyByteArray_Check(o))
1393 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1394 PyByteArray_GET_SIZE(o), 10);
1395
1396 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001397 PyObject *bytes;
Martin Pantereeb896c2015-11-07 02:32:21 +00001398
1399 /* Copy to NUL-terminated buffer. */
1400 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1401 if (bytes == NULL) {
1402 PyBuffer_Release(&view);
1403 return NULL;
1404 }
1405 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1406 PyBytes_GET_SIZE(bytes), 10);
1407 Py_DECREF(bytes);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001408 PyBuffer_Release(&view);
1409 return result;
1410 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001411
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001412 return type_error("int() argument must be a string, a bytes-like object "
1413 "or a number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001414}
1415
1416PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001417PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001420
Victor Stinner71aea8e2016-08-19 16:59:55 +02001421 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001423 }
1424
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001425 if (PyFloat_CheckExact(o)) {
1426 Py_INCREF(o);
1427 return o;
1428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 m = o->ob_type->tp_as_number;
1430 if (m && m->nb_float) { /* This should include subclasses of float */
1431 PyObject *res = m->nb_float(o);
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001432 double val;
1433 if (!res || PyFloat_CheckExact(res)) {
1434 return res;
1435 }
1436 if (!PyFloat_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001438 "%.50s.__float__ returned non-float (type %.50s)",
1439 o->ob_type->tp_name, res->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 Py_DECREF(res);
1441 return NULL;
1442 }
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001443 /* Issue #26983: warn if 'res' not of exact type float. */
1444 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1445 "%.50s.__float__ returned non-float (type %.50s). "
1446 "The ability to return an instance of a strict subclass of float "
1447 "is deprecated, and may be removed in a future version of Python.",
1448 o->ob_type->tp_name, res->ob_type->tp_name)) {
1449 Py_DECREF(res);
1450 return NULL;
1451 }
1452 val = PyFloat_AS_DOUBLE(res);
1453 Py_DECREF(res);
1454 return PyFloat_FromDouble(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 }
1456 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001457 return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 }
1459 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001460}
1461
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001462
1463PyObject *
1464PyNumber_ToBase(PyObject *n, int base)
1465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 PyObject *res = NULL;
1467 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (!index)
1470 return NULL;
1471 if (PyLong_Check(index))
1472 res = _PyLong_Format(index, base);
1473 else
1474 /* It should not be possible to get here, as
1475 PyNumber_Index already has a check for the same
1476 condition */
Serhiy Storchaka95949422013-08-27 19:40:23 +03001477 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not int");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 Py_DECREF(index);
1479 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001480}
1481
1482
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001483/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001484
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001485int
Fred Drake79912472000-07-09 04:06:11 +00001486PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 if (PyDict_Check(s))
1489 return 0;
1490 return s != NULL && s->ob_type->tp_as_sequence &&
1491 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001492}
1493
Martin v. Löwis18e16552006-02-15 17:27:45 +00001494Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001495PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (s == NULL) {
1500 null_error();
1501 return -1;
1502 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 m = s->ob_type->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001505 if (m && m->sq_length) {
1506 Py_ssize_t len = m->sq_length(s);
1507 assert(len >= 0 || PyErr_Occurred());
1508 return len;
1509 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 type_error("object of type '%.200s' has no len()", s);
1512 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001513}
1514
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001515#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001516Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001517PySequence_Length(PyObject *s)
1518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001520}
1521#define PySequence_Length PySequence_Size
1522
Guido van Rossume15dee51995-07-18 14:12:02 +00001523PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001524PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001527
Victor Stinner71aea8e2016-08-19 16:59:55 +02001528 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001530 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 m = s->ob_type->tp_as_sequence;
1533 if (m && m->sq_concat)
1534 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 /* Instances of user classes defining an __add__() method only
1537 have an nb_add slot, not an sq_concat slot. So we fall back
1538 to nb_add if both arguments appear to be sequences. */
1539 if (PySequence_Check(s) && PySequence_Check(o)) {
1540 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1541 if (result != Py_NotImplemented)
1542 return result;
1543 Py_DECREF(result);
1544 }
1545 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001546}
1547
1548PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001549PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001552
Victor Stinner71aea8e2016-08-19 16:59:55 +02001553 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001555 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 m = o->ob_type->tp_as_sequence;
1558 if (m && m->sq_repeat)
1559 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 /* Instances of user classes defining a __mul__() method only
1562 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1563 to nb_multiply if o appears to be a sequence. */
1564 if (PySequence_Check(o)) {
1565 PyObject *n, *result;
1566 n = PyLong_FromSsize_t(count);
1567 if (n == NULL)
1568 return NULL;
1569 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1570 Py_DECREF(n);
1571 if (result != Py_NotImplemented)
1572 return result;
1573 Py_DECREF(result);
1574 }
1575 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001576}
1577
1578PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001579PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001582
Victor Stinner71aea8e2016-08-19 16:59:55 +02001583 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001585 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 m = s->ob_type->tp_as_sequence;
1588 if (m && m->sq_inplace_concat)
1589 return m->sq_inplace_concat(s, o);
1590 if (m && m->sq_concat)
1591 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (PySequence_Check(s) && PySequence_Check(o)) {
1594 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1595 NB_SLOT(nb_add));
1596 if (result != Py_NotImplemented)
1597 return result;
1598 Py_DECREF(result);
1599 }
1600 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001601}
1602
1603PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001604PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001607
Victor Stinner71aea8e2016-08-19 16:59:55 +02001608 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001610 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 m = o->ob_type->tp_as_sequence;
1613 if (m && m->sq_inplace_repeat)
1614 return m->sq_inplace_repeat(o, count);
1615 if (m && m->sq_repeat)
1616 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (PySequence_Check(o)) {
1619 PyObject *n, *result;
1620 n = PyLong_FromSsize_t(count);
1621 if (n == NULL)
1622 return NULL;
1623 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1624 NB_SLOT(nb_multiply));
1625 Py_DECREF(n);
1626 if (result != Py_NotImplemented)
1627 return result;
1628 Py_DECREF(result);
1629 }
1630 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001631}
1632
1633PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001634PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001637
Victor Stinner71aea8e2016-08-19 16:59:55 +02001638 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001640 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 m = s->ob_type->tp_as_sequence;
1643 if (m && m->sq_item) {
1644 if (i < 0) {
1645 if (m->sq_length) {
1646 Py_ssize_t l = (*m->sq_length)(s);
Victor Stinnere20310f2015-11-05 13:56:58 +01001647 if (l < 0) {
1648 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 return NULL;
Victor Stinnere20310f2015-11-05 13:56:58 +01001650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 i += l;
1652 }
1653 }
1654 return m->sq_item(s, i);
1655 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001658}
1659
1660PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001661PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001664
Victor Stinner71aea8e2016-08-19 16:59:55 +02001665 if (!s) {
1666 return null_error();
1667 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001670 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 PyObject *res;
1672 PyObject *slice = _PySlice_FromIndices(i1, i2);
1673 if (!slice)
1674 return NULL;
1675 res = mp->mp_subscript(s, slice);
1676 Py_DECREF(slice);
1677 return res;
1678 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001681}
1682
1683int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001684PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (s == NULL) {
1689 null_error();
1690 return -1;
1691 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 m = s->ob_type->tp_as_sequence;
1694 if (m && m->sq_ass_item) {
1695 if (i < 0) {
1696 if (m->sq_length) {
1697 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001698 if (l < 0) {
1699 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 i += l;
1703 }
1704 }
1705 return m->sq_ass_item(s, i, o);
1706 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 type_error("'%.200s' object does not support item assignment", s);
1709 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001710}
1711
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001712int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001713PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (s == NULL) {
1718 null_error();
1719 return -1;
1720 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 m = s->ob_type->tp_as_sequence;
1723 if (m && m->sq_ass_item) {
1724 if (i < 0) {
1725 if (m->sq_length) {
1726 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001727 if (l < 0) {
1728 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001730 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 i += l;
1732 }
1733 }
1734 return m->sq_ass_item(s, i, (PyObject *)NULL);
1735 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 type_error("'%.200s' object doesn't support item deletion", s);
1738 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001739}
1740
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001741int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001742PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (s == NULL) {
1747 null_error();
1748 return -1;
1749 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001752 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 int res;
1754 PyObject *slice = _PySlice_FromIndices(i1, i2);
1755 if (!slice)
1756 return -1;
1757 res = mp->mp_ass_subscript(s, slice, o);
1758 Py_DECREF(slice);
1759 return res;
1760 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 type_error("'%.200s' object doesn't support slice assignment", s);
1763 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001764}
1765
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001766int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001767PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (s == NULL) {
1772 null_error();
1773 return -1;
1774 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001777 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 int res;
1779 PyObject *slice = _PySlice_FromIndices(i1, i2);
1780 if (!slice)
1781 return -1;
1782 res = mp->mp_ass_subscript(s, slice, NULL);
1783 Py_DECREF(slice);
1784 return res;
1785 }
1786 type_error("'%.200s' object doesn't support slice deletion", s);
1787 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001788}
1789
Guido van Rossume15dee51995-07-18 14:12:02 +00001790PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001791PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 PyObject *it; /* iter(v) */
1794 Py_ssize_t n; /* guess for result tuple size */
1795 PyObject *result = NULL;
1796 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001797
Victor Stinner71aea8e2016-08-19 16:59:55 +02001798 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001800 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 /* Special-case the common tuple and list cases, for efficiency. */
1803 if (PyTuple_CheckExact(v)) {
1804 /* Note that we can't know whether it's safe to return
1805 a tuple *subclass* instance as-is, hence the restriction
1806 to exact tuples here. In contrast, lists always make
1807 a copy, so there's no need for exactness below. */
1808 Py_INCREF(v);
1809 return v;
1810 }
Raymond Hettinger610a51f2015-05-17 14:45:58 -07001811 if (PyList_CheckExact(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* Get iterator. */
1815 it = PyObject_GetIter(v);
1816 if (it == NULL)
1817 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001820 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 if (n == -1)
1822 goto Fail;
1823 result = PyTuple_New(n);
1824 if (result == NULL)
1825 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 /* Fill the tuple. */
1828 for (j = 0; ; ++j) {
1829 PyObject *item = PyIter_Next(it);
1830 if (item == NULL) {
1831 if (PyErr_Occurred())
1832 goto Fail;
1833 break;
1834 }
1835 if (j >= n) {
Martin Pantere8db8612016-07-25 02:30:05 +00001836 size_t newn = (size_t)n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 /* The over-allocation strategy can grow a bit faster
1838 than for lists because unlike lists the
1839 over-allocation isn't permanent -- we reclaim
1840 the excess before the end of this routine.
1841 So, grow by ten and then add 25%.
1842 */
Martin Pantere8db8612016-07-25 02:30:05 +00001843 newn += 10u;
1844 newn += newn >> 2;
1845 if (newn > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 /* Check for overflow */
1847 PyErr_NoMemory();
1848 Py_DECREF(item);
1849 goto Fail;
1850 }
Martin Pantere8db8612016-07-25 02:30:05 +00001851 n = (Py_ssize_t)newn;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 if (_PyTuple_Resize(&result, n) != 0) {
1853 Py_DECREF(item);
1854 goto Fail;
1855 }
1856 }
1857 PyTuple_SET_ITEM(result, j, item);
1858 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 /* Cut tuple back if guess was too large. */
1861 if (j < n &&
1862 _PyTuple_Resize(&result, j) != 0)
1863 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 Py_DECREF(it);
1866 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001867
1868Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 Py_XDECREF(result);
1870 Py_DECREF(it);
1871 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001872}
1873
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001874PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001875PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *result; /* result list */
1878 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001879
Victor Stinner71aea8e2016-08-19 16:59:55 +02001880 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001882 }
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 result = PyList_New(0);
1885 if (result == NULL)
1886 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 rv = _PyList_Extend((PyListObject *)result, v);
1889 if (rv == NULL) {
1890 Py_DECREF(result);
1891 return NULL;
1892 }
1893 Py_DECREF(rv);
1894 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001895}
1896
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001897PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001898PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001901
Victor Stinner71aea8e2016-08-19 16:59:55 +02001902 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001904 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1907 Py_INCREF(v);
1908 return v;
1909 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 it = PyObject_GetIter(v);
1912 if (it == NULL) {
1913 if (PyErr_ExceptionMatches(PyExc_TypeError))
1914 PyErr_SetString(PyExc_TypeError, m);
1915 return NULL;
1916 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 v = PySequence_List(it);
1919 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001922}
1923
Tim Peters16a77ad2001-09-08 04:00:12 +00001924/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1926 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1927 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001928 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1929*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001930Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001931_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 Py_ssize_t n;
1934 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1935 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (seq == NULL || obj == NULL) {
1938 null_error();
1939 return -1;
1940 }
Tim Peters75f8e352001-05-05 11:33:43 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 it = PyObject_GetIter(seq);
1943 if (it == NULL) {
1944 type_error("argument of type '%.200s' is not iterable", seq);
1945 return -1;
1946 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 n = wrapped = 0;
1949 for (;;) {
1950 int cmp;
1951 PyObject *item = PyIter_Next(it);
1952 if (item == NULL) {
1953 if (PyErr_Occurred())
1954 goto Fail;
1955 break;
1956 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1959 Py_DECREF(item);
1960 if (cmp < 0)
1961 goto Fail;
1962 if (cmp > 0) {
1963 switch (operation) {
1964 case PY_ITERSEARCH_COUNT:
1965 if (n == PY_SSIZE_T_MAX) {
1966 PyErr_SetString(PyExc_OverflowError,
1967 "count exceeds C integer size");
1968 goto Fail;
1969 }
1970 ++n;
1971 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 case PY_ITERSEARCH_INDEX:
1974 if (wrapped) {
1975 PyErr_SetString(PyExc_OverflowError,
1976 "index exceeds C integer size");
1977 goto Fail;
1978 }
1979 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 case PY_ITERSEARCH_CONTAINS:
1982 n = 1;
1983 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 default:
1986 assert(!"unknown operation");
1987 }
1988 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (operation == PY_ITERSEARCH_INDEX) {
1991 if (n == PY_SSIZE_T_MAX)
1992 wrapped = 1;
1993 ++n;
1994 }
1995 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (operation != PY_ITERSEARCH_INDEX)
1998 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyErr_SetString(PyExc_ValueError,
2001 "sequence.index(x): x not in sequence");
2002 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00002003Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 n = -1;
2005 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00002006Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 Py_DECREF(it);
2008 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00002009
Guido van Rossume15dee51995-07-18 14:12:02 +00002010}
2011
Tim Peters16a77ad2001-09-08 04:00:12 +00002012/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002013Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002014PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00002017}
2018
Tim Peterscb8d3682001-05-05 21:05:01 +00002019/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00002020 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00002021 */
2022int
2023PySequence_Contains(PyObject *seq, PyObject *ob)
2024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 Py_ssize_t result;
2026 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
2027 if (sqm != NULL && sqm->sq_contains != NULL)
2028 return (*sqm->sq_contains)(seq, ob);
2029 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
2030 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00002031}
2032
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002033/* Backwards compatibility */
2034#undef PySequence_In
2035int
Fred Drake79912472000-07-09 04:06:11 +00002036PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002039}
2040
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002041Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00002042PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00002045}
2046
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002047/* Operations on mappings */
2048
2049int
Fred Drake79912472000-07-09 04:06:11 +00002050PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002051{
Benjamin Peterson21992272011-12-28 12:01:31 -06002052 return o && o->ob_type->tp_as_mapping &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002054}
2055
Martin v. Löwis18e16552006-02-15 17:27:45 +00002056Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002057PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 if (o == NULL) {
2062 null_error();
2063 return -1;
2064 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 m = o->ob_type->tp_as_mapping;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03002067 if (m && m->mp_length) {
2068 Py_ssize_t len = m->mp_length(o);
2069 assert(len >= 0 || PyErr_Occurred());
2070 return len;
2071 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 type_error("object of type '%.200s' has no len()", o);
2074 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002075}
2076
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002077#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002078Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002079PyMapping_Length(PyObject *o)
2080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002082}
2083#define PyMapping_Length PyMapping_Size
2084
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002085PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002086PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002089
Victor Stinner71aea8e2016-08-19 16:59:55 +02002090 if (key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002092 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 okey = PyUnicode_FromString(key);
2095 if (okey == NULL)
2096 return NULL;
2097 r = PyObject_GetItem(o, okey);
2098 Py_DECREF(okey);
2099 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002100}
2101
2102int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002103PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 PyObject *okey;
2106 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (key == NULL) {
2109 null_error();
2110 return -1;
2111 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 okey = PyUnicode_FromString(key);
2114 if (okey == NULL)
2115 return -1;
2116 r = PyObject_SetItem(o, okey, value);
2117 Py_DECREF(okey);
2118 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002119}
2120
2121int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002122PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 v = PyMapping_GetItemString(o, key);
2127 if (v) {
2128 Py_DECREF(v);
2129 return 1;
2130 }
2131 PyErr_Clear();
2132 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002133}
2134
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002135int
Fred Drake79912472000-07-09 04:06:11 +00002136PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 v = PyObject_GetItem(o, key);
2141 if (v) {
2142 Py_DECREF(v);
2143 return 1;
2144 }
2145 PyErr_Clear();
2146 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002147}
2148
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002149PyObject *
2150PyMapping_Keys(PyObject *o)
2151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 PyObject *keys;
2153 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002154 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 if (PyDict_CheckExact(o))
2157 return PyDict_Keys(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002158 keys = _PyObject_CallMethodId(o, &PyId_keys, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (keys == NULL)
2160 return NULL;
2161 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2162 Py_DECREF(keys);
2163 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002164}
2165
2166PyObject *
2167PyMapping_Items(PyObject *o)
2168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyObject *items;
2170 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002171 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (PyDict_CheckExact(o))
2174 return PyDict_Items(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002175 items = _PyObject_CallMethodId(o, &PyId_items, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 if (items == NULL)
2177 return NULL;
2178 fast = PySequence_Fast(items, "o.items() are not iterable");
2179 Py_DECREF(items);
2180 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002181}
2182
2183PyObject *
2184PyMapping_Values(PyObject *o)
2185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 PyObject *values;
2187 PyObject *fast;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002188 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 if (PyDict_CheckExact(o))
2191 return PyDict_Values(o);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002192 values = _PyObject_CallMethodId(o, &PyId_values, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 if (values == NULL)
2194 return NULL;
2195 fast = PySequence_Fast(values, "o.values() are not iterable");
2196 Py_DECREF(values);
2197 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002198}
2199
Guido van Rossum823649d2001-03-21 18:40:58 +00002200/* isinstance(), issubclass() */
2201
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002202/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002203 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002204 * 1. getattr(cls, '__bases__') could raise an AttributeError
2205 * 2. getattr(cls, '__bases__') could raise some other exception
2206 * 3. getattr(cls, '__bases__') could return a tuple
2207 * 4. getattr(cls, '__bases__') could return something other than a tuple
2208 *
2209 * Only state #3 is a non-error state and only it returns a non-NULL object
2210 * (it returns the retrieved tuple).
2211 *
2212 * Any raised AttributeErrors are masked by clearing the exception and
2213 * returning NULL. If an object other than a tuple comes out of __bases__,
2214 * then again, the return value is NULL. So yes, these two situations
2215 * produce exactly the same results: NULL is returned and no error is set.
2216 *
2217 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002219 * exception to be propagated along.
2220 *
2221 * Callers are expected to test for PyErr_Occurred() when the return value
2222 * is NULL to decide whether a valid exception should be propagated or not.
2223 * When there's no exception to propagate, it's customary for the caller to
2224 * set a TypeError.
2225 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002226static PyObject *
2227abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002228{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002229 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 Py_ALLOW_RECURSION
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002233 bases = _PyObject_GetAttrId(cls, &PyId___bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 Py_END_ALLOW_RECURSION
2235 if (bases == NULL) {
2236 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2237 PyErr_Clear();
2238 return NULL;
2239 }
2240 if (!PyTuple_Check(bases)) {
2241 Py_DECREF(bases);
2242 return NULL;
2243 }
2244 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002245}
2246
2247
2248static int
2249abstract_issubclass(PyObject *derived, PyObject *cls)
2250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 PyObject *bases = NULL;
2252 Py_ssize_t i, n;
2253 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 while (1) {
2256 if (derived == cls)
2257 return 1;
2258 bases = abstract_get_bases(derived);
2259 if (bases == NULL) {
2260 if (PyErr_Occurred())
2261 return -1;
2262 return 0;
2263 }
2264 n = PyTuple_GET_SIZE(bases);
2265 if (n == 0) {
2266 Py_DECREF(bases);
2267 return 0;
2268 }
2269 /* Avoid recursivity in the single inheritance case */
2270 if (n == 1) {
2271 derived = PyTuple_GET_ITEM(bases, 0);
2272 Py_DECREF(bases);
2273 continue;
2274 }
2275 for (i = 0; i < n; i++) {
2276 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2277 if (r != 0)
2278 break;
2279 }
2280 Py_DECREF(bases);
2281 return r;
2282 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002283}
2284
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002285static int
2286check_class(PyObject *cls, const char *error)
2287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyObject *bases = abstract_get_bases(cls);
2289 if (bases == NULL) {
2290 /* Do not mask errors. */
2291 if (!PyErr_Occurred())
2292 PyErr_SetString(PyExc_TypeError, error);
2293 return 0;
2294 }
2295 Py_DECREF(bases);
2296 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002297}
2298
Brett Cannon4f653312004-03-20 22:52:14 +00002299static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002300recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyObject *icls;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 int retval = 0;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002304 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 if (PyType_Check(cls)) {
2307 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2308 if (retval == 0) {
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002309 PyObject *c = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (c == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002311 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002312 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002313 else
R. David Murray6bb99892010-11-20 16:33:30 +00002314 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
2316 else {
2317 if (c != (PyObject *)(inst->ob_type) &&
2318 PyType_Check(c))
2319 retval = PyType_IsSubtype(
2320 (PyTypeObject *)c,
2321 (PyTypeObject *)cls);
2322 Py_DECREF(c);
2323 }
2324 }
2325 }
2326 else {
2327 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002328 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 return -1;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002330 icls = _PyObject_GetAttrId(inst, &PyId___class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (icls == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002332 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002333 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002334 else
R. David Murray6bb99892010-11-20 16:33:30 +00002335 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 }
2337 else {
2338 retval = abstract_issubclass(icls, cls);
2339 Py_DECREF(icls);
2340 }
2341 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002344}
2345
2346int
Brett Cannon4f653312004-03-20 22:52:14 +00002347PyObject_IsInstance(PyObject *inst, PyObject *cls)
2348{
Benjamin Petersonce798522012-01-22 11:24:29 -05002349 _Py_IDENTIFIER(__instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 /* Quick test for an exact match */
2353 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2354 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002355
Georg Brandl72b8a802014-10-03 09:26:37 +02002356 /* We know what type's __instancecheck__ does. */
2357 if (PyType_CheckExact(cls)) {
2358 return recursive_isinstance(inst, cls);
2359 }
2360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (PyTuple_Check(cls)) {
2362 Py_ssize_t i;
2363 Py_ssize_t n;
2364 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2367 return -1;
2368 n = PyTuple_GET_SIZE(cls);
2369 for (i = 0; i < n; ++i) {
2370 PyObject *item = PyTuple_GET_ITEM(cls, i);
2371 r = PyObject_IsInstance(inst, item);
2372 if (r != 0)
2373 /* either found it, or got an error */
2374 break;
2375 }
2376 Py_LeaveRecursiveCall();
2377 return r;
2378 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002379
Benjamin Petersonce798522012-01-22 11:24:29 -05002380 checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 if (checker != NULL) {
2382 PyObject *res;
2383 int ok = -1;
2384 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2385 Py_DECREF(checker);
2386 return ok;
2387 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002388 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 Py_LeaveRecursiveCall();
2390 Py_DECREF(checker);
2391 if (res != NULL) {
2392 ok = PyObject_IsTrue(res);
2393 Py_DECREF(res);
2394 }
2395 return ok;
2396 }
2397 else if (PyErr_Occurred())
2398 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02002399 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002401}
2402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002404recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 if (PyType_Check(cls) && PyType_Check(derived)) {
2407 /* Fast path (non-recursive) */
2408 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2409 }
2410 if (!check_class(derived,
2411 "issubclass() arg 1 must be a class"))
2412 return -1;
2413 if (!check_class(cls,
2414 "issubclass() arg 2 must be a class"
2415 " or tuple of classes"))
2416 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002419}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002420
Brett Cannon4f653312004-03-20 22:52:14 +00002421int
2422PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2423{
Benjamin Petersonce798522012-01-22 11:24:29 -05002424 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002426
Georg Brandl72b8a802014-10-03 09:26:37 +02002427 /* We know what type's __subclasscheck__ does. */
2428 if (PyType_CheckExact(cls)) {
2429 /* Quick test for an exact match */
2430 if (derived == cls)
2431 return 1;
2432 return recursive_issubclass(derived, cls);
2433 }
2434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 if (PyTuple_Check(cls)) {
2436 Py_ssize_t i;
2437 Py_ssize_t n;
2438 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2441 return -1;
2442 n = PyTuple_GET_SIZE(cls);
2443 for (i = 0; i < n; ++i) {
2444 PyObject *item = PyTuple_GET_ITEM(cls, i);
2445 r = PyObject_IsSubclass(derived, item);
2446 if (r != 0)
2447 /* either found it, or got an error */
2448 break;
2449 }
2450 Py_LeaveRecursiveCall();
2451 return r;
2452 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002453
Benjamin Petersonce798522012-01-22 11:24:29 -05002454 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 if (checker != NULL) {
2456 PyObject *res;
2457 int ok = -1;
2458 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2459 Py_DECREF(checker);
2460 return ok;
2461 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002462 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 Py_LeaveRecursiveCall();
2464 Py_DECREF(checker);
2465 if (res != NULL) {
2466 ok = PyObject_IsTrue(res);
2467 Py_DECREF(res);
2468 }
2469 return ok;
2470 }
2471 else if (PyErr_Occurred())
2472 return -1;
Georg Brandl72b8a802014-10-03 09:26:37 +02002473 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002475}
2476
2477int
2478_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002481}
2482
2483int
2484_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002487}
2488
2489
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002490PyObject *
2491PyObject_GetIter(PyObject *o)
2492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 PyTypeObject *t = o->ob_type;
Victor Stinner14e6d092016-12-09 17:08:59 +01002494 getiterfunc f;
2495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 f = t->tp_iter;
2497 if (f == NULL) {
2498 if (PySequence_Check(o))
2499 return PySeqIter_New(o);
2500 return type_error("'%.200s' object is not iterable", o);
2501 }
2502 else {
2503 PyObject *res = (*f)(o);
2504 if (res != NULL && !PyIter_Check(res)) {
2505 PyErr_Format(PyExc_TypeError,
2506 "iter() returned non-iterator "
2507 "of type '%.100s'",
2508 res->ob_type->tp_name);
2509 Py_DECREF(res);
2510 res = NULL;
2511 }
2512 return res;
2513 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002514}
2515
Tim Petersf4848da2001-05-05 00:14:56 +00002516/* Return next item.
2517 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2518 * If the iteration terminates normally, return NULL and clear the
2519 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2520 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002522 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002523PyObject *
2524PyIter_Next(PyObject *iter)
2525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 PyObject *result;
2527 result = (*iter->ob_type->tp_iternext)(iter);
2528 if (result == NULL &&
2529 PyErr_Occurred() &&
2530 PyErr_ExceptionMatches(PyExc_StopIteration))
2531 PyErr_Clear();
2532 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002533}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002534
2535
2536/*
2537 * Flatten a sequence of bytes() objects into a C array of
2538 * NULL terminated string pointers with a NULL char* terminating the array.
2539 * (ie: an argv or env list)
2540 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002541 * Memory allocated for the returned list is allocated using PyMem_Malloc()
2542 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002543 */
2544char *const *
2545_PySequence_BytesToCharpArray(PyObject* self)
2546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 char **array;
2548 Py_ssize_t i, argc;
2549 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002550 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 argc = PySequence_Size(self);
2553 if (argc == -1)
2554 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002555
Stefan Krah7cacd2e2012-08-21 08:16:09 +02002556 assert(argc >= 0);
2557
2558 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2559 PyErr_NoMemory();
2560 return NULL;
2561 }
2562
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002563 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 if (array == NULL) {
2565 PyErr_NoMemory();
2566 return NULL;
2567 }
2568 for (i = 0; i < argc; ++i) {
2569 char *data;
2570 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02002571 if (item == NULL) {
2572 /* NULL terminate before freeing. */
2573 array[i] = NULL;
2574 goto fail;
2575 }
Serhiy Storchakad174d242017-06-23 19:39:27 +03002576 /* check for embedded null bytes */
2577 if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 /* NULL terminate before freeing. */
2579 array[i] = NULL;
2580 goto fail;
2581 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002582 size = PyBytes_GET_SIZE(item) + 1;
2583 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 if (!array[i]) {
2585 PyErr_NoMemory();
2586 goto fail;
2587 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002588 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 Py_DECREF(item);
2590 }
2591 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002594
2595fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 Py_XDECREF(item);
2597 _Py_FreeCharPArray(array);
2598 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002599}
2600
2601
2602/* Free's a NULL terminated char** array of C strings. */
2603void
2604_Py_FreeCharPArray(char *const array[])
2605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 Py_ssize_t i;
2607 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002608 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002610 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002611}