blob: 02e4ad70718cd94d0838ccd80439e07ead71ca30 [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"
Victor Stinner224481a2020-03-13 10:19:38 +01004#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pystate.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00007#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00008#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00009#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +000010
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000012
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000013/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000014
15static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000017{
Victor Stinner0d76d2b2020-02-07 01:53:23 +010018 PyErr_Format(PyExc_TypeError, msg, Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000020}
21
Guido van Rossum052b7e11996-11-11 15:08:19 +000022static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000023null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 if (!PyErr_Occurred())
26 PyErr_SetString(PyExc_SystemError,
27 "null argument to internal routine");
28 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000029}
30
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000031/* Operations on any object */
32
Guido van Rossume15dee51995-07-18 14:12:02 +000033PyObject *
Fred Drake79912472000-07-09 04:06:11 +000034PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000037
Victor Stinner71aea8e2016-08-19 16:59:55 +020038 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +020040 }
41
Victor Stinner0d76d2b2020-02-07 01:53:23 +010042 v = (PyObject *)Py_TYPE(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 Py_INCREF(v);
44 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000045}
46
Martin v. Löwis18e16552006-02-15 17:27:45 +000047Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000048PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (o == NULL) {
53 null_error();
54 return -1;
55 }
Guido van Rossume15dee51995-07-18 14:12:02 +000056
Victor Stinner0d76d2b2020-02-07 01:53:23 +010057 m = Py_TYPE(o)->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +030058 if (m && m->sq_length) {
59 Py_ssize_t len = m->sq_length(o);
60 assert(len >= 0 || PyErr_Occurred());
61 return len;
62 }
Guido van Rossume15dee51995-07-18 14:12:02 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000065}
66
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000067#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000068Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000069PyObject_Length(PyObject *o)
70{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000072}
73#define PyObject_Length PyObject_Size
74
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020075int
76_PyObject_HasLen(PyObject *o) {
77 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
78 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
79}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000080
Christian Heimes255f53b2007-12-08 15:33:56 +000081/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020082 or o.__length_hint__(). If those methods aren't found the defaultvalue is
83 returned. If one of the calls fails with an exception other than TypeError
84 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000085*/
86
87Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020088PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000089{
Christian Heimesb70e8a12012-10-06 17:16:39 +020090 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020091 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050092 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030093 if (_PyObject_HasLen(o)) {
94 res = PyObject_Length(o);
Serhiy Storchaka813f9432017-04-16 09:21:44 +030095 if (res < 0) {
96 assert(PyErr_Occurred());
Serhiy Storchakaf740d462013-10-24 23:19:51 +030097 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
98 return -1;
99 }
100 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200101 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +0300102 else {
103 return res;
104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Christian Heimes6314d162012-10-06 17:13:29 +0200106 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200107 if (hint == NULL) {
108 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return defaultvalue;
112 }
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100113 result = _PyObject_CallNoArg(hint);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200114 Py_DECREF(hint);
115 if (result == NULL) {
116 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
117 PyErr_Clear();
118 return defaultvalue;
119 }
120 return -1;
121 }
122 else if (result == Py_NotImplemented) {
123 Py_DECREF(result);
124 return defaultvalue;
125 }
126 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200127 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200128 Py_TYPE(result)->tp_name);
129 Py_DECREF(result);
130 return -1;
131 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200132 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200133 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200134 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200135 return -1;
136 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200137 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200138 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
139 return -1;
140 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200141 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000142}
143
Guido van Rossume15dee51995-07-18 14:12:02 +0000144PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000145PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 PyMappingMethods *m;
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000148 PySequenceMethods *ms;
Guido van Rossume15dee51995-07-18 14:12:02 +0000149
Victor Stinner71aea8e2016-08-19 16:59:55 +0200150 if (o == NULL || key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +0200152 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000153
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100154 m = Py_TYPE(o)->tp_as_mapping;
Victor Stinnere20310f2015-11-05 13:56:58 +0100155 if (m && m->mp_subscript) {
156 PyObject *item = m->mp_subscript(o, key);
157 assert((item != NULL) ^ (PyErr_Occurred() != NULL));
158 return item;
159 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000160
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100161 ms = Py_TYPE(o)->tp_as_sequence;
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000162 if (ms && ms->sq_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (PyIndex_Check(key)) {
164 Py_ssize_t key_value;
165 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
166 if (key_value == -1 && PyErr_Occurred())
167 return NULL;
168 return PySequence_GetItem(o, key_value);
169 }
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000170 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return type_error("sequence index must "
172 "be integer, not '%.200s'", key);
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000175
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100176 if (PyType_Check(o)) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200177 PyObject *meth, *result;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100178 _Py_IDENTIFIER(__class_getitem__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200179 if (_PyObject_LookupAttrId(o, &PyId___class_getitem__, &meth) < 0) {
180 return NULL;
181 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100182 if (meth) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100183 result = PyObject_CallOneArg(meth, key);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100184 Py_DECREF(meth);
185 return result;
186 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100187 }
188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000190}
191
192int
Fred Drake79912472000-07-09 04:06:11 +0000193PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (o == NULL || key == NULL || value == NULL) {
198 null_error();
199 return -1;
200 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100201 m = Py_TYPE(o)->tp_as_mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (m && m->mp_ass_subscript)
203 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000204
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100205 if (Py_TYPE(o)->tp_as_sequence) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (PyIndex_Check(key)) {
207 Py_ssize_t key_value;
208 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
209 if (key_value == -1 && PyErr_Occurred())
210 return -1;
211 return PySequence_SetItem(o, key_value, value);
212 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100213 else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 type_error("sequence index must be "
215 "integer, not '%.200s'", key);
216 return -1;
217 }
218 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 type_error("'%.200s' object does not support item assignment", o);
221 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000222}
223
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000224int
Fred Drake79912472000-07-09 04:06:11 +0000225PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (o == NULL || key == NULL) {
230 null_error();
231 return -1;
232 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100233 m = Py_TYPE(o)->tp_as_mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 if (m && m->mp_ass_subscript)
235 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000236
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100237 if (Py_TYPE(o)->tp_as_sequence) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (PyIndex_Check(key)) {
239 Py_ssize_t key_value;
240 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
241 if (key_value == -1 && PyErr_Occurred())
242 return -1;
243 return PySequence_DelItem(o, key_value);
244 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100245 else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 type_error("sequence index must be "
247 "integer, not '%.200s'", key);
248 return -1;
249 }
250 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 type_error("'%.200s' object does not support item deletion", o);
253 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000254}
255
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000256int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300257PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyObject *okey;
260 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (o == NULL || key == NULL) {
263 null_error();
264 return -1;
265 }
266 okey = PyUnicode_FromString(key);
267 if (okey == NULL)
268 return -1;
269 ret = PyObject_DelItem(o, okey);
270 Py_DECREF(okey);
271 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000272}
273
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000274/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000275 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000276 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000277int
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000278PyObject_CheckReadBuffer(PyObject *obj)
279{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100280 PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (pb == NULL ||
284 pb->bf_getbuffer == NULL)
285 return 0;
286 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
287 PyErr_Clear();
288 return 0;
289 }
290 PyBuffer_Release(&view);
291 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000292}
293
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200294static int
295as_read_buffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
300 null_error();
301 return -1;
302 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200303 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 *buffer = view.buf;
307 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200308 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000310}
311
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200312int
313PyObject_AsCharBuffer(PyObject *obj,
314 const char **buffer,
315 Py_ssize_t *buffer_len)
316{
317 return as_read_buffer(obj, (const void **)buffer, buffer_len);
318}
319
320int PyObject_AsReadBuffer(PyObject *obj,
321 const void **buffer,
322 Py_ssize_t *buffer_len)
323{
324 return as_read_buffer(obj, buffer, buffer_len);
325}
326
Guido van Rossum4c08d552000-03-10 22:55:18 +0000327int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 void **buffer,
329 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyBufferProcs *pb;
332 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
335 null_error();
336 return -1;
337 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100338 pb = Py_TYPE(obj)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (pb == NULL ||
340 pb->bf_getbuffer == NULL ||
341 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
342 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400343 "expected a writable bytes-like object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return -1;
345 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 *buffer = view.buf;
348 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200349 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000351}
352
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000353/* Buffer C-API for Python 3.0 */
354
355int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000356PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000357{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100358 PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200359
360 if (pb == NULL || pb->bf_getbuffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400362 "a bytes-like object is required, not '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_TYPE(obj)->tp_name);
364 return -1;
365 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200366 return (*pb->bf_getbuffer)(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000367}
368
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000369static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100370_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_ssize_t sd, dim;
373 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000374
Stefan Krah363af442015-02-01 14:53:54 +0100375 /* 1) len = product(shape) * itemsize
376 2) itemsize > 0
377 3) len = 0 <==> exists i: shape[i] = 0 */
378 if (view->len == 0) return 1;
379 if (view->strides == NULL) { /* C-contiguous by definition */
380 /* Trivially F-contiguous */
381 if (view->ndim <= 1) return 1;
382
383 /* ndim > 1 implies shape != NULL */
384 assert(view->shape != NULL);
385
386 /* Effectively 1-d */
387 sd = 0;
388 for (i=0; i<view->ndim; i++) {
389 if (view->shape[i] > 1) sd += 1;
390 }
391 return sd <= 1;
392 }
393
394 /* strides != NULL implies both of these */
395 assert(view->ndim > 0);
396 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 for (i=0; i<view->ndim; i++) {
400 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100401 if (dim > 1 && view->strides[i] != sd) {
402 return 0;
403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 sd *= dim;
405 }
406 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000407}
408
409static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100410_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 Py_ssize_t sd, dim;
413 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000414
Stefan Krah363af442015-02-01 14:53:54 +0100415 /* 1) len = product(shape) * itemsize
416 2) itemsize > 0
417 3) len = 0 <==> exists i: shape[i] = 0 */
418 if (view->len == 0) return 1;
419 if (view->strides == NULL) return 1; /* C-contiguous by definition */
420
421 /* strides != NULL implies both of these */
422 assert(view->ndim > 0);
423 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 for (i=view->ndim-1; i>=0; i--) {
427 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100428 if (dim > 1 && view->strides[i] != sd) {
429 return 0;
430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 sd *= dim;
432 }
433 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000434}
435
436int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100437PyBuffer_IsContiguous(const Py_buffer *view, char order)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000438{
439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000441
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100442 if (order == 'C')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return _IsCContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100444 else if (order == 'F')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return _IsFortranContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100446 else if (order == 'A')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 return (_IsCContiguous(view) || _IsFortranContiguous(view));
448 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000449}
450
451
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000453PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 char* pointer;
456 int i;
457 pointer = (char *)view->buf;
458 for (i = 0; i < view->ndim; i++) {
459 pointer += view->strides[i]*indices[i];
460 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
461 pointer = *((char**)pointer) + view->suboffsets[i];
462 }
463 }
464 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000465}
466
467
Guido van Rossum98297ee2007-11-06 21:34:58 +0000468void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000469_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 +0000470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 for (k=0; k<nd; k++) {
474 if (index[k] < shape[k]-1) {
475 index[k]++;
476 break;
477 }
478 else {
479 index[k] = 0;
480 }
481 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000482}
483
Guido van Rossum98297ee2007-11-06 21:34:58 +0000484void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000485_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 +0000486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 for (k=nd-1; k>=0; k--) {
490 if (index[k] < shape[k]-1) {
491 index[k]++;
492 break;
493 }
494 else {
495 index[k] = 0;
496 }
497 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000498}
499
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300500Py_ssize_t
501PyBuffer_SizeFromFormat(const char *format)
502{
503 PyObject *structmodule = NULL;
504 PyObject *calcsize = NULL;
505 PyObject *res = NULL;
506 PyObject *fmt = NULL;
507 Py_ssize_t itemsize = -1;
508
509 structmodule = PyImport_ImportModule("struct");
510 if (structmodule == NULL) {
511 return itemsize;
512 }
513
514 calcsize = PyObject_GetAttrString(structmodule, "calcsize");
515 if (calcsize == NULL) {
516 goto done;
517 }
518
519 fmt = PyUnicode_FromString(format);
520 if (fmt == NULL) {
521 goto done;
522 }
523
524 res = PyObject_CallFunctionObjArgs(calcsize, fmt, NULL);
525 if (res == NULL) {
526 goto done;
527 }
528
529 itemsize = PyLong_AsSsize_t(res);
530 if (itemsize < 0) {
531 goto done;
532 }
533
534done:
535 Py_DECREF(structmodule);
536 Py_XDECREF(calcsize);
537 Py_XDECREF(fmt);
538 Py_XDECREF(res);
539 return itemsize;
540}
541
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000542int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000543PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000546 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 Py_ssize_t *indices, elements;
548 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 if (len > view->len) {
551 len = view->len;
552 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (PyBuffer_IsContiguous(view, fort)) {
555 /* simplest copy is all that is needed */
556 memcpy(view->buf, buf, len);
557 return 0;
558 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000561
Stefan Krah7213fcc2015-02-01 16:19:23 +0100562 /* view->ndim <= 64 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
564 if (indices == NULL) {
565 PyErr_NoMemory();
566 return -1;
567 }
568 for (k=0; k<view->ndim;k++) {
569 indices[k] = 0;
570 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000573 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
575 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000576 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 }
578 src = buf;
579 /* XXX : This is not going to be the fastest code in the world
580 several optimizations are possible.
581 */
582 elements = len / view->itemsize;
583 while (elements--) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 ptr = PyBuffer_GetPointer(view, indices);
585 memcpy(ptr, src, view->itemsize);
586 src += view->itemsize;
Stefan Krah7213fcc2015-02-01 16:19:23 +0100587 addone(view->ndim, indices, view->shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PyMem_Free(indices);
591 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000592}
593
Guido van Rossum98297ee2007-11-06 21:34:58 +0000594int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_buffer view_dest, view_src;
597 int k;
598 Py_ssize_t *indices, elements;
599 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (!PyObject_CheckBuffer(dest) ||
602 !PyObject_CheckBuffer(src)) {
603 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400604 "both destination and source must be "\
605 "bytes-like objects");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return -1;
607 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
610 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
611 PyBuffer_Release(&view_dest);
612 return -1;
613 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (view_dest.len < view_src.len) {
616 PyErr_SetString(PyExc_BufferError,
617 "destination is too small to receive data from source");
618 PyBuffer_Release(&view_dest);
619 PyBuffer_Release(&view_src);
620 return -1;
621 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
624 PyBuffer_IsContiguous(&view_src, 'C')) ||
625 (PyBuffer_IsContiguous(&view_dest, 'F') &&
626 PyBuffer_IsContiguous(&view_src, 'F'))) {
627 /* simplest copy is all that is needed */
628 memcpy(view_dest.buf, view_src.buf, view_src.len);
629 PyBuffer_Release(&view_dest);
630 PyBuffer_Release(&view_src);
631 return 0;
632 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* XXX(nnorwitz): need to check for overflow! */
637 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
638 if (indices == NULL) {
639 PyErr_NoMemory();
640 PyBuffer_Release(&view_dest);
641 PyBuffer_Release(&view_src);
642 return -1;
643 }
644 for (k=0; k<view_src.ndim;k++) {
645 indices[k] = 0;
646 }
647 elements = 1;
648 for (k=0; k<view_src.ndim; k++) {
649 /* XXX(nnorwitz): can this overflow? */
650 elements *= view_src.shape[k];
651 }
652 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000653 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 dptr = PyBuffer_GetPointer(&view_dest, indices);
655 sptr = PyBuffer_GetPointer(&view_src, indices);
656 memcpy(dptr, sptr, view_src.itemsize);
657 }
658 PyMem_Free(indices);
659 PyBuffer_Release(&view_dest);
660 PyBuffer_Release(&view_src);
661 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000662}
663
664void
665PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_ssize_t *strides, int itemsize,
667 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 int k;
670 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 sd = itemsize;
673 if (fort == 'F') {
674 for (k=0; k<nd; k++) {
675 strides[k] = sd;
676 sd *= shape[k];
677 }
678 }
679 else {
680 for (k=nd-1; k>=0; k--) {
681 strides[k] = sd;
682 sd *= shape[k];
683 }
684 }
685 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000686}
687
688int
Martin v. Löwis423be952008-08-13 15:53:07 +0000689PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Stefan Krah4e141742012-03-06 15:27:31 +0100690 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000691{
Stefan Krah5178d912015-02-03 16:57:21 +0100692 if (view == NULL) {
693 PyErr_SetString(PyExc_BufferError,
694 "PyBuffer_FillInfo: view==NULL argument is obsolete");
695 return -1;
696 }
697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
699 (readonly == 1)) {
700 PyErr_SetString(PyExc_BufferError,
701 "Object is not writable.");
702 return -1;
703 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 view->obj = obj;
706 if (obj)
707 Py_INCREF(obj);
708 view->buf = buf;
709 view->len = len;
710 view->readonly = readonly;
711 view->itemsize = 1;
712 view->format = NULL;
713 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
714 view->format = "B";
715 view->ndim = 1;
716 view->shape = NULL;
717 if ((flags & PyBUF_ND) == PyBUF_ND)
718 view->shape = &(view->len);
719 view->strides = NULL;
720 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
721 view->strides = &(view->itemsize);
722 view->suboffsets = NULL;
723 view->internal = NULL;
724 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000725}
726
Martin v. Löwis423be952008-08-13 15:53:07 +0000727void
728PyBuffer_Release(Py_buffer *view)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 PyObject *obj = view->obj;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200731 PyBufferProcs *pb;
732 if (obj == NULL)
733 return;
734 pb = Py_TYPE(obj)->tp_as_buffer;
735 if (pb && pb->bf_releasebuffer)
736 pb->bf_releasebuffer(obj, view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 view->obj = NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200738 Py_DECREF(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +0000739}
740
Eric Smith8fd3eba2008-02-17 19:48:00 +0000741PyObject *
742PyObject_Format(PyObject *obj, PyObject *format_spec)
743{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000744 PyObject *meth;
745 PyObject *empty = NULL;
746 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500747 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000748
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300749 if (format_spec != NULL && !PyUnicode_Check(format_spec)) {
750 PyErr_Format(PyExc_SystemError,
751 "Format specifier must be a string, not %.200s",
752 Py_TYPE(format_spec)->tp_name);
753 return NULL;
754 }
755
756 /* Fast path for common types. */
757 if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
758 if (PyUnicode_CheckExact(obj)) {
759 Py_INCREF(obj);
760 return obj;
761 }
762 if (PyLong_CheckExact(obj)) {
763 return PyObject_Str(obj);
764 }
765 }
766
Eric Smith8fd3eba2008-02-17 19:48:00 +0000767 /* If no format_spec is provided, use an empty string */
768 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100769 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000770 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000771 }
772
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300773 /* Find the (unbound!) __format__ method */
Benjamin Petersonce798522012-01-22 11:24:29 -0500774 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000775 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000776 if (!PyErr_Occurred())
777 PyErr_Format(PyExc_TypeError,
778 "Type %.100s doesn't define __format__",
779 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000781 }
782
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000783 /* And call it. */
Petr Viktorinffd97532020-02-11 17:46:57 +0100784 result = PyObject_CallOneArg(meth, format_spec);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000785 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000786
787 if (result && !PyUnicode_Check(result)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800788 PyErr_Format(PyExc_TypeError,
789 "__format__ must return a str, not %.200s",
790 Py_TYPE(result)->tp_name);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000791 Py_DECREF(result);
792 result = NULL;
793 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000794 }
795
796done:
797 Py_XDECREF(empty);
798 return result;
799}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000800/* Operations on numbers */
801
802int
Fred Drake79912472000-07-09 04:06:11 +0000803PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000804{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100805 return o && Py_TYPE(o)->tp_as_number &&
806 (Py_TYPE(o)->tp_as_number->nb_index ||
807 Py_TYPE(o)->tp_as_number->nb_int ||
808 Py_TYPE(o)->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000809}
810
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000811/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000812
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000813#define NB_SLOT(x) offsetof(PyNumberMethods, x)
814#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000816#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000818
819/*
820 Calling scheme used for binary operations:
821
Neal Norwitz4886cc32006-08-21 17:06:07 +0000822 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000824
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100825 [*] only when Py_TYPE(v) != Py_TYPE(w) && Py_TYPE(w) is a subclass of
826 Py_TYPE(v)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000827 */
828
829static PyObject *
830binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyObject *x;
833 binaryfunc slotv = NULL;
834 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000835
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100836 if (Py_TYPE(v)->tp_as_number != NULL)
837 slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot);
Andy Lester55728702020-03-06 16:53:17 -0600838 if (!Py_IS_TYPE(w, Py_TYPE(v)) &&
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100839 Py_TYPE(w)->tp_as_number != NULL) {
840 slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 if (slotw == slotv)
842 slotw = NULL;
843 }
844 if (slotv) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100845 if (slotw && PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 x = slotw(v, w);
847 if (x != Py_NotImplemented)
848 return x;
849 Py_DECREF(x); /* can't do it */
850 slotw = NULL;
851 }
852 x = slotv(v, w);
853 if (x != Py_NotImplemented)
854 return x;
855 Py_DECREF(x); /* can't do it */
856 }
857 if (slotw) {
858 x = slotw(v, w);
859 if (x != Py_NotImplemented)
860 return x;
861 Py_DECREF(x); /* can't do it */
862 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500863 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000864}
Guido van Rossum77660912002-04-16 16:32:50 +0000865
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000866static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000867binop_type_error(PyObject *v, PyObject *w, const char *op_name)
868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyErr_Format(PyExc_TypeError,
870 "unsupported operand type(s) for %.100s: "
871 "'%.100s' and '%.100s'",
872 op_name,
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100873 Py_TYPE(v)->tp_name,
874 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000876}
877
878static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000879binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *result = binary_op1(v, w, op_slot);
882 if (result == Py_NotImplemented) {
883 Py_DECREF(result);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530884
885 if (op_slot == NB_SLOT(nb_rshift) &&
886 PyCFunction_Check(v) &&
887 strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
888 {
889 PyErr_Format(PyExc_TypeError,
890 "unsupported operand type(s) for %.100s: "
891 "'%.100s' and '%.100s'. Did you mean \"print(<message>, "
Sanyam Khuranaa7c449b2017-08-18 17:48:14 +0530892 "file=<output_stream>)\"?",
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530893 op_name,
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100894 Py_TYPE(v)->tp_name,
895 Py_TYPE(w)->tp_name);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530896 return NULL;
897 }
898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 return binop_type_error(v, w, op_name);
900 }
901 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000902}
903
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000904
905/*
906 Calling scheme used for ternary operations:
907
Neal Norwitz4886cc32006-08-21 17:06:07 +0000908 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000910 */
911
912static PyObject *
913ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 PyObject *w,
915 PyObject *z,
916 const int op_slot,
917 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 PyNumberMethods *mv, *mw, *mz;
920 PyObject *x = NULL;
921 ternaryfunc slotv = NULL;
922 ternaryfunc slotw = NULL;
923 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000924
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100925 mv = Py_TYPE(v)->tp_as_number;
926 mw = Py_TYPE(w)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (mv != NULL)
928 slotv = NB_TERNOP(mv, op_slot);
Andy Lester55728702020-03-06 16:53:17 -0600929 if (!Py_IS_TYPE(w, Py_TYPE(v)) && mw != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 slotw = NB_TERNOP(mw, op_slot);
931 if (slotw == slotv)
932 slotw = NULL;
933 }
934 if (slotv) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100935 if (slotw && PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 x = slotw(v, w, z);
937 if (x != Py_NotImplemented)
938 return x;
939 Py_DECREF(x); /* can't do it */
940 slotw = NULL;
941 }
942 x = slotv(v, w, z);
943 if (x != Py_NotImplemented)
944 return x;
945 Py_DECREF(x); /* can't do it */
946 }
947 if (slotw) {
948 x = slotw(v, w, z);
949 if (x != Py_NotImplemented)
950 return x;
951 Py_DECREF(x); /* can't do it */
952 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100953 mz = Py_TYPE(z)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (mz != NULL) {
955 slotz = NB_TERNOP(mz, op_slot);
956 if (slotz == slotv || slotz == slotw)
957 slotz = NULL;
958 if (slotz) {
959 x = slotz(v, w, z);
960 if (x != Py_NotImplemented)
961 return x;
962 Py_DECREF(x); /* can't do it */
963 }
964 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (z == Py_None)
967 PyErr_Format(
968 PyExc_TypeError,
969 "unsupported operand type(s) for ** or pow(): "
970 "'%.100s' and '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100971 Py_TYPE(v)->tp_name,
972 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 else
974 PyErr_Format(
975 PyExc_TypeError,
976 "unsupported operand type(s) for pow(): "
977 "'%.100s', '%.100s', '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100978 Py_TYPE(v)->tp_name,
979 Py_TYPE(w)->tp_name,
980 Py_TYPE(z)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000982}
983
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000984#define BINARY_FUNC(func, op, op_name) \
985 PyObject * \
986 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000988 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000989
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000990BINARY_FUNC(PyNumber_Or, nb_or, "|")
991BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
992BINARY_FUNC(PyNumber_And, nb_and, "&")
993BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
994BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
995BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000996BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000997
998PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000999PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +00001000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
1002 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001003 PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 Py_DECREF(result);
1005 if (m && m->sq_concat) {
1006 return (*m->sq_concat)(v, w);
1007 }
1008 result = binop_type_error(v, w, "+");
1009 }
1010 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +00001011}
1012
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001013static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001014sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Py_ssize_t count;
1017 if (PyIndex_Check(n)) {
1018 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
1019 if (count == -1 && PyErr_Occurred())
1020 return NULL;
1021 }
1022 else {
1023 return type_error("can't multiply sequence by "
1024 "non-int of type '%.200s'", n);
1025 }
1026 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001027}
1028
1029PyObject *
1030PyNumber_Multiply(PyObject *v, PyObject *w)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
1033 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001034 PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
1035 PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 Py_DECREF(result);
1037 if (mv && mv->sq_repeat) {
1038 return sequence_repeat(mv->sq_repeat, v, w);
1039 }
1040 else if (mw && mw->sq_repeat) {
1041 return sequence_repeat(mw->sq_repeat, w, v);
1042 }
1043 result = binop_type_error(v, w, "*");
1044 }
1045 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001046}
1047
Guido van Rossume15dee51995-07-18 14:12:02 +00001048PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001049PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
1050{
1051 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
1052}
1053
1054PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001055PyNumber_FloorDivide(PyObject *v, PyObject *w)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +00001058}
1059
1060PyObject *
1061PyNumber_TrueDivide(PyObject *v, PyObject *w)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +00001064}
1065
1066PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001067PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +00001070}
1071
1072PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001073PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +00001074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +00001076}
1077
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001078/* Binary in-place operators */
1079
1080/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001081 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001082
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001083 - If the left hand object has the appropriate struct members, and
1084 they are filled, call the appropriate function and return the
1085 result. No coercion is done on the arguments; the left-hand object
1086 is the one the operation is performed on, and it's up to the
1087 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001088
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001089 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001090 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001091
1092 */
1093
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001094static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001095binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001096{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001097 PyNumberMethods *mv = Py_TYPE(v)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (mv != NULL) {
1099 binaryfunc slot = NB_BINOP(mv, iop_slot);
1100 if (slot) {
1101 PyObject *x = (slot)(v, w);
1102 if (x != Py_NotImplemented) {
1103 return x;
1104 }
1105 Py_DECREF(x);
1106 }
1107 }
1108 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001109}
1110
1111static PyObject *
1112binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1116 if (result == Py_NotImplemented) {
1117 Py_DECREF(result);
1118 return binop_type_error(v, w, op_name);
1119 }
1120 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001121}
1122
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001123#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 PyObject * \
1125 func(PyObject *v, PyObject *w) { \
1126 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1127 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001128
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001129INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1130INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1131INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1132INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1133INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1134INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001135INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001136
1137PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001138PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1141 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001142}
1143
1144PyObject *
1145PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1148 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001149}
1150
1151PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001152PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1155 NB_SLOT(nb_add));
1156 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001157 PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 Py_DECREF(result);
1159 if (m != NULL) {
1160 binaryfunc f = NULL;
1161 f = m->sq_inplace_concat;
1162 if (f == NULL)
1163 f = m->sq_concat;
1164 if (f != NULL)
1165 return (*f)(v, w);
1166 }
1167 result = binop_type_error(v, w, "+=");
1168 }
1169 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001170}
1171
1172PyObject *
1173PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1176 NB_SLOT(nb_multiply));
1177 if (result == Py_NotImplemented) {
1178 ssizeargfunc f = NULL;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001179 PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
1180 PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(result);
1182 if (mv != NULL) {
1183 f = mv->sq_inplace_repeat;
1184 if (f == NULL)
1185 f = mv->sq_repeat;
1186 if (f != NULL)
1187 return sequence_repeat(f, v, w);
1188 }
1189 else if (mw != NULL) {
1190 /* Note that the right hand operand should not be
1191 * mutated in this case so sq_inplace_repeat is not
1192 * used. */
1193 if (mw->sq_repeat)
1194 return sequence_repeat(mw->sq_repeat, w, v);
1195 }
1196 result = binop_type_error(v, w, "*=");
1197 }
1198 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001199}
1200
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001201PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001202PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1203{
1204 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1205 NB_SLOT(nb_matrix_multiply), "@=");
1206}
1207
1208PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001209PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1212 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001213}
1214
1215PyObject *
1216PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1217{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001218 if (Py_TYPE(v)->tp_as_number &&
1219 Py_TYPE(v)->tp_as_number->nb_inplace_power != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1221 }
1222 else {
1223 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1224 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001225}
1226
1227
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001228/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001229
1230PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001231PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001234
Victor Stinner71aea8e2016-08-19 16:59:55 +02001235 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001237 }
1238
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001239 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (m && m->nb_negative)
1241 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001244}
1245
1246PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001247PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001250
Victor Stinner71aea8e2016-08-19 16:59:55 +02001251 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001253 }
1254
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001255 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (m && m->nb_positive)
1257 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001260}
1261
1262PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001263PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001266
Victor Stinner71aea8e2016-08-19 16:59:55 +02001267 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001269 }
1270
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001271 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (m && m->nb_invert)
1273 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001276}
1277
1278PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001279PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001282
Victor Stinner71aea8e2016-08-19 16:59:55 +02001283 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001285 }
1286
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001287 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (m && m->nb_absolute)
1289 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001292}
1293
Christian Tismerea62ce72018-06-09 20:32:25 +02001294#undef PyIndex_Check
Christian Tismer83987132018-06-11 00:48:28 +02001295
Christian Tismerea62ce72018-06-09 20:32:25 +02001296int
1297PyIndex_Check(PyObject *obj)
1298{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001299 return Py_TYPE(obj)->tp_as_number != NULL &&
1300 Py_TYPE(obj)->tp_as_number->nb_index != NULL;
Christian Tismerea62ce72018-06-09 20:32:25 +02001301}
1302
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001303/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001304 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001305 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001306*/
1307PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001308PyNumber_Index(PyObject *item)
1309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 PyObject *result = NULL;
Victor Stinner71aea8e2016-08-19 16:59:55 +02001311 if (item == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001313 }
1314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (PyLong_Check(item)) {
1316 Py_INCREF(item);
1317 return item;
1318 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001319 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyErr_Format(PyExc_TypeError,
1321 "'%.200s' object cannot be interpreted "
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001322 "as an integer", Py_TYPE(item)->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001323 return NULL;
1324 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001325 result = Py_TYPE(item)->tp_as_number->nb_index(item);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001326 if (!result || PyLong_CheckExact(result))
1327 return result;
1328 if (!PyLong_Check(result)) {
1329 PyErr_Format(PyExc_TypeError,
1330 "__index__ returned non-int (type %.200s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001331 Py_TYPE(result)->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001332 Py_DECREF(result);
1333 return NULL;
1334 }
1335 /* Issue #17576: warn if 'result' not of exact type int. */
1336 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1337 "__index__ returned non-int (type %.200s). "
1338 "The ability to return an instance of a strict subclass of int "
1339 "is deprecated, and may be removed in a future version of Python.",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001340 Py_TYPE(result)->tp_name)) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001341 Py_DECREF(result);
1342 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 }
1344 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001345}
1346
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001347/* Return an error on Overflow only if err is not NULL*/
1348
1349Py_ssize_t
1350PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_ssize_t result;
1353 PyObject *runerr;
1354 PyObject *value = PyNumber_Index(item);
1355 if (value == NULL)
1356 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* We're done if PyLong_AsSsize_t() returns without error. */
1359 result = PyLong_AsSsize_t(value);
1360 if (result != -1 || !(runerr = PyErr_Occurred()))
1361 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* Error handling code -- only manage OverflowError differently */
1364 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1365 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyErr_Clear();
1368 /* If no error-handling desired then the default clipping
1369 is sufficient.
1370 */
1371 if (!err) {
1372 assert(PyLong_Check(value));
1373 /* Whether or not it is less than or equal to
1374 zero is determined by the sign of ob_size
1375 */
1376 if (_PyLong_Sign(value) < 0)
1377 result = PY_SSIZE_T_MIN;
1378 else
1379 result = PY_SSIZE_T_MAX;
1380 }
1381 else {
1382 /* Otherwise replace the error with caller's error object. */
1383 PyErr_Format(err,
1384 "cannot fit '%.200s' into an index-sized integer",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001385 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001387
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001388 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 Py_DECREF(value);
1390 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001391}
1392
1393
Guido van Rossume15dee51995-07-18 14:12:02 +00001394PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001395PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001396{
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001397 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PyObject *trunc_func;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001400 Py_buffer view;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001401 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001402
Victor Stinner71aea8e2016-08-19 16:59:55 +02001403 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001405 }
1406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (PyLong_CheckExact(o)) {
1408 Py_INCREF(o);
1409 return o;
1410 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001411 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001413 result = _PyLong_FromNbInt(o);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001414 if (result != NULL && !PyLong_CheckExact(result)) {
1415 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1416 }
1417 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 }
Serhiy Storchakabdbad712019-06-02 00:05:48 +03001419 if (m && m->nb_index) {
1420 result = _PyLong_FromNbIndexOrNbInt(o);
1421 if (result != NULL && !PyLong_CheckExact(result)) {
1422 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1423 }
1424 return result;
1425 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001426 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (trunc_func) {
INADA Naoki72dccde2017-02-16 09:26:01 +09001428 result = _PyObject_CallNoArg(trunc_func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 Py_DECREF(trunc_func);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001430 if (result == NULL || PyLong_CheckExact(result)) {
1431 return result;
1432 }
1433 if (PyLong_Check(result)) {
1434 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1435 return result;
1436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 /* __trunc__ is specified to return an Integral type,
Martin Panter7462b6492015-11-02 03:37:02 +00001438 but int() needs to return an int. */
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001439 m = Py_TYPE(result)->tp_as_number;
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001440 if (m == NULL || (m->nb_index == NULL && m->nb_int == NULL)) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001441 PyErr_Format(
1442 PyExc_TypeError,
1443 "__trunc__ returned non-Integral (type %.200s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001444 Py_TYPE(result)->tp_name);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001445 Py_DECREF(result);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001446 return NULL;
1447 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001448 Py_SETREF(result, _PyLong_FromNbIndexOrNbInt(result));
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001449 if (result != NULL && !PyLong_CheckExact(result)) {
1450 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1451 }
1452 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001454 if (PyErr_Occurred())
1455 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001456
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001457 if (PyUnicode_Check(o))
1458 /* The below check is done in PyLong_FromUnicode(). */
1459 return PyLong_FromUnicodeObject(o, 10);
1460
Martin Pantereeb896c2015-11-07 02:32:21 +00001461 if (PyBytes_Check(o))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001463 * doesn't do. In particular int('9\x005') must raise an
1464 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 */
Martin Pantereeb896c2015-11-07 02:32:21 +00001466 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1467 PyBytes_GET_SIZE(o), 10);
1468
1469 if (PyByteArray_Check(o))
1470 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1471 PyByteArray_GET_SIZE(o), 10);
1472
1473 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001474 PyObject *bytes;
Martin Pantereeb896c2015-11-07 02:32:21 +00001475
1476 /* Copy to NUL-terminated buffer. */
1477 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1478 if (bytes == NULL) {
1479 PyBuffer_Release(&view);
1480 return NULL;
1481 }
1482 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1483 PyBytes_GET_SIZE(bytes), 10);
1484 Py_DECREF(bytes);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001485 PyBuffer_Release(&view);
1486 return result;
1487 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001488
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001489 return type_error("int() argument must be a string, a bytes-like object "
1490 "or a number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001491}
1492
1493PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001494PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001497
Victor Stinner71aea8e2016-08-19 16:59:55 +02001498 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001500 }
1501
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001502 if (PyFloat_CheckExact(o)) {
1503 Py_INCREF(o);
1504 return o;
1505 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001506 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (m && m->nb_float) { /* This should include subclasses of float */
1508 PyObject *res = m->nb_float(o);
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001509 double val;
1510 if (!res || PyFloat_CheckExact(res)) {
1511 return res;
1512 }
1513 if (!PyFloat_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001515 "%.50s.__float__ returned non-float (type %.50s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001516 Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 Py_DECREF(res);
1518 return NULL;
1519 }
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001520 /* Issue #26983: warn if 'res' not of exact type float. */
1521 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1522 "%.50s.__float__ returned non-float (type %.50s). "
1523 "The ability to return an instance of a strict subclass of float "
1524 "is deprecated, and may be removed in a future version of Python.",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001525 Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)) {
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001526 Py_DECREF(res);
1527 return NULL;
1528 }
1529 val = PyFloat_AS_DOUBLE(res);
1530 Py_DECREF(res);
1531 return PyFloat_FromDouble(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 }
Serhiy Storchakabdbad712019-06-02 00:05:48 +03001533 if (m && m->nb_index) {
1534 PyObject *res = PyNumber_Index(o);
1535 if (!res) {
1536 return NULL;
1537 }
1538 double val = PyLong_AsDouble(res);
1539 Py_DECREF(res);
1540 if (val == -1.0 && PyErr_Occurred()) {
1541 return NULL;
1542 }
1543 return PyFloat_FromDouble(val);
1544 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001546 return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 }
1548 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001549}
1550
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001551
1552PyObject *
1553PyNumber_ToBase(PyObject *n, int base)
1554{
Serhiy Storchakae5ccc942020-03-09 20:03:38 +02001555 if (!(base == 2 || base == 8 || base == 10 || base == 16)) {
1556 PyErr_SetString(PyExc_SystemError,
1557 "PyNumber_ToBase: base must be 2, 8, 10 or 16");
1558 return NULL;
1559 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PyObject *index = PyNumber_Index(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (!index)
1562 return NULL;
Serhiy Storchakae5ccc942020-03-09 20:03:38 +02001563 PyObject *res = _PyLong_Format(index, base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 Py_DECREF(index);
1565 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001566}
1567
1568
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001569/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001570
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001571int
Fred Drake79912472000-07-09 04:06:11 +00001572PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (PyDict_Check(s))
1575 return 0;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001576 return Py_TYPE(s)->tp_as_sequence &&
1577 Py_TYPE(s)->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001578}
1579
Martin v. Löwis18e16552006-02-15 17:27:45 +00001580Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001581PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (s == NULL) {
1586 null_error();
1587 return -1;
1588 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001589
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001590 m = Py_TYPE(s)->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001591 if (m && m->sq_length) {
1592 Py_ssize_t len = m->sq_length(s);
1593 assert(len >= 0 || PyErr_Occurred());
1594 return len;
1595 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001596
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001597 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_length) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001598 type_error("%.200s is not a sequence", s);
1599 return -1;
1600 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 type_error("object of type '%.200s' has no len()", s);
1602 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001603}
1604
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001605#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001606Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001607PySequence_Length(PyObject *s)
1608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001610}
1611#define PySequence_Length PySequence_Size
1612
Guido van Rossume15dee51995-07-18 14:12:02 +00001613PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001614PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001617
Victor Stinner71aea8e2016-08-19 16:59:55 +02001618 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001620 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001621
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001622 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 if (m && m->sq_concat)
1624 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* Instances of user classes defining an __add__() method only
1627 have an nb_add slot, not an sq_concat slot. So we fall back
1628 to nb_add if both arguments appear to be sequences. */
1629 if (PySequence_Check(s) && PySequence_Check(o)) {
1630 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1631 if (result != Py_NotImplemented)
1632 return result;
1633 Py_DECREF(result);
1634 }
1635 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001636}
1637
1638PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001639PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001642
Victor Stinner71aea8e2016-08-19 16:59:55 +02001643 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001645 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001646
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001647 m = Py_TYPE(o)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (m && m->sq_repeat)
1649 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 /* Instances of user classes defining a __mul__() method only
1652 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1653 to nb_multiply if o appears to be a sequence. */
1654 if (PySequence_Check(o)) {
1655 PyObject *n, *result;
1656 n = PyLong_FromSsize_t(count);
1657 if (n == NULL)
1658 return NULL;
1659 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1660 Py_DECREF(n);
1661 if (result != Py_NotImplemented)
1662 return result;
1663 Py_DECREF(result);
1664 }
1665 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001666}
1667
1668PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001669PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001672
Victor Stinner71aea8e2016-08-19 16:59:55 +02001673 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001675 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001676
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001677 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 if (m && m->sq_inplace_concat)
1679 return m->sq_inplace_concat(s, o);
1680 if (m && m->sq_concat)
1681 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 if (PySequence_Check(s) && PySequence_Check(o)) {
1684 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1685 NB_SLOT(nb_add));
1686 if (result != Py_NotImplemented)
1687 return result;
1688 Py_DECREF(result);
1689 }
1690 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001691}
1692
1693PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001694PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001697
Victor Stinner71aea8e2016-08-19 16:59:55 +02001698 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001700 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001701
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001702 m = Py_TYPE(o)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (m && m->sq_inplace_repeat)
1704 return m->sq_inplace_repeat(o, count);
1705 if (m && m->sq_repeat)
1706 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 if (PySequence_Check(o)) {
1709 PyObject *n, *result;
1710 n = PyLong_FromSsize_t(count);
1711 if (n == NULL)
1712 return NULL;
1713 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1714 NB_SLOT(nb_multiply));
1715 Py_DECREF(n);
1716 if (result != Py_NotImplemented)
1717 return result;
1718 Py_DECREF(result);
1719 }
1720 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001721}
1722
1723PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001724PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001727
Victor Stinner71aea8e2016-08-19 16:59:55 +02001728 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001730 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001731
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001732 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 if (m && m->sq_item) {
1734 if (i < 0) {
1735 if (m->sq_length) {
1736 Py_ssize_t l = (*m->sq_length)(s);
Victor Stinnere20310f2015-11-05 13:56:58 +01001737 if (l < 0) {
1738 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return NULL;
Victor Stinnere20310f2015-11-05 13:56:58 +01001740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 i += l;
1742 }
1743 }
1744 return m->sq_item(s, i);
1745 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001746
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001747 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001748 return type_error("%.200s is not a sequence", s);
1749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001751}
1752
1753PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001754PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001757
Victor Stinner71aea8e2016-08-19 16:59:55 +02001758 if (!s) {
1759 return null_error();
1760 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001761
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001762 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001763 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 PyObject *res;
1765 PyObject *slice = _PySlice_FromIndices(i1, i2);
1766 if (!slice)
1767 return NULL;
1768 res = mp->mp_subscript(s, slice);
1769 Py_DECREF(slice);
1770 return res;
1771 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001774}
1775
1776int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001777PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 if (s == NULL) {
1782 null_error();
1783 return -1;
1784 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001785
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001786 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (m && m->sq_ass_item) {
1788 if (i < 0) {
1789 if (m->sq_length) {
1790 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001791 if (l < 0) {
1792 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001794 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 i += l;
1796 }
1797 }
1798 return m->sq_ass_item(s, i, o);
1799 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001800
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001801 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001802 type_error("%.200s is not a sequence", s);
1803 return -1;
1804 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 type_error("'%.200s' object does not support item assignment", s);
1806 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001807}
1808
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001809int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001810PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 if (s == NULL) {
1815 null_error();
1816 return -1;
1817 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001818
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001819 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 if (m && m->sq_ass_item) {
1821 if (i < 0) {
1822 if (m->sq_length) {
1823 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001824 if (l < 0) {
1825 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001827 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 i += l;
1829 }
1830 }
1831 return m->sq_ass_item(s, i, (PyObject *)NULL);
1832 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001833
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001834 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001835 type_error("%.200s is not a sequence", s);
1836 return -1;
1837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 type_error("'%.200s' object doesn't support item deletion", s);
1839 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001840}
1841
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001842int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001843PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (s == NULL) {
1848 null_error();
1849 return -1;
1850 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001851
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001852 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001853 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 int res;
1855 PyObject *slice = _PySlice_FromIndices(i1, i2);
1856 if (!slice)
1857 return -1;
1858 res = mp->mp_ass_subscript(s, slice, o);
1859 Py_DECREF(slice);
1860 return res;
1861 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 type_error("'%.200s' object doesn't support slice assignment", s);
1864 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001865}
1866
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001867int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001868PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 if (s == NULL) {
1873 null_error();
1874 return -1;
1875 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001876
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001877 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001878 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 int res;
1880 PyObject *slice = _PySlice_FromIndices(i1, i2);
1881 if (!slice)
1882 return -1;
1883 res = mp->mp_ass_subscript(s, slice, NULL);
1884 Py_DECREF(slice);
1885 return res;
1886 }
1887 type_error("'%.200s' object doesn't support slice deletion", s);
1888 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001889}
1890
Guido van Rossume15dee51995-07-18 14:12:02 +00001891PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001892PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 PyObject *it; /* iter(v) */
1895 Py_ssize_t n; /* guess for result tuple size */
1896 PyObject *result = NULL;
1897 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001898
Victor Stinner71aea8e2016-08-19 16:59:55 +02001899 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001901 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 /* Special-case the common tuple and list cases, for efficiency. */
1904 if (PyTuple_CheckExact(v)) {
1905 /* Note that we can't know whether it's safe to return
1906 a tuple *subclass* instance as-is, hence the restriction
1907 to exact tuples here. In contrast, lists always make
1908 a copy, so there's no need for exactness below. */
1909 Py_INCREF(v);
1910 return v;
1911 }
Raymond Hettinger610a51f2015-05-17 14:45:58 -07001912 if (PyList_CheckExact(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 /* Get iterator. */
1916 it = PyObject_GetIter(v);
1917 if (it == NULL)
1918 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001921 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 if (n == -1)
1923 goto Fail;
1924 result = PyTuple_New(n);
1925 if (result == NULL)
1926 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 /* Fill the tuple. */
1929 for (j = 0; ; ++j) {
1930 PyObject *item = PyIter_Next(it);
1931 if (item == NULL) {
1932 if (PyErr_Occurred())
1933 goto Fail;
1934 break;
1935 }
1936 if (j >= n) {
Martin Pantere8db8612016-07-25 02:30:05 +00001937 size_t newn = (size_t)n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 /* The over-allocation strategy can grow a bit faster
1939 than for lists because unlike lists the
1940 over-allocation isn't permanent -- we reclaim
1941 the excess before the end of this routine.
1942 So, grow by ten and then add 25%.
1943 */
Martin Pantere8db8612016-07-25 02:30:05 +00001944 newn += 10u;
1945 newn += newn >> 2;
1946 if (newn > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 /* Check for overflow */
1948 PyErr_NoMemory();
1949 Py_DECREF(item);
1950 goto Fail;
1951 }
Martin Pantere8db8612016-07-25 02:30:05 +00001952 n = (Py_ssize_t)newn;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 if (_PyTuple_Resize(&result, n) != 0) {
1954 Py_DECREF(item);
1955 goto Fail;
1956 }
1957 }
1958 PyTuple_SET_ITEM(result, j, item);
1959 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 /* Cut tuple back if guess was too large. */
1962 if (j < n &&
1963 _PyTuple_Resize(&result, j) != 0)
1964 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 Py_DECREF(it);
1967 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001968
1969Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 Py_XDECREF(result);
1971 Py_DECREF(it);
1972 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001973}
1974
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001975PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001976PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 PyObject *result; /* result list */
1979 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001980
Victor Stinner71aea8e2016-08-19 16:59:55 +02001981 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001983 }
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 result = PyList_New(0);
1986 if (result == NULL)
1987 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 rv = _PyList_Extend((PyListObject *)result, v);
1990 if (rv == NULL) {
1991 Py_DECREF(result);
1992 return NULL;
1993 }
1994 Py_DECREF(rv);
1995 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001996}
1997
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001998PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001999PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00002002
Victor Stinner71aea8e2016-08-19 16:59:55 +02002003 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002005 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
2008 Py_INCREF(v);
2009 return v;
2010 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 it = PyObject_GetIter(v);
2013 if (it == NULL) {
2014 if (PyErr_ExceptionMatches(PyExc_TypeError))
2015 PyErr_SetString(PyExc_TypeError, m);
2016 return NULL;
2017 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 v = PySequence_List(it);
2020 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002023}
2024
Tim Peters16a77ad2001-09-08 04:00:12 +00002025/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
2027 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
2028 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00002029 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
2030*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002031Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002032_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00002033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 Py_ssize_t n;
2035 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
2036 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (seq == NULL || obj == NULL) {
2039 null_error();
2040 return -1;
2041 }
Tim Peters75f8e352001-05-05 11:33:43 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 it = PyObject_GetIter(seq);
2044 if (it == NULL) {
2045 type_error("argument of type '%.200s' is not iterable", seq);
2046 return -1;
2047 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 n = wrapped = 0;
2050 for (;;) {
2051 int cmp;
2052 PyObject *item = PyIter_Next(it);
2053 if (item == NULL) {
2054 if (PyErr_Occurred())
2055 goto Fail;
2056 break;
2057 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002058
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03002059 cmp = PyObject_RichCompareBool(item, obj, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 Py_DECREF(item);
2061 if (cmp < 0)
2062 goto Fail;
2063 if (cmp > 0) {
2064 switch (operation) {
2065 case PY_ITERSEARCH_COUNT:
2066 if (n == PY_SSIZE_T_MAX) {
2067 PyErr_SetString(PyExc_OverflowError,
2068 "count exceeds C integer size");
2069 goto Fail;
2070 }
2071 ++n;
2072 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00002073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 case PY_ITERSEARCH_INDEX:
2075 if (wrapped) {
2076 PyErr_SetString(PyExc_OverflowError,
2077 "index exceeds C integer size");
2078 goto Fail;
2079 }
2080 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 case PY_ITERSEARCH_CONTAINS:
2083 n = 1;
2084 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07002087 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 }
2089 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (operation == PY_ITERSEARCH_INDEX) {
2092 if (n == PY_SSIZE_T_MAX)
2093 wrapped = 1;
2094 ++n;
2095 }
2096 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (operation != PY_ITERSEARCH_INDEX)
2099 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 PyErr_SetString(PyExc_ValueError,
2102 "sequence.index(x): x not in sequence");
2103 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00002104Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 n = -1;
2106 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00002107Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 Py_DECREF(it);
2109 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00002110
Guido van Rossume15dee51995-07-18 14:12:02 +00002111}
2112
Tim Peters16a77ad2001-09-08 04:00:12 +00002113/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002114Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002115PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00002118}
2119
Tim Peterscb8d3682001-05-05 21:05:01 +00002120/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00002121 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00002122 */
2123int
2124PySequence_Contains(PyObject *seq, PyObject *ob)
2125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 Py_ssize_t result;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002127 PySequenceMethods *sqm = Py_TYPE(seq)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 if (sqm != NULL && sqm->sq_contains != NULL)
2129 return (*sqm->sq_contains)(seq, ob);
2130 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
2131 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00002132}
2133
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002134/* Backwards compatibility */
2135#undef PySequence_In
2136int
Fred Drake79912472000-07-09 04:06:11 +00002137PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002140}
2141
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002142Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00002143PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00002146}
2147
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002148/* Operations on mappings */
2149
2150int
Fred Drake79912472000-07-09 04:06:11 +00002151PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002152{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002153 return o && Py_TYPE(o)->tp_as_mapping &&
2154 Py_TYPE(o)->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002155}
2156
Martin v. Löwis18e16552006-02-15 17:27:45 +00002157Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002158PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (o == NULL) {
2163 null_error();
2164 return -1;
2165 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002166
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002167 m = Py_TYPE(o)->tp_as_mapping;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03002168 if (m && m->mp_length) {
2169 Py_ssize_t len = m->mp_length(o);
2170 assert(len >= 0 || PyErr_Occurred());
2171 return len;
2172 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002173
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002174 if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03002175 type_error("%.200s is not a mapping", o);
2176 return -1;
2177 }
2178 /* PyMapping_Size() can be called from PyObject_Size(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 type_error("object of type '%.200s' has no len()", o);
2180 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002181}
2182
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002183#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002184Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002185PyMapping_Length(PyObject *o)
2186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002188}
2189#define PyMapping_Length PyMapping_Size
2190
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002191PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002192PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002195
Victor Stinner71aea8e2016-08-19 16:59:55 +02002196 if (key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002198 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 okey = PyUnicode_FromString(key);
2201 if (okey == NULL)
2202 return NULL;
2203 r = PyObject_GetItem(o, okey);
2204 Py_DECREF(okey);
2205 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002206}
2207
2208int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002209PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 PyObject *okey;
2212 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (key == NULL) {
2215 null_error();
2216 return -1;
2217 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 okey = PyUnicode_FromString(key);
2220 if (okey == NULL)
2221 return -1;
2222 r = PyObject_SetItem(o, okey, value);
2223 Py_DECREF(okey);
2224 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002225}
2226
2227int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002228PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 v = PyMapping_GetItemString(o, key);
2233 if (v) {
2234 Py_DECREF(v);
2235 return 1;
2236 }
2237 PyErr_Clear();
2238 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002239}
2240
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002241int
Fred Drake79912472000-07-09 04:06:11 +00002242PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 v = PyObject_GetItem(o, key);
2247 if (v) {
2248 Py_DECREF(v);
2249 return 1;
2250 }
2251 PyErr_Clear();
2252 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002253}
2254
Oren Milman0ccc0f62017-10-08 11:17:46 +03002255/* This function is quite similar to PySequence_Fast(), but specialized to be
2256 a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values().
2257 */
2258static PyObject *
2259method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
2260{
2261 PyObject *it, *result, *meth_output;
2262
2263 assert(o != NULL);
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002264 meth_output = _PyObject_CallMethodIdNoArgs(o, meth_id);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002265 if (meth_output == NULL || PyList_CheckExact(meth_output)) {
2266 return meth_output;
2267 }
2268 it = PyObject_GetIter(meth_output);
2269 if (it == NULL) {
2270 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
2271 PyErr_Format(PyExc_TypeError,
2272 "%.200s.%U() returned a non-iterable (type %.200s)",
2273 Py_TYPE(o)->tp_name,
2274 meth_id->object,
2275 Py_TYPE(meth_output)->tp_name);
2276 }
2277 Py_DECREF(meth_output);
2278 return NULL;
2279 }
2280 Py_DECREF(meth_output);
2281 result = PySequence_List(it);
2282 Py_DECREF(it);
2283 return result;
2284}
2285
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002286PyObject *
2287PyMapping_Keys(PyObject *o)
2288{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002289 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002290
Oren Milman0ccc0f62017-10-08 11:17:46 +03002291 if (o == NULL) {
2292 return null_error();
2293 }
2294 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 return PyDict_Keys(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002296 }
2297 return method_output_as_list(o, &PyId_keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002298}
2299
2300PyObject *
2301PyMapping_Items(PyObject *o)
2302{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002303 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002304
Oren Milman0ccc0f62017-10-08 11:17:46 +03002305 if (o == NULL) {
2306 return null_error();
2307 }
2308 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 return PyDict_Items(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002310 }
2311 return method_output_as_list(o, &PyId_items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002312}
2313
2314PyObject *
2315PyMapping_Values(PyObject *o)
2316{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002317 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002318
Oren Milman0ccc0f62017-10-08 11:17:46 +03002319 if (o == NULL) {
2320 return null_error();
2321 }
2322 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 return PyDict_Values(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002324 }
2325 return method_output_as_list(o, &PyId_values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002326}
2327
Guido van Rossum823649d2001-03-21 18:40:58 +00002328/* isinstance(), issubclass() */
2329
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002330/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002331 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002332 * 1. getattr(cls, '__bases__') could raise an AttributeError
2333 * 2. getattr(cls, '__bases__') could raise some other exception
2334 * 3. getattr(cls, '__bases__') could return a tuple
2335 * 4. getattr(cls, '__bases__') could return something other than a tuple
2336 *
2337 * Only state #3 is a non-error state and only it returns a non-NULL object
2338 * (it returns the retrieved tuple).
2339 *
2340 * Any raised AttributeErrors are masked by clearing the exception and
2341 * returning NULL. If an object other than a tuple comes out of __bases__,
2342 * then again, the return value is NULL. So yes, these two situations
2343 * produce exactly the same results: NULL is returned and no error is set.
2344 *
2345 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002347 * exception to be propagated along.
2348 *
2349 * Callers are expected to test for PyErr_Occurred() when the return value
2350 * is NULL to decide whether a valid exception should be propagated or not.
2351 * When there's no exception to propagate, it's customary for the caller to
2352 * set a TypeError.
2353 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002354static PyObject *
2355abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002356{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002357 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 Py_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002361 (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 Py_END_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002363 if (bases != NULL && !PyTuple_Check(bases)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 Py_DECREF(bases);
2365 return NULL;
2366 }
2367 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002368}
2369
2370
2371static int
2372abstract_issubclass(PyObject *derived, PyObject *cls)
2373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 PyObject *bases = NULL;
2375 Py_ssize_t i, n;
2376 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 while (1) {
Yonatan Goldschmidt1c56f8f2020-02-22 15:11:48 +02002379 if (derived == cls) {
2380 Py_XDECREF(bases); /* See below comment */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 return 1;
Yonatan Goldschmidt1c56f8f2020-02-22 15:11:48 +02002382 }
2383 /* Use XSETREF to drop bases reference *after* finishing with
2384 derived; bases might be the only reference to it.
2385 XSETREF is used instead of SETREF, because bases is NULL on the
2386 first iteration of the loop.
2387 */
2388 Py_XSETREF(bases, abstract_get_bases(derived));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (bases == NULL) {
2390 if (PyErr_Occurred())
2391 return -1;
2392 return 0;
2393 }
2394 n = PyTuple_GET_SIZE(bases);
2395 if (n == 0) {
2396 Py_DECREF(bases);
2397 return 0;
2398 }
2399 /* Avoid recursivity in the single inheritance case */
2400 if (n == 1) {
2401 derived = PyTuple_GET_ITEM(bases, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 continue;
2403 }
2404 for (i = 0; i < n; i++) {
2405 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2406 if (r != 0)
2407 break;
2408 }
2409 Py_DECREF(bases);
2410 return r;
2411 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002412}
2413
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002414static int
2415check_class(PyObject *cls, const char *error)
2416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 PyObject *bases = abstract_get_bases(cls);
2418 if (bases == NULL) {
2419 /* Do not mask errors. */
2420 if (!PyErr_Occurred())
2421 PyErr_SetString(PyExc_TypeError, error);
2422 return 0;
2423 }
2424 Py_DECREF(bases);
2425 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002426}
2427
Brett Cannon4f653312004-03-20 22:52:14 +00002428static int
Victor Stinner850a4bd2020-02-04 13:42:13 +01002429object_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 PyObject *icls;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002432 int retval;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002433 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 if (PyType_Check(cls)) {
2436 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2437 if (retval == 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002438 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2439 if (icls != NULL) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002440 if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 retval = PyType_IsSubtype(
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002442 (PyTypeObject *)icls,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 (PyTypeObject *)cls);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002444 }
2445 else {
2446 retval = 0;
2447 }
2448 Py_DECREF(icls);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 }
2450 }
2451 }
2452 else {
2453 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002454 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 return -1;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002456 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2457 if (icls != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 retval = abstract_issubclass(icls, cls);
2459 Py_DECREF(icls);
2460 }
2461 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002464}
2465
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002466static int
Victor Stinner850a4bd2020-02-04 13:42:13 +01002467object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
Brett Cannon4f653312004-03-20 22:52:14 +00002468{
Benjamin Petersonce798522012-01-22 11:24:29 -05002469 _Py_IDENTIFIER(__instancecheck__);
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Quick test for an exact match */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002472 if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 return 1;
Victor Stinner850a4bd2020-02-04 13:42:13 +01002474 }
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002475
Georg Brandl72b8a802014-10-03 09:26:37 +02002476 /* We know what type's __instancecheck__ does. */
2477 if (PyType_CheckExact(cls)) {
Victor Stinner850a4bd2020-02-04 13:42:13 +01002478 return object_isinstance(inst, cls);
Georg Brandl72b8a802014-10-03 09:26:37 +02002479 }
2480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 if (PyTuple_Check(cls)) {
Victor Stinner850a4bd2020-02-04 13:42:13 +01002482 /* Not a general sequence -- that opens up the road to
2483 recursion and stack overflow. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002484 if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002486 }
2487 Py_ssize_t n = PyTuple_GET_SIZE(cls);
2488 int r = 0;
2489 for (Py_ssize_t i = 0; i < n; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 PyObject *item = PyTuple_GET_ITEM(cls, i);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002491 r = object_recursive_isinstance(tstate, inst, item);
2492 if (r != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 /* either found it, or got an error */
2494 break;
Victor Stinner850a4bd2020-02-04 13:42:13 +01002495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002497 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 return r;
2499 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002500
Victor Stinner850a4bd2020-02-04 13:42:13 +01002501 PyObject *checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 if (checker != NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002503 if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 Py_DECREF(checker);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002505 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 }
Victor Stinner850a4bd2020-02-04 13:42:13 +01002507
Petr Viktorinffd97532020-02-11 17:46:57 +01002508 PyObject *res = PyObject_CallOneArg(checker, inst);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002509 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 Py_DECREF(checker);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002511
2512 if (res == NULL) {
2513 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 }
Victor Stinner850a4bd2020-02-04 13:42:13 +01002515 int ok = PyObject_IsTrue(res);
2516 Py_DECREF(res);
2517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 return ok;
2519 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002520 else if (_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002522 }
2523
Victor Stinner850a4bd2020-02-04 13:42:13 +01002524 /* cls has no __instancecheck__() method */
2525 return object_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002526}
2527
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002528
2529int
2530PyObject_IsInstance(PyObject *inst, PyObject *cls)
2531{
2532 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner850a4bd2020-02-04 13:42:13 +01002533 return object_recursive_isinstance(tstate, inst, cls);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002534}
2535
2536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002538recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (PyType_Check(cls) && PyType_Check(derived)) {
2541 /* Fast path (non-recursive) */
2542 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2543 }
2544 if (!check_class(derived,
2545 "issubclass() arg 1 must be a class"))
2546 return -1;
2547 if (!check_class(cls,
2548 "issubclass() arg 2 must be a class"
2549 " or tuple of classes"))
2550 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002553}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002554
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002555static int
2556object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
Brett Cannon4f653312004-03-20 22:52:14 +00002557{
Benjamin Petersonce798522012-01-22 11:24:29 -05002558 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002560
Georg Brandl72b8a802014-10-03 09:26:37 +02002561 /* We know what type's __subclasscheck__ does. */
2562 if (PyType_CheckExact(cls)) {
2563 /* Quick test for an exact match */
2564 if (derived == cls)
2565 return 1;
2566 return recursive_issubclass(derived, cls);
2567 }
2568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 if (PyTuple_Check(cls)) {
Antoine Pitrouec569b72008-08-26 22:40:48 +00002570
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002571 if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002573 }
2574 Py_ssize_t n = PyTuple_GET_SIZE(cls);
2575 int r = 0;
2576 for (Py_ssize_t i = 0; i < n; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 PyObject *item = PyTuple_GET_ITEM(cls, i);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002578 r = object_issubclass(tstate, derived, item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (r != 0)
2580 /* either found it, or got an error */
2581 break;
2582 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002583 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 return r;
2585 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002586
Benjamin Petersonce798522012-01-22 11:24:29 -05002587 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 if (checker != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 int ok = -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002590 if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 Py_DECREF(checker);
2592 return ok;
2593 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002594 PyObject *res = PyObject_CallOneArg(checker, derived);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002595 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 Py_DECREF(checker);
2597 if (res != NULL) {
2598 ok = PyObject_IsTrue(res);
2599 Py_DECREF(res);
2600 }
2601 return ok;
2602 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002603 else if (_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002605 }
2606
Georg Brandl72b8a802014-10-03 09:26:37 +02002607 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002609}
2610
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002611
2612int
2613PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2614{
2615 PyThreadState *tstate = _PyThreadState_GET();
2616 return object_issubclass(tstate, derived, cls);
2617}
2618
2619
Antoine Pitrouec569b72008-08-26 22:40:48 +00002620int
2621_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2622{
Victor Stinner850a4bd2020-02-04 13:42:13 +01002623 return object_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002624}
2625
2626int
2627_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002630}
2631
2632
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002633PyObject *
2634PyObject_GetIter(PyObject *o)
2635{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002636 PyTypeObject *t = Py_TYPE(o);
Victor Stinner14e6d092016-12-09 17:08:59 +01002637 getiterfunc f;
2638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 f = t->tp_iter;
2640 if (f == NULL) {
2641 if (PySequence_Check(o))
2642 return PySeqIter_New(o);
2643 return type_error("'%.200s' object is not iterable", o);
2644 }
2645 else {
2646 PyObject *res = (*f)(o);
2647 if (res != NULL && !PyIter_Check(res)) {
2648 PyErr_Format(PyExc_TypeError,
2649 "iter() returned non-iterator "
2650 "of type '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002651 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 Py_DECREF(res);
2653 res = NULL;
2654 }
2655 return res;
2656 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002657}
2658
Christian Tismerea62ce72018-06-09 20:32:25 +02002659#undef PyIter_Check
Christian Tismer83987132018-06-11 00:48:28 +02002660
Christian Tismerea62ce72018-06-09 20:32:25 +02002661int PyIter_Check(PyObject *obj)
2662{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002663 return Py_TYPE(obj)->tp_iternext != NULL &&
2664 Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented;
Christian Tismerea62ce72018-06-09 20:32:25 +02002665}
2666
Tim Petersf4848da2001-05-05 00:14:56 +00002667/* Return next item.
2668 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2669 * If the iteration terminates normally, return NULL and clear the
2670 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2671 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002673 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002674PyObject *
2675PyIter_Next(PyObject *iter)
2676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 PyObject *result;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002678 result = (*Py_TYPE(iter)->tp_iternext)(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 if (result == NULL &&
2680 PyErr_Occurred() &&
2681 PyErr_ExceptionMatches(PyExc_StopIteration))
2682 PyErr_Clear();
2683 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002684}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002685
2686
2687/*
2688 * Flatten a sequence of bytes() objects into a C array of
2689 * NULL terminated string pointers with a NULL char* terminating the array.
2690 * (ie: an argv or env list)
2691 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002692 * Memory allocated for the returned list is allocated using PyMem_Malloc()
2693 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002694 */
2695char *const *
2696_PySequence_BytesToCharpArray(PyObject* self)
2697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 char **array;
2699 Py_ssize_t i, argc;
2700 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002701 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 argc = PySequence_Size(self);
2704 if (argc == -1)
2705 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002706
Stefan Krah7cacd2e2012-08-21 08:16:09 +02002707 assert(argc >= 0);
2708
2709 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2710 PyErr_NoMemory();
2711 return NULL;
2712 }
2713
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002714 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 if (array == NULL) {
2716 PyErr_NoMemory();
2717 return NULL;
2718 }
2719 for (i = 0; i < argc; ++i) {
2720 char *data;
2721 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02002722 if (item == NULL) {
2723 /* NULL terminate before freeing. */
2724 array[i] = NULL;
2725 goto fail;
2726 }
Serhiy Storchakad174d242017-06-23 19:39:27 +03002727 /* check for embedded null bytes */
2728 if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 /* NULL terminate before freeing. */
2730 array[i] = NULL;
2731 goto fail;
2732 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002733 size = PyBytes_GET_SIZE(item) + 1;
2734 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 if (!array[i]) {
2736 PyErr_NoMemory();
2737 goto fail;
2738 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002739 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_DECREF(item);
2741 }
2742 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002745
2746fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 Py_XDECREF(item);
2748 _Py_FreeCharPArray(array);
2749 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002750}
2751
2752
2753/* Free's a NULL terminated char** array of C strings. */
2754void
2755_Py_FreeCharPArray(char *const array[])
2756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_ssize_t i;
2758 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002759 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002761 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002762}