blob: f992573d332eb225287b9bb661c747d6ea7495a0 [file] [log] [blame]
Guido van Rossume15dee51995-07-18 14:12:02 +00001/* Abstract Object Interface (many thanks to Jim Fulton) */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002
Guido van Rossume15dee51995-07-18 14:12:02 +00003#include "Python.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00004#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00005#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00006#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00009
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000010/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000011
12static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013type_error(const char *msg, PyObject *obj)
Guido van Rossume15dee51995-07-18 14:12:02 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
16 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000017}
18
Guido van Rossum052b7e11996-11-11 15:08:19 +000019static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000020null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (!PyErr_Occurred())
23 PyErr_SetString(PyExc_SystemError,
24 "null argument to internal routine");
25 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000026}
27
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000028/* Operations on any object */
29
Guido van Rossume15dee51995-07-18 14:12:02 +000030PyObject *
Fred Drake79912472000-07-09 04:06:11 +000031PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 if (o == NULL)
36 return null_error();
37 v = (PyObject *)o->ob_type;
38 Py_INCREF(v);
39 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +000040}
41
Martin v. Löwis18e16552006-02-15 17:27:45 +000042Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +000043PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 if (o == NULL) {
48 null_error();
49 return -1;
50 }
Guido van Rossume15dee51995-07-18 14:12:02 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 m = o->ob_type->tp_as_sequence;
53 if (m && m->sq_length)
54 return m->sq_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000057}
58
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000059#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +000060Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000061PyObject_Length(PyObject *o)
62{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 return PyObject_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000064}
65#define PyObject_Length PyObject_Size
66
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000067
Christian Heimes255f53b2007-12-08 15:33:56 +000068/* The length hint function returns a non-negative value from o.__len__()
69 or o.__length_hint__(). If those methods aren't found or return a negative
Raymond Hettingere8364232009-02-02 22:55:09 +000070 value, then the defaultvalue is returned. If one of the calls fails,
71 this function returns -1.
Christian Heimes255f53b2007-12-08 15:33:56 +000072*/
73
74Py_ssize_t
75_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
76{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 static PyObject *hintstrobj = NULL;
78 PyObject *ro, *hintmeth;
79 Py_ssize_t rv;
Christian Heimes255f53b2007-12-08 15:33:56 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 /* try o.__len__() */
82 rv = PyObject_Size(o);
83 if (rv >= 0)
84 return rv;
85 if (PyErr_Occurred()) {
86 if (!PyErr_ExceptionMatches(PyExc_TypeError))
87 return -1;
88 PyErr_Clear();
89 }
Christian Heimes255f53b2007-12-08 15:33:56 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 /* try o.__length_hint__() */
92 hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
93 if (hintmeth == NULL) {
94 if (PyErr_Occurred())
95 return -1;
96 else
97 return defaultvalue;
98 }
99 ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
100 Py_DECREF(hintmeth);
101 if (ro == NULL) {
102 if (!PyErr_ExceptionMatches(PyExc_TypeError))
103 return -1;
104 PyErr_Clear();
105 return defaultvalue;
106 }
107 rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
108 Py_DECREF(ro);
109 return rv;
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000110}
111
Guido van Rossume15dee51995-07-18 14:12:02 +0000112PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000113PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (o == NULL || key == NULL)
118 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 m = o->ob_type->tp_as_mapping;
121 if (m && m->mp_subscript)
122 return m->mp_subscript(o, key);
Guido van Rossume15dee51995-07-18 14:12:02 +0000123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 if (o->ob_type->tp_as_sequence) {
125 if (PyIndex_Check(key)) {
126 Py_ssize_t key_value;
127 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
128 if (key_value == -1 && PyErr_Occurred())
129 return NULL;
130 return PySequence_GetItem(o, key_value);
131 }
132 else if (o->ob_type->tp_as_sequence->sq_item)
133 return type_error("sequence index must "
134 "be integer, not '%.200s'", key);
135 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 return type_error("'%.200s' object is not subscriptable", o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000138}
139
140int
Fred Drake79912472000-07-09 04:06:11 +0000141PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (o == NULL || key == NULL || value == NULL) {
146 null_error();
147 return -1;
148 }
149 m = o->ob_type->tp_as_mapping;
150 if (m && m->mp_ass_subscript)
151 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (o->ob_type->tp_as_sequence) {
154 if (PyIndex_Check(key)) {
155 Py_ssize_t key_value;
156 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
157 if (key_value == -1 && PyErr_Occurred())
158 return -1;
159 return PySequence_SetItem(o, key_value, value);
160 }
161 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
162 type_error("sequence index must be "
163 "integer, not '%.200s'", key);
164 return -1;
165 }
166 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 type_error("'%.200s' object does not support item assignment", o);
169 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000170}
171
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000172int
Fred Drake79912472000-07-09 04:06:11 +0000173PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (o == NULL || key == NULL) {
178 null_error();
179 return -1;
180 }
181 m = o->ob_type->tp_as_mapping;
182 if (m && m->mp_ass_subscript)
183 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (o->ob_type->tp_as_sequence) {
186 if (PyIndex_Check(key)) {
187 Py_ssize_t key_value;
188 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
189 if (key_value == -1 && PyErr_Occurred())
190 return -1;
191 return PySequence_DelItem(o, key_value);
192 }
193 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
194 type_error("sequence index must be "
195 "integer, not '%.200s'", key);
196 return -1;
197 }
198 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 type_error("'%.200s' object does not support item deletion", o);
201 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000202}
203
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000204int
205PyObject_DelItemString(PyObject *o, char *key)
206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 PyObject *okey;
208 int ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (o == NULL || key == NULL) {
211 null_error();
212 return -1;
213 }
214 okey = PyUnicode_FromString(key);
215 if (okey == NULL)
216 return -1;
217 ret = PyObject_DelItem(o, okey);
218 Py_DECREF(okey);
219 return ret;
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000220}
221
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000222/* We release the buffer right after use of this function which could
Guido van Rossum98297ee2007-11-06 21:34:58 +0000223 cause issues later on. Don't use these functions in new code.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000224 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000225int
226PyObject_AsCharBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 const char **buffer,
228 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 PyBufferProcs *pb;
231 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
234 null_error();
235 return -1;
236 }
237 pb = obj->ob_type->tp_as_buffer;
238 if (pb == NULL || pb->bf_getbuffer == NULL) {
239 PyErr_SetString(PyExc_TypeError,
Victor Stinnerf2a94212011-05-30 23:21:30 +0200240 "expected bytes, bytearray "
241 "or buffer compatible object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return -1;
243 }
244 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 *buffer = view.buf;
247 *buffer_len = view.len;
248 if (pb->bf_releasebuffer != NULL)
249 (*pb->bf_releasebuffer)(obj, &view);
250 Py_XDECREF(view.obj);
251 return 0;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000252}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000253
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000254int
255PyObject_CheckReadBuffer(PyObject *obj)
256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
258 Py_buffer view;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (pb == NULL ||
261 pb->bf_getbuffer == NULL)
262 return 0;
263 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
264 PyErr_Clear();
265 return 0;
266 }
267 PyBuffer_Release(&view);
268 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000269}
270
271int PyObject_AsReadBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 const void **buffer,
273 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyBufferProcs *pb;
276 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
279 null_error();
280 return -1;
281 }
282 pb = obj->ob_type->tp_as_buffer;
283 if (pb == NULL ||
284 pb->bf_getbuffer == NULL) {
285 PyErr_SetString(PyExc_TypeError,
286 "expected an object with a buffer interface");
287 return -1;
288 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 *buffer = view.buf;
293 *buffer_len = view.len;
294 if (pb->bf_releasebuffer != NULL)
295 (*pb->bf_releasebuffer)(obj, &view);
296 Py_XDECREF(view.obj);
297 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000298}
299
300int PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 void **buffer,
302 Py_ssize_t *buffer_len)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 PyBufferProcs *pb;
305 Py_buffer view;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
308 null_error();
309 return -1;
310 }
311 pb = obj->ob_type->tp_as_buffer;
312 if (pb == NULL ||
313 pb->bf_getbuffer == NULL ||
314 ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
315 PyErr_SetString(PyExc_TypeError,
316 "expected an object with a writable buffer interface");
317 return -1;
318 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 *buffer = view.buf;
321 *buffer_len = view.len;
322 if (pb->bf_releasebuffer != NULL)
323 (*pb->bf_releasebuffer)(obj, &view);
324 Py_XDECREF(view.obj);
325 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000326}
327
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000328/* Buffer C-API for Python 3.0 */
329
330int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000331PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (!PyObject_CheckBuffer(obj)) {
334 PyErr_Format(PyExc_TypeError,
Victor Stinner0a5f65a2011-03-22 01:09:21 +0100335 "'%.100s' does not support the buffer interface",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 Py_TYPE(obj)->tp_name);
337 return -1;
338 }
339 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000340}
341
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000342static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000343_IsFortranContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_ssize_t sd, dim;
346 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (view->ndim == 0) return 1;
349 if (view->strides == NULL) return (view->ndim == 1);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 sd = view->itemsize;
352 if (view->ndim == 1) return (view->shape[0] == 1 ||
353 sd == view->strides[0]);
354 for (i=0; i<view->ndim; i++) {
355 dim = view->shape[i];
356 if (dim == 0) return 1;
357 if (view->strides[i] != sd) return 0;
358 sd *= dim;
359 }
360 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000361}
362
363static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000364_IsCContiguous(Py_buffer *view)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 Py_ssize_t sd, dim;
367 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (view->ndim == 0) return 1;
370 if (view->strides == NULL) return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 sd = view->itemsize;
373 if (view->ndim == 1) return (view->shape[0] == 1 ||
374 sd == view->strides[0]);
375 for (i=view->ndim-1; i>=0; i--) {
376 dim = view->shape[i];
377 if (dim == 0) return 1;
378 if (view->strides[i] != sd) return 0;
379 sd *= dim;
380 }
381 return 1;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000382}
383
384int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000385PyBuffer_IsContiguous(Py_buffer *view, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000386{
387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (view->suboffsets != NULL) return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (fort == 'C')
391 return _IsCContiguous(view);
392 else if (fort == 'F')
393 return _IsFortranContiguous(view);
394 else if (fort == 'A')
395 return (_IsCContiguous(view) || _IsFortranContiguous(view));
396 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000397}
398
399
Guido van Rossum98297ee2007-11-06 21:34:58 +0000400void*
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000401PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 char* pointer;
404 int i;
405 pointer = (char *)view->buf;
406 for (i = 0; i < view->ndim; i++) {
407 pointer += view->strides[i]*indices[i];
408 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
409 pointer = *((char**)pointer) + view->suboffsets[i];
410 }
411 }
412 return (void*)pointer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000413}
414
415
Guido van Rossum98297ee2007-11-06 21:34:58 +0000416void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000417_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 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 int k;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 for (k=0; k<nd; k++) {
422 if (index[k] < shape[k]-1) {
423 index[k]++;
424 break;
425 }
426 else {
427 index[k] = 0;
428 }
429 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000430}
431
Guido van Rossum98297ee2007-11-06 21:34:58 +0000432void
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000433_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 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 int k;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 for (k=nd-1; k>=0; k--) {
438 if (index[k] < shape[k]-1) {
439 index[k]++;
440 break;
441 }
442 else {
443 index[k] = 0;
444 }
445 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000446}
447
448 /* view is not checked for consistency in either of these. It is
Guido van Rossum98297ee2007-11-06 21:34:58 +0000449 assumed that the size of the buffer is view->len in
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000450 view->len / view->itemsize elements.
451 */
452
Guido van Rossum98297ee2007-11-06 21:34:58 +0000453int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000454PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000457 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_ssize_t *indices, elements;
459 char *dest, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (len > view->len) {
462 len = view->len;
463 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (PyBuffer_IsContiguous(view, fort)) {
466 /* simplest copy is all that is needed */
467 memcpy(buf, view->buf, len);
468 return 0;
469 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 /* XXX(nnorwitz): need to check for overflow! */
474 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
475 if (indices == NULL) {
476 PyErr_NoMemory();
477 return -1;
478 }
479 for (k=0; k<view->ndim;k++) {
480 indices[k] = 0;
481 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000484 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 }
486 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000487 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 }
489 dest = buf;
490 /* XXX : This is not going to be the fastest code in the world
491 several optimizations are possible.
492 */
493 elements = len / view->itemsize;
494 while (elements--) {
495 addone(view->ndim, indices, view->shape);
496 ptr = PyBuffer_GetPointer(view, indices);
497 memcpy(dest, ptr, view->itemsize);
498 dest += view->itemsize;
499 }
500 PyMem_Free(indices);
501 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000502}
503
504int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000505PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 int k;
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000508 void (*addone)(int, Py_ssize_t *, const Py_ssize_t *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_ssize_t *indices, elements;
510 char *src, *ptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (len > view->len) {
513 len = view->len;
514 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (PyBuffer_IsContiguous(view, fort)) {
517 /* simplest copy is all that is needed */
518 memcpy(view->buf, buf, len);
519 return 0;
520 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* Otherwise a more elaborate scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 /* XXX(nnorwitz): need to check for overflow! */
525 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
526 if (indices == NULL) {
527 PyErr_NoMemory();
528 return -1;
529 }
530 for (k=0; k<view->ndim;k++) {
531 indices[k] = 0;
532 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (fort == 'F') {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000535 addone = _Py_add_one_to_index_F;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
537 else {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000538 addone = _Py_add_one_to_index_C;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
540 src = buf;
541 /* XXX : This is not going to be the fastest code in the world
542 several optimizations are possible.
543 */
544 elements = len / view->itemsize;
545 while (elements--) {
546 addone(view->ndim, indices, view->shape);
547 ptr = PyBuffer_GetPointer(view, indices);
548 memcpy(ptr, src, view->itemsize);
549 src += view->itemsize;
550 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 PyMem_Free(indices);
553 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000554}
555
Guido van Rossum98297ee2007-11-06 21:34:58 +0000556int PyObject_CopyData(PyObject *dest, PyObject *src)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 Py_buffer view_dest, view_src;
559 int k;
560 Py_ssize_t *indices, elements;
561 char *dptr, *sptr;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (!PyObject_CheckBuffer(dest) ||
564 !PyObject_CheckBuffer(src)) {
565 PyErr_SetString(PyExc_TypeError,
566 "both destination and source must have the "\
567 "buffer interface");
568 return -1;
569 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
572 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
573 PyBuffer_Release(&view_dest);
574 return -1;
575 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (view_dest.len < view_src.len) {
578 PyErr_SetString(PyExc_BufferError,
579 "destination is too small to receive data from source");
580 PyBuffer_Release(&view_dest);
581 PyBuffer_Release(&view_src);
582 return -1;
583 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
586 PyBuffer_IsContiguous(&view_src, 'C')) ||
587 (PyBuffer_IsContiguous(&view_dest, 'F') &&
588 PyBuffer_IsContiguous(&view_src, 'F'))) {
589 /* simplest copy is all that is needed */
590 memcpy(view_dest.buf, view_src.buf, view_src.len);
591 PyBuffer_Release(&view_dest);
592 PyBuffer_Release(&view_src);
593 return 0;
594 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 /* Otherwise a more elaborate copy scheme is needed */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* XXX(nnorwitz): need to check for overflow! */
599 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
600 if (indices == NULL) {
601 PyErr_NoMemory();
602 PyBuffer_Release(&view_dest);
603 PyBuffer_Release(&view_src);
604 return -1;
605 }
606 for (k=0; k<view_src.ndim;k++) {
607 indices[k] = 0;
608 }
609 elements = 1;
610 for (k=0; k<view_src.ndim; k++) {
611 /* XXX(nnorwitz): can this overflow? */
612 elements *= view_src.shape[k];
613 }
614 while (elements--) {
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000615 _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 dptr = PyBuffer_GetPointer(&view_dest, indices);
617 sptr = PyBuffer_GetPointer(&view_src, indices);
618 memcpy(dptr, sptr, view_src.itemsize);
619 }
620 PyMem_Free(indices);
621 PyBuffer_Release(&view_dest);
622 PyBuffer_Release(&view_src);
623 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000624}
625
626void
627PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 Py_ssize_t *strides, int itemsize,
629 char fort)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 int k;
632 Py_ssize_t sd;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 sd = itemsize;
635 if (fort == 'F') {
636 for (k=0; k<nd; k++) {
637 strides[k] = sd;
638 sd *= shape[k];
639 }
640 }
641 else {
642 for (k=nd-1; k>=0; k--) {
643 strides[k] = sd;
644 sd *= shape[k];
645 }
646 }
647 return;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000648}
649
650int
Martin v. Löwis423be952008-08-13 15:53:07 +0000651PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 int readonly, int flags)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 if (view == NULL) return 0;
655 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
656 (readonly == 1)) {
657 PyErr_SetString(PyExc_BufferError,
658 "Object is not writable.");
659 return -1;
660 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 view->obj = obj;
663 if (obj)
664 Py_INCREF(obj);
665 view->buf = buf;
666 view->len = len;
667 view->readonly = readonly;
668 view->itemsize = 1;
669 view->format = NULL;
670 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
671 view->format = "B";
672 view->ndim = 1;
673 view->shape = NULL;
674 if ((flags & PyBUF_ND) == PyBUF_ND)
675 view->shape = &(view->len);
676 view->strides = NULL;
677 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
678 view->strides = &(view->itemsize);
679 view->suboffsets = NULL;
680 view->internal = NULL;
681 return 0;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000682}
683
Martin v. Löwis423be952008-08-13 15:53:07 +0000684void
685PyBuffer_Release(Py_buffer *view)
686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyObject *obj = view->obj;
688 if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
689 Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
690 Py_XDECREF(obj);
691 view->obj = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000692}
693
Eric Smith8fd3eba2008-02-17 19:48:00 +0000694PyObject *
695PyObject_Format(PyObject *obj, PyObject *format_spec)
696{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000697 PyObject *meth;
698 PyObject *empty = NULL;
699 PyObject *result = NULL;
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000700 static PyObject *format_cache = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000701
702 /* If no format_spec is provided, use an empty string */
703 if (format_spec == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000704 empty = PyUnicode_FromUnicode(NULL, 0);
705 format_spec = empty;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000706 }
707
Eric Smith8fd3eba2008-02-17 19:48:00 +0000708 /* Find the (unbound!) __format__ method (a borrowed reference) */
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000709 meth = _PyObject_LookupSpecial(obj, "__format__", &format_cache);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000710 if (meth == NULL) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000711 if (!PyErr_Occurred())
712 PyErr_Format(PyExc_TypeError,
713 "Type %.100s doesn't define __format__",
714 Py_TYPE(obj)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000716 }
717
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000718 /* And call it. */
719 result = PyObject_CallFunctionObjArgs(meth, format_spec, NULL);
Benjamin Peterson6f889ad32010-06-05 02:11:45 +0000720 Py_DECREF(meth);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000721
722 if (result && !PyUnicode_Check(result)) {
Benjamin Petersonda2cf042010-06-05 00:45:37 +0000723 PyErr_SetString(PyExc_TypeError,
724 "__format__ method did not return string");
725 Py_DECREF(result);
726 result = NULL;
727 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000728 }
729
730done:
731 Py_XDECREF(empty);
732 return result;
733}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000734/* Operations on numbers */
735
736int
Fred Drake79912472000-07-09 04:06:11 +0000737PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 return o && o->ob_type->tp_as_number &&
740 (o->ob_type->tp_as_number->nb_int ||
741 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000742}
743
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000744/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000745
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000746#define NB_SLOT(x) offsetof(PyNumberMethods, x)
747#define NB_BINOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000749#define NB_TERNOP(nb_methods, slot) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000751
752/*
753 Calling scheme used for binary operations:
754
Neal Norwitz4886cc32006-08-21 17:06:07 +0000755 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000757
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000758 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
759 v->ob_type
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000760 */
761
762static PyObject *
763binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyObject *x;
766 binaryfunc slotv = NULL;
767 binaryfunc slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (v->ob_type->tp_as_number != NULL)
770 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
771 if (w->ob_type != v->ob_type &&
772 w->ob_type->tp_as_number != NULL) {
773 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
774 if (slotw == slotv)
775 slotw = NULL;
776 }
777 if (slotv) {
778 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
779 x = slotw(v, w);
780 if (x != Py_NotImplemented)
781 return x;
782 Py_DECREF(x); /* can't do it */
783 slotw = NULL;
784 }
785 x = slotv(v, w);
786 if (x != Py_NotImplemented)
787 return x;
788 Py_DECREF(x); /* can't do it */
789 }
790 if (slotw) {
791 x = slotw(v, w);
792 if (x != Py_NotImplemented)
793 return x;
794 Py_DECREF(x); /* can't do it */
795 }
796 Py_INCREF(Py_NotImplemented);
797 return Py_NotImplemented;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000798}
Guido van Rossum77660912002-04-16 16:32:50 +0000799
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000800static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000801binop_type_error(PyObject *v, PyObject *w, const char *op_name)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PyErr_Format(PyExc_TypeError,
804 "unsupported operand type(s) for %.100s: "
805 "'%.100s' and '%.100s'",
806 op_name,
807 v->ob_type->tp_name,
808 w->ob_type->tp_name);
809 return NULL;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000810}
811
812static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000813binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PyObject *result = binary_op1(v, w, op_slot);
816 if (result == Py_NotImplemented) {
817 Py_DECREF(result);
818 return binop_type_error(v, w, op_name);
819 }
820 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000821}
822
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000823
824/*
825 Calling scheme used for ternary operations:
826
Neal Norwitz4886cc32006-08-21 17:06:07 +0000827 Order operations are tried until either a valid result or error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000829 */
830
831static PyObject *
832ternary_op(PyObject *v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyObject *w,
834 PyObject *z,
835 const int op_slot,
836 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 PyNumberMethods *mv, *mw, *mz;
839 PyObject *x = NULL;
840 ternaryfunc slotv = NULL;
841 ternaryfunc slotw = NULL;
842 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 mv = v->ob_type->tp_as_number;
845 mw = w->ob_type->tp_as_number;
846 if (mv != NULL)
847 slotv = NB_TERNOP(mv, op_slot);
848 if (w->ob_type != v->ob_type &&
849 mw != NULL) {
850 slotw = NB_TERNOP(mw, op_slot);
851 if (slotw == slotv)
852 slotw = NULL;
853 }
854 if (slotv) {
855 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
856 x = slotw(v, w, z);
857 if (x != Py_NotImplemented)
858 return x;
859 Py_DECREF(x); /* can't do it */
860 slotw = NULL;
861 }
862 x = slotv(v, w, z);
863 if (x != Py_NotImplemented)
864 return x;
865 Py_DECREF(x); /* can't do it */
866 }
867 if (slotw) {
868 x = slotw(v, w, z);
869 if (x != Py_NotImplemented)
870 return x;
871 Py_DECREF(x); /* can't do it */
872 }
873 mz = z->ob_type->tp_as_number;
874 if (mz != NULL) {
875 slotz = NB_TERNOP(mz, op_slot);
876 if (slotz == slotv || slotz == slotw)
877 slotz = NULL;
878 if (slotz) {
879 x = slotz(v, w, z);
880 if (x != Py_NotImplemented)
881 return x;
882 Py_DECREF(x); /* can't do it */
883 }
884 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (z == Py_None)
887 PyErr_Format(
888 PyExc_TypeError,
889 "unsupported operand type(s) for ** or pow(): "
890 "'%.100s' and '%.100s'",
891 v->ob_type->tp_name,
892 w->ob_type->tp_name);
893 else
894 PyErr_Format(
895 PyExc_TypeError,
896 "unsupported operand type(s) for pow(): "
897 "'%.100s', '%.100s', '%.100s'",
898 v->ob_type->tp_name,
899 w->ob_type->tp_name,
900 z->ob_type->tp_name);
901 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000902}
903
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000904#define BINARY_FUNC(func, op, op_name) \
905 PyObject * \
906 func(PyObject *v, PyObject *w) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 return binary_op(v, w, NB_SLOT(op), op_name); \
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000908 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000909
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000910BINARY_FUNC(PyNumber_Or, nb_or, "|")
911BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
912BINARY_FUNC(PyNumber_And, nb_and, "&")
913BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
914BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
915BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000916BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000917
918PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000919PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
922 if (result == Py_NotImplemented) {
923 PySequenceMethods *m = v->ob_type->tp_as_sequence;
924 Py_DECREF(result);
925 if (m && m->sq_concat) {
926 return (*m->sq_concat)(v, w);
927 }
928 result = binop_type_error(v, w, "+");
929 }
930 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000931}
932
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000933static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_ssize_t count;
937 if (PyIndex_Check(n)) {
938 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
939 if (count == -1 && PyErr_Occurred())
940 return NULL;
941 }
942 else {
943 return type_error("can't multiply sequence by "
944 "non-int of type '%.200s'", n);
945 }
946 return (*repeatfunc)(seq, count);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000947}
948
949PyObject *
950PyNumber_Multiply(PyObject *v, PyObject *w)
951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
953 if (result == Py_NotImplemented) {
954 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
955 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
956 Py_DECREF(result);
957 if (mv && mv->sq_repeat) {
958 return sequence_repeat(mv->sq_repeat, v, w);
959 }
960 else if (mw && mw->sq_repeat) {
961 return sequence_repeat(mw->sq_repeat, w, v);
962 }
963 result = binop_type_error(v, w, "*");
964 }
965 return result;
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000966}
967
Guido van Rossume15dee51995-07-18 14:12:02 +0000968PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000969PyNumber_FloorDivide(PyObject *v, PyObject *w)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
Guido van Rossum4668b002001-08-08 05:00:18 +0000972}
973
974PyObject *
975PyNumber_TrueDivide(PyObject *v, PyObject *w)
976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
Guido van Rossum4668b002001-08-08 05:00:18 +0000978}
979
980PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000981PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000984}
985
986PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000987PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000990}
991
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000992/* Binary in-place operators */
993
994/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000995 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000996
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000997 - If the left hand object has the appropriate struct members, and
998 they are filled, call the appropriate function and return the
999 result. No coercion is done on the arguments; the left-hand object
1000 is the one the operation is performed on, and it's up to the
1001 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +00001002
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001003 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001004 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001005
1006 */
1007
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001008static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001009binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyNumberMethods *mv = v->ob_type->tp_as_number;
1012 if (mv != NULL) {
1013 binaryfunc slot = NB_BINOP(mv, iop_slot);
1014 if (slot) {
1015 PyObject *x = (slot)(v, w);
1016 if (x != Py_NotImplemented) {
1017 return x;
1018 }
1019 Py_DECREF(x);
1020 }
1021 }
1022 return binary_op1(v, w, op_slot);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001023}
1024
1025static PyObject *
1026binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 const char *op_name)
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +00001028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
1030 if (result == Py_NotImplemented) {
1031 Py_DECREF(result);
1032 return binop_type_error(v, w, op_name);
1033 }
1034 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001035}
1036
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001037#define INPLACE_BINOP(func, iop, op, op_name) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 PyObject * \
1039 func(PyObject *v, PyObject *w) { \
1040 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1041 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001042
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00001043INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1044INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1045INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1046INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1047INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1048INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001049
1050PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +00001051PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1054 NB_SLOT(nb_floor_divide), "//=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001055}
1056
1057PyObject *
1058PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1061 NB_SLOT(nb_true_divide), "/=");
Guido van Rossum4668b002001-08-08 05:00:18 +00001062}
1063
1064PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001065PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
1068 NB_SLOT(nb_add));
1069 if (result == Py_NotImplemented) {
1070 PySequenceMethods *m = v->ob_type->tp_as_sequence;
1071 Py_DECREF(result);
1072 if (m != NULL) {
1073 binaryfunc f = NULL;
1074 f = m->sq_inplace_concat;
1075 if (f == NULL)
1076 f = m->sq_concat;
1077 if (f != NULL)
1078 return (*f)(v, w);
1079 }
1080 result = binop_type_error(v, w, "+=");
1081 }
1082 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001083}
1084
1085PyObject *
1086PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
1089 NB_SLOT(nb_multiply));
1090 if (result == Py_NotImplemented) {
1091 ssizeargfunc f = NULL;
1092 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
1093 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
1094 Py_DECREF(result);
1095 if (mv != NULL) {
1096 f = mv->sq_inplace_repeat;
1097 if (f == NULL)
1098 f = mv->sq_repeat;
1099 if (f != NULL)
1100 return sequence_repeat(f, v, w);
1101 }
1102 else if (mw != NULL) {
1103 /* Note that the right hand operand should not be
1104 * mutated in this case so sq_inplace_repeat is not
1105 * used. */
1106 if (mw->sq_repeat)
1107 return sequence_repeat(mw->sq_repeat, w, v);
1108 }
1109 result = binop_type_error(v, w, "*=");
1110 }
1111 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001112}
1113
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001114PyObject *
1115PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1118 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001119}
1120
1121PyObject *
1122PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (v->ob_type->tp_as_number &&
1125 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
1126 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
1127 }
1128 else {
1129 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
1130 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001131}
1132
1133
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001134/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +00001135
1136PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001137PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 if (o == NULL)
1142 return null_error();
1143 m = o->ob_type->tp_as_number;
1144 if (m && m->nb_negative)
1145 return (*m->nb_negative)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 return type_error("bad operand type for unary -: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001148}
1149
1150PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001151PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (o == NULL)
1156 return null_error();
1157 m = o->ob_type->tp_as_number;
1158 if (m && m->nb_positive)
1159 return (*m->nb_positive)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 return type_error("bad operand type for unary +: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001162}
1163
1164PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001165PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 PyNumberMethods *m;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (o == NULL)
1170 return null_error();
1171 m = o->ob_type->tp_as_number;
1172 if (m && m->nb_invert)
1173 return (*m->nb_invert)(o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return type_error("bad operand type for unary ~: '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001176}
1177
1178PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001179PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (o == NULL)
1184 return null_error();
1185 m = o->ob_type->tp_as_number;
1186 if (m && m->nb_absolute)
1187 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 return type_error("bad operand type for abs(): '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001190}
1191
Guido van Rossum98297ee2007-11-06 21:34:58 +00001192/* Return a Python Int or Long from the object item
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001193 Raise TypeError if the result is not an int-or-long
Guido van Rossum98297ee2007-11-06 21:34:58 +00001194 or if the object cannot be interpreted as an index.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001195*/
1196PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001197PyNumber_Index(PyObject *item)
1198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 PyObject *result = NULL;
1200 if (item == NULL)
1201 return null_error();
1202 if (PyLong_Check(item)) {
1203 Py_INCREF(item);
1204 return item;
1205 }
1206 if (PyIndex_Check(item)) {
1207 result = item->ob_type->tp_as_number->nb_index(item);
1208 if (result && !PyLong_Check(result)) {
1209 PyErr_Format(PyExc_TypeError,
1210 "__index__ returned non-int "
1211 "(type %.200s)",
1212 result->ob_type->tp_name);
1213 Py_DECREF(result);
1214 return NULL;
1215 }
1216 }
1217 else {
1218 PyErr_Format(PyExc_TypeError,
1219 "'%.200s' object cannot be interpreted "
1220 "as an integer", item->ob_type->tp_name);
1221 }
1222 return result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001223}
1224
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001225/* Return an error on Overflow only if err is not NULL*/
1226
1227Py_ssize_t
1228PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_ssize_t result;
1231 PyObject *runerr;
1232 PyObject *value = PyNumber_Index(item);
1233 if (value == NULL)
1234 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* We're done if PyLong_AsSsize_t() returns without error. */
1237 result = PyLong_AsSsize_t(value);
1238 if (result != -1 || !(runerr = PyErr_Occurred()))
1239 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* Error handling code -- only manage OverflowError differently */
1242 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
1243 goto finish;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyErr_Clear();
1246 /* If no error-handling desired then the default clipping
1247 is sufficient.
1248 */
1249 if (!err) {
1250 assert(PyLong_Check(value));
1251 /* Whether or not it is less than or equal to
1252 zero is determined by the sign of ob_size
1253 */
1254 if (_PyLong_Sign(value) < 0)
1255 result = PY_SSIZE_T_MIN;
1256 else
1257 result = PY_SSIZE_T_MAX;
1258 }
1259 else {
1260 /* Otherwise replace the error with caller's error object. */
1261 PyErr_Format(err,
1262 "cannot fit '%.200s' into an index-sized integer",
1263 item->ob_type->tp_name);
1264 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001265
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001266 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Py_DECREF(value);
1268 return result;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001269}
1270
1271
Christian Heimes15ebc882008-02-04 18:48:49 +00001272PyObject *
1273_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
1274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 static PyObject *int_name = NULL;
1276 if (int_name == NULL) {
1277 int_name = PyUnicode_InternFromString("__int__");
1278 if (int_name == NULL)
1279 return NULL;
1280 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (integral && !PyLong_Check(integral)) {
1283 /* Don't go through tp_as_number->nb_int to avoid
1284 hitting the classic class fallback to __trunc__. */
1285 PyObject *int_func = PyObject_GetAttr(integral, int_name);
1286 if (int_func == NULL) {
1287 PyErr_Clear(); /* Raise a different error. */
1288 goto non_integral_error;
1289 }
1290 Py_DECREF(integral);
1291 integral = PyEval_CallObject(int_func, NULL);
1292 Py_DECREF(int_func);
1293 if (integral && !PyLong_Check(integral)) {
1294 goto non_integral_error;
1295 }
1296 }
1297 return integral;
Christian Heimes15ebc882008-02-04 18:48:49 +00001298
1299non_integral_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
1301 Py_DECREF(integral);
1302 return NULL;
Christian Heimes15ebc882008-02-04 18:48:49 +00001303}
1304
1305
Guido van Rossum9e896b32000-04-05 20:11:21 +00001306/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001307static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308long_from_string(const char *s, Py_ssize_t len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 char *end;
1311 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 x = PyLong_FromString((char*)s, &end, 10);
1314 if (x == NULL)
1315 return NULL;
1316 if (end != s + len) {
1317 PyErr_SetString(PyExc_ValueError,
1318 "null byte in argument for int()");
1319 Py_DECREF(x);
1320 return NULL;
1321 }
1322 return x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001323}
1324
Guido van Rossume15dee51995-07-18 14:12:02 +00001325PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001326PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyNumberMethods *m;
1329 static PyObject *trunc_name = NULL;
1330 PyObject *trunc_func;
1331 const char *buffer;
1332 Py_ssize_t buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (trunc_name == NULL) {
1335 trunc_name = PyUnicode_InternFromString("__trunc__");
1336 if (trunc_name == NULL)
1337 return NULL;
1338 }
Christian Heimes15ebc882008-02-04 18:48:49 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (o == NULL)
1341 return null_error();
1342 if (PyLong_CheckExact(o)) {
1343 Py_INCREF(o);
1344 return o;
1345 }
1346 m = o->ob_type->tp_as_number;
1347 if (m && m->nb_int) { /* This should include subclasses of int */
1348 PyObject *res = m->nb_int(o);
1349 if (res && !PyLong_Check(res)) {
1350 PyErr_Format(PyExc_TypeError,
1351 "__int__ returned non-int (type %.200s)",
1352 res->ob_type->tp_name);
1353 Py_DECREF(res);
1354 return NULL;
1355 }
1356 return res;
1357 }
1358 if (PyLong_Check(o)) /* An int subclass without nb_int */
1359 return _PyLong_Copy((PyLongObject *)o);
1360 trunc_func = PyObject_GetAttr(o, trunc_name);
1361 if (trunc_func) {
1362 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
1363 PyObject *int_instance;
1364 Py_DECREF(trunc_func);
1365 /* __trunc__ is specified to return an Integral type,
1366 but long() needs to return a long. */
1367 int_instance = _PyNumber_ConvertIntegralToInt(
1368 truncated,
1369 "__trunc__ returned non-Integral (type %.200s)");
1370 return int_instance;
1371 }
1372 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
Christian Heimes15ebc882008-02-04 18:48:49 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (PyBytes_Check(o))
1375 /* need to do extra error checking that PyLong_FromString()
1376 * doesn't do. In particular long('9.5') must raise an
1377 * exception, not truncate the float.
1378 */
1379 return long_from_string(PyBytes_AS_STRING(o),
1380 PyBytes_GET_SIZE(o));
1381 if (PyUnicode_Check(o))
1382 /* The above check is done in PyLong_FromUnicode(). */
1383 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
1384 PyUnicode_GET_SIZE(o),
1385 10);
1386 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
1387 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return type_error("int() argument must be a string or a "
1390 "number, not '%.200s'", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001391}
1392
1393PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001394PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (o == NULL)
1399 return null_error();
1400 m = o->ob_type->tp_as_number;
1401 if (m && m->nb_float) { /* This should include subclasses of float */
1402 PyObject *res = m->nb_float(o);
1403 if (res && !PyFloat_Check(res)) {
1404 PyErr_Format(PyExc_TypeError,
1405 "__float__ returned non-float (type %.200s)",
1406 res->ob_type->tp_name);
1407 Py_DECREF(res);
1408 return NULL;
1409 }
1410 return res;
1411 }
1412 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
1413 PyFloatObject *po = (PyFloatObject *)o;
1414 return PyFloat_FromDouble(po->ob_fval);
1415 }
1416 return PyFloat_FromString(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001417}
1418
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001419
1420PyObject *
1421PyNumber_ToBase(PyObject *n, int base)
1422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyObject *res = NULL;
1424 PyObject *index = PyNumber_Index(n);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (!index)
1427 return NULL;
1428 if (PyLong_Check(index))
1429 res = _PyLong_Format(index, base);
1430 else
1431 /* It should not be possible to get here, as
1432 PyNumber_Index already has a check for the same
1433 condition */
1434 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
1435 "int or long");
1436 Py_DECREF(index);
1437 return res;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001438}
1439
1440
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001441/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001442
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001443int
Fred Drake79912472000-07-09 04:06:11 +00001444PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (PyDict_Check(s))
1447 return 0;
1448 return s != NULL && s->ob_type->tp_as_sequence &&
1449 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001450}
1451
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001453PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (s == NULL) {
1458 null_error();
1459 return -1;
1460 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 m = s->ob_type->tp_as_sequence;
1463 if (m && m->sq_length)
1464 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 type_error("object of type '%.200s' has no len()", s);
1467 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001468}
1469
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001470#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001471Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001472PySequence_Length(PyObject *s)
1473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 return PySequence_Size(s);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001475}
1476#define PySequence_Length PySequence_Size
1477
Guido van Rossume15dee51995-07-18 14:12:02 +00001478PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001479PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (s == NULL || o == NULL)
1484 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 m = s->ob_type->tp_as_sequence;
1487 if (m && m->sq_concat)
1488 return m->sq_concat(s, o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 /* Instances of user classes defining an __add__() method only
1491 have an nb_add slot, not an sq_concat slot. So we fall back
1492 to nb_add if both arguments appear to be sequences. */
1493 if (PySequence_Check(s) && PySequence_Check(o)) {
1494 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
1495 if (result != Py_NotImplemented)
1496 return result;
1497 Py_DECREF(result);
1498 }
1499 return type_error("'%.200s' object can't be concatenated", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001500}
1501
1502PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001503PySequence_Repeat(PyObject *o, Py_ssize_t count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (o == NULL)
1508 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 m = o->ob_type->tp_as_sequence;
1511 if (m && m->sq_repeat)
1512 return m->sq_repeat(o, count);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 /* Instances of user classes defining a __mul__() method only
1515 have an nb_multiply slot, not an sq_repeat slot. so we fall back
1516 to nb_multiply if o appears to be a sequence. */
1517 if (PySequence_Check(o)) {
1518 PyObject *n, *result;
1519 n = PyLong_FromSsize_t(count);
1520 if (n == NULL)
1521 return NULL;
1522 result = binary_op1(o, n, NB_SLOT(nb_multiply));
1523 Py_DECREF(n);
1524 if (result != Py_NotImplemented)
1525 return result;
1526 Py_DECREF(result);
1527 }
1528 return type_error("'%.200s' object can't be repeated", o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001529}
1530
1531PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001532PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (s == NULL || o == NULL)
1537 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 m = s->ob_type->tp_as_sequence;
1540 if (m && m->sq_inplace_concat)
1541 return m->sq_inplace_concat(s, o);
1542 if (m && m->sq_concat)
1543 return m->sq_concat(s, o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 if (PySequence_Check(s) && PySequence_Check(o)) {
1546 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
1547 NB_SLOT(nb_add));
1548 if (result != Py_NotImplemented)
1549 return result;
1550 Py_DECREF(result);
1551 }
1552 return type_error("'%.200s' object can't be concatenated", s);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001553}
1554
1555PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001556PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 PySequenceMethods *m;
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (o == NULL)
1561 return null_error();
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 m = o->ob_type->tp_as_sequence;
1564 if (m && m->sq_inplace_repeat)
1565 return m->sq_inplace_repeat(o, count);
1566 if (m && m->sq_repeat)
1567 return m->sq_repeat(o, count);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (PySequence_Check(o)) {
1570 PyObject *n, *result;
1571 n = PyLong_FromSsize_t(count);
1572 if (n == NULL)
1573 return NULL;
1574 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
1575 NB_SLOT(nb_multiply));
1576 Py_DECREF(n);
1577 if (result != Py_NotImplemented)
1578 return result;
1579 Py_DECREF(result);
1580 }
1581 return type_error("'%.200s' object can't be repeated", o);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001582}
1583
1584PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001585PySequence_GetItem(PyObject *s, Py_ssize_t i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (s == NULL)
1590 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 m = s->ob_type->tp_as_sequence;
1593 if (m && m->sq_item) {
1594 if (i < 0) {
1595 if (m->sq_length) {
1596 Py_ssize_t l = (*m->sq_length)(s);
1597 if (l < 0)
1598 return NULL;
1599 i += l;
1600 }
1601 }
1602 return m->sq_item(s, i);
1603 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return type_error("'%.200s' object does not support indexing", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001606}
1607
1608PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001609PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001616 if (mp && mp->mp_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 PyObject *res;
1618 PyObject *slice = _PySlice_FromIndices(i1, i2);
1619 if (!slice)
1620 return NULL;
1621 res = mp->mp_subscript(s, slice);
1622 Py_DECREF(slice);
1623 return res;
1624 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 return type_error("'%.200s' object is unsliceable", s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001627}
1628
1629int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (s == NULL) {
1635 null_error();
1636 return -1;
1637 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 m = s->ob_type->tp_as_sequence;
1640 if (m && m->sq_ass_item) {
1641 if (i < 0) {
1642 if (m->sq_length) {
1643 Py_ssize_t l = (*m->sq_length)(s);
1644 if (l < 0)
1645 return -1;
1646 i += l;
1647 }
1648 }
1649 return m->sq_ass_item(s, i, o);
1650 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 type_error("'%.200s' object does not support item assignment", s);
1653 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001654}
1655
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001656int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001657PySequence_DelItem(PyObject *s, Py_ssize_t i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (s == NULL) {
1662 null_error();
1663 return -1;
1664 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 m = s->ob_type->tp_as_sequence;
1667 if (m && m->sq_ass_item) {
1668 if (i < 0) {
1669 if (m->sq_length) {
1670 Py_ssize_t l = (*m->sq_length)(s);
1671 if (l < 0)
1672 return -1;
1673 i += l;
1674 }
1675 }
1676 return m->sq_ass_item(s, i, (PyObject *)NULL);
1677 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 type_error("'%.200s' object doesn't support item deletion", s);
1680 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001681}
1682
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001683int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001684PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (s == NULL) {
1689 null_error();
1690 return -1;
1691 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001694 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 int res;
1696 PyObject *slice = _PySlice_FromIndices(i1, i2);
1697 if (!slice)
1698 return -1;
1699 res = mp->mp_ass_subscript(s, slice, o);
1700 Py_DECREF(slice);
1701 return res;
1702 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 type_error("'%.200s' object doesn't support slice assignment", s);
1705 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001706}
1707
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001708int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001709PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyMappingMethods *mp;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (s == NULL) {
1714 null_error();
1715 return -1;
1716 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 mp = s->ob_type->tp_as_mapping;
Benjamin Peterson568867a2010-09-11 16:02:03 +00001719 if (mp && mp->mp_ass_subscript) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 int res;
1721 PyObject *slice = _PySlice_FromIndices(i1, i2);
1722 if (!slice)
1723 return -1;
1724 res = mp->mp_ass_subscript(s, slice, NULL);
1725 Py_DECREF(slice);
1726 return res;
1727 }
1728 type_error("'%.200s' object doesn't support slice deletion", s);
1729 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001730}
1731
Guido van Rossume15dee51995-07-18 14:12:02 +00001732PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001733PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyObject *it; /* iter(v) */
1736 Py_ssize_t n; /* guess for result tuple size */
1737 PyObject *result = NULL;
1738 Py_ssize_t j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (v == NULL)
1741 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* Special-case the common tuple and list cases, for efficiency. */
1744 if (PyTuple_CheckExact(v)) {
1745 /* Note that we can't know whether it's safe to return
1746 a tuple *subclass* instance as-is, hence the restriction
1747 to exact tuples here. In contrast, lists always make
1748 a copy, so there's no need for exactness below. */
1749 Py_INCREF(v);
1750 return v;
1751 }
1752 if (PyList_Check(v))
1753 return PyList_AsTuple(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 /* Get iterator. */
1756 it = PyObject_GetIter(v);
1757 if (it == NULL)
1758 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 /* Guess result size and allocate space. */
1761 n = _PyObject_LengthHint(v, 10);
1762 if (n == -1)
1763 goto Fail;
1764 result = PyTuple_New(n);
1765 if (result == NULL)
1766 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 /* Fill the tuple. */
1769 for (j = 0; ; ++j) {
1770 PyObject *item = PyIter_Next(it);
1771 if (item == NULL) {
1772 if (PyErr_Occurred())
1773 goto Fail;
1774 break;
1775 }
1776 if (j >= n) {
1777 Py_ssize_t oldn = n;
1778 /* The over-allocation strategy can grow a bit faster
1779 than for lists because unlike lists the
1780 over-allocation isn't permanent -- we reclaim
1781 the excess before the end of this routine.
1782 So, grow by ten and then add 25%.
1783 */
1784 n += 10;
1785 n += n >> 2;
1786 if (n < oldn) {
1787 /* Check for overflow */
1788 PyErr_NoMemory();
1789 Py_DECREF(item);
1790 goto Fail;
1791 }
1792 if (_PyTuple_Resize(&result, n) != 0) {
1793 Py_DECREF(item);
1794 goto Fail;
1795 }
1796 }
1797 PyTuple_SET_ITEM(result, j, item);
1798 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 /* Cut tuple back if guess was too large. */
1801 if (j < n &&
1802 _PyTuple_Resize(&result, j) != 0)
1803 goto Fail;
Tim Peters6912d4d2001-05-05 03:56:37 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 Py_DECREF(it);
1806 return result;
Tim Peters6912d4d2001-05-05 03:56:37 +00001807
1808Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 Py_XDECREF(result);
1810 Py_DECREF(it);
1811 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001812}
1813
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001814PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001815PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 PyObject *result; /* result list */
1818 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 if (v == NULL)
1821 return null_error();
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 result = PyList_New(0);
1824 if (result == NULL)
1825 return NULL;
Tim Petersf553f892001-05-01 20:45:31 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 rv = _PyList_Extend((PyListObject *)result, v);
1828 if (rv == NULL) {
1829 Py_DECREF(result);
1830 return NULL;
1831 }
1832 Py_DECREF(rv);
1833 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001834}
1835
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001836PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001837PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 PyObject *it;
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (v == NULL)
1842 return null_error();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
1845 Py_INCREF(v);
1846 return v;
1847 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 it = PyObject_GetIter(v);
1850 if (it == NULL) {
1851 if (PyErr_ExceptionMatches(PyExc_TypeError))
1852 PyErr_SetString(PyExc_TypeError, m);
1853 return NULL;
1854 }
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 v = PySequence_List(it);
1857 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 return v;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001860}
1861
Tim Peters16a77ad2001-09-08 04:00:12 +00001862/* Iterate over seq. Result depends on the operation:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1864 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
1865 set ValueError and return -1 if none found; also return -1 on error.
Tim Peters16a77ad2001-09-08 04:00:12 +00001866 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1867*/
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001868Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001869_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 Py_ssize_t n;
1872 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1873 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (seq == NULL || obj == NULL) {
1876 null_error();
1877 return -1;
1878 }
Tim Peters75f8e352001-05-05 11:33:43 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 it = PyObject_GetIter(seq);
1881 if (it == NULL) {
1882 type_error("argument of type '%.200s' is not iterable", seq);
1883 return -1;
1884 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 n = wrapped = 0;
1887 for (;;) {
1888 int cmp;
1889 PyObject *item = PyIter_Next(it);
1890 if (item == NULL) {
1891 if (PyErr_Occurred())
1892 goto Fail;
1893 break;
1894 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
1897 Py_DECREF(item);
1898 if (cmp < 0)
1899 goto Fail;
1900 if (cmp > 0) {
1901 switch (operation) {
1902 case PY_ITERSEARCH_COUNT:
1903 if (n == PY_SSIZE_T_MAX) {
1904 PyErr_SetString(PyExc_OverflowError,
1905 "count exceeds C integer size");
1906 goto Fail;
1907 }
1908 ++n;
1909 break;
Tim Peters16a77ad2001-09-08 04:00:12 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 case PY_ITERSEARCH_INDEX:
1912 if (wrapped) {
1913 PyErr_SetString(PyExc_OverflowError,
1914 "index exceeds C integer size");
1915 goto Fail;
1916 }
1917 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 case PY_ITERSEARCH_CONTAINS:
1920 n = 1;
1921 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 default:
1924 assert(!"unknown operation");
1925 }
1926 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (operation == PY_ITERSEARCH_INDEX) {
1929 if (n == PY_SSIZE_T_MAX)
1930 wrapped = 1;
1931 ++n;
1932 }
1933 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 if (operation != PY_ITERSEARCH_INDEX)
1936 goto Done;
Tim Peters16a77ad2001-09-08 04:00:12 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyErr_SetString(PyExc_ValueError,
1939 "sequence.index(x): x not in sequence");
1940 /* fall into failure code */
Tim Peters16a77ad2001-09-08 04:00:12 +00001941Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 n = -1;
1943 /* fall through */
Tim Peters16a77ad2001-09-08 04:00:12 +00001944Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 Py_DECREF(it);
1946 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001947
Guido van Rossume15dee51995-07-18 14:12:02 +00001948}
1949
Tim Peters16a77ad2001-09-08 04:00:12 +00001950/* Return # of times o appears in s. */
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001951Py_ssize_t
Tim Peters16a77ad2001-09-08 04:00:12 +00001952PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001955}
1956
Tim Peterscb8d3682001-05-05 21:05:01 +00001957/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001958 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001959 */
1960int
1961PySequence_Contains(PyObject *seq, PyObject *ob)
1962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 Py_ssize_t result;
1964 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1965 if (sqm != NULL && sqm->sq_contains != NULL)
1966 return (*sqm->sq_contains)(seq, ob);
1967 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
1968 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
Tim Peterscb8d3682001-05-05 21:05:01 +00001969}
1970
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001971/* Backwards compatibility */
1972#undef PySequence_In
1973int
Fred Drake79912472000-07-09 04:06:11 +00001974PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 return PySequence_Contains(w, v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001977}
1978
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001979Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001980PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001983}
1984
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001985/* Operations on mappings */
1986
1987int
Fred Drake79912472000-07-09 04:06:11 +00001988PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 return o && o->ob_type->tp_as_mapping &&
1991 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00001992}
1993
Martin v. Löwis18e16552006-02-15 17:27:45 +00001994Py_ssize_t
Jeremy Hylton6253f832000-07-12 12:56:19 +00001995PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (o == NULL) {
2000 null_error();
2001 return -1;
2002 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 m = o->ob_type->tp_as_mapping;
2005 if (m && m->mp_length)
2006 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 type_error("object of type '%.200s' has no len()", o);
2009 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00002010}
2011
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002012#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00002013Py_ssize_t
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002014PyMapping_Length(PyObject *o)
2015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 return PyMapping_Size(o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00002017}
2018#define PyMapping_Length PyMapping_Size
2019
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002020PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002021PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 PyObject *okey, *r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (key == NULL)
2026 return null_error();
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 okey = PyUnicode_FromString(key);
2029 if (okey == NULL)
2030 return NULL;
2031 r = PyObject_GetItem(o, okey);
2032 Py_DECREF(okey);
2033 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002034}
2035
2036int
Fred Drake79912472000-07-09 04:06:11 +00002037PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 PyObject *okey;
2040 int r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 if (key == NULL) {
2043 null_error();
2044 return -1;
2045 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 okey = PyUnicode_FromString(key);
2048 if (okey == NULL)
2049 return -1;
2050 r = PyObject_SetItem(o, okey, value);
2051 Py_DECREF(okey);
2052 return r;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002053}
2054
2055int
Fred Drake79912472000-07-09 04:06:11 +00002056PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 v = PyMapping_GetItemString(o, key);
2061 if (v) {
2062 Py_DECREF(v);
2063 return 1;
2064 }
2065 PyErr_Clear();
2066 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002067}
2068
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002069int
Fred Drake79912472000-07-09 04:06:11 +00002070PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00002071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00002073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 v = PyObject_GetItem(o, key);
2075 if (v) {
2076 Py_DECREF(v);
2077 return 1;
2078 }
2079 PyErr_Clear();
2080 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00002081}
2082
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002083PyObject *
2084PyMapping_Keys(PyObject *o)
2085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 PyObject *keys;
2087 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 if (PyDict_CheckExact(o))
2090 return PyDict_Keys(o);
2091 keys = PyObject_CallMethod(o, "keys", NULL);
2092 if (keys == NULL)
2093 return NULL;
2094 fast = PySequence_Fast(keys, "o.keys() are not iterable");
2095 Py_DECREF(keys);
2096 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002097}
2098
2099PyObject *
2100PyMapping_Items(PyObject *o)
2101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 PyObject *items;
2103 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (PyDict_CheckExact(o))
2106 return PyDict_Items(o);
2107 items = PyObject_CallMethod(o, "items", NULL);
2108 if (items == NULL)
2109 return NULL;
2110 fast = PySequence_Fast(items, "o.items() are not iterable");
2111 Py_DECREF(items);
2112 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002113}
2114
2115PyObject *
2116PyMapping_Values(PyObject *o)
2117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 PyObject *values;
2119 PyObject *fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 if (PyDict_CheckExact(o))
2122 return PyDict_Values(o);
2123 values = PyObject_CallMethod(o, "values", NULL);
2124 if (values == NULL)
2125 return NULL;
2126 fast = PySequence_Fast(values, "o.values() are not iterable");
2127 Py_DECREF(values);
2128 return fast;
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002129}
2130
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002131/* Operations on callable objects */
2132
2133/* XXX PyCallable_Check() is in object.c */
2134
Guido van Rossume15dee51995-07-18 14:12:02 +00002135PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002136PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00002137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002139}
Guido van Rossume15dee51995-07-18 14:12:02 +00002140
2141PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002142PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
2143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 ternaryfunc call;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 if ((call = func->ob_type->tp_call) != NULL) {
2147 PyObject *result;
2148 if (Py_EnterRecursiveCall(" while calling a Python object"))
2149 return NULL;
2150 result = (*call)(func, arg, kw);
2151 Py_LeaveRecursiveCall();
2152 if (result == NULL && !PyErr_Occurred())
2153 PyErr_SetString(
2154 PyExc_SystemError,
2155 "NULL result without error in PyObject_Call");
2156 return result;
2157 }
2158 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
2159 func->ob_type->tp_name);
2160 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002161}
2162
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002163static PyObject*
2164call_function_tail(PyObject *callable, PyObject *args)
2165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 PyObject *retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (args == NULL)
2169 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 if (!PyTuple_Check(args)) {
2172 PyObject *a;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 a = PyTuple_New(1);
2175 if (a == NULL) {
2176 Py_DECREF(args);
2177 return NULL;
2178 }
2179 PyTuple_SET_ITEM(a, 0, args);
2180 args = a;
2181 }
2182 retval = PyObject_Call(callable, args, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002187}
2188
Tim Peters6d6c1a32001-08-02 04:15:00 +00002189PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002190PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 va_list va;
2193 PyObject *args;
Guido van Rossume15dee51995-07-18 14:12:02 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (callable == NULL)
2196 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 if (format && *format) {
2199 va_start(va, format);
2200 args = Py_VaBuildValue(format, va);
2201 va_end(va);
2202 }
2203 else
2204 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 return call_function_tail(callable, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002207}
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002208
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002209PyObject *
2210_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
2211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 va_list va;
2213 PyObject *args;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 if (callable == NULL)
2216 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 if (format && *format) {
2219 va_start(va, format);
2220 args = _Py_VaBuildValue_SizeT(format, va);
2221 va_end(va);
2222 }
2223 else
2224 args = PyTuple_New(0);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 return call_function_tail(callable, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002227}
2228
2229PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00002230PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00002231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 va_list va;
2233 PyObject *args;
2234 PyObject *func = NULL;
2235 PyObject *retval = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (o == NULL || name == NULL)
2238 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 func = PyObject_GetAttrString(o, name);
2241 if (func == NULL) {
2242 PyErr_SetString(PyExc_AttributeError, name);
2243 return 0;
2244 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 if (!PyCallable_Check(func)) {
2247 type_error("attribute of type '%.200s' is not callable", func);
2248 goto exit;
2249 }
Guido van Rossume15dee51995-07-18 14:12:02 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (format && *format) {
2252 va_start(va, format);
2253 args = Py_VaBuildValue(format, va);
2254 va_end(va);
2255 }
2256 else
2257 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00002258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 retval = call_function_tail(func, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00002260
Michael W. Hudson0edc7a02005-07-12 10:21:19 +00002261 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 /* args gets consumed in call_function_tail */
2263 Py_XDECREF(func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266}
2267
2268PyObject *
2269_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 va_list va;
2272 PyObject *args;
2273 PyObject *func = NULL;
2274 PyObject *retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 if (o == NULL || name == NULL)
2277 return null_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 func = PyObject_GetAttrString(o, name);
2280 if (func == NULL) {
2281 PyErr_SetString(PyExc_AttributeError, name);
2282 return 0;
2283 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 if (!PyCallable_Check(func)) {
2286 type_error("attribute of type '%.200s' is not callable", func);
2287 goto exit;
2288 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (format && *format) {
2291 va_start(va, format);
2292 args = _Py_VaBuildValue_SizeT(format, va);
2293 va_end(va);
2294 }
2295 else
2296 args = PyTuple_New(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 retval = call_function_tail(func, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002299
2300 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 /* args gets consumed in call_function_tail */
2302 Py_XDECREF(func);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00002305}
Guido van Rossum823649d2001-03-21 18:40:58 +00002306
2307
Fred Drakeb421b8c2001-10-26 16:21:32 +00002308static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002309objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 int i, n = 0;
2312 va_list countva;
2313 PyObject *result, *tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002314
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00002315 Py_VA_COPY(countva, va);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
2318 ++n;
2319 result = PyTuple_New(n);
2320 if (result != NULL && n > 0) {
2321 for (i = 0; i < n; ++i) {
2322 tmp = (PyObject *)va_arg(va, PyObject *);
2323 PyTuple_SET_ITEM(result, i, tmp);
2324 Py_INCREF(tmp);
2325 }
2326 }
2327 return result;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002328}
2329
2330PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002331PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 PyObject *args, *tmp;
2334 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (callable == NULL || name == NULL)
2337 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 callable = PyObject_GetAttr(callable, name);
2340 if (callable == NULL)
2341 return NULL;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 /* count the args */
2344 va_start(vargs, name);
2345 args = objargs_mktuple(vargs);
2346 va_end(vargs);
2347 if (args == NULL) {
2348 Py_DECREF(callable);
2349 return NULL;
2350 }
2351 tmp = PyObject_Call(callable, args, NULL);
2352 Py_DECREF(args);
2353 Py_DECREF(callable);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002356}
2357
2358PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00002359PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00002360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 PyObject *args, *tmp;
2362 va_list vargs;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (callable == NULL)
2365 return null_error();
Fred Drakeb421b8c2001-10-26 16:21:32 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* count the args */
2368 va_start(vargs, callable);
2369 args = objargs_mktuple(vargs);
2370 va_end(vargs);
2371 if (args == NULL)
2372 return NULL;
2373 tmp = PyObject_Call(callable, args, NULL);
2374 Py_DECREF(args);
Fred Drakeb421b8c2001-10-26 16:21:32 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 return tmp;
Fred Drakeb421b8c2001-10-26 16:21:32 +00002377}
2378
2379
Guido van Rossum823649d2001-03-21 18:40:58 +00002380/* isinstance(), issubclass() */
2381
Barry Warsawf16951c2002-04-23 22:45:44 +00002382/* abstract_get_bases() has logically 4 return states, with a sort of 0th
2383 * state that will almost never happen.
2384 *
2385 * 0. creating the __bases__ static string could get a MemoryError
2386 * 1. getattr(cls, '__bases__') could raise an AttributeError
2387 * 2. getattr(cls, '__bases__') could raise some other exception
2388 * 3. getattr(cls, '__bases__') could return a tuple
2389 * 4. getattr(cls, '__bases__') could return something other than a tuple
2390 *
2391 * Only state #3 is a non-error state and only it returns a non-NULL object
2392 * (it returns the retrieved tuple).
2393 *
2394 * Any raised AttributeErrors are masked by clearing the exception and
2395 * returning NULL. If an object other than a tuple comes out of __bases__,
2396 * then again, the return value is NULL. So yes, these two situations
2397 * produce exactly the same results: NULL is returned and no error is set.
2398 *
2399 * If some exception other than AttributeError is raised, then NULL is also
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 * returned, but the exception is not cleared. That's because we want the
Barry Warsawf16951c2002-04-23 22:45:44 +00002401 * exception to be propagated along.
2402 *
2403 * Callers are expected to test for PyErr_Occurred() when the return value
2404 * is NULL to decide whether a valid exception should be propagated or not.
2405 * When there's no exception to propagate, it's customary for the caller to
2406 * set a TypeError.
2407 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00002408static PyObject *
2409abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 static PyObject *__bases__ = NULL;
2412 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 if (__bases__ == NULL) {
2415 __bases__ = PyUnicode_InternFromString("__bases__");
2416 if (__bases__ == NULL)
2417 return NULL;
2418 }
2419 Py_ALLOW_RECURSION
2420 bases = PyObject_GetAttr(cls, __bases__);
2421 Py_END_ALLOW_RECURSION
2422 if (bases == NULL) {
2423 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2424 PyErr_Clear();
2425 return NULL;
2426 }
2427 if (!PyTuple_Check(bases)) {
2428 Py_DECREF(bases);
2429 return NULL;
2430 }
2431 return bases;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002432}
2433
2434
2435static int
2436abstract_issubclass(PyObject *derived, PyObject *cls)
2437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 PyObject *bases = NULL;
2439 Py_ssize_t i, n;
2440 int r = 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 while (1) {
2443 if (derived == cls)
2444 return 1;
2445 bases = abstract_get_bases(derived);
2446 if (bases == NULL) {
2447 if (PyErr_Occurred())
2448 return -1;
2449 return 0;
2450 }
2451 n = PyTuple_GET_SIZE(bases);
2452 if (n == 0) {
2453 Py_DECREF(bases);
2454 return 0;
2455 }
2456 /* Avoid recursivity in the single inheritance case */
2457 if (n == 1) {
2458 derived = PyTuple_GET_ITEM(bases, 0);
2459 Py_DECREF(bases);
2460 continue;
2461 }
2462 for (i = 0; i < n; i++) {
2463 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2464 if (r != 0)
2465 break;
2466 }
2467 Py_DECREF(bases);
2468 return r;
2469 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002470}
2471
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002472static int
2473check_class(PyObject *cls, const char *error)
2474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PyObject *bases = abstract_get_bases(cls);
2476 if (bases == NULL) {
2477 /* Do not mask errors. */
2478 if (!PyErr_Occurred())
2479 PyErr_SetString(PyExc_TypeError, error);
2480 return 0;
2481 }
2482 Py_DECREF(bases);
2483 return -1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002484}
2485
Brett Cannon4f653312004-03-20 22:52:14 +00002486static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002487recursive_isinstance(PyObject *inst, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 PyObject *icls;
2490 static PyObject *__class__ = NULL;
2491 int retval = 0;
Guido van Rossum823649d2001-03-21 18:40:58 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 if (__class__ == NULL) {
2494 __class__ = PyUnicode_InternFromString("__class__");
2495 if (__class__ == NULL)
2496 return -1;
2497 }
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (PyType_Check(cls)) {
2500 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2501 if (retval == 0) {
2502 PyObject *c = PyObject_GetAttr(inst, __class__);
2503 if (c == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002504 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002505 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002506 else
R. David Murray6bb99892010-11-20 16:33:30 +00002507 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 }
2509 else {
2510 if (c != (PyObject *)(inst->ob_type) &&
2511 PyType_Check(c))
2512 retval = PyType_IsSubtype(
2513 (PyTypeObject *)c,
2514 (PyTypeObject *)cls);
2515 Py_DECREF(c);
2516 }
2517 }
2518 }
2519 else {
2520 if (!check_class(cls,
Benjamin Petersone893af52010-06-28 19:43:42 +00002521 "isinstance() arg 2 must be a type or tuple of types"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 return -1;
2523 icls = PyObject_GetAttr(inst, __class__);
2524 if (icls == NULL) {
Benjamin Peterson72288d42010-11-20 17:21:08 +00002525 if (PyErr_ExceptionMatches(PyExc_AttributeError))
R. David Murray6bb99892010-11-20 16:33:30 +00002526 PyErr_Clear();
Benjamin Peterson72288d42010-11-20 17:21:08 +00002527 else
R. David Murray6bb99892010-11-20 16:33:30 +00002528 retval = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 }
2530 else {
2531 retval = abstract_issubclass(icls, cls);
2532 Py_DECREF(icls);
2533 }
2534 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 return retval;
Guido van Rossum823649d2001-03-21 18:40:58 +00002537}
2538
2539int
Brett Cannon4f653312004-03-20 22:52:14 +00002540PyObject_IsInstance(PyObject *inst, PyObject *cls)
2541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 static PyObject *name = NULL;
2543 PyObject *checker;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 /* Quick test for an exact match */
2546 if (Py_TYPE(inst) == (PyTypeObject *)cls)
2547 return 1;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00002548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 if (PyTuple_Check(cls)) {
2550 Py_ssize_t i;
2551 Py_ssize_t n;
2552 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 if (Py_EnterRecursiveCall(" in __instancecheck__"))
2555 return -1;
2556 n = PyTuple_GET_SIZE(cls);
2557 for (i = 0; i < n; ++i) {
2558 PyObject *item = PyTuple_GET_ITEM(cls, i);
2559 r = PyObject_IsInstance(inst, item);
2560 if (r != 0)
2561 /* either found it, or got an error */
2562 break;
2563 }
2564 Py_LeaveRecursiveCall();
2565 return r;
2566 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
2569 if (checker != NULL) {
2570 PyObject *res;
2571 int ok = -1;
2572 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
2573 Py_DECREF(checker);
2574 return ok;
2575 }
2576 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
2577 Py_LeaveRecursiveCall();
2578 Py_DECREF(checker);
2579 if (res != NULL) {
2580 ok = PyObject_IsTrue(res);
2581 Py_DECREF(res);
2582 }
2583 return ok;
2584 }
2585 else if (PyErr_Occurred())
2586 return -1;
2587 return recursive_isinstance(inst, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002588}
2589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590static int
Antoine Pitrouec569b72008-08-26 22:40:48 +00002591recursive_issubclass(PyObject *derived, PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00002592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (PyType_Check(cls) && PyType_Check(derived)) {
2594 /* Fast path (non-recursive) */
2595 return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2596 }
2597 if (!check_class(derived,
2598 "issubclass() arg 1 must be a class"))
2599 return -1;
2600 if (!check_class(cls,
2601 "issubclass() arg 2 must be a class"
2602 " or tuple of classes"))
2603 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 return abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002606}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002607
Brett Cannon4f653312004-03-20 22:52:14 +00002608int
2609PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 static PyObject *name = NULL;
2612 PyObject *checker;
Georg Brandldcfe8e42009-05-17 08:22:45 +00002613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 if (PyTuple_Check(cls)) {
2615 Py_ssize_t i;
2616 Py_ssize_t n;
2617 int r = 0;
Antoine Pitrouec569b72008-08-26 22:40:48 +00002618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
2620 return -1;
2621 n = PyTuple_GET_SIZE(cls);
2622 for (i = 0; i < n; ++i) {
2623 PyObject *item = PyTuple_GET_ITEM(cls, i);
2624 r = PyObject_IsSubclass(derived, item);
2625 if (r != 0)
2626 /* either found it, or got an error */
2627 break;
2628 }
2629 Py_LeaveRecursiveCall();
2630 return r;
2631 }
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
2634 if (checker != NULL) {
2635 PyObject *res;
2636 int ok = -1;
2637 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
2638 Py_DECREF(checker);
2639 return ok;
2640 }
2641 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
2642 Py_LeaveRecursiveCall();
2643 Py_DECREF(checker);
2644 if (res != NULL) {
2645 ok = PyObject_IsTrue(res);
2646 Py_DECREF(res);
2647 }
2648 return ok;
2649 }
2650 else if (PyErr_Occurred())
2651 return -1;
2652 return recursive_issubclass(derived, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002653}
2654
2655int
2656_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 return recursive_isinstance(inst, cls);
Antoine Pitrouec569b72008-08-26 22:40:48 +00002659}
2660
2661int
2662_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 return recursive_issubclass(derived, cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002665}
2666
2667
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002668PyObject *
2669PyObject_GetIter(PyObject *o)
2670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 PyTypeObject *t = o->ob_type;
2672 getiterfunc f = NULL;
2673 f = t->tp_iter;
2674 if (f == NULL) {
2675 if (PySequence_Check(o))
2676 return PySeqIter_New(o);
2677 return type_error("'%.200s' object is not iterable", o);
2678 }
2679 else {
2680 PyObject *res = (*f)(o);
2681 if (res != NULL && !PyIter_Check(res)) {
2682 PyErr_Format(PyExc_TypeError,
2683 "iter() returned non-iterator "
2684 "of type '%.100s'",
2685 res->ob_type->tp_name);
2686 Py_DECREF(res);
2687 res = NULL;
2688 }
2689 return res;
2690 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002691}
2692
Tim Petersf4848da2001-05-05 00:14:56 +00002693/* Return next item.
2694 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2695 * If the iteration terminates normally, return NULL and clear the
2696 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2697 * will be false.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 * Else return the next object. PyErr_Occurred() will be false.
Tim Petersf4848da2001-05-05 00:14:56 +00002699 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002700PyObject *
2701PyIter_Next(PyObject *iter)
2702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 PyObject *result;
2704 result = (*iter->ob_type->tp_iternext)(iter);
2705 if (result == NULL &&
2706 PyErr_Occurred() &&
2707 PyErr_ExceptionMatches(PyExc_StopIteration))
2708 PyErr_Clear();
2709 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002710}
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002711
2712
2713/*
2714 * Flatten a sequence of bytes() objects into a C array of
2715 * NULL terminated string pointers with a NULL char* terminating the array.
2716 * (ie: an argv or env list)
2717 *
2718 * Memory allocated for the returned list is allocated using malloc() and MUST
2719 * be freed by the caller using a free() loop or _Py_FreeCharPArray().
2720 */
2721char *const *
2722_PySequence_BytesToCharpArray(PyObject* self)
2723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 char **array;
2725 Py_ssize_t i, argc;
2726 PyObject *item = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 argc = PySequence_Size(self);
2729 if (argc == -1)
2730 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 array = malloc((argc + 1) * sizeof(char *));
2733 if (array == NULL) {
2734 PyErr_NoMemory();
2735 return NULL;
2736 }
2737 for (i = 0; i < argc; ++i) {
2738 char *data;
2739 item = PySequence_GetItem(self, i);
2740 data = PyBytes_AsString(item);
2741 if (data == NULL) {
2742 /* NULL terminate before freeing. */
2743 array[i] = NULL;
2744 goto fail;
2745 }
2746 array[i] = strdup(data);
2747 if (!array[i]) {
2748 PyErr_NoMemory();
2749 goto fail;
2750 }
2751 Py_DECREF(item);
2752 }
2753 array[argc] = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 return array;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002756
2757fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 Py_XDECREF(item);
2759 _Py_FreeCharPArray(array);
2760 return NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002761}
2762
2763
2764/* Free's a NULL terminated char** array of C strings. */
2765void
2766_Py_FreeCharPArray(char *const array[])
2767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 Py_ssize_t i;
2769 for (i = 0; array[i] != NULL; ++i) {
2770 free(array[i]);
2771 }
2772 free((void*)array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002773}