blob: f9be4227890ec70720db303ac13113cb0ec3705a [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 Stinnerbe434dc2019-11-05 00:51:22 +01004#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00006#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00007#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00008#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00009
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000011
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000012/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000013
14static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000016{
Victor Stinner0d76d2b2020-02-07 01:53:23 +010017 PyErr_Format(PyExc_TypeError, msg, Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000019}
20
Guido van Rossum052b7e11996-11-11 15:08:19 +000021static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000022null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 if (!PyErr_Occurred())
25 PyErr_SetString(PyExc_SystemError,
26 "null argument to internal routine");
27 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000028}
29
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000030/* Operations on any object */
31
Guido van Rossume15dee51995-07-18 14:12:02 +000032PyObject *
Fred Drake79912472000-07-09 04:06:11 +000033PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000036
Victor Stinner71aea8e2016-08-19 16:59:55 +020037 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +020039 }
40
Victor Stinner0d76d2b2020-02-07 01:53:23 +010041 v = (PyObject *)Py_TYPE(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 Py_INCREF(v);
43 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000044}
45
Martin v. Löwis18e16552006-02-15 17:27:45 +000046Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000047PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 if (o == NULL) {
52 null_error();
53 return -1;
54 }
Guido van Rossume15dee51995-07-18 14:12:02 +000055
Victor Stinner0d76d2b2020-02-07 01:53:23 +010056 m = Py_TYPE(o)->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +030057 if (m && m->sq_length) {
58 Py_ssize_t len = m->sq_length(o);
59 assert(len >= 0 || PyErr_Occurred());
60 return len;
61 }
Guido van Rossume15dee51995-07-18 14:12:02 +000062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000064}
65
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000066#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000067Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000068PyObject_Length(PyObject *o)
69{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000071}
72#define PyObject_Length PyObject_Size
73
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020074int
75_PyObject_HasLen(PyObject *o) {
76 return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
77 (Py_TYPE(o)->tp_as_mapping && Py_TYPE(o)->tp_as_mapping->mp_length);
78}
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000079
Christian Heimes255f53b2007-12-08 15:33:56 +000080/* The length hint function returns a non-negative value from o.__len__()
Armin Ronacher74b38b12012-10-07 10:29:32 +020081 or o.__length_hint__(). If those methods aren't found the defaultvalue is
82 returned. If one of the calls fails with an exception other than TypeError
83 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000084*/
85
86Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +020087PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
Christian Heimes255f53b2007-12-08 15:33:56 +000088{
Christian Heimesb70e8a12012-10-06 17:16:39 +020089 PyObject *hint, *result;
Christian Heimes6314d162012-10-06 17:13:29 +020090 Py_ssize_t res;
Benjamin Petersonce798522012-01-22 11:24:29 -050091 _Py_IDENTIFIER(__length_hint__);
Serhiy Storchakaf740d462013-10-24 23:19:51 +030092 if (_PyObject_HasLen(o)) {
93 res = PyObject_Length(o);
Serhiy Storchaka813f9432017-04-16 09:21:44 +030094 if (res < 0) {
95 assert(PyErr_Occurred());
Serhiy Storchakaf740d462013-10-24 23:19:51 +030096 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
97 return -1;
98 }
99 PyErr_Clear();
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200100 }
Serhiy Storchakaf740d462013-10-24 23:19:51 +0300101 else {
102 return res;
103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 }
Christian Heimes6314d162012-10-06 17:13:29 +0200105 hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200106 if (hint == NULL) {
107 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return -1;
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200109 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return defaultvalue;
111 }
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100112 result = _PyObject_CallNoArg(hint);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200113 Py_DECREF(hint);
114 if (result == NULL) {
115 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
116 PyErr_Clear();
117 return defaultvalue;
118 }
119 return -1;
120 }
121 else if (result == Py_NotImplemented) {
122 Py_DECREF(result);
123 return defaultvalue;
124 }
125 if (!PyLong_Check(result)) {
Armin Ronacher74b38b12012-10-07 10:29:32 +0200126 PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200127 Py_TYPE(result)->tp_name);
128 Py_DECREF(result);
129 return -1;
130 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200131 res = PyLong_AsSsize_t(result);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200132 Py_DECREF(result);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200133 if (res < 0 && PyErr_Occurred()) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200134 return -1;
135 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200136 if (res < 0) {
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200137 PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
138 return -1;
139 }
Armin Ronacher74b38b12012-10-07 10:29:32 +0200140 return res;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000141}
142
Guido van Rossume15dee51995-07-18 14:12:02 +0000143PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000144PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 PyMappingMethods *m;
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000147 PySequenceMethods *ms;
Guido van Rossume15dee51995-07-18 14:12:02 +0000148
Victor Stinner71aea8e2016-08-19 16:59:55 +0200149 if (o == NULL || key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +0200151 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000152
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100153 m = Py_TYPE(o)->tp_as_mapping;
Victor Stinnere20310f2015-11-05 13:56:58 +0100154 if (m && m->mp_subscript) {
155 PyObject *item = m->mp_subscript(o, key);
156 assert((item != NULL) ^ (PyErr_Occurred() != NULL));
157 return item;
158 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000159
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100160 ms = Py_TYPE(o)->tp_as_sequence;
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000161 if (ms && ms->sq_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (PyIndex_Check(key)) {
163 Py_ssize_t key_value;
164 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
165 if (key_value == -1 && PyErr_Occurred())
166 return NULL;
167 return PySequence_GetItem(o, key_value);
168 }
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000169 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return type_error("sequence index must "
171 "be integer, not '%.200s'", key);
Ivan Levkivskyiac281472019-02-17 23:13:46 +0000172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000174
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100175 if (PyType_Check(o)) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200176 PyObject *meth, *result;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100177 _Py_IDENTIFIER(__class_getitem__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200178 if (_PyObject_LookupAttrId(o, &PyId___class_getitem__, &meth) < 0) {
179 return NULL;
180 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100181 if (meth) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100182 result = PyObject_CallOneArg(meth, key);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100183 Py_DECREF(meth);
184 return result;
185 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100186 }
187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000189}
190
191int
Fred Drake79912472000-07-09 04:06:11 +0000192PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (o == NULL || key == NULL || value == NULL) {
197 null_error();
198 return -1;
199 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100200 m = Py_TYPE(o)->tp_as_mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (m && m->mp_ass_subscript)
202 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000203
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100204 if (Py_TYPE(o)->tp_as_sequence) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 if (PyIndex_Check(key)) {
206 Py_ssize_t key_value;
207 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
208 if (key_value == -1 && PyErr_Occurred())
209 return -1;
210 return PySequence_SetItem(o, key_value, value);
211 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100212 else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 type_error("sequence index must be "
214 "integer, not '%.200s'", key);
215 return -1;
216 }
217 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 type_error("'%.200s' object does not support item assignment", o);
220 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000221}
222
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000223int
Fred Drake79912472000-07-09 04:06:11 +0000224PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (o == NULL || key == NULL) {
229 null_error();
230 return -1;
231 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100232 m = Py_TYPE(o)->tp_as_mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (m && m->mp_ass_subscript)
234 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000235
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100236 if (Py_TYPE(o)->tp_as_sequence) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (PyIndex_Check(key)) {
238 Py_ssize_t key_value;
239 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
240 if (key_value == -1 && PyErr_Occurred())
241 return -1;
242 return PySequence_DelItem(o, key_value);
243 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100244 else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 type_error("sequence index must be "
246 "integer, not '%.200s'", key);
247 return -1;
248 }
249 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 type_error("'%.200s' object does not support item deletion", o);
252 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000253}
254
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000255int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300256PyObject_DelItemString(PyObject *o, const char *key)
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyObject *okey;
259 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (o == NULL || key == NULL) {
262 null_error();
263 return -1;
264 }
265 okey = PyUnicode_FromString(key);
266 if (okey == NULL)
267 return -1;
268 ret = PyObject_DelItem(o, okey);
269 Py_DECREF(okey);
270 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000271}
272
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000273/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000274 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000275 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000276int
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000277PyObject_CheckReadBuffer(PyObject *obj)
278{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100279 PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (pb == NULL ||
283 pb->bf_getbuffer == NULL)
284 return 0;
285 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
286 PyErr_Clear();
287 return 0;
288 }
289 PyBuffer_Release(&view);
290 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000291}
292
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200293static int
294as_read_buffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
299 null_error();
300 return -1;
301 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200302 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 *buffer = view.buf;
306 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200307 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000309}
310
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200311int
312PyObject_AsCharBuffer(PyObject *obj,
313 const char **buffer,
314 Py_ssize_t *buffer_len)
315{
316 return as_read_buffer(obj, (const void **)buffer, buffer_len);
317}
318
319int PyObject_AsReadBuffer(PyObject *obj,
320 const void **buffer,
321 Py_ssize_t *buffer_len)
322{
323 return as_read_buffer(obj, buffer, buffer_len);
324}
325
Guido van Rossum4c08d552000-03-10 22:55:18 +0000326int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 void **buffer,
328 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyBufferProcs *pb;
331 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
334 null_error();
335 return -1;
336 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100337 pb = Py_TYPE(obj)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (pb == NULL ||
339 pb->bf_getbuffer == NULL ||
340 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
341 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400342 "expected a writable bytes-like object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return -1;
344 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 *buffer = view.buf;
347 *buffer_len = view.len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200348 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000350}
351
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000352/* Buffer C-API for Python 3.0 */
353
354int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000355PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000356{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100357 PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200358
359 if (pb == NULL || pb->bf_getbuffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 PyErr_Format(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400361 "a bytes-like object is required, not '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_TYPE(obj)->tp_name);
363 return -1;
364 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200365 return (*pb->bf_getbuffer)(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000366}
367
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000368static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100369_IsFortranContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 Py_ssize_t sd, dim;
372 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000373
Stefan Krah363af442015-02-01 14:53:54 +0100374 /* 1) len = product(shape) * itemsize
375 2) itemsize > 0
376 3) len = 0 <==> exists i: shape[i] = 0 */
377 if (view->len == 0) return 1;
378 if (view->strides == NULL) { /* C-contiguous by definition */
379 /* Trivially F-contiguous */
380 if (view->ndim <= 1) return 1;
381
382 /* ndim > 1 implies shape != NULL */
383 assert(view->shape != NULL);
384
385 /* Effectively 1-d */
386 sd = 0;
387 for (i=0; i<view->ndim; i++) {
388 if (view->shape[i] > 1) sd += 1;
389 }
390 return sd <= 1;
391 }
392
393 /* strides != NULL implies both of these */
394 assert(view->ndim > 0);
395 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 for (i=0; i<view->ndim; i++) {
399 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100400 if (dim > 1 && view->strides[i] != sd) {
401 return 0;
402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 sd *= dim;
404 }
405 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000406}
407
408static int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100409_IsCContiguous(const Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 Py_ssize_t sd, dim;
412 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000413
Stefan Krah363af442015-02-01 14:53:54 +0100414 /* 1) len = product(shape) * itemsize
415 2) itemsize > 0
416 3) len = 0 <==> exists i: shape[i] = 0 */
417 if (view->len == 0) return 1;
418 if (view->strides == NULL) return 1; /* C-contiguous by definition */
419
420 /* strides != NULL implies both of these */
421 assert(view->ndim > 0);
422 assert(view->shape != NULL);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 sd = view->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 for (i=view->ndim-1; i>=0; i--) {
426 dim = view->shape[i];
Stefan Krah363af442015-02-01 14:53:54 +0100427 if (dim > 1 && view->strides[i] != sd) {
428 return 0;
429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 sd *= dim;
431 }
432 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000433}
434
435int
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100436PyBuffer_IsContiguous(const Py_buffer *view, char order)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000437{
438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000440
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100441 if (order == 'C')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return _IsCContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100443 else if (order == 'F')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 return _IsFortranContiguous(view);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100445 else if (order == 'A')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return (_IsCContiguous(view) || _IsFortranContiguous(view));
447 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000448}
449
450
Guido van Rossum98297ee2007-11-06 21:34:58 +0000451void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000452PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 char* pointer;
455 int i;
456 pointer = (char *)view->buf;
457 for (i = 0; i < view->ndim; i++) {
458 pointer += view->strides[i]*indices[i];
459 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
460 pointer = *((char**)pointer) + view->suboffsets[i];
461 }
462 }
463 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000464}
465
466
Guido van Rossum98297ee2007-11-06 21:34:58 +0000467void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000468_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 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 for (k=0; k<nd; k++) {
473 if (index[k] < shape[k]-1) {
474 index[k]++;
475 break;
476 }
477 else {
478 index[k] = 0;
479 }
480 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000481}
482
Guido van Rossum98297ee2007-11-06 21:34:58 +0000483void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000484_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 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 for (k=nd-1; k>=0; k--) {
489 if (index[k] < shape[k]-1) {
490 index[k]++;
491 break;
492 }
493 else {
494 index[k] = 0;
495 }
496 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000497}
498
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300499Py_ssize_t
500PyBuffer_SizeFromFormat(const char *format)
501{
502 PyObject *structmodule = NULL;
503 PyObject *calcsize = NULL;
504 PyObject *res = NULL;
505 PyObject *fmt = NULL;
506 Py_ssize_t itemsize = -1;
507
508 structmodule = PyImport_ImportModule("struct");
509 if (structmodule == NULL) {
510 return itemsize;
511 }
512
513 calcsize = PyObject_GetAttrString(structmodule, "calcsize");
514 if (calcsize == NULL) {
515 goto done;
516 }
517
518 fmt = PyUnicode_FromString(format);
519 if (fmt == NULL) {
520 goto done;
521 }
522
523 res = PyObject_CallFunctionObjArgs(calcsize, fmt, NULL);
524 if (res == NULL) {
525 goto done;
526 }
527
528 itemsize = PyLong_AsSsize_t(res);
529 if (itemsize < 0) {
530 goto done;
531 }
532
533done:
534 Py_DECREF(structmodule);
535 Py_XDECREF(calcsize);
536 Py_XDECREF(fmt);
537 Py_XDECREF(res);
538 return itemsize;
539}
540
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000541int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000542PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000545 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t *indices, elements;
547 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (len > view->len) {
550 len = view->len;
551 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (PyBuffer_IsContiguous(view, fort)) {
554 /* simplest copy is all that is needed */
555 memcpy(view->buf, buf, len);
556 return 0;
557 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000560
Stefan Krah7213fcc2015-02-01 16:19:23 +0100561 /* view->ndim <= 64 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
563 if (indices == NULL) {
564 PyErr_NoMemory();
565 return -1;
566 }
567 for (k=0; k<view->ndim;k++) {
568 indices[k] = 0;
569 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000572 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
574 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000575 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 }
577 src = buf;
578 /* XXX : This is not going to be the fastest code in the world
579 several optimizations are possible.
580 */
581 elements = len / view->itemsize;
582 while (elements--) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 ptr = PyBuffer_GetPointer(view, indices);
584 memcpy(ptr, src, view->itemsize);
585 src += view->itemsize;
Stefan Krah7213fcc2015-02-01 16:19:23 +0100586 addone(view->ndim, indices, view->shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyMem_Free(indices);
590 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000591}
592
Guido van Rossum98297ee2007-11-06 21:34:58 +0000593int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 Py_buffer view_dest, view_src;
596 int k;
597 Py_ssize_t *indices, elements;
598 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (!PyObject_CheckBuffer(dest) ||
601 !PyObject_CheckBuffer(src)) {
602 PyErr_SetString(PyExc_TypeError,
R David Murray861470c2014-10-05 11:47:01 -0400603 "both destination and source must be "\
604 "bytes-like objects");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return -1;
606 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
609 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
610 PyBuffer_Release(&view_dest);
611 return -1;
612 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (view_dest.len < view_src.len) {
615 PyErr_SetString(PyExc_BufferError,
616 "destination is too small to receive data from source");
617 PyBuffer_Release(&view_dest);
618 PyBuffer_Release(&view_src);
619 return -1;
620 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
623 PyBuffer_IsContiguous(&view_src, 'C')) ||
624 (PyBuffer_IsContiguous(&view_dest, 'F') &&
625 PyBuffer_IsContiguous(&view_src, 'F'))) {
626 /* simplest copy is all that is needed */
627 memcpy(view_dest.buf, view_src.buf, view_src.len);
628 PyBuffer_Release(&view_dest);
629 PyBuffer_Release(&view_src);
630 return 0;
631 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* XXX(nnorwitz): need to check for overflow! */
636 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
637 if (indices == NULL) {
638 PyErr_NoMemory();
639 PyBuffer_Release(&view_dest);
640 PyBuffer_Release(&view_src);
641 return -1;
642 }
643 for (k=0; k<view_src.ndim;k++) {
644 indices[k] = 0;
645 }
646 elements = 1;
647 for (k=0; k<view_src.ndim; k++) {
648 /* XXX(nnorwitz): can this overflow? */
649 elements *= view_src.shape[k];
650 }
651 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000652 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 dptr = PyBuffer_GetPointer(&view_dest, indices);
654 sptr = PyBuffer_GetPointer(&view_src, indices);
655 memcpy(dptr, sptr, view_src.itemsize);
656 }
657 PyMem_Free(indices);
658 PyBuffer_Release(&view_dest);
659 PyBuffer_Release(&view_src);
660 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000661}
662
663void
664PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 Py_ssize_t *strides, int itemsize,
666 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 int k;
669 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 sd = itemsize;
672 if (fort == 'F') {
673 for (k=0; k<nd; k++) {
674 strides[k] = sd;
675 sd *= shape[k];
676 }
677 }
678 else {
679 for (k=nd-1; k>=0; k--) {
680 strides[k] = sd;
681 sd *= shape[k];
682 }
683 }
684 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000685}
686
687int
Martin v. Löwis423be952008-08-13 15:53:07 +0000688PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Stefan Krah4e141742012-03-06 15:27:31 +0100689 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000690{
Stefan Krah5178d912015-02-03 16:57:21 +0100691 if (view == NULL) {
692 PyErr_SetString(PyExc_BufferError,
693 "PyBuffer_FillInfo: view==NULL argument is obsolete");
694 return -1;
695 }
696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
698 (readonly == 1)) {
699 PyErr_SetString(PyExc_BufferError,
700 "Object is not writable.");
701 return -1;
702 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 view->obj = obj;
705 if (obj)
706 Py_INCREF(obj);
707 view->buf = buf;
708 view->len = len;
709 view->readonly = readonly;
710 view->itemsize = 1;
711 view->format = NULL;
712 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
713 view->format = "B";
714 view->ndim = 1;
715 view->shape = NULL;
716 if ((flags & PyBUF_ND) == PyBUF_ND)
717 view->shape = &(view->len);
718 view->strides = NULL;
719 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
720 view->strides = &(view->itemsize);
721 view->suboffsets = NULL;
722 view->internal = NULL;
723 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000724}
725
Martin v. Löwis423be952008-08-13 15:53:07 +0000726void
727PyBuffer_Release(Py_buffer *view)
728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 PyObject *obj = view->obj;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200730 PyBufferProcs *pb;
731 if (obj == NULL)
732 return;
733 pb = Py_TYPE(obj)->tp_as_buffer;
734 if (pb && pb->bf_releasebuffer)
735 pb->bf_releasebuffer(obj, view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 view->obj = NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200737 Py_DECREF(obj);
Martin v. Löwis423be952008-08-13 15:53:07 +0000738}
739
Eric Smith8fd3eba2008-02-17 19:48:00 +0000740PyObject *
741PyObject_Format(PyObject *obj, PyObject *format_spec)
742{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000743 PyObject *meth;
744 PyObject *empty = NULL;
745 PyObject *result = NULL;
Benjamin Petersonce798522012-01-22 11:24:29 -0500746 _Py_IDENTIFIER(__format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000747
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300748 if (format_spec != NULL && !PyUnicode_Check(format_spec)) {
749 PyErr_Format(PyExc_SystemError,
750 "Format specifier must be a string, not %.200s",
751 Py_TYPE(format_spec)->tp_name);
752 return NULL;
753 }
754
755 /* Fast path for common types. */
756 if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
757 if (PyUnicode_CheckExact(obj)) {
758 Py_INCREF(obj);
759 return obj;
760 }
761 if (PyLong_CheckExact(obj)) {
762 return PyObject_Str(obj);
763 }
764 }
765
Eric Smith8fd3eba2008-02-17 19:48:00 +0000766 /* If no format_spec is provided, use an empty string */
767 if (format_spec == NULL) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100768 empty = PyUnicode_New(0, 0);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000769 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000770 }
771
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300772 /* Find the (unbound!) __format__ method */
Benjamin Petersonce798522012-01-22 11:24:29 -0500773 meth = _PyObject_LookupSpecial(obj, &PyId___format__);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000774 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000775 if (!PyErr_Occurred())
776 PyErr_Format(PyExc_TypeError,
777 "Type %.100s doesn't define __format__",
778 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000780 }
781
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000782 /* And call it. */
Petr Viktorinffd97532020-02-11 17:46:57 +0100783 result = PyObject_CallOneArg(meth, format_spec);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000784 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000785
786 if (result && !PyUnicode_Check(result)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800787 PyErr_Format(PyExc_TypeError,
788 "__format__ must return a str, not %.200s",
789 Py_TYPE(result)->tp_name);
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000790 Py_DECREF(result);
791 result = NULL;
792 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000793 }
794
795done:
796 Py_XDECREF(empty);
797 return result;
798}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000799/* Operations on numbers */
800
801int
Fred Drake79912472000-07-09 04:06:11 +0000802PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000803{
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100804 return o && Py_TYPE(o)->tp_as_number &&
805 (Py_TYPE(o)->tp_as_number->nb_index ||
806 Py_TYPE(o)->tp_as_number->nb_int ||
807 Py_TYPE(o)->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000808}
809
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000810/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000811
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000812#define NB_SLOT(x) offsetof(PyNumberMethods, x)
813#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000815#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000817
818/*
819 Calling scheme used for binary operations:
820
Neal Norwitz4886cc32006-08-21 17:06:07 +0000821 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000823
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100824 [*] only when Py_TYPE(v) != Py_TYPE(w) && Py_TYPE(w) is a subclass of
825 Py_TYPE(v)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000826 */
827
828static PyObject *
829binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyObject *x;
832 binaryfunc slotv = NULL;
833 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000834
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100835 if (Py_TYPE(v)->tp_as_number != NULL)
836 slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot);
Andy Lester55728702020-03-06 16:53:17 -0600837 if (!Py_IS_TYPE(w, Py_TYPE(v)) &&
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100838 Py_TYPE(w)->tp_as_number != NULL) {
839 slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (slotw == slotv)
841 slotw = NULL;
842 }
843 if (slotv) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100844 if (slotw && PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 x = slotw(v, w);
846 if (x != Py_NotImplemented)
847 return x;
848 Py_DECREF(x); /* can't do it */
849 slotw = NULL;
850 }
851 x = slotv(v, w);
852 if (x != Py_NotImplemented)
853 return x;
854 Py_DECREF(x); /* can't do it */
855 }
856 if (slotw) {
857 x = slotw(v, w);
858 if (x != Py_NotImplemented)
859 return x;
860 Py_DECREF(x); /* can't do it */
861 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500862 Py_RETURN_NOTIMPLEMENTED;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000863}
Guido van Rossum77660912002-04-16 16:32:50 +0000864
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000865static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000866binop_type_error(PyObject *v, PyObject *w, const char *op_name)
867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PyErr_Format(PyExc_TypeError,
869 "unsupported operand type(s) for %.100s: "
870 "'%.100s' and '%.100s'",
871 op_name,
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100872 Py_TYPE(v)->tp_name,
873 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000875}
876
877static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000878binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *result = binary_op1(v, w, op_slot);
881 if (result == Py_NotImplemented) {
882 Py_DECREF(result);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530883
884 if (op_slot == NB_SLOT(nb_rshift) &&
885 PyCFunction_Check(v) &&
886 strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
887 {
888 PyErr_Format(PyExc_TypeError,
889 "unsupported operand type(s) for %.100s: "
890 "'%.100s' and '%.100s'. Did you mean \"print(<message>, "
Sanyam Khuranaa7c449b2017-08-18 17:48:14 +0530891 "file=<output_stream>)\"?",
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530892 op_name,
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100893 Py_TYPE(v)->tp_name,
894 Py_TYPE(w)->tp_name);
Sanyam Khurana5e2eb352017-08-18 16:07:36 +0530895 return NULL;
896 }
897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 return binop_type_error(v, w, op_name);
899 }
900 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000901}
902
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000903
904/*
905 Calling scheme used for ternary operations:
906
Neal Norwitz4886cc32006-08-21 17:06:07 +0000907 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000909 */
910
911static PyObject *
912ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *w,
914 PyObject *z,
915 const int op_slot,
916 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyNumberMethods *mv, *mw, *mz;
919 PyObject *x = NULL;
920 ternaryfunc slotv = NULL;
921 ternaryfunc slotw = NULL;
922 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000923
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100924 mv = Py_TYPE(v)->tp_as_number;
925 mw = Py_TYPE(w)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (mv != NULL)
927 slotv = NB_TERNOP(mv, op_slot);
Andy Lester55728702020-03-06 16:53:17 -0600928 if (!Py_IS_TYPE(w, Py_TYPE(v)) && mw != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 slotw = NB_TERNOP(mw, op_slot);
930 if (slotw == slotv)
931 slotw = NULL;
932 }
933 if (slotv) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100934 if (slotw && PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 x = slotw(v, w, z);
936 if (x != Py_NotImplemented)
937 return x;
938 Py_DECREF(x); /* can't do it */
939 slotw = NULL;
940 }
941 x = slotv(v, w, z);
942 if (x != Py_NotImplemented)
943 return x;
944 Py_DECREF(x); /* can't do it */
945 }
946 if (slotw) {
947 x = slotw(v, w, z);
948 if (x != Py_NotImplemented)
949 return x;
950 Py_DECREF(x); /* can't do it */
951 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100952 mz = Py_TYPE(z)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 if (mz != NULL) {
954 slotz = NB_TERNOP(mz, op_slot);
955 if (slotz == slotv || slotz == slotw)
956 slotz = NULL;
957 if (slotz) {
958 x = slotz(v, w, z);
959 if (x != Py_NotImplemented)
960 return x;
961 Py_DECREF(x); /* can't do it */
962 }
963 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (z == Py_None)
966 PyErr_Format(
967 PyExc_TypeError,
968 "unsupported operand type(s) for ** or pow(): "
969 "'%.100s' and '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100970 Py_TYPE(v)->tp_name,
971 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 else
973 PyErr_Format(
974 PyExc_TypeError,
975 "unsupported operand type(s) for pow(): "
976 "'%.100s', '%.100s', '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +0100977 Py_TYPE(v)->tp_name,
978 Py_TYPE(w)->tp_name,
979 Py_TYPE(z)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000981}
982
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000983#define BINARY_FUNC(func, op, op_name) \
984 PyObject * \
985 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000987 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000988
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000989BINARY_FUNC(PyNumber_Or, nb_or, "|")
990BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
991BINARY_FUNC(PyNumber_And, nb_and, "&")
992BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
993BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
994BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000995BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000996
997PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000998PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
1001 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001002 PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_DECREF(result);
1004 if (m && m->sq_concat) {
1005 return (*m->sq_concat)(v, w);
1006 }
1007 result = binop_type_error(v, w, "+");
1008 }
1009 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +00001010}
1011
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001012static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001013sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 Py_ssize_t count;
1016 if (PyIndex_Check(n)) {
1017 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
1018 if (count == -1 && PyErr_Occurred())
1019 return NULL;
1020 }
1021 else {
1022 return type_error("can't multiply sequence by "
1023 "non-int of type '%.200s'", n);
1024 }
1025 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001026}
1027
1028PyObject *
1029PyNumber_Multiply(PyObject *v, PyObject *w)
1030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
1032 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001033 PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
1034 PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 Py_DECREF(result);
1036 if (mv && mv->sq_repeat) {
1037 return sequence_repeat(mv->sq_repeat, v, w);
1038 }
1039 else if (mw && mw->sq_repeat) {
1040 return sequence_repeat(mw->sq_repeat, w, v);
1041 }
1042 result = binop_type_error(v, w, "*");
1043 }
1044 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001045}
1046
Guido van Rossume15dee51995-07-18 14:12:02 +00001047PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001048PyNumber_MatrixMultiply(PyObject *v, PyObject *w)
1049{
1050 return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@");
1051}
1052
1053PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001054PyNumber_FloorDivide(PyObject *v, PyObject *w)
1055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +00001057}
1058
1059PyObject *
1060PyNumber_TrueDivide(PyObject *v, PyObject *w)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +00001063}
1064
1065PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001066PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +00001067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +00001069}
1070
1071PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001072PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +00001073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +00001075}
1076
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001077/* Binary in-place operators */
1078
1079/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001080 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001081
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001082 - If the left hand object has the appropriate struct members, and
1083 they are filled, call the appropriate function and return the
1084 result. No coercion is done on the arguments; the left-hand object
1085 is the one the operation is performed on, and it's up to the
1086 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001087
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001088 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001089 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001090
1091 */
1092
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001093static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001094binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001095{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001096 PyNumberMethods *mv = Py_TYPE(v)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (mv != NULL) {
1098 binaryfunc slot = NB_BINOP(mv, iop_slot);
1099 if (slot) {
1100 PyObject *x = (slot)(v, w);
1101 if (x != Py_NotImplemented) {
1102 return x;
1103 }
1104 Py_DECREF(x);
1105 }
1106 }
1107 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001108}
1109
1110static PyObject *
1111binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1115 if (result == Py_NotImplemented) {
1116 Py_DECREF(result);
1117 return binop_type_error(v, w, op_name);
1118 }
1119 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001120}
1121
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001122#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 PyObject * \
1124 func(PyObject *v, PyObject *w) { \
1125 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1126 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001127
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001128INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1129INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1130INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1131INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1132INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1133INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Benjamin Petersond51374e2014-04-09 23:55:56 -04001134INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001135
1136PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001137PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1140 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001141}
1142
1143PyObject *
1144PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1147 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001148}
1149
1150PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001151PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1154 NB_SLOT(nb_add));
1155 if (result == Py_NotImplemented) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001156 PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 Py_DECREF(result);
1158 if (m != NULL) {
1159 binaryfunc f = NULL;
1160 f = m->sq_inplace_concat;
1161 if (f == NULL)
1162 f = m->sq_concat;
1163 if (f != NULL)
1164 return (*f)(v, w);
1165 }
1166 result = binop_type_error(v, w, "+=");
1167 }
1168 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001169}
1170
1171PyObject *
1172PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1175 NB_SLOT(nb_multiply));
1176 if (result == Py_NotImplemented) {
1177 ssizeargfunc f = NULL;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001178 PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
1179 PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 Py_DECREF(result);
1181 if (mv != NULL) {
1182 f = mv->sq_inplace_repeat;
1183 if (f == NULL)
1184 f = mv->sq_repeat;
1185 if (f != NULL)
1186 return sequence_repeat(f, v, w);
1187 }
1188 else if (mw != NULL) {
1189 /* Note that the right hand operand should not be
1190 * mutated in this case so sq_inplace_repeat is not
1191 * used. */
1192 if (mw->sq_repeat)
1193 return sequence_repeat(mw->sq_repeat, w, v);
1194 }
1195 result = binop_type_error(v, w, "*=");
1196 }
1197 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001198}
1199
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001200PyObject *
Benjamin Petersond51374e2014-04-09 23:55:56 -04001201PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1202{
1203 return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1204 NB_SLOT(nb_matrix_multiply), "@=");
1205}
1206
1207PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001208PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1211 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001212}
1213
1214PyObject *
1215PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1216{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001217 if (Py_TYPE(v)->tp_as_number &&
1218 Py_TYPE(v)->tp_as_number->nb_inplace_power != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1220 }
1221 else {
1222 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1223 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001224}
1225
1226
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001227/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001228
1229PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001230PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001233
Victor Stinner71aea8e2016-08-19 16:59:55 +02001234 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001236 }
1237
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001238 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (m && m->nb_negative)
1240 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001243}
1244
1245PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001246PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001249
Victor Stinner71aea8e2016-08-19 16:59:55 +02001250 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001252 }
1253
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001254 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (m && m->nb_positive)
1256 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001259}
1260
1261PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001262PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001265
Victor Stinner71aea8e2016-08-19 16:59:55 +02001266 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001268 }
1269
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001270 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if (m && m->nb_invert)
1272 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001275}
1276
1277PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001278PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001281
Victor Stinner71aea8e2016-08-19 16:59:55 +02001282 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001284 }
1285
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001286 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (m && m->nb_absolute)
1288 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001291}
1292
Christian Tismerea62ce72018-06-09 20:32:25 +02001293#undef PyIndex_Check
Christian Tismer83987132018-06-11 00:48:28 +02001294
Christian Tismerea62ce72018-06-09 20:32:25 +02001295int
1296PyIndex_Check(PyObject *obj)
1297{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001298 return Py_TYPE(obj)->tp_as_number != NULL &&
1299 Py_TYPE(obj)->tp_as_number->nb_index != NULL;
Christian Tismerea62ce72018-06-09 20:32:25 +02001300}
1301
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001302/* Return a Python int from the object item.
Serhiy Storchaka95949422013-08-27 19:40:23 +03001303 Raise TypeError if the result is not an int
Guido van Rossum98297ee2007-11-06 21:34:58 +00001304 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001305*/
1306PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001307PyNumber_Index(PyObject *item)
1308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 PyObject *result = NULL;
Victor Stinner71aea8e2016-08-19 16:59:55 +02001310 if (item == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001312 }
1313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (PyLong_Check(item)) {
1315 Py_INCREF(item);
1316 return item;
1317 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001318 if (!PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 PyErr_Format(PyExc_TypeError,
1320 "'%.200s' object cannot be interpreted "
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001321 "as an integer", Py_TYPE(item)->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001322 return NULL;
1323 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001324 result = Py_TYPE(item)->tp_as_number->nb_index(item);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001325 if (!result || PyLong_CheckExact(result))
1326 return result;
1327 if (!PyLong_Check(result)) {
1328 PyErr_Format(PyExc_TypeError,
1329 "__index__ returned non-int (type %.200s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001330 Py_TYPE(result)->tp_name);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001331 Py_DECREF(result);
1332 return NULL;
1333 }
1334 /* Issue #17576: warn if 'result' not of exact type int. */
1335 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1336 "__index__ returned non-int (type %.200s). "
1337 "The ability to return an instance of a strict subclass of int "
1338 "is deprecated, and may be removed in a future version of Python.",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001339 Py_TYPE(result)->tp_name)) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001340 Py_DECREF(result);
1341 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 }
1343 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001344}
1345
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001346/* Return an error on Overflow only if err is not NULL*/
1347
1348Py_ssize_t
1349PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 Py_ssize_t result;
1352 PyObject *runerr;
1353 PyObject *value = PyNumber_Index(item);
1354 if (value == NULL)
1355 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* We're done if PyLong_AsSsize_t() returns without error. */
1358 result = PyLong_AsSsize_t(value);
1359 if (result != -1 || !(runerr = PyErr_Occurred()))
1360 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 /* Error handling code -- only manage OverflowError differently */
1363 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1364 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 PyErr_Clear();
1367 /* If no error-handling desired then the default clipping
1368 is sufficient.
1369 */
1370 if (!err) {
1371 assert(PyLong_Check(value));
1372 /* Whether or not it is less than or equal to
1373 zero is determined by the sign of ob_size
1374 */
1375 if (_PyLong_Sign(value) < 0)
1376 result = PY_SSIZE_T_MIN;
1377 else
1378 result = PY_SSIZE_T_MAX;
1379 }
1380 else {
1381 /* Otherwise replace the error with caller's error object. */
1382 PyErr_Format(err,
1383 "cannot fit '%.200s' into an index-sized integer",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001384 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001386
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001387 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_DECREF(value);
1389 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001390}
1391
1392
Guido van Rossume15dee51995-07-18 14:12:02 +00001393PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001394PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001395{
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001396 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyNumberMethods *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyObject *trunc_func;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001399 Py_buffer view;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04001400 _Py_IDENTIFIER(__trunc__);
Christian Heimes15ebc882008-02-04 18:48:49 +00001401
Victor Stinner71aea8e2016-08-19 16:59:55 +02001402 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001404 }
1405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (PyLong_CheckExact(o)) {
1407 Py_INCREF(o);
1408 return o;
1409 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001410 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (m && m->nb_int) { /* This should include subclasses of int */
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001412 result = _PyLong_FromNbInt(o);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001413 if (result != NULL && !PyLong_CheckExact(result)) {
1414 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1415 }
1416 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 }
Serhiy Storchakabdbad712019-06-02 00:05:48 +03001418 if (m && m->nb_index) {
1419 result = _PyLong_FromNbIndexOrNbInt(o);
1420 if (result != NULL && !PyLong_CheckExact(result)) {
1421 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1422 }
1423 return result;
1424 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001425 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (trunc_func) {
INADA Naoki72dccde2017-02-16 09:26:01 +09001427 result = _PyObject_CallNoArg(trunc_func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 Py_DECREF(trunc_func);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001429 if (result == NULL || PyLong_CheckExact(result)) {
1430 return result;
1431 }
1432 if (PyLong_Check(result)) {
1433 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1434 return result;
1435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* __trunc__ is specified to return an Integral type,
Martin Panter7462b6492015-11-02 03:37:02 +00001437 but int() needs to return an int. */
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001438 m = Py_TYPE(result)->tp_as_number;
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001439 if (m == NULL || (m->nb_index == NULL && m->nb_int == NULL)) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001440 PyErr_Format(
1441 PyExc_TypeError,
1442 "__trunc__ returned non-Integral (type %.200s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001443 Py_TYPE(result)->tp_name);
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001444 Py_DECREF(result);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001445 return NULL;
1446 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001447 Py_SETREF(result, _PyLong_FromNbIndexOrNbInt(result));
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001448 if (result != NULL && !PyLong_CheckExact(result)) {
1449 Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1450 }
1451 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 }
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04001453 if (PyErr_Occurred())
1454 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001455
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001456 if (PyUnicode_Check(o))
1457 /* The below check is done in PyLong_FromUnicode(). */
1458 return PyLong_FromUnicodeObject(o, 10);
1459
Martin Pantereeb896c2015-11-07 02:32:21 +00001460 if (PyBytes_Check(o))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 /* need to do extra error checking that PyLong_FromString()
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03001462 * doesn't do. In particular int('9\x005') must raise an
1463 * exception, not truncate at the null.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 */
Martin Pantereeb896c2015-11-07 02:32:21 +00001465 return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1466 PyBytes_GET_SIZE(o), 10);
1467
1468 if (PyByteArray_Check(o))
1469 return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1470 PyByteArray_GET_SIZE(o), 10);
1471
1472 if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
Serhiy Storchaka54cd1962016-08-21 20:03:08 +03001473 PyObject *bytes;
Martin Pantereeb896c2015-11-07 02:32:21 +00001474
1475 /* Copy to NUL-terminated buffer. */
1476 bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1477 if (bytes == NULL) {
1478 PyBuffer_Release(&view);
1479 return NULL;
1480 }
1481 result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1482 PyBytes_GET_SIZE(bytes), 10);
1483 Py_DECREF(bytes);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001484 PyBuffer_Release(&view);
1485 return result;
1486 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001487
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001488 return type_error("int() argument must be a string, a bytes-like object "
1489 "or a number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001490}
1491
1492PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001493PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001496
Victor Stinner71aea8e2016-08-19 16:59:55 +02001497 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001499 }
1500
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001501 if (PyFloat_CheckExact(o)) {
1502 Py_INCREF(o);
1503 return o;
1504 }
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001505 m = Py_TYPE(o)->tp_as_number;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (m && m->nb_float) { /* This should include subclasses of float */
1507 PyObject *res = m->nb_float(o);
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001508 double val;
1509 if (!res || PyFloat_CheckExact(res)) {
1510 return res;
1511 }
1512 if (!PyFloat_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001514 "%.50s.__float__ returned non-float (type %.50s)",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001515 Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 Py_DECREF(res);
1517 return NULL;
1518 }
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001519 /* Issue #26983: warn if 'res' not of exact type float. */
1520 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1521 "%.50s.__float__ returned non-float (type %.50s). "
1522 "The ability to return an instance of a strict subclass of float "
1523 "is deprecated, and may be removed in a future version of Python.",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001524 Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)) {
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001525 Py_DECREF(res);
1526 return NULL;
1527 }
1528 val = PyFloat_AS_DOUBLE(res);
1529 Py_DECREF(res);
1530 return PyFloat_FromDouble(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 }
Serhiy Storchakabdbad712019-06-02 00:05:48 +03001532 if (m && m->nb_index) {
1533 PyObject *res = PyNumber_Index(o);
1534 if (!res) {
1535 return NULL;
1536 }
1537 double val = PyLong_AsDouble(res);
1538 Py_DECREF(res);
1539 if (val == -1.0 && PyErr_Occurred()) {
1540 return NULL;
1541 }
1542 return PyFloat_FromDouble(val);
1543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
Serhiy Storchaka16931c32016-06-03 21:42:55 +03001545 return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 }
1547 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001548}
1549
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001550
1551PyObject *
1552PyNumber_ToBase(PyObject *n, int base)
1553{
Serhiy Storchakae5ccc942020-03-09 20:03:38 +02001554 if (!(base == 2 || base == 8 || base == 10 || base == 16)) {
1555 PyErr_SetString(PyExc_SystemError,
1556 "PyNumber_ToBase: base must be 2, 8, 10 or 16");
1557 return NULL;
1558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 PyObject *index = PyNumber_Index(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (!index)
1561 return NULL;
Serhiy Storchakae5ccc942020-03-09 20:03:38 +02001562 PyObject *res = _PyLong_Format(index, base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 Py_DECREF(index);
1564 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001565}
1566
1567
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001568/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001569
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001570int
Fred Drake79912472000-07-09 04:06:11 +00001571PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (PyDict_Check(s))
1574 return 0;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001575 return Py_TYPE(s)->tp_as_sequence &&
1576 Py_TYPE(s)->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001577}
1578
Martin v. Löwis18e16552006-02-15 17:27:45 +00001579Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001580PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 if (s == NULL) {
1585 null_error();
1586 return -1;
1587 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001588
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001589 m = Py_TYPE(s)->tp_as_sequence;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001590 if (m && m->sq_length) {
1591 Py_ssize_t len = m->sq_length(s);
1592 assert(len >= 0 || PyErr_Occurred());
1593 return len;
1594 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001595
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001596 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_length) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001597 type_error("%.200s is not a sequence", s);
1598 return -1;
1599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 type_error("object of type '%.200s' has no len()", s);
1601 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001602}
1603
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001604#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001605Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001606PySequence_Length(PyObject *s)
1607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001609}
1610#define PySequence_Length PySequence_Size
1611
Guido van Rossume15dee51995-07-18 14:12:02 +00001612PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001613PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001616
Victor Stinner71aea8e2016-08-19 16:59:55 +02001617 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001619 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001620
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001621 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (m && m->sq_concat)
1623 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 /* Instances of user classes defining an __add__() method only
1626 have an nb_add slot, not an sq_concat slot. So we fall back
1627 to nb_add if both arguments appear to be sequences. */
1628 if (PySequence_Check(s) && PySequence_Check(o)) {
1629 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1630 if (result != Py_NotImplemented)
1631 return result;
1632 Py_DECREF(result);
1633 }
1634 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001635}
1636
1637PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001638PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001641
Victor Stinner71aea8e2016-08-19 16:59:55 +02001642 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001644 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001645
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001646 m = Py_TYPE(o)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (m && m->sq_repeat)
1648 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 /* Instances of user classes defining a __mul__() method only
1651 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1652 to nb_multiply if o appears to be a sequence. */
1653 if (PySequence_Check(o)) {
1654 PyObject *n, *result;
1655 n = PyLong_FromSsize_t(count);
1656 if (n == NULL)
1657 return NULL;
1658 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1659 Py_DECREF(n);
1660 if (result != Py_NotImplemented)
1661 return result;
1662 Py_DECREF(result);
1663 }
1664 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001665}
1666
1667PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001668PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001671
Victor Stinner71aea8e2016-08-19 16:59:55 +02001672 if (s == NULL || o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001674 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001675
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001676 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (m && m->sq_inplace_concat)
1678 return m->sq_inplace_concat(s, o);
1679 if (m && m->sq_concat)
1680 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (PySequence_Check(s) && PySequence_Check(o)) {
1683 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1684 NB_SLOT(nb_add));
1685 if (result != Py_NotImplemented)
1686 return result;
1687 Py_DECREF(result);
1688 }
1689 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001690}
1691
1692PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001693PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001696
Victor Stinner71aea8e2016-08-19 16:59:55 +02001697 if (o == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001699 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001700
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001701 m = Py_TYPE(o)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 if (m && m->sq_inplace_repeat)
1703 return m->sq_inplace_repeat(o, count);
1704 if (m && m->sq_repeat)
1705 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (PySequence_Check(o)) {
1708 PyObject *n, *result;
1709 n = PyLong_FromSsize_t(count);
1710 if (n == NULL)
1711 return NULL;
1712 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1713 NB_SLOT(nb_multiply));
1714 Py_DECREF(n);
1715 if (result != Py_NotImplemented)
1716 return result;
1717 Py_DECREF(result);
1718 }
1719 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001720}
1721
1722PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001723PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001726
Victor Stinner71aea8e2016-08-19 16:59:55 +02001727 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001729 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001730
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001731 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 if (m && m->sq_item) {
1733 if (i < 0) {
1734 if (m->sq_length) {
1735 Py_ssize_t l = (*m->sq_length)(s);
Victor Stinnere20310f2015-11-05 13:56:58 +01001736 if (l < 0) {
1737 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 return NULL;
Victor Stinnere20310f2015-11-05 13:56:58 +01001739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 i += l;
1741 }
1742 }
1743 return m->sq_item(s, i);
1744 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001745
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001746 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001747 return type_error("%.200s is not a sequence", s);
1748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001750}
1751
1752PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001753PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001756
Victor Stinner71aea8e2016-08-19 16:59:55 +02001757 if (!s) {
1758 return null_error();
1759 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001760
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001761 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001762 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 PyObject *res;
1764 PyObject *slice = _PySlice_FromIndices(i1, i2);
1765 if (!slice)
1766 return NULL;
1767 res = mp->mp_subscript(s, slice);
1768 Py_DECREF(slice);
1769 return res;
1770 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001773}
1774
1775int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001776PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 if (s == NULL) {
1781 null_error();
1782 return -1;
1783 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001784
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001785 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (m && m->sq_ass_item) {
1787 if (i < 0) {
1788 if (m->sq_length) {
1789 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001790 if (l < 0) {
1791 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 i += l;
1795 }
1796 }
1797 return m->sq_ass_item(s, i, o);
1798 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001799
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001800 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001801 type_error("%.200s is not a sequence", s);
1802 return -1;
1803 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 type_error("'%.200s' object does not support item assignment", s);
1805 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001806}
1807
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001808int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001809PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 if (s == NULL) {
1814 null_error();
1815 return -1;
1816 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001817
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001818 m = Py_TYPE(s)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (m && m->sq_ass_item) {
1820 if (i < 0) {
1821 if (m->sq_length) {
1822 Py_ssize_t l = (*m->sq_length)(s);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001823 if (l < 0) {
1824 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 return -1;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 i += l;
1828 }
1829 }
1830 return m->sq_ass_item(s, i, (PyObject *)NULL);
1831 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001832
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001833 if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03001834 type_error("%.200s is not a sequence", s);
1835 return -1;
1836 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 type_error("'%.200s' object doesn't support item deletion", s);
1838 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001839}
1840
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001841int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001842PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (s == NULL) {
1847 null_error();
1848 return -1;
1849 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001850
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001851 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001852 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 int res;
1854 PyObject *slice = _PySlice_FromIndices(i1, i2);
1855 if (!slice)
1856 return -1;
1857 res = mp->mp_ass_subscript(s, slice, o);
1858 Py_DECREF(slice);
1859 return res;
1860 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 type_error("'%.200s' object doesn't support slice assignment", s);
1863 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001864}
1865
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001866int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001867PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 if (s == NULL) {
1872 null_error();
1873 return -1;
1874 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001875
Victor Stinner0d76d2b2020-02-07 01:53:23 +01001876 mp = Py_TYPE(s)->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001877 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 int res;
1879 PyObject *slice = _PySlice_FromIndices(i1, i2);
1880 if (!slice)
1881 return -1;
1882 res = mp->mp_ass_subscript(s, slice, NULL);
1883 Py_DECREF(slice);
1884 return res;
1885 }
1886 type_error("'%.200s' object doesn't support slice deletion", s);
1887 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001888}
1889
Guido van Rossume15dee51995-07-18 14:12:02 +00001890PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001891PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyObject *it; /* iter(v) */
1894 Py_ssize_t n; /* guess for result tuple size */
1895 PyObject *result = NULL;
1896 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001897
Victor Stinner71aea8e2016-08-19 16:59:55 +02001898 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001900 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* Special-case the common tuple and list cases, for efficiency. */
1903 if (PyTuple_CheckExact(v)) {
1904 /* Note that we can't know whether it's safe to return
1905 a tuple *subclass* instance as-is, hence the restriction
1906 to exact tuples here. In contrast, lists always make
1907 a copy, so there's no need for exactness below. */
1908 Py_INCREF(v);
1909 return v;
1910 }
Raymond Hettinger610a51f2015-05-17 14:45:58 -07001911 if (PyList_CheckExact(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 /* Get iterator. */
1915 it = PyObject_GetIter(v);
1916 if (it == NULL)
1917 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 /* Guess result size and allocate space. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02001920 n = PyObject_LengthHint(v, 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (n == -1)
1922 goto Fail;
1923 result = PyTuple_New(n);
1924 if (result == NULL)
1925 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 /* Fill the tuple. */
1928 for (j = 0; ; ++j) {
1929 PyObject *item = PyIter_Next(it);
1930 if (item == NULL) {
1931 if (PyErr_Occurred())
1932 goto Fail;
1933 break;
1934 }
1935 if (j >= n) {
Martin Pantere8db8612016-07-25 02:30:05 +00001936 size_t newn = (size_t)n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* The over-allocation strategy can grow a bit faster
1938 than for lists because unlike lists the
1939 over-allocation isn't permanent -- we reclaim
1940 the excess before the end of this routine.
1941 So, grow by ten and then add 25%.
1942 */
Martin Pantere8db8612016-07-25 02:30:05 +00001943 newn += 10u;
1944 newn += newn >> 2;
1945 if (newn > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 /* Check for overflow */
1947 PyErr_NoMemory();
1948 Py_DECREF(item);
1949 goto Fail;
1950 }
Martin Pantere8db8612016-07-25 02:30:05 +00001951 n = (Py_ssize_t)newn;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (_PyTuple_Resize(&result, n) != 0) {
1953 Py_DECREF(item);
1954 goto Fail;
1955 }
1956 }
1957 PyTuple_SET_ITEM(result, j, item);
1958 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 /* Cut tuple back if guess was too large. */
1961 if (j < n &&
1962 _PyTuple_Resize(&result, j) != 0)
1963 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 Py_DECREF(it);
1966 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001967
1968Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 Py_XDECREF(result);
1970 Py_DECREF(it);
1971 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001972}
1973
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001974PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001975PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 PyObject *result; /* result list */
1978 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001979
Victor Stinner71aea8e2016-08-19 16:59:55 +02001980 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02001982 }
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 result = PyList_New(0);
1985 if (result == NULL)
1986 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 rv = _PyList_Extend((PyListObject *)result, v);
1989 if (rv == NULL) {
1990 Py_DECREF(result);
1991 return NULL;
1992 }
1993 Py_DECREF(rv);
1994 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001995}
1996
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001997PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001998PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00002001
Victor Stinner71aea8e2016-08-19 16:59:55 +02002002 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002004 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
2007 Py_INCREF(v);
2008 return v;
2009 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 it = PyObject_GetIter(v);
2012 if (it == NULL) {
2013 if (PyErr_ExceptionMatches(PyExc_TypeError))
2014 PyErr_SetString(PyExc_TypeError, m);
2015 return NULL;
2016 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 v = PySequence_List(it);
2019 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00002022}
2023
Tim Peters16a77ad2001-09-08 04:00:12 +00002024/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
2026 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
2027 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00002028 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
2029*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002030Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002031_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00002032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 Py_ssize_t n;
2034 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
2035 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (seq == NULL || obj == NULL) {
2038 null_error();
2039 return -1;
2040 }
Tim Peters75f8e352001-05-05 11:33:43 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 it = PyObject_GetIter(seq);
2043 if (it == NULL) {
2044 type_error("argument of type '%.200s' is not iterable", seq);
2045 return -1;
2046 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 n = wrapped = 0;
2049 for (;;) {
2050 int cmp;
2051 PyObject *item = PyIter_Next(it);
2052 if (item == NULL) {
2053 if (PyErr_Occurred())
2054 goto Fail;
2055 break;
2056 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002057
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03002058 cmp = PyObject_RichCompareBool(item, obj, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 Py_DECREF(item);
2060 if (cmp < 0)
2061 goto Fail;
2062 if (cmp > 0) {
2063 switch (operation) {
2064 case PY_ITERSEARCH_COUNT:
2065 if (n == PY_SSIZE_T_MAX) {
2066 PyErr_SetString(PyExc_OverflowError,
2067 "count exceeds C integer size");
2068 goto Fail;
2069 }
2070 ++n;
2071 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 case PY_ITERSEARCH_INDEX:
2074 if (wrapped) {
2075 PyErr_SetString(PyExc_OverflowError,
2076 "index exceeds C integer size");
2077 goto Fail;
2078 }
2079 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 case PY_ITERSEARCH_CONTAINS:
2082 n = 1;
2083 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07002086 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 }
2088 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (operation == PY_ITERSEARCH_INDEX) {
2091 if (n == PY_SSIZE_T_MAX)
2092 wrapped = 1;
2093 ++n;
2094 }
2095 }
Tim Peters16a77ad2001-09-08 04:00:12 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (operation != PY_ITERSEARCH_INDEX)
2098 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00002099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 PyErr_SetString(PyExc_ValueError,
2101 "sequence.index(x): x not in sequence");
2102 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00002103Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 n = -1;
2105 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00002106Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 Py_DECREF(it);
2108 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00002109
Guido van Rossume15dee51995-07-18 14:12:02 +00002110}
2111
Tim Peters16a77ad2001-09-08 04:00:12 +00002112/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002113Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00002114PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00002117}
2118
Tim Peterscb8d3682001-05-05 21:05:01 +00002119/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00002120 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00002121 */
2122int
2123PySequence_Contains(PyObject *seq, PyObject *ob)
2124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 Py_ssize_t result;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002126 PySequenceMethods *sqm = Py_TYPE(seq)->tp_as_sequence;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 if (sqm != NULL && sqm->sq_contains != NULL)
2128 return (*sqm->sq_contains)(seq, ob);
2129 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
2130 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00002131}
2132
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002133/* Backwards compatibility */
2134#undef PySequence_In
2135int
Fred Drake79912472000-07-09 04:06:11 +00002136PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002139}
2140
Neal Norwitz1fc4b772006-03-04 18:49:58 +00002141Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00002142PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00002145}
2146
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002147/* Operations on mappings */
2148
2149int
Fred Drake79912472000-07-09 04:06:11 +00002150PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002151{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002152 return o && Py_TYPE(o)->tp_as_mapping &&
2153 Py_TYPE(o)->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00002154}
2155
Martin v. Löwis18e16552006-02-15 17:27:45 +00002156Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00002157PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 if (o == NULL) {
2162 null_error();
2163 return -1;
2164 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002165
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002166 m = Py_TYPE(o)->tp_as_mapping;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03002167 if (m && m->mp_length) {
2168 Py_ssize_t len = m->mp_length(o);
2169 assert(len >= 0 || PyErr_Occurred());
2170 return len;
2171 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002172
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002173 if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) {
Serhiy Storchakaa6fdddb2018-07-23 23:43:42 +03002174 type_error("%.200s is not a mapping", o);
2175 return -1;
2176 }
2177 /* PyMapping_Size() can be called from PyObject_Size(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 type_error("object of type '%.200s' has no len()", o);
2179 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002180}
2181
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002182#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002183Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002184PyMapping_Length(PyObject *o)
2185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002187}
2188#define PyMapping_Length PyMapping_Size
2189
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002190PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002191PyMapping_GetItemString(PyObject *o, const char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002194
Victor Stinner71aea8e2016-08-19 16:59:55 +02002195 if (key == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 return null_error();
Victor Stinner71aea8e2016-08-19 16:59:55 +02002197 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 okey = PyUnicode_FromString(key);
2200 if (okey == NULL)
2201 return NULL;
2202 r = PyObject_GetItem(o, okey);
2203 Py_DECREF(okey);
2204 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002205}
2206
2207int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002208PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 PyObject *okey;
2211 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 if (key == NULL) {
2214 null_error();
2215 return -1;
2216 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 okey = PyUnicode_FromString(key);
2219 if (okey == NULL)
2220 return -1;
2221 r = PyObject_SetItem(o, okey, value);
2222 Py_DECREF(okey);
2223 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002224}
2225
2226int
Serhiy Storchakac6792272013-10-19 21:03:34 +03002227PyMapping_HasKeyString(PyObject *o, const char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 v = PyMapping_GetItemString(o, key);
2232 if (v) {
2233 Py_DECREF(v);
2234 return 1;
2235 }
2236 PyErr_Clear();
2237 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002238}
2239
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002240int
Fred Drake79912472000-07-09 04:06:11 +00002241PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 v = PyObject_GetItem(o, key);
2246 if (v) {
2247 Py_DECREF(v);
2248 return 1;
2249 }
2250 PyErr_Clear();
2251 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002252}
2253
Oren Milman0ccc0f62017-10-08 11:17:46 +03002254/* This function is quite similar to PySequence_Fast(), but specialized to be
2255 a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values().
2256 */
2257static PyObject *
2258method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
2259{
2260 PyObject *it, *result, *meth_output;
2261
2262 assert(o != NULL);
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002263 meth_output = _PyObject_CallMethodIdNoArgs(o, meth_id);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002264 if (meth_output == NULL || PyList_CheckExact(meth_output)) {
2265 return meth_output;
2266 }
2267 it = PyObject_GetIter(meth_output);
2268 if (it == NULL) {
2269 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
2270 PyErr_Format(PyExc_TypeError,
2271 "%.200s.%U() returned a non-iterable (type %.200s)",
2272 Py_TYPE(o)->tp_name,
2273 meth_id->object,
2274 Py_TYPE(meth_output)->tp_name);
2275 }
2276 Py_DECREF(meth_output);
2277 return NULL;
2278 }
2279 Py_DECREF(meth_output);
2280 result = PySequence_List(it);
2281 Py_DECREF(it);
2282 return result;
2283}
2284
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002285PyObject *
2286PyMapping_Keys(PyObject *o)
2287{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002288 _Py_IDENTIFIER(keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002289
Oren Milman0ccc0f62017-10-08 11:17:46 +03002290 if (o == NULL) {
2291 return null_error();
2292 }
2293 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return PyDict_Keys(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002295 }
2296 return method_output_as_list(o, &PyId_keys);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002297}
2298
2299PyObject *
2300PyMapping_Items(PyObject *o)
2301{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002302 _Py_IDENTIFIER(items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002303
Oren Milman0ccc0f62017-10-08 11:17:46 +03002304 if (o == NULL) {
2305 return null_error();
2306 }
2307 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 return PyDict_Items(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002309 }
2310 return method_output_as_list(o, &PyId_items);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002311}
2312
2313PyObject *
2314PyMapping_Values(PyObject *o)
2315{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002316 _Py_IDENTIFIER(values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002317
Oren Milman0ccc0f62017-10-08 11:17:46 +03002318 if (o == NULL) {
2319 return null_error();
2320 }
2321 if (PyDict_CheckExact(o)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 return PyDict_Values(o);
Oren Milman0ccc0f62017-10-08 11:17:46 +03002323 }
2324 return method_output_as_list(o, &PyId_values);
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002325}
2326
Guido van Rossum823649d2001-03-21 18:40:58 +00002327/* isinstance(), issubclass() */
2328
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002329/* abstract_get_bases() has logically 4 return states:
Barry Warsawf16951c2002-04-23 22:45:44 +00002330 *
Barry Warsawf16951c2002-04-23 22:45:44 +00002331 * 1. getattr(cls, '__bases__') could raise an AttributeError
2332 * 2. getattr(cls, '__bases__') could raise some other exception
2333 * 3. getattr(cls, '__bases__') could return a tuple
2334 * 4. getattr(cls, '__bases__') could return something other than a tuple
2335 *
2336 * Only state #3 is a non-error state and only it returns a non-NULL object
2337 * (it returns the retrieved tuple).
2338 *
2339 * Any raised AttributeErrors are masked by clearing the exception and
2340 * returning NULL. If an object other than a tuple comes out of __bases__,
2341 * then again, the return value is NULL. So yes, these two situations
2342 * produce exactly the same results: NULL is returned and no error is set.
2343 *
2344 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002346 * exception to be propagated along.
2347 *
2348 * Callers are expected to test for PyErr_Occurred() when the return value
2349 * is NULL to decide whether a valid exception should be propagated or not.
2350 * When there's no exception to propagate, it's customary for the caller to
2351 * set a TypeError.
2352 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002353static PyObject *
2354abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002355{
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002356 _Py_IDENTIFIER(__bases__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 Py_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002360 (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 Py_END_ALLOW_RECURSION
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002362 if (bases != NULL && !PyTuple_Check(bases)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_DECREF(bases);
2364 return NULL;
2365 }
2366 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002367}
2368
2369
2370static int
2371abstract_issubclass(PyObject *derived, PyObject *cls)
2372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 PyObject *bases = NULL;
2374 Py_ssize_t i, n;
2375 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 while (1) {
Yonatan Goldschmidt1c56f8f2020-02-22 15:11:48 +02002378 if (derived == cls) {
2379 Py_XDECREF(bases); /* See below comment */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 return 1;
Yonatan Goldschmidt1c56f8f2020-02-22 15:11:48 +02002381 }
2382 /* Use XSETREF to drop bases reference *after* finishing with
2383 derived; bases might be the only reference to it.
2384 XSETREF is used instead of SETREF, because bases is NULL on the
2385 first iteration of the loop.
2386 */
2387 Py_XSETREF(bases, abstract_get_bases(derived));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 if (bases == NULL) {
2389 if (PyErr_Occurred())
2390 return -1;
2391 return 0;
2392 }
2393 n = PyTuple_GET_SIZE(bases);
2394 if (n == 0) {
2395 Py_DECREF(bases);
2396 return 0;
2397 }
2398 /* Avoid recursivity in the single inheritance case */
2399 if (n == 1) {
2400 derived = PyTuple_GET_ITEM(bases, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 continue;
2402 }
2403 for (i = 0; i < n; i++) {
2404 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2405 if (r != 0)
2406 break;
2407 }
2408 Py_DECREF(bases);
2409 return r;
2410 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002411}
2412
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002413static int
2414check_class(PyObject *cls, const char *error)
2415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 PyObject *bases = abstract_get_bases(cls);
2417 if (bases == NULL) {
2418 /* Do not mask errors. */
2419 if (!PyErr_Occurred())
2420 PyErr_SetString(PyExc_TypeError, error);
2421 return 0;
2422 }
2423 Py_DECREF(bases);
2424 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002425}
2426
Brett Cannon4f653312004-03-20 22:52:14 +00002427static int
Victor Stinner850a4bd2020-02-04 13:42:13 +01002428object_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 PyObject *icls;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002431 int retval;
Benjamin Peterson9fc9bf42012-03-20 23:26:41 -04002432 _Py_IDENTIFIER(__class__);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 if (PyType_Check(cls)) {
2435 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2436 if (retval == 0) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002437 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2438 if (icls != NULL) {
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002439 if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 retval = PyType_IsSubtype(
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002441 (PyTypeObject *)icls,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 (PyTypeObject *)cls);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002443 }
2444 else {
2445 retval = 0;
2446 }
2447 Py_DECREF(icls);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 }
2449 }
2450 }
2451 else {
2452 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002453 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 return -1;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002455 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2456 if (icls != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 retval = abstract_issubclass(icls, cls);
2458 Py_DECREF(icls);
2459 }
2460 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002463}
2464
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002465static int
Victor Stinner850a4bd2020-02-04 13:42:13 +01002466object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
Brett Cannon4f653312004-03-20 22:52:14 +00002467{
Benjamin Petersonce798522012-01-22 11:24:29 -05002468 _Py_IDENTIFIER(__instancecheck__);
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Quick test for an exact match */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002471 if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 return 1;
Victor Stinner850a4bd2020-02-04 13:42:13 +01002473 }
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002474
Georg Brandl72b8a802014-10-03 09:26:37 +02002475 /* We know what type's __instancecheck__ does. */
2476 if (PyType_CheckExact(cls)) {
Victor Stinner850a4bd2020-02-04 13:42:13 +01002477 return object_isinstance(inst, cls);
Georg Brandl72b8a802014-10-03 09:26:37 +02002478 }
2479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 if (PyTuple_Check(cls)) {
Victor Stinner850a4bd2020-02-04 13:42:13 +01002481 /* Not a general sequence -- that opens up the road to
2482 recursion and stack overflow. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002483 if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002485 }
2486 Py_ssize_t n = PyTuple_GET_SIZE(cls);
2487 int r = 0;
2488 for (Py_ssize_t i = 0; i < n; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 PyObject *item = PyTuple_GET_ITEM(cls, i);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002490 r = object_recursive_isinstance(tstate, inst, item);
2491 if (r != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* either found it, or got an error */
2493 break;
Victor Stinner850a4bd2020-02-04 13:42:13 +01002494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002496 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 return r;
2498 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002499
Victor Stinner850a4bd2020-02-04 13:42:13 +01002500 PyObject *checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 if (checker != NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002502 if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 Py_DECREF(checker);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002504 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 }
Victor Stinner850a4bd2020-02-04 13:42:13 +01002506
Petr Viktorinffd97532020-02-11 17:46:57 +01002507 PyObject *res = PyObject_CallOneArg(checker, inst);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002508 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 Py_DECREF(checker);
Victor Stinner850a4bd2020-02-04 13:42:13 +01002510
2511 if (res == NULL) {
2512 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 }
Victor Stinner850a4bd2020-02-04 13:42:13 +01002514 int ok = PyObject_IsTrue(res);
2515 Py_DECREF(res);
2516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 return ok;
2518 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002519 else if (_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002521 }
2522
Victor Stinner850a4bd2020-02-04 13:42:13 +01002523 /* cls has no __instancecheck__() method */
2524 return object_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002525}
2526
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002527
2528int
2529PyObject_IsInstance(PyObject *inst, PyObject *cls)
2530{
2531 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner850a4bd2020-02-04 13:42:13 +01002532 return object_recursive_isinstance(tstate, inst, cls);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002533}
2534
2535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002537recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (PyType_Check(cls) && PyType_Check(derived)) {
2540 /* Fast path (non-recursive) */
2541 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2542 }
2543 if (!check_class(derived,
2544 "issubclass() arg 1 must be a class"))
2545 return -1;
2546 if (!check_class(cls,
2547 "issubclass() arg 2 must be a class"
2548 " or tuple of classes"))
2549 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002552}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002553
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002554static int
2555object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
Brett Cannon4f653312004-03-20 22:52:14 +00002556{
Benjamin Petersonce798522012-01-22 11:24:29 -05002557 _Py_IDENTIFIER(__subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002559
Georg Brandl72b8a802014-10-03 09:26:37 +02002560 /* We know what type's __subclasscheck__ does. */
2561 if (PyType_CheckExact(cls)) {
2562 /* Quick test for an exact match */
2563 if (derived == cls)
2564 return 1;
2565 return recursive_issubclass(derived, cls);
2566 }
2567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 if (PyTuple_Check(cls)) {
Antoine Pitrouec569b72008-08-26 22:40:48 +00002569
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002570 if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002572 }
2573 Py_ssize_t n = PyTuple_GET_SIZE(cls);
2574 int r = 0;
2575 for (Py_ssize_t i = 0; i < n; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 PyObject *item = PyTuple_GET_ITEM(cls, i);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002577 r = object_issubclass(tstate, derived, item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 if (r != 0)
2579 /* either found it, or got an error */
2580 break;
2581 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002582 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 return r;
2584 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002585
Benjamin Petersonce798522012-01-22 11:24:29 -05002586 checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (checker != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 int ok = -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002589 if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 Py_DECREF(checker);
2591 return ok;
2592 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002593 PyObject *res = PyObject_CallOneArg(checker, derived);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002594 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 Py_DECREF(checker);
2596 if (res != NULL) {
2597 ok = PyObject_IsTrue(res);
2598 Py_DECREF(res);
2599 }
2600 return ok;
2601 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002602 else if (_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 return -1;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002604 }
2605
Georg Brandl72b8a802014-10-03 09:26:37 +02002606 /* Probably never reached anymore. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002608}
2609
Victor Stinnerbe434dc2019-11-05 00:51:22 +01002610
2611int
2612PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2613{
2614 PyThreadState *tstate = _PyThreadState_GET();
2615 return object_issubclass(tstate, derived, cls);
2616}
2617
2618
Antoine Pitrouec569b72008-08-26 22:40:48 +00002619int
2620_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2621{
Victor Stinner850a4bd2020-02-04 13:42:13 +01002622 return object_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002623}
2624
2625int
2626_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002629}
2630
2631
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002632PyObject *
2633PyObject_GetIter(PyObject *o)
2634{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002635 PyTypeObject *t = Py_TYPE(o);
Victor Stinner14e6d092016-12-09 17:08:59 +01002636 getiterfunc f;
2637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 f = t->tp_iter;
2639 if (f == NULL) {
2640 if (PySequence_Check(o))
2641 return PySeqIter_New(o);
2642 return type_error("'%.200s' object is not iterable", o);
2643 }
2644 else {
2645 PyObject *res = (*f)(o);
2646 if (res != NULL && !PyIter_Check(res)) {
2647 PyErr_Format(PyExc_TypeError,
2648 "iter() returned non-iterator "
2649 "of type '%.100s'",
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002650 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 Py_DECREF(res);
2652 res = NULL;
2653 }
2654 return res;
2655 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002656}
2657
Christian Tismerea62ce72018-06-09 20:32:25 +02002658#undef PyIter_Check
Christian Tismer83987132018-06-11 00:48:28 +02002659
Christian Tismerea62ce72018-06-09 20:32:25 +02002660int PyIter_Check(PyObject *obj)
2661{
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002662 return Py_TYPE(obj)->tp_iternext != NULL &&
2663 Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented;
Christian Tismerea62ce72018-06-09 20:32:25 +02002664}
2665
Tim Petersf4848da2001-05-05 00:14:56 +00002666/* Return next item.
2667 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2668 * If the iteration terminates normally, return NULL and clear the
2669 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2670 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002672 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002673PyObject *
2674PyIter_Next(PyObject *iter)
2675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyObject *result;
Victor Stinner0d76d2b2020-02-07 01:53:23 +01002677 result = (*Py_TYPE(iter)->tp_iternext)(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 if (result == NULL &&
2679 PyErr_Occurred() &&
2680 PyErr_ExceptionMatches(PyExc_StopIteration))
2681 PyErr_Clear();
2682 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002683}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002684
2685
2686/*
2687 * Flatten a sequence of bytes() objects into a C array of
2688 * NULL terminated string pointers with a NULL char* terminating the array.
2689 * (ie: an argv or env list)
2690 *
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002691 * Memory allocated for the returned list is allocated using PyMem_Malloc()
2692 * and MUST be freed by _Py_FreeCharPArray().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002693 */
2694char *const *
2695_PySequence_BytesToCharpArray(PyObject* self)
2696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 char **array;
2698 Py_ssize_t i, argc;
2699 PyObject *item = NULL;
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002700 Py_ssize_t size;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 argc = PySequence_Size(self);
2703 if (argc == -1)
2704 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002705
Stefan Krah7cacd2e2012-08-21 08:16:09 +02002706 assert(argc >= 0);
2707
2708 if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2709 PyErr_NoMemory();
2710 return NULL;
2711 }
2712
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002713 array = PyMem_Malloc((argc + 1) * sizeof(char *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 if (array == NULL) {
2715 PyErr_NoMemory();
2716 return NULL;
2717 }
2718 for (i = 0; i < argc; ++i) {
2719 char *data;
2720 item = PySequence_GetItem(self, i);
Stefan Krahfd24f9e2012-08-20 11:04:24 +02002721 if (item == NULL) {
2722 /* NULL terminate before freeing. */
2723 array[i] = NULL;
2724 goto fail;
2725 }
Serhiy Storchakad174d242017-06-23 19:39:27 +03002726 /* check for embedded null bytes */
2727 if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 /* NULL terminate before freeing. */
2729 array[i] = NULL;
2730 goto fail;
2731 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002732 size = PyBytes_GET_SIZE(item) + 1;
2733 array[i] = PyMem_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 if (!array[i]) {
2735 PyErr_NoMemory();
2736 goto fail;
2737 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002738 memcpy(array[i], data, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 Py_DECREF(item);
2740 }
2741 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002744
2745fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 Py_XDECREF(item);
2747 _Py_FreeCharPArray(array);
2748 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002749}
2750
2751
2752/* Free's a NULL terminated char** array of C strings. */
2753void
2754_Py_FreeCharPArray(char *const array[])
2755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 Py_ssize_t i;
2757 for (i = 0; array[i] != NULL; ++i) {
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002758 PyMem_Free(array[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
Victor Stinner0e2d3cf2013-07-07 17:22:41 +02002760 PyMem_Free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002761}