/home/mdboom/Work/builds/cpython/Objects/abstract.c
Line | Count | Source (jump to first uncovered line) |
1 | /* Abstract Object Interface (many thanks to Jim Fulton) */ |
2 | |
3 | #include "Python.h" |
4 | #include "pycore_abstract.h" // _PyIndex_Check() |
5 | #include "pycore_call.h" // _PyObject_CallNoArgs() |
6 | #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate() |
7 | #include "pycore_object.h" // _Py_CheckSlotResult() |
8 | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
9 | #include "pycore_pystate.h" // _PyThreadState_GET() |
10 | #include "pycore_unionobject.h" // _PyUnion_Check() |
11 | #include <ctype.h> |
12 | #include <stddef.h> // offsetof() |
13 | |
14 | |
15 | |
16 | /* Shorthands to return certain errors */ |
17 | |
18 | static PyObject * |
19 | type_error(const char *msg, PyObject *obj) |
20 | { |
21 | PyErr_Format(PyExc_TypeError, msg, Py_TYPE(obj)->tp_name); |
22 | return NULL; |
23 | } |
24 | |
25 | static PyObject * |
26 | null_error(void) |
27 | { |
28 | PyThreadState *tstate = _PyThreadState_GET(); |
29 | if (!_PyErr_Occurred(tstate)) { Branch (29:9): [True: 0, False: 0]
|
30 | _PyErr_SetString(tstate, PyExc_SystemError, |
31 | "null argument to internal routine"); |
32 | } |
33 | return NULL; |
34 | } |
35 | |
36 | /* Operations on any object */ |
37 | |
38 | PyObject * |
39 | PyObject_Type(PyObject *o) |
40 | { |
41 | PyObject *v; |
42 | |
43 | if (o == NULL) { Branch (43:9): [True: 0, False: 1.37k]
|
44 | return null_error(); |
45 | } |
46 | |
47 | v = (PyObject *)Py_TYPE(o); |
48 | Py_INCREF(v); |
49 | return v; |
50 | } |
51 | |
52 | Py_ssize_t |
53 | PyObject_Size(PyObject *o) |
54 | { |
55 | if (o == NULL) { Branch (55:9): [True: 0, False: 20.5M]
|
56 | null_error(); |
57 | return -1; |
58 | } |
59 | |
60 | PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; |
61 | if (m && m->sq_length20.5M ) { Branch (61:9): [True: 20.5M, False: 286]
Branch (61:14): [True: 19.1M, False: 1.48M]
|
62 | Py_ssize_t len = m->sq_length(o); |
63 | assert(_Py_CheckSlotResult(o, "__len__", len >= 0)); |
64 | return len; |
65 | } |
66 | |
67 | return PyMapping_Size(o); |
68 | } |
69 | |
70 | #undef PyObject_Length |
71 | Py_ssize_t |
72 | PyObject_Length(PyObject *o) |
73 | { |
74 | return PyObject_Size(o); |
75 | } |
76 | #define PyObject_Length PyObject_Size |
77 | |
78 | int |
79 | _PyObject_HasLen(PyObject *o) { |
80 | return (Py_TYPE(o)->tp_as_sequence && Py_TYPE221k (o)->tp_as_sequence->sq_length221k ) || Branch (80:13): [True: 221k, False: 719k]
Branch (80:43): [True: 163k, False: 57.5k]
|
81 | (777k Py_TYPE777k (o)->tp_as_mapping777k && Py_TYPE57.8k (o)->tp_as_mapping->mp_length57.8k ); Branch (81:10): [True: 57.8k, False: 719k]
Branch (81:39): [True: 34.2k, False: 23.6k]
|
82 | } |
83 | |
84 | /* The length hint function returns a non-negative value from o.__len__() |
85 | or o.__length_hint__(). If those methods aren't found the defaultvalue is |
86 | returned. If one of the calls fails with an exception other than TypeError |
87 | this function returns -1. |
88 | */ |
89 | |
90 | Py_ssize_t |
91 | PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) |
92 | { |
93 | PyObject *hint, *result; |
94 | Py_ssize_t res; |
95 | if (_PyObject_HasLen(o)) { Branch (95:9): [True: 197k, False: 742k]
|
96 | res = PyObject_Length(o); |
97 | if (res < 0) { Branch (97:13): [True: 5, False: 197k]
|
98 | PyThreadState *tstate = _PyThreadState_GET(); |
99 | assert(_PyErr_Occurred(tstate)); |
100 | if (!_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) { Branch (100:17): [True: 3, False: 2]
|
101 | return -1; |
102 | } |
103 | _PyErr_Clear(tstate); |
104 | } |
105 | else { |
106 | return res; |
107 | } |
108 | } |
109 | hint = _PyObject_LookupSpecial(o, &_Py_ID(__length_hint__)); |
110 | if (hint == NULL) { Branch (110:9): [True: 534k, False: 208k]
|
111 | if (PyErr_Occurred()) { Branch (111:13): [True: 1, False: 534k]
|
112 | return -1; |
113 | } |
114 | return defaultvalue; |
115 | } |
116 | result = _PyObject_CallNoArgs(hint); |
117 | Py_DECREF(hint); |
118 | if (result == NULL) { Branch (118:9): [True: 7, False: 208k]
|
119 | PyThreadState *tstate = _PyThreadState_GET(); |
120 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) { Branch (120:13): [True: 2, False: 5]
|
121 | _PyErr_Clear(tstate); |
122 | return defaultvalue; |
123 | } |
124 | return -1; |
125 | } |
126 | else if (result == Py_NotImplemented) { Branch (126:14): [True: 49, False: 208k]
|
127 | Py_DECREF(result); |
128 | return defaultvalue; |
129 | } |
130 | if (!PyLong_Check(result)) { Branch (130:9): [True: 1, False: 208k]
|
131 | PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s", |
132 | Py_TYPE(result)->tp_name); |
133 | Py_DECREF(result); |
134 | return -1; |
135 | } |
136 | res = PyLong_AsSsize_t(result); |
137 | Py_DECREF(result); |
138 | if (res < 0 && PyErr_Occurred()1 ) { Branch (138:9): [True: 1, False: 208k]
Branch (138:20): [True: 0, False: 1]
|
139 | return -1; |
140 | } |
141 | if (res < 0) { Branch (141:9): [True: 1, False: 208k]
|
142 | PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0"); |
143 | return -1; |
144 | } |
145 | return res; |
146 | } |
147 | |
148 | PyObject * |
149 | PyObject_GetItem(PyObject *o, PyObject *key) |
150 | { |
151 | if (o == NULL || key == NULL) { Branch (151:9): [True: 0, False: 36.9M]
Branch (151:22): [True: 0, False: 36.9M]
|
152 | return null_error(); |
153 | } |
154 | |
155 | PyMappingMethods *m = Py_TYPE(o)->tp_as_mapping; |
156 | if (m && m->mp_subscript36.8M ) { Branch (156:9): [True: 36.8M, False: 105k]
Branch (156:14): [True: 36.8M, False: 307]
|
157 | PyObject *item = m->mp_subscript(o, key); |
158 | assert(_Py_CheckSlotResult(o, "__getitem__", item != NULL)); |
159 | return item; |
160 | } |
161 | |
162 | PySequenceMethods *ms = Py_TYPE(o)->tp_as_sequence; |
163 | if (ms && ms->sq_item103k ) { Branch (163:9): [True: 103k, False: 2.53k]
Branch (163:15): [True: 103k, False: 311]
|
164 | if (_PyIndex_Check(key)) { Branch (164:13): [True: 103k, False: 0]
|
165 | Py_ssize_t key_value; |
166 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
167 | if (key_value == -1 && PyErr_Occurred()241 ) Branch (167:17): [True: 241, False: 102k]
Branch (167:36): [True: 0, False: 241]
|
168 | return NULL; |
169 | return PySequence_GetItem(o, key_value); |
170 | } |
171 | else { |
172 | return type_error("sequence index must " |
173 | "be integer, not '%.200s'", key); |
174 | } |
175 | } |
176 | |
177 | if (PyType_Check(o)) { |
178 | PyObject *meth, *result; |
179 | |
180 | // Special case type[int], but disallow other types so str[int] fails |
181 | if ((PyTypeObject*)o == &PyType_Type) { Branch (181:13): [True: 9, False: 2.80k]
|
182 | return Py_GenericAlias(o, key); |
183 | } |
184 | |
185 | if (_PyObject_LookupAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) { Branch (185:13): [True: 0, False: 2.80k]
|
186 | return NULL; |
187 | } |
188 | if (meth && meth != 2.79k Py_None2.79k ) { Branch (188:13): [True: 2.79k, False: 8]
Branch (188:21): [True: 2.79k, False: 1]
|
189 | result = PyObject_CallOneArg(meth, key); |
190 | Py_DECREF(meth); |
191 | return result; |
192 | } |
193 | Py_XDECREF(meth); |
194 | PyErr_Format(PyExc_TypeError, "type '%.200s' is not subscriptable", |
195 | ((PyTypeObject *)o)->tp_name); |
196 | return NULL; |
197 | } |
198 | |
199 | return type_error("'%.200s' object is not subscriptable", o); |
200 | } |
201 | |
202 | int |
203 | PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) |
204 | { |
205 | if (o == NULL || key == NULL || value == NULL) { Branch (205:9): [True: 0, False: 3.96M]
Branch (205:22): [True: 0, False: 3.96M]
Branch (205:37): [True: 0, False: 3.96M]
|
206 | null_error(); |
207 | return -1; |
208 | } |
209 | |
210 | PyMappingMethods *m = Py_TYPE(o)->tp_as_mapping; |
211 | if (m && m->mp_ass_subscript3.95M ) { Branch (211:9): [True: 3.95M, False: 5.01k]
Branch (211:14): [True: 3.95M, False: 77]
|
212 | int res = m->mp_ass_subscript(o, key, value); |
213 | assert(_Py_CheckSlotResult(o, "__setitem__", res >= 0)); |
214 | return res; |
215 | } |
216 | |
217 | if (Py_TYPE(o)->tp_as_sequence) { Branch (217:9): [True: 5.08k, False: 0]
|
218 | if (_PyIndex_Check(key)) { Branch (218:13): [True: 5.03k, False: 59]
|
219 | Py_ssize_t key_value; |
220 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
221 | if (key_value == -1 && PyErr_Occurred()0 ) Branch (221:17): [True: 0, False: 5.03k]
Branch (221:36): [True: 0, False: 0]
|
222 | return -1; |
223 | return PySequence_SetItem(o, key_value, value); |
224 | } |
225 | else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) { Branch (225:18): [True: 2, False: 57]
|
226 | type_error("sequence index must be " |
227 | "integer, not '%.200s'", key); |
228 | return -1; |
229 | } |
230 | } |
231 | |
232 | type_error("'%.200s' object does not support item assignment", o); |
233 | return -1; |
234 | } |
235 | |
236 | int |
237 | PyObject_DelItem(PyObject *o, PyObject *key) |
238 | { |
239 | if (o == NULL || key == NULL) { Branch (239:9): [True: 0, False: 2.35M]
Branch (239:22): [True: 0, False: 2.35M]
|
240 | null_error(); |
241 | return -1; |
242 | } |
243 | |
244 | PyMappingMethods *m = Py_TYPE(o)->tp_as_mapping; |
245 | if (m && m->mp_ass_subscript2.35M ) { Branch (245:9): [True: 2.35M, False: 3.57k]
Branch (245:14): [True: 2.35M, False: 19]
|
246 | int res = m->mp_ass_subscript(o, key, (PyObject*)NULL); |
247 | assert(_Py_CheckSlotResult(o, "__delitem__", res >= 0)); |
248 | return res; |
249 | } |
250 | |
251 | if (Py_TYPE(o)->tp_as_sequence) { Branch (251:9): [True: 3.59k, False: 0]
|
252 | if (_PyIndex_Check(key)) { Branch (252:13): [True: 3.59k, False: 5]
|
253 | Py_ssize_t key_value; |
254 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
255 | if (key_value == -1 && PyErr_Occurred()3 ) Branch (255:17): [True: 3, False: 3.58k]
Branch (255:36): [True: 0, False: 3]
|
256 | return -1; |
257 | return PySequence_DelItem(o, key_value); |
258 | } |
259 | else if (Py_TYPE(o)->tp_as_sequence->sq_ass_item) { Branch (259:18): [True: 0, False: 5]
|
260 | type_error("sequence index must be " |
261 | "integer, not '%.200s'", key); |
262 | return -1; |
263 | } |
264 | } |
265 | |
266 | type_error("'%.200s' object does not support item deletion", o); |
267 | return -1; |
268 | } |
269 | |
270 | int |
271 | PyObject_DelItemString(PyObject *o, const char *key) |
272 | { |
273 | PyObject *okey; |
274 | int ret; |
275 |
|
276 | if (o == NULL || key == NULL) { Branch (276:9): [True: 0, False: 0]
Branch (276:22): [True: 0, False: 0]
|
277 | null_error(); |
278 | return -1; |
279 | } |
280 | okey = PyUnicode_FromString(key); |
281 | if (okey == NULL) Branch (281:9): [True: 0, False: 0]
|
282 | return -1; |
283 | ret = PyObject_DelItem(o, okey); |
284 | Py_DECREF(okey); |
285 | return ret; |
286 | } |
287 | |
288 | |
289 | /* Return 1 if the getbuffer function is available, otherwise return 0. */ |
290 | int |
291 | PyObject_CheckBuffer(PyObject *obj) |
292 | { |
293 | PyBufferProcs *tp_as_buffer = Py_TYPE(obj)->tp_as_buffer; |
294 | return (tp_as_buffer != NULL && tp_as_buffer->bf_getbuffer != NULL1.99M ); Branch (294:13): [True: 1.99M, False: 960k]
Branch (294:37): [True: 1.96M, False: 30.3k]
|
295 | } |
296 | |
297 | |
298 | /* We release the buffer right after use of this function which could |
299 | cause issues later on. Don't use these functions in new code. |
300 | */ |
301 | int |
302 | PyObject_CheckReadBuffer(PyObject *obj) |
303 | { |
304 | PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer; |
305 | Py_buffer view; |
306 |
|
307 | if (pb == NULL || Branch (307:9): [True: 0, False: 0]
|
308 | pb->bf_getbuffer == NULL) Branch (308:9): [True: 0, False: 0]
|
309 | return 0; |
310 | if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) { Branch (310:9): [True: 0, False: 0]
|
311 | PyErr_Clear(); |
312 | return 0; |
313 | } |
314 | PyBuffer_Release(&view); |
315 | return 1; |
316 | } |
317 | |
318 | static int |
319 | as_read_buffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len) |
320 | { |
321 | Py_buffer view; |
322 |
|
323 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { Branch (323:9): [True: 0, False: 0]
Branch (323:24): [True: 0, False: 0]
Branch (323:42): [True: 0, False: 0]
|
324 | null_error(); |
325 | return -1; |
326 | } |
327 | if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) Branch (327:9): [True: 0, False: 0]
|
328 | return -1; |
329 | |
330 | *buffer = view.buf; |
331 | *buffer_len = view.len; |
332 | PyBuffer_Release(&view); |
333 | return 0; |
334 | } |
335 | |
336 | int |
337 | PyObject_AsCharBuffer(PyObject *obj, |
338 | const char **buffer, |
339 | Py_ssize_t *buffer_len) |
340 | { |
341 | return as_read_buffer(obj, (const void **)buffer, buffer_len); |
342 | } |
343 | |
344 | int PyObject_AsReadBuffer(PyObject *obj, |
345 | const void **buffer, |
346 | Py_ssize_t *buffer_len) |
347 | { |
348 | return as_read_buffer(obj, buffer, buffer_len); |
349 | } |
350 | |
351 | int PyObject_AsWriteBuffer(PyObject *obj, |
352 | void **buffer, |
353 | Py_ssize_t *buffer_len) |
354 | { |
355 | PyBufferProcs *pb; |
356 | Py_buffer view; |
357 |
|
358 | if (obj == NULL || buffer == NULL || buffer_len == NULL) { Branch (358:9): [True: 0, False: 0]
Branch (358:24): [True: 0, False: 0]
Branch (358:42): [True: 0, False: 0]
|
359 | null_error(); |
360 | return -1; |
361 | } |
362 | pb = Py_TYPE(obj)->tp_as_buffer; |
363 | if (pb == NULL || Branch (363:9): [True: 0, False: 0]
|
364 | pb->bf_getbuffer == NULL || Branch (364:9): [True: 0, False: 0]
|
365 | ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) { Branch (365:9): [True: 0, False: 0]
|
366 | PyErr_SetString(PyExc_TypeError, |
367 | "expected a writable bytes-like object"); |
368 | return -1; |
369 | } |
370 | |
371 | *buffer = view.buf; |
372 | *buffer_len = view.len; |
373 | PyBuffer_Release(&view); |
374 | return 0; |
375 | } |
376 | |
377 | /* Buffer C-API for Python 3.0 */ |
378 | |
379 | int |
380 | PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) |
381 | { |
382 | PyBufferProcs *pb = Py_TYPE(obj)->tp_as_buffer; |
383 | |
384 | if (pb == NULL || pb->bf_getbuffer == NULL29.1M ) { Branch (384:9): [True: 22.1k, False: 29.1M]
Branch (384:23): [True: 1.08k, False: 29.1M]
|
385 | PyErr_Format(PyExc_TypeError, |
386 | "a bytes-like object is required, not '%.100s'", |
387 | Py_TYPE(obj)->tp_name); |
388 | return -1; |
389 | } |
390 | int res = (*pb->bf_getbuffer)(obj, view, flags); |
391 | assert(_Py_CheckSlotResult(obj, "getbuffer", res >= 0)); |
392 | return res; |
393 | } |
394 | |
395 | static int |
396 | _IsFortranContiguous(const Py_buffer *view) |
397 | { |
398 | Py_ssize_t sd, dim; |
399 | int i; |
400 | |
401 | /* 1) len = product(shape) * itemsize |
402 | 2) itemsize > 0 |
403 | 3) len = 0 <==> exists i: shape[i] = 0 */ |
404 | if (view->len == 0) return 1350 ; Branch (404:9): [True: 350, False: 996k]
|
405 | if (view->strides == NULL) { /* C-contiguous by definition */ Branch (405:9): [True: 6.85k, False: 989k]
|
406 | /* Trivially F-contiguous */ |
407 | if (view->ndim <= 1) return 15.88k ; Branch (407:13): [True: 5.88k, False: 978]
|
408 | |
409 | /* ndim > 1 implies shape != NULL */ |
410 | assert(view->shape != NULL); |
411 | |
412 | /* Effectively 1-d */ |
413 | sd = 0; |
414 | for (i=0; i<view->ndim; i++2.61k ) { Branch (414:19): [True: 2.61k, False: 978]
|
415 | if (view->shape[i] > 1) sd += 11.14k ; Branch (415:17): [True: 1.14k, False: 1.46k]
|
416 | } |
417 | return sd <= 1; |
418 | } |
419 | |
420 | /* strides != NULL implies both of these */ |
421 | assert(view->ndim > 0); |
422 | assert(view->shape != NULL); |
423 | |
424 | sd = view->itemsize; |
425 | for (i=0; i<view->ndim; i++1.01M ) { Branch (425:15): [True: 1.48M, False: 526k]
|
426 | dim = view->shape[i]; |
427 | if (dim > 1 && view->strides[i] != sd1.06M ) { Branch (427:13): [True: 1.06M, False: 414k]
Branch (427:24): [True: 463k, False: 602k]
|
428 | return 0; |
429 | } |
430 | sd *= dim; |
431 | } |
432 | return 1; |
433 | } |
434 | |
435 | static int |
436 | _IsCContiguous(const Py_buffer *view) |
437 | { |
438 | Py_ssize_t sd, dim; |
439 | int i; |
440 | |
441 | /* 1) len = product(shape) * itemsize |
442 | 2) itemsize > 0 |
443 | 3) len = 0 <==> exists i: shape[i] = 0 */ |
444 | if (view->len == 0) return 1307k ; Branch (444:9): [True: 307k, False: 18.5M]
|
445 | if (view->strides == NULL) return 117.2M ; /* C-contiguous by definition */ Branch (445:9): [True: 17.2M, False: 1.31M]
|
446 | |
447 | /* strides != NULL implies both of these */ |
448 | assert(view->ndim > 0); |
449 | assert(view->shape != NULL); |
450 | |
451 | sd = view->itemsize; |
452 | for (i=view->ndim-1; i>=0; i--1.50M ) { Branch (452:26): [True: 1.96M, False: 850k]
|
453 | dim = view->shape[i]; |
454 | if (dim > 1 && view->strides[i] != sd1.47M ) { Branch (454:13): [True: 1.47M, False: 489k]
Branch (454:24): [True: 460k, False: 1.01M]
|
455 | return 0; |
456 | } |
457 | sd *= dim; |
458 | } |
459 | return 1; |
460 | } |
461 | |
462 | int |
463 | PyBuffer_IsContiguous(const Py_buffer *view, char order) |
464 | { |
465 | |
466 | if (view->suboffsets != NULL) return 0474k ; Branch (466:9): [True: 474k, False: 19.8M]
|
467 | |
468 | if (order == 'C') Branch (468:9): [True: 18.8M, False: 1.02M]
|
469 | return _IsCContiguous(view); |
470 | else if (order == 'F') Branch (470:14): [True: 949k, False: 80.6k]
|
471 | return _IsFortranContiguous(view); |
472 | else if (order == 'A') Branch (472:14): [True: 80.6k, False: 0]
|
473 | return (_IsCContiguous(view) || _IsFortranContiguous(view)48.0k ); Branch (473:17): [True: 32.5k, False: 48.0k]
Branch (473:41): [True: 3.24k, False: 44.8k]
|
474 | return 0; |
475 | } |
476 | |
477 | |
478 | void* |
479 | PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices) |
480 | { |
481 | char* pointer; |
482 | int i; |
483 | pointer = (char *)view->buf; |
484 | for (i = 0; i < view->ndim; i++131k ) { Branch (484:17): [True: 131k, False: 82.1k]
|
485 | pointer += view->strides[i]*indices[i]; |
486 | if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)40.2k ) { Branch (486:13): [True: 40.2k, False: 91.7k]
Branch (486:43): [True: 25.0k, False: 15.1k]
|
487 | pointer = *((char**)pointer) + view->suboffsets[i]; |
488 | } |
489 | } |
490 | return (void*)pointer; |
491 | } |
492 | |
493 | |
494 | void |
495 | _Py_add_one_to_index_F(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
496 | { |
497 | int k; |
498 |
|
499 | for (k=0; k<nd; k++) { Branch (499:15): [True: 0, False: 0]
|
500 | if (index[k] < shape[k]-1) { Branch (500:13): [True: 0, False: 0]
|
501 | index[k]++; |
502 | break; |
503 | } |
504 | else { |
505 | index[k] = 0; |
506 | } |
507 | } |
508 | } |
509 | |
510 | void |
511 | _Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
512 | { |
513 | int k; |
514 | |
515 | for (k=nd-1; k>=0; k--2 ) { Branch (515:18): [True: 10, False: 2]
|
516 | if (index[k] < shape[k]-1) { Branch (516:13): [True: 8, False: 2]
|
517 | index[k]++; |
518 | break; |
519 | } |
520 | else { |
521 | index[k] = 0; |
522 | } |
523 | } |
524 | } |
525 | |
526 | Py_ssize_t |
527 | PyBuffer_SizeFromFormat(const char *format) |
528 | { |
529 | PyObject *calcsize = NULL; |
530 | PyObject *res = NULL; |
531 | PyObject *fmt = NULL; |
532 | Py_ssize_t itemsize = -1; |
533 | |
534 | calcsize = _PyImport_GetModuleAttrString("struct", "calcsize"); |
535 | if (calcsize == NULL) { Branch (535:9): [True: 0, False: 3]
|
536 | goto done; |
537 | } |
538 | |
539 | fmt = PyUnicode_FromString(format); |
540 | if (fmt == NULL) { Branch (540:9): [True: 0, False: 3]
|
541 | goto done; |
542 | } |
543 | |
544 | res = PyObject_CallFunctionObjArgs(calcsize, fmt, NULL); |
545 | if (res == NULL) { Branch (545:9): [True: 0, False: 3]
|
546 | goto done; |
547 | } |
548 | |
549 | itemsize = PyLong_AsSsize_t(res); |
550 | if (itemsize < 0) { Branch (550:9): [True: 0, False: 3]
|
551 | goto done; |
552 | } |
553 | |
554 | done: |
555 | Py_XDECREF(calcsize); |
556 | Py_XDECREF(fmt); |
557 | Py_XDECREF(res); |
558 | return itemsize; |
559 | } |
560 | |
561 | int |
562 | PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort) |
563 | { |
564 | int k; |
565 | void (*addone)(int, Py_ssize_t *, const Py_ssize_t *); |
566 | Py_ssize_t *indices, elements; |
567 | char *ptr; |
568 | const char *src; |
569 | |
570 | if (len > view->len) { Branch (570:9): [True: 0, False: 2]
|
571 | len = view->len; |
572 | } |
573 | |
574 | if (PyBuffer_IsContiguous(view, fort)) { Branch (574:9): [True: 0, False: 2]
|
575 | /* simplest copy is all that is needed */ |
576 | memcpy(view->buf, buf, len); |
577 | return 0; |
578 | } |
579 | |
580 | /* Otherwise a more elaborate scheme is needed */ |
581 | |
582 | /* view->ndim <= 64 */ |
583 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim)); |
584 | if (indices == NULL) { Branch (584:9): [True: 0, False: 2]
|
585 | PyErr_NoMemory(); |
586 | return -1; |
587 | } |
588 | for (k=0; 2 k<view->ndim;k++2 ) { Branch (588:15): [True: 2, False: 2]
|
589 | indices[k] = 0; |
590 | } |
591 | |
592 | if (fort == 'F') { Branch (592:9): [True: 0, False: 2]
|
593 | addone = _Py_add_one_to_index_F; |
594 | } |
595 | else { |
596 | addone = _Py_add_one_to_index_C; |
597 | } |
598 | src = buf; |
599 | /* XXX : This is not going to be the fastest code in the world |
600 | several optimizations are possible. |
601 | */ |
602 | elements = len / view->itemsize; |
603 | while (elements--) { Branch (603:12): [True: 10, False: 2]
|
604 | ptr = PyBuffer_GetPointer(view, indices); |
605 | memcpy(ptr, src, view->itemsize); |
606 | src += view->itemsize; |
607 | addone(view->ndim, indices, view->shape); |
608 | } |
609 | |
610 | PyMem_Free(indices); |
611 | return 0; |
612 | } |
613 | |
614 | int PyObject_CopyData(PyObject *dest, PyObject *src) |
615 | { |
616 | Py_buffer view_dest, view_src; |
617 | int k; |
618 | Py_ssize_t *indices, elements; |
619 | char *dptr, *sptr; |
620 |
|
621 | if (!PyObject_CheckBuffer(dest) || Branch (621:9): [True: 0, False: 0]
|
622 | !PyObject_CheckBuffer(src)) { Branch (622:9): [True: 0, False: 0]
|
623 | PyErr_SetString(PyExc_TypeError, |
624 | "both destination and source must be "\ |
625 | "bytes-like objects"); |
626 | return -1; |
627 | } |
628 | |
629 | if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1; Branch (629:9): [True: 0, False: 0]
|
630 | if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) { Branch (630:9): [True: 0, False: 0]
|
631 | PyBuffer_Release(&view_dest); |
632 | return -1; |
633 | } |
634 | |
635 | if (view_dest.len < view_src.len) { Branch (635:9): [True: 0, False: 0]
|
636 | PyErr_SetString(PyExc_BufferError, |
637 | "destination is too small to receive data from source"); |
638 | PyBuffer_Release(&view_dest); |
639 | PyBuffer_Release(&view_src); |
640 | return -1; |
641 | } |
642 | |
643 | if ((PyBuffer_IsContiguous(&view_dest, 'C') && Branch (643:10): [True: 0, False: 0]
|
644 | PyBuffer_IsContiguous(&view_src, 'C')) || Branch (644:10): [True: 0, False: 0]
|
645 | (PyBuffer_IsContiguous(&view_dest, 'F') && Branch (645:10): [True: 0, False: 0]
|
646 | PyBuffer_IsContiguous(&view_src, 'F'))) { Branch (646:10): [True: 0, False: 0]
|
647 | /* simplest copy is all that is needed */ |
648 | memcpy(view_dest.buf, view_src.buf, view_src.len); |
649 | PyBuffer_Release(&view_dest); |
650 | PyBuffer_Release(&view_src); |
651 | return 0; |
652 | } |
653 | |
654 | /* Otherwise a more elaborate copy scheme is needed */ |
655 | |
656 | /* XXX(nnorwitz): need to check for overflow! */ |
657 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim); |
658 | if (indices == NULL) { Branch (658:9): [True: 0, False: 0]
|
659 | PyErr_NoMemory(); |
660 | PyBuffer_Release(&view_dest); |
661 | PyBuffer_Release(&view_src); |
662 | return -1; |
663 | } |
664 | for (k=0; k<view_src.ndim;k++) { Branch (664:15): [True: 0, False: 0]
|
665 | indices[k] = 0; |
666 | } |
667 | elements = 1; |
668 | for (k=0; k<view_src.ndim; k++) { Branch (668:15): [True: 0, False: 0]
|
669 | /* XXX(nnorwitz): can this overflow? */ |
670 | elements *= view_src.shape[k]; |
671 | } |
672 | while (elements--) { Branch (672:12): [True: 0, False: 0]
|
673 | _Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape); |
674 | dptr = PyBuffer_GetPointer(&view_dest, indices); |
675 | sptr = PyBuffer_GetPointer(&view_src, indices); |
676 | memcpy(dptr, sptr, view_src.itemsize); |
677 | } |
678 | PyMem_Free(indices); |
679 | PyBuffer_Release(&view_dest); |
680 | PyBuffer_Release(&view_src); |
681 | return 0; |
682 | } |
683 | |
684 | void |
685 | PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape, |
686 | Py_ssize_t *strides, int itemsize, |
687 | char fort) |
688 | { |
689 | int k; |
690 | Py_ssize_t sd; |
691 |
|
692 | sd = itemsize; |
693 | if (fort == 'F') { Branch (693:9): [True: 0, False: 0]
|
694 | for (k=0; k<nd; k++) { Branch (694:19): [True: 0, False: 0]
|
695 | strides[k] = sd; |
696 | sd *= shape[k]; |
697 | } |
698 | } |
699 | else { |
700 | for (k=nd-1; k>=0; k--) { Branch (700:22): [True: 0, False: 0]
|
701 | strides[k] = sd; |
702 | sd *= shape[k]; |
703 | } |
704 | } |
705 | return; |
706 | } |
707 | |
708 | int |
709 | PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, |
710 | int readonly, int flags) |
711 | { |
712 | if (view == NULL) { Branch (712:9): [True: 1, False: 24.6M]
|
713 | PyErr_SetString(PyExc_BufferError, |
714 | "PyBuffer_FillInfo: view==NULL argument is obsolete"); |
715 | return -1; |
716 | } |
717 | |
718 | if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) && Branch (718:9): [True: 233k, False: 24.4M]
|
719 | (readonly == 1)233k ) { Branch (719:9): [True: 19, False: 233k]
|
720 | PyErr_SetString(PyExc_BufferError, |
721 | "Object is not writable."); |
722 | return -1; |
723 | } |
724 | |
725 | view->obj = obj; |
726 | if (obj) Branch (726:9): [True: 20.6M, False: 4.00M]
|
727 | Py_INCREF(obj); |
728 | view->buf = buf; |
729 | view->len = len; |
730 | view->readonly = readonly; |
731 | view->itemsize = 1; |
732 | view->format = NULL; |
733 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) Branch (733:9): [True: 526k, False: 24.1M]
|
734 | view->format = "B"; |
735 | view->ndim = 1; |
736 | view->shape = NULL; |
737 | if ((flags & PyBUF_ND) == PyBUF_ND) Branch (737:9): [True: 8.09M, False: 16.5M]
|
738 | view->shape = &(view->len); |
739 | view->strides = NULL; |
740 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) Branch (740:9): [True: 526k, False: 24.1M]
|
741 | view->strides = &(view->itemsize); |
742 | view->suboffsets = NULL; |
743 | view->internal = NULL; |
744 | return 0; |
745 | } |
746 | |
747 | void |
748 | PyBuffer_Release(Py_buffer *view) |
749 | { |
750 | PyObject *obj = view->obj; |
751 | PyBufferProcs *pb; |
752 | if (obj == NULL) Branch (752:9): [True: 4.49M, False: 30.6M]
|
753 | return; |
754 | pb = Py_TYPE(obj)->tp_as_buffer; |
755 | if (pb && pb->bf_releasebuffer30.6M ) { Branch (755:9): [True: 30.6M, False: 14]
Branch (755:15): [True: 10.0M, False: 20.6M]
|
756 | pb->bf_releasebuffer(obj, view); |
757 | } |
758 | view->obj = NULL; |
759 | Py_DECREF(obj); |
760 | } |
761 | |
762 | PyObject * |
763 | PyObject_Format(PyObject *obj, PyObject *format_spec) |
764 | { |
765 | PyObject *meth; |
766 | PyObject *empty = NULL; |
767 | PyObject *result = NULL; |
768 | |
769 | if (format_spec != NULL && !17.7k PyUnicode_Check17.7k (format_spec)) { Branch (769:9): [True: 17.7k, False: 167k]
Branch (769:32): [True: 0, False: 17.7k]
|
770 | PyErr_Format(PyExc_SystemError, |
771 | "Format specifier must be a string, not %.200s", |
772 | Py_TYPE(format_spec)->tp_name); |
773 | return NULL; |
774 | } |
775 | |
776 | /* Fast path for common types. */ |
777 | if (format_spec == NULL || PyUnicode_GET_LENGTH17.7k (format_spec) == 017.7k ) { Branch (777:9): [True: 167k, False: 17.7k]
Branch (777:32): [True: 985, False: 16.7k]
|
778 | if (PyUnicode_CheckExact(obj)) { |
779 | Py_INCREF(obj); |
780 | return obj; |
781 | } |
782 | if (PyLong_CheckExact(obj)) { |
783 | return PyObject_Str(obj); |
784 | } |
785 | } |
786 | |
787 | /* If no format_spec is provided, use an empty string */ |
788 | if (format_spec == NULL) { Branch (788:9): [True: 6.58k, False: 17.4k]
|
789 | empty = PyUnicode_New(0, 0); |
790 | format_spec = empty; |
791 | } |
792 | |
793 | /* Find the (unbound!) __format__ method */ |
794 | meth = _PyObject_LookupSpecial(obj, &_Py_ID(__format__)); |
795 | if (meth == NULL) { Branch (795:9): [True: 1, False: 24.0k]
|
796 | PyThreadState *tstate = _PyThreadState_GET(); |
797 | if (!_PyErr_Occurred(tstate)) { Branch (797:13): [True: 0, False: 1]
|
798 | _PyErr_Format(tstate, PyExc_TypeError, |
799 | "Type %.100s doesn't define __format__", |
800 | Py_TYPE(obj)->tp_name); |
801 | } |
802 | goto done; |
803 | } |
804 | |
805 | /* And call it. */ |
806 | result = PyObject_CallOneArg(meth, format_spec); |
807 | Py_DECREF(meth); |
808 | |
809 | if (result && !23.7k PyUnicode_Check23.7k (result)) { Branch (809:9): [True: 23.7k, False: 357]
Branch (809:19): [True: 1, False: 23.7k]
|
810 | PyErr_Format(PyExc_TypeError, |
811 | "__format__ must return a str, not %.200s", |
812 | Py_TYPE(result)->tp_name); |
813 | Py_DECREF(result); |
814 | result = NULL; |
815 | goto done; |
816 | } |
817 | |
818 | done: |
819 | Py_XDECREF(empty); |
820 | return result; |
821 | } |
822 | /* Operations on numbers */ |
823 | |
824 | int |
825 | PyNumber_Check(PyObject *o) |
826 | { |
827 | if (o == NULL) Branch (827:9): [True: 0, False: 9.37M]
|
828 | return 0; |
829 | PyNumberMethods *nb = Py_TYPE(o)->tp_as_number; |
830 | return nb && (9.37M nb->nb_index9.37M || nb->nb_int82.9k || nb->nb_float76.3k || PyComplex_Check9.37M (o)); Branch (830:12): [True: 9.37M, False: 1.61k]
Branch (830:19): [True: 9.29M, False: 82.9k]
Branch (830:35): [True: 6.54k, False: 76.3k]
Branch (830:49): [True: 0, False: 76.3k]
|
831 | } |
832 | |
833 | /* Binary operators */ |
834 | |
835 | #define NB_SLOT(x) offsetof(PyNumberMethods, x) |
836 | #define NB_BINOP(nb_methods, slot) \ |
837 | (*(binaryfunc*)(& ((char*)nb_methods)[slot])) |
838 | #define NB_TERNOP(nb_methods, slot) \ |
839 | (*(ternaryfunc*)(& ((char*)nb_methods)[slot])) |
840 | |
841 | /* |
842 | Calling scheme used for binary operations: |
843 | |
844 | Order operations are tried until either a valid result or error: |
845 | w.op(v,w)[*], v.op(v,w), w.op(v,w) |
846 | |
847 | [*] only when Py_TYPE(v) != Py_TYPE(w) && Py_TYPE(w) is a subclass of |
848 | Py_TYPE(v) |
849 | */ |
850 | |
851 | static PyObject * |
852 | binary_op1(PyObject *v, PyObject *w, const int op_slot |
853 | #ifndef NDEBUG |
854 | , const char *op_name |
855 | #endif |
856 | ) |
857 | { |
858 | binaryfunc slotv; |
859 | if (Py_TYPE(v)->tp_as_number != NULL) { Branch (859:9): [True: 47.8M, False: 475k]
|
860 | slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot); |
861 | } |
862 | else { |
863 | slotv = NULL; |
864 | } |
865 | |
866 | binaryfunc slotw; |
867 | if (!Py_IS_TYPE(w, Py_TYPE(v)) && Py_TYPE9.23M (w)->tp_as_number != NULL9.23M ) { Branch (867:9): [True: 9.23M, False: 39.1M]
Branch (867:39): [True: 6.21M, False: 3.02M]
|
868 | slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot); |
869 | if (slotw == slotv) { Branch (869:13): [True: 259k, False: 5.95M]
|
870 | slotw = NULL; |
871 | } |
872 | } |
873 | else { |
874 | slotw = NULL; |
875 | } |
876 | |
877 | if (slotv) { Branch (877:9): [True: 42.7M, False: 5.60M]
|
878 | PyObject *x; |
879 | if (slotw && PyType_IsSubtype(5.14M Py_TYPE5.14M (w), Py_TYPE5.14M (v))) { Branch (879:13): [True: 5.14M, False: 37.5M]
Branch (879:22): [True: 184k, False: 4.96M]
|
880 | x = slotw(v, w); |
881 | if (x != Py_NotImplemented) Branch (881:17): [True: 184k, False: 0]
|
882 | return x; |
883 | Py_DECREF(x); /* can't do it */ |
884 | slotw = NULL; |
885 | } |
886 | x = slotv(v, w); |
887 | assert(_Py_CheckSlotResult(v, op_name, x != NULL)); |
888 | if (x != Py_NotImplemented) { Branch (888:13): [True: 41.6M, False: 945k]
|
889 | return x; |
890 | } |
891 | Py_DECREF(x); /* can't do it */ |
892 | } |
893 | if (slotw) { Branch (893:9): [True: 1.56M, False: 4.99M]
|
894 | PyObject *x = slotw(v, w); |
895 | assert(_Py_CheckSlotResult(w, op_name, x != NULL)); |
896 | if (x != Py_NotImplemented) { Branch (896:13): [True: 856k, False: 704k]
|
897 | return x; |
898 | } |
899 | Py_DECREF(x); /* can't do it */ |
900 | } |
901 | Py_RETURN_NOTIMPLEMENTED5.69M ; |
902 | } |
903 | |
904 | #ifdef NDEBUG |
905 | # define BINARY_OP1(v, w, op_slot, op_name) binary_op1(v, w, op_slot) |
906 | #else |
907 | # define BINARY_OP1(v, w, op_slot, op_name) binary_op1(v, w, op_slot, op_name) |
908 | #endif |
909 | |
910 | static PyObject * |
911 | binop_type_error(PyObject *v, PyObject *w, const char *op_name) |
912 | { |
913 | PyErr_Format(PyExc_TypeError, |
914 | "unsupported operand type(s) for %.100s: " |
915 | "'%.100s' and '%.100s'", |
916 | op_name, |
917 | Py_TYPE(v)->tp_name, |
918 | Py_TYPE(w)->tp_name); |
919 | return NULL; |
920 | } |
921 | |
922 | static PyObject * |
923 | binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) |
924 | { |
925 | PyObject *result = BINARY_OP1(v, w, op_slot, op_name); |
926 | if (result == Py_NotImplemented) { Branch (926:9): [True: 294, False: 25.9M]
|
927 | Py_DECREF(result); |
928 | |
929 | if (op_slot == NB_SLOT(nb_rshift) && Branch (929:13): [True: 21, False: 273]
|
930 | PyCFunction_CheckExact(v) && |
931 | strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 03 ) Branch (931:13): [True: 2, False: 1]
|
932 | { |
933 | PyErr_Format(PyExc_TypeError, |
934 | "unsupported operand type(s) for %.100s: " |
935 | "'%.100s' and '%.100s'. Did you mean \"print(<message>, " |
936 | "file=<output_stream>)\"?", |
937 | op_name, |
938 | Py_TYPE(v)->tp_name, |
939 | Py_TYPE(w)->tp_name); |
940 | return NULL; |
941 | } |
942 | return binop_type_error(v, w, op_name); |
943 | } |
944 | return result; |
945 | } |
946 | |
947 | |
948 | /* |
949 | Calling scheme used for ternary operations: |
950 | |
951 | Order operations are tried until either a valid result or error: |
952 | v.op(v,w,z), w.op(v,w,z), z.op(v,w,z) |
953 | */ |
954 | |
955 | static PyObject * |
956 | ternary_op(PyObject *v, |
957 | PyObject *w, |
958 | PyObject *z, |
959 | const int op_slot, |
960 | const char *op_name |
961 | ) |
962 | { |
963 | PyNumberMethods *mv = Py_TYPE(v)->tp_as_number; |
964 | PyNumberMethods *mw = Py_TYPE(w)->tp_as_number; |
965 | |
966 | ternaryfunc slotv; |
967 | if (mv != NULL) { Branch (967:9): [True: 2.54M, False: 1]
|
968 | slotv = NB_TERNOP(mv, op_slot); |
969 | } |
970 | else { |
971 | slotv = NULL; |
972 | } |
973 | |
974 | ternaryfunc slotw; |
975 | if (!Py_IS_TYPE(w, Py_TYPE(v)) && mw != NULL358k ) { Branch (975:9): [True: 358k, False: 2.18M]
Branch (975:39): [True: 358k, False: 0]
|
976 | slotw = NB_TERNOP(mw, op_slot); |
977 | if (slotw == slotv) { Branch (977:13): [True: 16, False: 358k]
|
978 | slotw = NULL; |
979 | } |
980 | } |
981 | else { |
982 | slotw = NULL; |
983 | } |
984 | |
985 | if (slotv) { Branch (985:9): [True: 2.54M, False: 10]
|
986 | PyObject *x; |
987 | if (slotw && PyType_IsSubtype(358k Py_TYPE358k (w), Py_TYPE358k (v))) { Branch (987:13): [True: 358k, False: 2.18M]
Branch (987:22): [True: 1, False: 358k]
|
988 | x = slotw(v, w, z); |
989 | if (x != Py_NotImplemented) { Branch (989:17): [True: 1, False: 0]
|
990 | return x; |
991 | } |
992 | Py_DECREF(x); /* can't do it */ |
993 | slotw = NULL; |
994 | } |
995 | x = slotv(v, w, z); |
996 | assert(_Py_CheckSlotResult(v, op_name, x != NULL)); |
997 | if (x != Py_NotImplemented) { Branch (997:13): [True: 2.54M, False: 134]
|
998 | return x; |
999 | } |
1000 | Py_DECREF(x); /* can't do it */ |
1001 | } |
1002 | if (slotw) { Branch (1002:9): [True: 139, False: 5]
|
1003 | PyObject *x = slotw(v, w, z); |
1004 | assert(_Py_CheckSlotResult(w, op_name, x != NULL)); |
1005 | if (x != Py_NotImplemented) { Branch (1005:13): [True: 128, False: 11]
|
1006 | return x; |
1007 | } |
1008 | Py_DECREF(x); /* can't do it */ |
1009 | } |
1010 | |
1011 | PyNumberMethods *mz = Py_TYPE(z)->tp_as_number; |
1012 | if (mz != NULL) { Branch (1012:9): [True: 16, False: 0]
|
1013 | ternaryfunc slotz = NB_TERNOP(mz, op_slot); |
1014 | if (slotz == slotv || slotz == slotw8 ) { Branch (1014:13): [True: 8, False: 8]
Branch (1014:31): [True: 2, False: 6]
|
1015 | slotz = NULL; |
1016 | } |
1017 | if (slotz) { Branch (1017:13): [True: 1, False: 15]
|
1018 | PyObject *x = slotz(v, w, z); |
1019 | assert(_Py_CheckSlotResult(z, op_name, x != NULL)); |
1020 | if (x != Py_NotImplemented) { Branch (1020:17): [True: 1, False: 0]
|
1021 | return x; |
1022 | } |
1023 | Py_DECREF(x); /* can't do it */ |
1024 | } |
1025 | } |
1026 | |
1027 | if (z == Py_None) { Branch (1027:9): [True: 13, False: 2]
|
1028 | PyErr_Format( |
1029 | PyExc_TypeError, |
1030 | "unsupported operand type(s) for %.100s: " |
1031 | "'%.100s' and '%.100s'", |
1032 | op_name, |
1033 | Py_TYPE(v)->tp_name, |
1034 | Py_TYPE(w)->tp_name); |
1035 | } |
1036 | else { |
1037 | PyErr_Format( |
1038 | PyExc_TypeError, |
1039 | "unsupported operand type(s) for %.100s: " |
1040 | "'%.100s', '%.100s', '%.100s'", |
1041 | op_name, |
1042 | Py_TYPE(v)->tp_name, |
1043 | Py_TYPE(w)->tp_name, |
1044 | Py_TYPE(z)->tp_name); |
1045 | } |
1046 | return NULL; |
1047 | } |
1048 | |
1049 | #define BINARY_FUNC(func, op, op_name) \ |
1050 | PyObject * \ |
1051 | func(PyObject *v, PyObject *w) { \ |
1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ |
1053 | } Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
Line | Count | Source | 1051 | func(PyObject *v, PyObject *w) { \ | 1052 | return binary_op(v, w, NB_SLOT(op), op_name); \ | 1053 | } |
|
1054 | |
1055 | BINARY_FUNC(PyNumber_Or, nb_or, "|") |
1056 | BINARY_FUNC(PyNumber_Xor, nb_xor, "^") |
1057 | BINARY_FUNC(PyNumber_And, nb_and, "&") |
1058 | BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<") |
1059 | BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>") |
1060 | BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-") |
1061 | BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()") |
1062 | |
1063 | PyObject * |
1064 | PyNumber_Add(PyObject *v, PyObject *w) |
1065 | { |
1066 | PyObject *result = BINARY_OP1(v, w, NB_SLOT(nb_add), "+"); |
1067 | if (result != Py_NotImplemented) { Branch (1067:9): [True: 5.61M, False: 4.70M]
|
1068 | return result; |
1069 | } |
1070 | Py_DECREF(result); |
1071 | |
1072 | PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence; |
1073 | if (m && m->sq_concat4.70M ) { Branch (1073:9): [True: 4.70M, False: 224]
Branch (1073:14): [True: 4.70M, False: 33]
|
1074 | result = (*m->sq_concat)(v, w); |
1075 | assert(_Py_CheckSlotResult(v, "+", result != NULL)); |
1076 | return result; |
1077 | } |
1078 | |
1079 | return binop_type_error(v, w, "+"); |
1080 | } |
1081 | |
1082 | static PyObject * |
1083 | sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n) |
1084 | { |
1085 | Py_ssize_t count; |
1086 | if (_PyIndex_Check(n)) { Branch (1086:9): [True: 793k, False: 16]
|
1087 | count = PyNumber_AsSsize_t(n, PyExc_OverflowError); |
1088 | if (count == -1 && PyErr_Occurred()3.16k ) { Branch (1088:13): [True: 3.16k, False: 790k]
Branch (1088:28): [True: 2, False: 3.16k]
|
1089 | return NULL; |
1090 | } |
1091 | } |
1092 | else { |
1093 | return type_error("can't multiply sequence by " |
1094 | "non-int of type '%.200s'", n); |
1095 | } |
1096 | PyObject *res = (*repeatfunc)(seq, count); |
1097 | assert(_Py_CheckSlotResult(seq, "*", res != NULL)); |
1098 | return res; |
1099 | } |
1100 | |
1101 | PyObject * |
1102 | PyNumber_Multiply(PyObject *v, PyObject *w) |
1103 | { |
1104 | PyObject *result = BINARY_OP1(v, w, NB_SLOT(nb_multiply), "*"); |
1105 | if (result == Py_NotImplemented) { Branch (1105:9): [True: 793k, False: 6.70M]
|
1106 | PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence; |
1107 | PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence; |
1108 | Py_DECREF(result); |
1109 | if (mv && mv->sq_repeat704k ) { Branch (1109:14): [True: 704k, False: 88.7k]
Branch (1109:20): [True: 704k, False: 31]
|
1110 | return sequence_repeat(mv->sq_repeat, v, w); |
1111 | } |
1112 | else if (mw && mw->sq_repeat88.7k ) { Branch (1112:18): [True: 88.7k, False: 16]
Branch (1112:24): [True: 88.7k, False: 17]
|
1113 | return sequence_repeat(mw->sq_repeat, w, v); |
1114 | } |
1115 | result = binop_type_error(v, w, "*"); |
1116 | } |
1117 | return result; |
1118 | } |
1119 | |
1120 | PyObject * |
1121 | PyNumber_MatrixMultiply(PyObject *v, PyObject *w) |
1122 | { |
1123 | return binary_op(v, w, NB_SLOT(nb_matrix_multiply), "@"); |
1124 | } |
1125 | |
1126 | PyObject * |
1127 | PyNumber_FloorDivide(PyObject *v, PyObject *w) |
1128 | { |
1129 | return binary_op(v, w, NB_SLOT(nb_floor_divide), "//"); |
1130 | } |
1131 | |
1132 | PyObject * |
1133 | PyNumber_TrueDivide(PyObject *v, PyObject *w) |
1134 | { |
1135 | return binary_op(v, w, NB_SLOT(nb_true_divide), "/"); |
1136 | } |
1137 | |
1138 | PyObject * |
1139 | PyNumber_Remainder(PyObject *v, PyObject *w) |
1140 | { |
1141 | return binary_op(v, w, NB_SLOT(nb_remainder), "%"); |
1142 | } |
1143 | |
1144 | PyObject * |
1145 | PyNumber_Power(PyObject *v, PyObject *w, PyObject *z) |
1146 | { |
1147 | return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()"); |
1148 | } |
1149 | |
1150 | PyObject * |
1151 | _PyNumber_PowerNoMod(PyObject *lhs, PyObject *rhs) |
1152 | { |
1153 | return PyNumber_Power(lhs, rhs, Py_None); |
1154 | } |
1155 | |
1156 | /* Binary in-place operators */ |
1157 | |
1158 | /* The in-place operators are defined to fall back to the 'normal', |
1159 | non in-place operations, if the in-place methods are not in place. |
1160 | |
1161 | - If the left hand object has the appropriate struct members, and |
1162 | they are filled, call the appropriate function and return the |
1163 | result. No coercion is done on the arguments; the left-hand object |
1164 | is the one the operation is performed on, and it's up to the |
1165 | function to deal with the right-hand object. |
1166 | |
1167 | - Otherwise, in-place modification is not supported. Handle it exactly as |
1168 | a non in-place operation of the same kind. |
1169 | |
1170 | */ |
1171 | |
1172 | static PyObject * |
1173 | binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot |
1174 | #ifndef NDEBUG |
1175 | , const char *op_name |
1176 | #endif |
1177 | ) |
1178 | { |
1179 | PyNumberMethods *mv = Py_TYPE(v)->tp_as_number; |
1180 | if (mv != NULL) { Branch (1180:9): [True: 4.88M, False: 21.2k]
|
1181 | binaryfunc slot = NB_BINOP(mv, iop_slot); |
1182 | if (slot) { Branch (1182:13): [True: 295k, False: 4.59M]
|
1183 | PyObject *x = (slot)(v, w); |
1184 | assert(_Py_CheckSlotResult(v, op_name, x != NULL)); |
1185 | if (x != Py_NotImplemented) { Branch (1185:17): [True: 295k, False: 60]
|
1186 | return x; |
1187 | } |
1188 | Py_DECREF(x); |
1189 | } |
1190 | } |
1191 | #ifdef NDEBUG |
1192 | return binary_op1(v, w, op_slot); |
1193 | #else |
1194 | return binary_op1(v, w, op_slot, op_name); |
1195 | #endif |
1196 | } |
1197 | |
1198 | #ifdef NDEBUG |
1199 | # define BINARY_IOP1(v, w, iop_slot, op_slot, op_name) binary_iop1(v, w, iop_slot, op_slot) |
1200 | #else |
1201 | # define BINARY_IOP1(v, w, iop_slot, op_slot, op_name) binary_iop1(v, w, iop_slot, op_slot, op_name) |
1202 | #endif |
1203 | |
1204 | static PyObject * |
1205 | binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, |
1206 | const char *op_name) |
1207 | { |
1208 | PyObject *result = BINARY_IOP1(v, w, iop_slot, op_slot, op_name); |
1209 | if (result == Py_NotImplemented) { Branch (1209:9): [True: 85, False: 4.27M]
|
1210 | Py_DECREF(result); |
1211 | return binop_type_error(v, w, op_name); |
1212 | } |
1213 | return result; |
1214 | } |
1215 | |
1216 | static PyObject * |
1217 | ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int op_slot, |
1218 | const char *op_name) |
1219 | { |
1220 | PyNumberMethods *mv = Py_TYPE(v)->tp_as_number; |
1221 | if (mv != NULL) { Branch (1221:9): [True: 20, False: 1]
|
1222 | ternaryfunc slot = NB_TERNOP(mv, iop_slot); |
1223 | if (slot) { Branch (1223:13): [True: 9, False: 11]
|
1224 | PyObject *x = (slot)(v, w, z); |
1225 | if (x != Py_NotImplemented) { Branch (1225:17): [True: 4, False: 5]
|
1226 | return x; |
1227 | } |
1228 | Py_DECREF(x); |
1229 | } |
1230 | } |
1231 | return ternary_op(v, w, z, op_slot, op_name); |
1232 | } |
1233 | |
1234 | #define INPLACE_BINOP(func, iop, op, op_name) \ |
1235 | PyObject * \ |
1236 | func(PyObject *v, PyObject *w) { \ |
1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ |
1238 | } Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
PyNumber_InPlaceMatrixMultiply Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
PyNumber_InPlaceFloorDivide Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
PyNumber_InPlaceTrueDivide Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
PyNumber_InPlaceRemainder Line | Count | Source | 1236 | func(PyObject *v, PyObject *w) { \ | 1237 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ | 1238 | } |
|
1239 | |
1240 | INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=") |
1241 | INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=") |
1242 | INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=") |
1243 | INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=") |
1244 | INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=") |
1245 | INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=") |
1246 | INPLACE_BINOP(PyNumber_InPlaceMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=") |
1247 | INPLACE_BINOP(PyNumber_InPlaceFloorDivide, nb_inplace_floor_divide, nb_floor_divide, "//=") |
1248 | INPLACE_BINOP(PyNumber_InPlaceTrueDivide, nb_inplace_true_divide, nb_true_divide, "/=") |
1249 | INPLACE_BINOP(PyNumber_InPlaceRemainder, nb_inplace_remainder, nb_remainder, "%=") |
1250 | |
1251 | PyObject * |
1252 | PyNumber_InPlaceAdd(PyObject *v, PyObject *w) |
1253 | { |
1254 | PyObject *result = BINARY_IOP1(v, w, NB_SLOT(nb_inplace_add), |
1255 | NB_SLOT(nb_add), "+="); |
1256 | if (result == Py_NotImplemented) { Branch (1256:9): [True: 196k, False: 424k]
|
1257 | PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence; |
1258 | Py_DECREF(result); |
1259 | if (m != NULL) { Branch (1259:13): [True: 196k, False: 0]
|
1260 | binaryfunc func = m->sq_inplace_concat; |
1261 | if (func == NULL) Branch (1261:17): [True: 162k, False: 34.1k]
|
1262 | func = m->sq_concat; |
1263 | if (func != NULL) { Branch (1263:17): [True: 196k, False: 7]
|
1264 | result = func(v, w); |
1265 | assert(_Py_CheckSlotResult(v, "+=", result != NULL)); |
1266 | return result; |
1267 | } |
1268 | } |
1269 | result = binop_type_error(v, w, "+="); |
1270 | } |
1271 | return result; |
1272 | } |
1273 | |
1274 | PyObject * |
1275 | PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) |
1276 | { |
1277 | PyObject *result = BINARY_IOP1(v, w, NB_SLOT(nb_inplace_multiply), |
1278 | NB_SLOT(nb_multiply), "*="); |
1279 | if (result == Py_NotImplemented) { Branch (1279:9): [True: 356, False: 10.3k]
|
1280 | ssizeargfunc f = NULL; |
1281 | PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence; |
1282 | PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence; |
1283 | Py_DECREF(result); |
1284 | if (mv != NULL) { Branch (1284:13): [True: 203, False: 153]
|
1285 | f = mv->sq_inplace_repeat; |
1286 | if (f == NULL) Branch (1286:17): [True: 48, False: 155]
|
1287 | f = mv->sq_repeat; |
1288 | if (f != NULL) Branch (1288:17): [True: 197, False: 6]
|
1289 | return sequence_repeat(f, v, w); |
1290 | } |
1291 | else if (mw != NULL) { Branch (1291:18): [True: 153, False: 0]
|
1292 | /* Note that the right hand operand should not be |
1293 | * mutated in this case so sq_inplace_repeat is not |
1294 | * used. */ |
1295 | if (mw->sq_repeat) Branch (1295:17): [True: 153, False: 0]
|
1296 | return sequence_repeat(mw->sq_repeat, w, v); |
1297 | } |
1298 | result = binop_type_error(v, w, "*="); |
1299 | } |
1300 | return result; |
1301 | } |
1302 | |
1303 | PyObject * |
1304 | PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z) |
1305 | { |
1306 | return ternary_iop(v, w, z, NB_SLOT(nb_inplace_power), |
1307 | NB_SLOT(nb_power), "**="); |
1308 | } |
1309 | |
1310 | PyObject * |
1311 | _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs) |
1312 | { |
1313 | return PyNumber_InPlacePower(lhs, rhs, Py_None); |
1314 | } |
1315 | |
1316 | |
1317 | /* Unary operators and functions */ |
1318 | |
1319 | PyObject * |
1320 | PyNumber_Negative(PyObject *o) |
1321 | { |
1322 | if (o == NULL) { Branch (1322:9): [True: 0, False: 1.02M]
|
1323 | return null_error(); |
1324 | } |
1325 | |
1326 | PyNumberMethods *m = Py_TYPE(o)->tp_as_number; |
1327 | if (m && m->nb_negative) { Branch (1327:9): [True: 1.02M, False: 0]
Branch (1327:14): [True: 1.02M, False: 8]
|
1328 | PyObject *res = (*m->nb_negative)(o); |
1329 | assert(_Py_CheckSlotResult(o, "__neg__", res != NULL)); |
1330 | return res; |
1331 | } |
1332 | |
1333 | return type_error("bad operand type for unary -: '%.200s'", o); |
1334 | } |
1335 | |
1336 | PyObject * |
1337 | PyNumber_Positive(PyObject *o) |
1338 | { |
1339 | if (o == NULL) { Branch (1339:9): [True: 0, False: 565]
|
1340 | return null_error(); |
1341 | } |
1342 | |
1343 | PyNumberMethods *m = Py_TYPE(o)->tp_as_number; |
1344 | if (m && m->nb_positive) { Branch (1344:9): [True: 565, False: 0]
Branch (1344:14): [True: 559, False: 6]
|
1345 | PyObject *res = (*m->nb_positive)(o); |
1346 | assert(_Py_CheckSlotResult(o, "__pos__", res != NULL)); |
1347 | return res; |
1348 | } |
1349 | |
1350 | return type_error("bad operand type for unary +: '%.200s'", o); |
1351 | } |
1352 | |
1353 | PyObject * |
1354 | PyNumber_Invert(PyObject *o) |
1355 | { |
1356 | if (o == NULL) { Branch (1356:9): [True: 0, False: 1.51M]
|
1357 | return null_error(); |
1358 | } |
1359 | |
1360 | PyNumberMethods *m = Py_TYPE(o)->tp_as_number; |
1361 | if (m && m->nb_invert) { Branch (1361:9): [True: 1.51M, False: 0]
Branch (1361:14): [True: 1.51M, False: 11]
|
1362 | PyObject *res = (*m->nb_invert)(o); |
1363 | assert(_Py_CheckSlotResult(o, "__invert__", res != NULL)); |
1364 | return res; |
1365 | } |
1366 | |
1367 | return type_error("bad operand type for unary ~: '%.200s'", o); |
1368 | } |
1369 | |
1370 | PyObject * |
1371 | PyNumber_Absolute(PyObject *o) |
1372 | { |
1373 | if (o == NULL) { Branch (1373:9): [True: 0, False: 2.82M]
|
1374 | return null_error(); |
1375 | } |
1376 | |
1377 | PyNumberMethods *m = Py_TYPE(o)->tp_as_number; |
1378 | if (m && m->nb_absolute) { Branch (1378:9): [True: 2.82M, False: 0]
Branch (1378:14): [True: 2.82M, False: 4]
|
1379 | PyObject *res = m->nb_absolute(o); |
1380 | assert(_Py_CheckSlotResult(o, "__abs__", res != NULL)); |
1381 | return res; |
1382 | } |
1383 | |
1384 | return type_error("bad operand type for abs(): '%.200s'", o); |
1385 | } |
1386 | |
1387 | |
1388 | int |
1389 | PyIndex_Check(PyObject *obj) |
1390 | { |
1391 | return _PyIndex_Check(obj); |
1392 | } |
1393 | |
1394 | |
1395 | /* Return a Python int from the object item. |
1396 | Can return an instance of int subclass. |
1397 | Raise TypeError if the result is not an int |
1398 | or if the object cannot be interpreted as an index. |
1399 | */ |
1400 | PyObject * |
1401 | _PyNumber_Index(PyObject *item) |
1402 | { |
1403 | if (item == NULL) { Branch (1403:9): [True: 0, False: 88.2M]
|
1404 | return null_error(); |
1405 | } |
1406 | |
1407 | if (PyLong_Check(item)) { |
1408 | Py_INCREF(item); |
1409 | return item; |
1410 | } |
1411 | if (!_PyIndex_Check(item)) { Branch (1411:9): [True: 707k, False: 3.33k]
|
1412 | PyErr_Format(PyExc_TypeError, |
1413 | "'%.200s' object cannot be interpreted " |
1414 | "as an integer", Py_TYPE(item)->tp_name); |
1415 | return NULL; |
1416 | } |
1417 | |
1418 | PyObject *result = Py_TYPE(item)->tp_as_number->nb_index(item); |
1419 | assert(_Py_CheckSlotResult(item, "__index__", result != NULL)); |
1420 | if (!result || PyLong_CheckExact3.26k (result)) { Branch (1420:9): [True: 71, False: 3.26k]
|
1421 | return result; |
1422 | } |
1423 | |
1424 | if (!PyLong_Check(result)) { Branch (1424:9): [True: 363, False: 12]
|
1425 | PyErr_Format(PyExc_TypeError, |
1426 | "__index__ returned non-int (type %.200s)", |
1427 | Py_TYPE(result)->tp_name); |
1428 | Py_DECREF(result); |
1429 | return NULL; |
1430 | } |
1431 | /* Issue #17576: warn if 'result' not of exact type int. */ |
1432 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, Branch (1432:9): [True: 0, False: 12]
|
1433 | "__index__ returned non-int (type %.200s). " |
1434 | "The ability to return an instance of a strict subclass of int " |
1435 | "is deprecated, and may be removed in a future version of Python.", |
1436 | Py_TYPE(result)->tp_name)) { |
1437 | Py_DECREF(result); |
1438 | return NULL; |
1439 | } |
1440 | return result; |
1441 | } |
1442 | |
1443 | /* Return an exact Python int from the object item. |
1444 | Raise TypeError if the result is not an int |
1445 | or if the object cannot be interpreted as an index. |
1446 | */ |
1447 | PyObject * |
1448 | PyNumber_Index(PyObject *item) |
1449 | { |
1450 | PyObject *result = _PyNumber_Index(item); |
1451 | if (result != NULL && !11.3M PyLong_CheckExact11.3M (result)) { Branch (1451:9): [True: 11.3M, False: 160]
Branch (1451:27): [True: 56, False: 11.3M]
|
1452 | Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); |
1453 | } |
1454 | return result; |
1455 | } |
1456 | |
1457 | /* Return an error on Overflow only if err is not NULL*/ |
1458 | |
1459 | Py_ssize_t |
1460 | PyNumber_AsSsize_t(PyObject *item, PyObject *err) |
1461 | { |
1462 | Py_ssize_t result; |
1463 | PyObject *runerr; |
1464 | PyObject *value = _PyNumber_Index(item); |
1465 | if (value == NULL) Branch (1465:9): [True: 627k, False: 69.1M]
|
1466 | return -1; |
1467 | |
1468 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
1469 | result = PyLong_AsSsize_t(value); |
1470 | if (result != -1) Branch (1470:9): [True: 61.6M, False: 7.54M]
|
1471 | goto finish; |
1472 | |
1473 | PyThreadState *tstate = _PyThreadState_GET(); |
1474 | runerr = _PyErr_Occurred(tstate); |
1475 | if (!runerr) { Branch (1475:9): [True: 7.53M, False: 2.55k]
|
1476 | goto finish; |
1477 | } |
1478 | |
1479 | /* Error handling code -- only manage OverflowError differently */ |
1480 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) { Branch (1480:9): [True: 0, False: 2.55k]
|
1481 | goto finish; |
1482 | } |
1483 | _PyErr_Clear(tstate); |
1484 | |
1485 | /* If no error-handling desired then the default clipping |
1486 | is sufficient. */ |
1487 | if (!err) { Branch (1487:9): [True: 2.52k, False: 30]
|
1488 | assert(PyLong_Check(value)); |
1489 | /* Whether or not it is less than or equal to |
1490 | zero is determined by the sign of ob_size |
1491 | */ |
1492 | if (_PyLong_Sign(value) < 0) Branch (1492:13): [True: 25, False: 2.50k]
|
1493 | result = PY_SSIZE_T_MIN; |
1494 | else |
1495 | result = PY_SSIZE_T_MAX; |
1496 | } |
1497 | else { |
1498 | /* Otherwise replace the error with caller's error object. */ |
1499 | _PyErr_Format(tstate, err, |
1500 | "cannot fit '%.200s' into an index-sized integer", |
1501 | Py_TYPE(item)->tp_name); |
1502 | } |
1503 | |
1504 | finish: |
1505 | Py_DECREF(value); |
1506 | return result; |
1507 | } |
1508 | |
1509 | |
1510 | PyObject * |
1511 | PyNumber_Long(PyObject *o) |
1512 | { |
1513 | PyObject *result; |
1514 | PyNumberMethods *m; |
1515 | PyObject *trunc_func; |
1516 | Py_buffer view; |
1517 | |
1518 | if (o == NULL) { Branch (1518:9): [True: 0, False: 5.11M]
|
1519 | return null_error(); |
1520 | } |
1521 | |
1522 | if (PyLong_CheckExact(o)) { |
1523 | Py_INCREF(o); |
1524 | return o; |
1525 | } |
1526 | m = Py_TYPE(o)->tp_as_number; |
1527 | if (m && m->nb_int4.48M ) { /* This should include subclasses of int */ Branch (1527:9): [True: 4.48M, False: 21.6k]
Branch (1527:14): [True: 4.02M, False: 464k]
|
1528 | /* Convert using the nb_int slot, which should return something |
1529 | of exact type int. */ |
1530 | result = m->nb_int(o); |
1531 | assert(_Py_CheckSlotResult(o, "__int__", result != NULL)); |
1532 | if (!result || PyLong_CheckExact4.02M (result)) { Branch (1532:13): [True: 12, False: 4.02M]
|
1533 | return result; |
1534 | } |
1535 | |
1536 | if (!PyLong_Check(result)) { Branch (1536:13): [True: 1, False: 2]
|
1537 | PyErr_Format(PyExc_TypeError, |
1538 | "__int__ returned non-int (type %.200s)", |
1539 | Py_TYPE(result)->tp_name); |
1540 | Py_DECREF(result); |
1541 | return NULL; |
1542 | } |
1543 | /* Issue #17576: warn if 'result' not of exact type int. */ |
1544 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, Branch (1544:13): [True: 0, False: 2]
|
1545 | "__int__ returned non-int (type %.200s). " |
1546 | "The ability to return an instance of a strict subclass of int " |
1547 | "is deprecated, and may be removed in a future version of Python.", |
1548 | Py_TYPE(result)->tp_name)) { |
1549 | Py_DECREF(result); |
1550 | return NULL; |
1551 | } |
1552 | Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); |
1553 | return result; |
1554 | } |
1555 | if (m && m->nb_index464k ) { Branch (1555:9): [True: 464k, False: 21.6k]
Branch (1555:14): [True: 1, False: 464k]
|
1556 | return PyNumber_Index(o); |
1557 | } |
1558 | trunc_func = _PyObject_LookupSpecial(o, &_Py_ID(__trunc__)); |
1559 | if (trunc_func) { Branch (1559:9): [True: 25, False: 485k]
|
1560 | if (PyErr_WarnEx(PyExc_DeprecationWarning, Branch (1560:13): [True: 0, False: 25]
|
1561 | "The delegation of int() to __trunc__ is deprecated.", 1)) { |
1562 | Py_DECREF(trunc_func); |
1563 | return NULL; |
1564 | } |
1565 | result = _PyObject_CallNoArgs(trunc_func); |
1566 | Py_DECREF(trunc_func); |
1567 | if (result == NULL || PyLong_CheckExact23 (result)) { Branch (1567:13): [True: 2, False: 23]
|
1568 | return result; |
1569 | } |
1570 | if (PyLong_Check(result)) { |
1571 | Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); |
1572 | return result; |
1573 | } |
1574 | /* __trunc__ is specified to return an Integral type, |
1575 | but int() needs to return an int. */ |
1576 | if (!PyIndex_Check(result)) { Branch (1576:13): [True: 9, False: 9]
|
1577 | PyErr_Format( |
1578 | PyExc_TypeError, |
1579 | "__trunc__ returned non-Integral (type %.200s)", |
1580 | Py_TYPE(result)->tp_name); |
1581 | Py_DECREF(result); |
1582 | return NULL; |
1583 | } |
1584 | Py_SETREF(result, PyNumber_Index(result)); |
1585 | return result; |
1586 | } |
1587 | if (PyErr_Occurred()) Branch (1587:9): [True: 0, False: 485k]
|
1588 | return NULL; |
1589 | |
1590 | if (PyUnicode_Check(o)) |
1591 | /* The below check is done in PyLong_FromUnicodeObject(). */ |
1592 | return PyLong_FromUnicodeObject(o, 10); |
1593 | |
1594 | if (PyBytes_Check(o)) |
1595 | /* need to do extra error checking that PyLong_FromString() |
1596 | * doesn't do. In particular int('9\x005') must raise an |
1597 | * exception, not truncate at the null. |
1598 | */ |
1599 | return _PyLong_FromBytes(PyBytes_AS_STRING(o), |
1600 | PyBytes_GET_SIZE(o), 10); |
1601 | |
1602 | if (PyByteArray_Check(o)) |
1603 | return _PyLong_FromBytes(PyByteArray_AS_STRING(o), |
1604 | PyByteArray_GET_SIZE(o), 10); |
1605 | |
1606 | if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) { Branch (1606:9): [True: 9, False: 22.7k]
|
1607 | PyObject *bytes; |
1608 | |
1609 | /* Copy to NUL-terminated buffer. */ |
1610 | bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len); |
1611 | if (bytes == NULL) { Branch (1611:13): [True: 0, False: 9]
|
1612 | PyBuffer_Release(&view); |
1613 | return NULL; |
1614 | } |
1615 | result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes), |
1616 | PyBytes_GET_SIZE(bytes), 10); |
1617 | Py_DECREF(bytes); |
1618 | PyBuffer_Release(&view); |
1619 | return result; |
1620 | } |
1621 | |
1622 | return type_error("int() argument must be a string, a bytes-like object " |
1623 | "or a real number, not '%.200s'", o); |
1624 | } |
1625 | |
1626 | PyObject * |
1627 | PyNumber_Float(PyObject *o) |
1628 | { |
1629 | if (o == NULL) { Branch (1629:9): [True: 0, False: 74.0k]
|
1630 | return null_error(); |
1631 | } |
1632 | |
1633 | if (PyFloat_CheckExact(o)) { |
1634 | return Py_NewRef(o); |
1635 | } |
1636 | |
1637 | PyNumberMethods *m = Py_TYPE(o)->tp_as_number; |
1638 | if (m && m->nb_float56.5k ) { /* This should include subclasses of float */ Branch (1638:9): [True: 56.5k, False: 7]
Branch (1638:14): [True: 56.4k, False: 147]
|
1639 | PyObject *res = m->nb_float(o); |
1640 | assert(_Py_CheckSlotResult(o, "__float__", res != NULL)); |
1641 | if (!res || PyFloat_CheckExact56.3k (res)) { Branch (1641:13): [True: 43, False: 56.3k]
|
1642 | return res; |
1643 | } |
1644 | |
1645 | if (!PyFloat_Check(res)) { Branch (1645:13): [True: 3, False: 5]
|
1646 | PyErr_Format(PyExc_TypeError, |
1647 | "%.50s.__float__ returned non-float (type %.50s)", |
1648 | Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name); |
1649 | Py_DECREF(res); |
1650 | return NULL; |
1651 | } |
1652 | /* Issue #26983: warn if 'res' not of exact type float. */ |
1653 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, Branch (1653:13): [True: 0, False: 5]
|
1654 | "%.50s.__float__ returned non-float (type %.50s). " |
1655 | "The ability to return an instance of a strict subclass of float " |
1656 | "is deprecated, and may be removed in a future version of Python.", |
1657 | Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)) { |
1658 | Py_DECREF(res); |
1659 | return NULL; |
1660 | } |
1661 | double val = PyFloat_AS_DOUBLE(res); |
1662 | Py_DECREF(res); |
1663 | return PyFloat_FromDouble(val); |
1664 | } |
1665 | |
1666 | if (m && m->nb_index147 ) { Branch (1666:9): [True: 147, False: 7]
Branch (1666:14): [True: 6, False: 141]
|
1667 | PyObject *res = _PyNumber_Index(o); |
1668 | if (!res) { Branch (1668:13): [True: 0, False: 6]
|
1669 | return NULL; |
1670 | } |
1671 | double val = PyLong_AsDouble(res); |
1672 | Py_DECREF(res); |
1673 | if (val == -1.0 && PyErr_Occurred()3 ) { Branch (1673:13): [True: 3, False: 3]
Branch (1673:28): [True: 3, False: 0]
|
1674 | return NULL; |
1675 | } |
1676 | return PyFloat_FromDouble(val); |
1677 | } |
1678 | |
1679 | /* A float subclass with nb_float == NULL */ |
1680 | if (PyFloat_Check(o)) { |
1681 | return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o)); |
1682 | } |
1683 | return PyFloat_FromString(o); |
1684 | } |
1685 | |
1686 | |
1687 | PyObject * |
1688 | PyNumber_ToBase(PyObject *n, int base) |
1689 | { |
1690 | if (!(base == 2 || base == 8512k || base == 10502k || base == 16442k )) { Branch (1690:11): [True: 134k, False: 512k]
Branch (1690:24): [True: 9.70k, False: 502k]
Branch (1690:37): [True: 60.2k, False: 442k]
Branch (1690:51): [True: 442k, False: 1]
|
1691 | PyErr_SetString(PyExc_SystemError, |
1692 | "PyNumber_ToBase: base must be 2, 8, 10 or 16"); |
1693 | return NULL; |
1694 | } |
1695 | PyObject *index = _PyNumber_Index(n); |
1696 | if (!index) Branch (1696:9): [True: 13, False: 646k]
|
1697 | return NULL; |
1698 | PyObject *res = _PyLong_Format(index, base); |
1699 | Py_DECREF(index); |
1700 | return res; |
1701 | } |
1702 | |
1703 | |
1704 | /* Operations on sequences */ |
1705 | |
1706 | int |
1707 | PySequence_Check(PyObject *s) |
1708 | { |
1709 | if (PyDict_Check(s)) |
1710 | return 0; |
1711 | return Py_TYPE(s)->tp_as_sequence && Branch (1711:12): [True: 116k, False: 23.5k]
|
1712 | Py_TYPE116k (s)->tp_as_sequence->sq_item != NULL116k ; Branch (1712:9): [True: 76.0k, False: 40.3k]
|
1713 | } |
1714 | |
1715 | Py_ssize_t |
1716 | PySequence_Size(PyObject *s) |
1717 | { |
1718 | if (s == NULL) { Branch (1718:9): [True: 0, False: 420k]
|
1719 | null_error(); |
1720 | return -1; |
1721 | } |
1722 | |
1723 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1724 | if (m && m->sq_length420k ) { Branch (1724:9): [True: 420k, False: 9]
Branch (1724:14): [True: 420k, False: 7]
|
1725 | Py_ssize_t len = m->sq_length(s); |
1726 | assert(_Py_CheckSlotResult(s, "__len__", len >= 0)); |
1727 | return len; |
1728 | } |
1729 | |
1730 | if (Py_TYPE(s)->tp_as_mapping && Py_TYPE7 (s)->tp_as_mapping->mp_length7 ) { Branch (1730:9): [True: 7, False: 9]
Branch (1730:38): [True: 2, False: 5]
|
1731 | type_error("%.200s is not a sequence", s); |
1732 | return -1; |
1733 | } |
1734 | type_error("object of type '%.200s' has no len()", s); |
1735 | return -1; |
1736 | } |
1737 | |
1738 | #undef PySequence_Length |
1739 | Py_ssize_t |
1740 | PySequence_Length(PyObject *s) |
1741 | { |
1742 | return PySequence_Size(s); |
1743 | } |
1744 | #define PySequence_Length PySequence_Size |
1745 | |
1746 | PyObject * |
1747 | PySequence_Concat(PyObject *s, PyObject *o) |
1748 | { |
1749 | if (s == NULL || o == NULL) { Branch (1749:9): [True: 0, False: 8.00k]
Branch (1749:22): [True: 0, False: 8.00k]
|
1750 | return null_error(); |
1751 | } |
1752 | |
1753 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1754 | if (m && m->sq_concat7.99k ) { Branch (1754:9): [True: 7.99k, False: 2]
Branch (1754:14): [True: 7.99k, False: 2]
|
1755 | PyObject *res = m->sq_concat(s, o); |
1756 | assert(_Py_CheckSlotResult(s, "+", res != NULL)); |
1757 | return res; |
1758 | } |
1759 | |
1760 | /* Instances of user classes defining an __add__() method only |
1761 | have an nb_add slot, not an sq_concat slot. So we fall back |
1762 | to nb_add if both arguments appear to be sequences. */ |
1763 | if (PySequence_Check(s) && PySequence_Check(o)2 ) { Branch (1763:9): [True: 2, False: 2]
Branch (1763:32): [True: 2, False: 0]
|
1764 | PyObject *result = BINARY_OP1(s, o, NB_SLOT(nb_add), "+"); |
1765 | if (result != Py_NotImplemented) Branch (1765:13): [True: 2, False: 0]
|
1766 | return result; |
1767 | Py_DECREF(result); |
1768 | } |
1769 | return type_error("'%.200s' object can't be concatenated", s); |
1770 | } |
1771 | |
1772 | PyObject * |
1773 | PySequence_Repeat(PyObject *o, Py_ssize_t count) |
1774 | { |
1775 | if (o == NULL) { Branch (1775:9): [True: 0, False: 0]
|
1776 | return null_error(); |
1777 | } |
1778 | |
1779 | PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; |
1780 | if (m && m->sq_repeat) { Branch (1780:9): [True: 0, False: 0]
Branch (1780:14): [True: 0, False: 0]
|
1781 | PyObject *res = m->sq_repeat(o, count); |
1782 | assert(_Py_CheckSlotResult(o, "*", res != NULL)); |
1783 | return res; |
1784 | } |
1785 | |
1786 | /* Instances of user classes defining a __mul__() method only |
1787 | have an nb_multiply slot, not an sq_repeat slot. so we fall back |
1788 | to nb_multiply if o appears to be a sequence. */ |
1789 | if (PySequence_Check(o)) { Branch (1789:9): [True: 0, False: 0]
|
1790 | PyObject *n, *result; |
1791 | n = PyLong_FromSsize_t(count); |
1792 | if (n == NULL) Branch (1792:13): [True: 0, False: 0]
|
1793 | return NULL; |
1794 | result = BINARY_OP1(o, n, NB_SLOT(nb_multiply), "*"); |
1795 | Py_DECREF(n); |
1796 | if (result != Py_NotImplemented) Branch (1796:13): [True: 0, False: 0]
|
1797 | return result; |
1798 | Py_DECREF(result); |
1799 | } |
1800 | return type_error("'%.200s' object can't be repeated", o); |
1801 | } |
1802 | |
1803 | PyObject * |
1804 | PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
1805 | { |
1806 | if (s == NULL || o == NULL) { Branch (1806:9): [True: 0, False: 1]
Branch (1806:22): [True: 0, False: 1]
|
1807 | return null_error(); |
1808 | } |
1809 | |
1810 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1811 | if (m && m->sq_inplace_concat) { Branch (1811:9): [True: 1, False: 0]
Branch (1811:14): [True: 0, False: 1]
|
1812 | PyObject *res = m->sq_inplace_concat(s, o); |
1813 | assert(_Py_CheckSlotResult(s, "+=", res != NULL)); |
1814 | return res; |
1815 | } |
1816 | if (m && m->sq_concat) { Branch (1816:9): [True: 1, False: 0]
Branch (1816:14): [True: 0, False: 1]
|
1817 | PyObject *res = m->sq_concat(s, o); |
1818 | assert(_Py_CheckSlotResult(s, "+", res != NULL)); |
1819 | return res; |
1820 | } |
1821 | |
1822 | if (PySequence_Check(s) && PySequence_Check(o)) { Branch (1822:9): [True: 1, False: 0]
Branch (1822:32): [True: 1, False: 0]
|
1823 | PyObject *result = BINARY_IOP1(s, o, NB_SLOT(nb_inplace_add), |
1824 | NB_SLOT(nb_add), "+="); |
1825 | if (result != Py_NotImplemented) Branch (1825:13): [True: 1, False: 0]
|
1826 | return result; |
1827 | Py_DECREF(result); |
1828 | } |
1829 | return type_error("'%.200s' object can't be concatenated", s); |
1830 | } |
1831 | |
1832 | PyObject * |
1833 | PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
1834 | { |
1835 | if (o == NULL) { Branch (1835:9): [True: 0, False: 0]
|
1836 | return null_error(); |
1837 | } |
1838 | |
1839 | PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; |
1840 | if (m && m->sq_inplace_repeat) { Branch (1840:9): [True: 0, False: 0]
Branch (1840:14): [True: 0, False: 0]
|
1841 | PyObject *res = m->sq_inplace_repeat(o, count); |
1842 | assert(_Py_CheckSlotResult(o, "*=", res != NULL)); |
1843 | return res; |
1844 | } |
1845 | if (m && m->sq_repeat) { Branch (1845:9): [True: 0, False: 0]
Branch (1845:14): [True: 0, False: 0]
|
1846 | PyObject *res = m->sq_repeat(o, count); |
1847 | assert(_Py_CheckSlotResult(o, "*", res != NULL)); |
1848 | return res; |
1849 | } |
1850 | |
1851 | if (PySequence_Check(o)) { Branch (1851:9): [True: 0, False: 0]
|
1852 | PyObject *n, *result; |
1853 | n = PyLong_FromSsize_t(count); |
1854 | if (n == NULL) Branch (1854:13): [True: 0, False: 0]
|
1855 | return NULL; |
1856 | result = BINARY_IOP1(o, n, NB_SLOT(nb_inplace_multiply), |
1857 | NB_SLOT(nb_multiply), "*="); |
1858 | Py_DECREF(n); |
1859 | if (result != Py_NotImplemented) Branch (1859:13): [True: 0, False: 0]
|
1860 | return result; |
1861 | Py_DECREF(result); |
1862 | } |
1863 | return type_error("'%.200s' object can't be repeated", o); |
1864 | } |
1865 | |
1866 | PyObject * |
1867 | PySequence_GetItem(PyObject *s, Py_ssize_t i) |
1868 | { |
1869 | if (s == NULL) { Branch (1869:9): [True: 0, False: 3.53M]
|
1870 | return null_error(); |
1871 | } |
1872 | |
1873 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1874 | if (m && m->sq_item) { Branch (1874:9): [True: 3.53M, False: 0]
Branch (1874:14): [True: 3.53M, False: 5]
|
1875 | if (i < 0) { Branch (1875:13): [True: 30.7k, False: 3.50M]
|
1876 | if (m->sq_length) { Branch (1876:17): [True: 30.7k, False: 0]
|
1877 | Py_ssize_t l = (*m->sq_length)(s); |
1878 | assert(_Py_CheckSlotResult(s, "__len__", l >= 0)); |
1879 | if (l < 0) { Branch (1879:21): [True: 0, False: 30.7k]
|
1880 | return NULL; |
1881 | } |
1882 | i += l; |
1883 | } |
1884 | } |
1885 | PyObject *res = m->sq_item(s, i); |
1886 | assert(_Py_CheckSlotResult(s, "__getitem__", res != NULL)); |
1887 | return res; |
1888 | } |
1889 | |
1890 | if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_subscript) { Branch (1890:9): [True: 5, False: 0]
Branch (1890:38): [True: 0, False: 5]
|
1891 | return type_error("%.200s is not a sequence", s); |
1892 | } |
1893 | return type_error("'%.200s' object does not support indexing", s); |
1894 | } |
1895 | |
1896 | PyObject * |
1897 | PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) |
1898 | { |
1899 | if (!s) { Branch (1899:9): [True: 0, False: 8.73k]
|
1900 | return null_error(); |
1901 | } |
1902 | |
1903 | PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping; |
1904 | if (mp && mp->mp_subscript) { Branch (1904:9): [True: 8.73k, False: 0]
Branch (1904:15): [True: 8.73k, False: 0]
|
1905 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
1906 | if (!slice) { Branch (1906:13): [True: 0, False: 8.73k]
|
1907 | return NULL; |
1908 | } |
1909 | PyObject *res = mp->mp_subscript(s, slice); |
1910 | assert(_Py_CheckSlotResult(s, "__getitem__", res != NULL)); |
1911 | Py_DECREF(slice); |
1912 | return res; |
1913 | } |
1914 | |
1915 | return type_error("'%.200s' object is unsliceable", s); |
1916 | } |
1917 | |
1918 | int |
1919 | PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o) |
1920 | { |
1921 | if (s == NULL) { Branch (1921:9): [True: 0, False: 5.89k]
|
1922 | null_error(); |
1923 | return -1; |
1924 | } |
1925 | |
1926 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1927 | if (m && m->sq_ass_item) { Branch (1927:9): [True: 5.89k, False: 0]
Branch (1927:14): [True: 5.88k, False: 8]
|
1928 | if (i < 0) { Branch (1928:13): [True: 0, False: 5.88k]
|
1929 | if (m->sq_length) { Branch (1929:17): [True: 0, False: 0]
|
1930 | Py_ssize_t l = (*m->sq_length)(s); |
1931 | assert(_Py_CheckSlotResult(s, "__len__", l >= 0)); |
1932 | if (l < 0) { Branch (1932:21): [True: 0, False: 0]
|
1933 | return -1; |
1934 | } |
1935 | i += l; |
1936 | } |
1937 | } |
1938 | int res = m->sq_ass_item(s, i, o); |
1939 | assert(_Py_CheckSlotResult(s, "__setitem__", res >= 0)); |
1940 | return res; |
1941 | } |
1942 | |
1943 | if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) { Branch (1943:9): [True: 8, False: 0]
Branch (1943:38): [True: 0, False: 8]
|
1944 | type_error("%.200s is not a sequence", s); |
1945 | return -1; |
1946 | } |
1947 | type_error("'%.200s' object does not support item assignment", s); |
1948 | return -1; |
1949 | } |
1950 | |
1951 | int |
1952 | PySequence_DelItem(PyObject *s, Py_ssize_t i) |
1953 | { |
1954 | if (s == NULL) { Branch (1954:9): [True: 0, False: 28.2k]
|
1955 | null_error(); |
1956 | return -1; |
1957 | } |
1958 | |
1959 | PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence; |
1960 | if (m && m->sq_ass_item) { Branch (1960:9): [True: 28.2k, False: 0]
Branch (1960:14): [True: 28.2k, False: 2]
|
1961 | if (i < 0) { Branch (1961:13): [True: 251, False: 28.0k]
|
1962 | if (m->sq_length) { Branch (1962:17): [True: 251, False: 0]
|
1963 | Py_ssize_t l = (*m->sq_length)(s); |
1964 | assert(_Py_CheckSlotResult(s, "__len__", l >= 0)); |
1965 | if (l < 0) { Branch (1965:21): [True: 0, False: 251]
|
1966 | return -1; |
1967 | } |
1968 | i += l; |
1969 | } |
1970 | } |
1971 | int res = m->sq_ass_item(s, i, (PyObject *)NULL); |
1972 | assert(_Py_CheckSlotResult(s, "__delitem__", res >= 0)); |
1973 | return res; |
1974 | } |
1975 | |
1976 | if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) { Branch (1976:9): [True: 2, False: 0]
Branch (1976:38): [True: 0, False: 2]
|
1977 | type_error("%.200s is not a sequence", s); |
1978 | return -1; |
1979 | } |
1980 | type_error("'%.200s' object doesn't support item deletion", s); |
1981 | return -1; |
1982 | } |
1983 | |
1984 | int |
1985 | PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o) |
1986 | { |
1987 | if (s == NULL) { Branch (1987:9): [True: 0, False: 0]
|
1988 | null_error(); |
1989 | return -1; |
1990 | } |
1991 | |
1992 | PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping; |
1993 | if (mp && mp->mp_ass_subscript) { Branch (1993:9): [True: 0, False: 0]
Branch (1993:15): [True: 0, False: 0]
|
1994 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
1995 | if (!slice) Branch (1995:13): [True: 0, False: 0]
|
1996 | return -1; |
1997 | int res = mp->mp_ass_subscript(s, slice, o); |
1998 | assert(_Py_CheckSlotResult(s, "__setitem__", res >= 0)); |
1999 | Py_DECREF(slice); |
2000 | return res; |
2001 | } |
2002 | |
2003 | type_error("'%.200s' object doesn't support slice assignment", s); |
2004 | return -1; |
2005 | } |
2006 | |
2007 | int |
2008 | PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) |
2009 | { |
2010 | if (s == NULL) { Branch (2010:9): [True: 0, False: 0]
|
2011 | null_error(); |
2012 | return -1; |
2013 | } |
2014 | |
2015 | PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping; |
2016 | if (mp && mp->mp_ass_subscript) { Branch (2016:9): [True: 0, False: 0]
Branch (2016:15): [True: 0, False: 0]
|
2017 | PyObject *slice = _PySlice_FromIndices(i1, i2); |
2018 | if (!slice) { Branch (2018:13): [True: 0, False: 0]
|
2019 | return -1; |
2020 | } |
2021 | int res = mp->mp_ass_subscript(s, slice, NULL); |
2022 | assert(_Py_CheckSlotResult(s, "__delitem__", res >= 0)); |
2023 | Py_DECREF(slice); |
2024 | return res; |
2025 | } |
2026 | type_error("'%.200s' object doesn't support slice deletion", s); |
2027 | return -1; |
2028 | } |
2029 | |
2030 | PyObject * |
2031 | PySequence_Tuple(PyObject *v) |
2032 | { |
2033 | PyObject *it; /* iter(v) */ |
2034 | Py_ssize_t n; /* guess for result tuple size */ |
2035 | PyObject *result = NULL; |
2036 | Py_ssize_t j; |
2037 | |
2038 | if (v == NULL) { Branch (2038:9): [True: 0, False: 1.94M]
|
2039 | return null_error(); |
2040 | } |
2041 | |
2042 | /* Special-case the common tuple and list cases, for efficiency. */ |
2043 | if (PyTuple_CheckExact(v)) { |
2044 | /* Note that we can't know whether it's safe to return |
2045 | a tuple *subclass* instance as-is, hence the restriction |
2046 | to exact tuples here. In contrast, lists always make |
2047 | a copy, so there's no need for exactness below. */ |
2048 | Py_INCREF(v); |
2049 | return v; |
2050 | } |
2051 | if (PyList_CheckExact(v)) |
2052 | return PyList_AsTuple(v); |
2053 | |
2054 | /* Get iterator. */ |
2055 | it = PyObject_GetIter(v); |
2056 | if (it == NULL) Branch (2056:9): [True: 32, False: 185k]
|
2057 | return NULL; |
2058 | |
2059 | /* Guess result size and allocate space. */ |
2060 | n = PyObject_LengthHint(v, 10); |
2061 | if (n == -1) Branch (2061:9): [True: 0, False: 185k]
|
2062 | goto Fail; |
2063 | result = PyTuple_New(n); |
2064 | if (result == NULL) Branch (2064:9): [True: 0, False: 185k]
|
2065 | goto Fail; |
2066 | |
2067 | /* Fill the tuple. */ |
2068 | for (j = 0; ; 185k ++j2.79M ) { |
2069 | PyObject *item = PyIter_Next(it); |
2070 | if (item == NULL) { Branch (2070:13): [True: 185k, False: 2.79M]
|
2071 | if (PyErr_Occurred()) Branch (2071:17): [True: 67, False: 185k]
|
2072 | goto Fail; |
2073 | break; |
2074 | } |
2075 | if (j >= n) { Branch (2075:13): [True: 6.12k, False: 2.79M]
|
2076 | size_t newn = (size_t)n; |
2077 | /* The over-allocation strategy can grow a bit faster |
2078 | than for lists because unlike lists the |
2079 | over-allocation isn't permanent -- we reclaim |
2080 | the excess before the end of this routine. |
2081 | So, grow by ten and then add 25%. |
2082 | */ |
2083 | newn += 10u; |
2084 | newn += newn >> 2; |
2085 | if (newn > PY_SSIZE_T_MAX) { Branch (2085:17): [True: 0, False: 6.12k]
|
2086 | /* Check for overflow */ |
2087 | PyErr_NoMemory(); |
2088 | Py_DECREF(item); |
2089 | goto Fail; |
2090 | } |
2091 | n = (Py_ssize_t)newn; |
2092 | if (_PyTuple_Resize(&result, n) != 0) { Branch (2092:17): [True: 0, False: 6.12k]
|
2093 | Py_DECREF(item); |
2094 | goto Fail; |
2095 | } |
2096 | } |
2097 | PyTuple_SET_ITEM(result, j, item); |
2098 | } |
2099 | |
2100 | /* Cut tuple back if guess was too large. */ |
2101 | if (j < n && Branch (2101:9): [True: 130k, False: 54.7k]
|
2102 | _PyTuple_Resize(&result, j) != 0130k ) Branch (2102:9): [True: 0, False: 130k]
|
2103 | goto Fail; |
2104 | |
2105 | Py_DECREF(it); |
2106 | return result; |
2107 | |
2108 | Fail: |
2109 | Py_XDECREF(result); |
2110 | Py_DECREF(it); |
2111 | return NULL; |
2112 | } |
2113 | |
2114 | PyObject * |
2115 | PySequence_List(PyObject *v) |
2116 | { |
2117 | PyObject *result; /* result list */ |
2118 | PyObject *rv; /* return value from PyList_Extend */ |
2119 | |
2120 | if (v == NULL) { Branch (2120:9): [True: 0, False: 989k]
|
2121 | return null_error(); |
2122 | } |
2123 | |
2124 | result = PyList_New(0); |
2125 | if (result == NULL) Branch (2125:9): [True: 0, False: 989k]
|
2126 | return NULL; |
2127 | |
2128 | rv = _PyList_Extend((PyListObject *)result, v); |
2129 | if (rv == NULL) { Branch (2129:9): [True: 32, False: 989k]
|
2130 | Py_DECREF(result); |
2131 | return NULL; |
2132 | } |
2133 | Py_DECREF(rv); |
2134 | return result; |
2135 | } |
2136 | |
2137 | PyObject * |
2138 | PySequence_Fast(PyObject *v, const char *m) |
2139 | { |
2140 | PyObject *it; |
2141 | |
2142 | if (v == NULL) { Branch (2142:9): [True: 0, False: 6.54M]
|
2143 | return null_error(); |
2144 | } |
2145 | |
2146 | if (PyList_CheckExact(v) || PyTuple_CheckExact4.71M (v)) { |
2147 | Py_INCREF(v); |
2148 | return v; |
2149 | } |
2150 | |
2151 | it = PyObject_GetIter(v); |
2152 | if (it == NULL) { Branch (2152:9): [True: 36, False: 258k]
|
2153 | PyThreadState *tstate = _PyThreadState_GET(); |
2154 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) { Branch (2154:13): [True: 36, False: 0]
|
2155 | _PyErr_SetString(tstate, PyExc_TypeError, m); |
2156 | } |
2157 | return NULL; |
2158 | } |
2159 | |
2160 | v = PySequence_List(it); |
2161 | Py_DECREF(it); |
2162 | |
2163 | return v; |
2164 | } |
2165 | |
2166 | /* Iterate over seq. Result depends on the operation: |
2167 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq. |
2168 | PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq; |
2169 | set ValueError and return -1 if none found; also return -1 on error. |
2170 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error. |
2171 | */ |
2172 | Py_ssize_t |
2173 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
2174 | { |
2175 | Py_ssize_t n; |
2176 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */ |
2177 | PyObject *it; /* iter(seq) */ |
2178 | |
2179 | if (seq == NULL || obj == NULL) { Branch (2179:9): [True: 0, False: 1.03k]
Branch (2179:24): [True: 0, False: 1.03k]
|
2180 | null_error(); |
2181 | return -1; |
2182 | } |
2183 | |
2184 | it = PyObject_GetIter(seq); |
2185 | if (it == NULL) { Branch (2185:9): [True: 36, False: 998]
|
2186 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { Branch (2186:13): [True: 30, False: 6]
|
2187 | type_error("argument of type '%.200s' is not iterable", seq); |
2188 | } |
2189 | return -1; |
2190 | } |
2191 | |
2192 | n = wrapped = 0; |
2193 | for (;;) { |
2194 | int cmp; |
2195 | PyObject *item = PyIter_Next(it); |
2196 | if (item == NULL) { Branch (2196:13): [True: 103, False: 4.96k]
|
2197 | if (PyErr_Occurred()) Branch (2197:17): [True: 2, False: 101]
|
2198 | goto Fail; |
2199 | break; |
2200 | } |
2201 | |
2202 | cmp = PyObject_RichCompareBool(item, obj, Py_EQ); |
2203 | Py_DECREF(item); |
2204 | if (cmp < 0) Branch (2204:13): [True: 2, False: 4.95k]
|
2205 | goto Fail; |
2206 | if (cmp > 0) { Branch (2206:13): [True: 929, False: 4.02k]
|
2207 | switch (operation) { |
2208 | case PY_ITERSEARCH_COUNT: Branch (2208:13): [True: 36, False: 893]
|
2209 | if (n == PY_SSIZE_T_MAX) { Branch (2209:21): [True: 0, False: 36]
|
2210 | PyErr_SetString(PyExc_OverflowError, |
2211 | "count exceeds C integer size"); |
2212 | goto Fail; |
2213 | } |
2214 | ++n; |
2215 | break; |
2216 | |
2217 | case PY_ITERSEARCH_INDEX: Branch (2217:13): [True: 299, False: 630]
|
2218 | if (wrapped) { Branch (2218:21): [True: 0, False: 299]
|
2219 | PyErr_SetString(PyExc_OverflowError, |
2220 | "index exceeds C integer size"); |
2221 | goto Fail; |
2222 | } |
2223 | goto Done; |
2224 | |
2225 | case PY_ITERSEARCH_CONTAINS: Branch (2225:13): [True: 594, False: 335]
|
2226 | n = 1; |
2227 | goto Done; |
2228 | |
2229 | default: Branch (2229:13): [True: 0, False: 929]
|
2230 | Py_UNREACHABLE(); |
2231 | } |
2232 | } |
2233 | |
2234 | if (operation == PY_ITERSEARCH_INDEX) { Branch (2234:13): [True: 328, False: 3.73k]
|
2235 | if (n == PY_SSIZE_T_MAX) Branch (2235:17): [True: 0, False: 328]
|
2236 | wrapped = 1; |
2237 | ++n; |
2238 | } |
2239 | } |
2240 | |
2241 | if (operation != PY_ITERSEARCH_INDEX) Branch (2241:9): [True: 94, False: 7]
|
2242 | goto Done; |
2243 | |
2244 | PyErr_SetString(PyExc_ValueError, |
2245 | "sequence.index(x): x not in sequence"); |
2246 | /* fall into failure code */ |
2247 | Fail: |
2248 | n = -1; |
2249 | /* fall through */ |
2250 | Done: |
2251 | Py_DECREF(it); |
2252 | return n; |
2253 | |
2254 | } |
2255 | |
2256 | /* Return # of times o appears in s. */ |
2257 | Py_ssize_t |
2258 | PySequence_Count(PyObject *s, PyObject *o) |
2259 | { |
2260 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
2261 | } |
2262 | |
2263 | /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq. |
2264 | * Use sq_contains if possible, else defer to _PySequence_IterSearch(). |
2265 | */ |
2266 | int |
2267 | PySequence_Contains(PyObject *seq, PyObject *ob) |
2268 | { |
2269 | PySequenceMethods *sqm = Py_TYPE(seq)->tp_as_sequence; |
2270 | if (sqm != NULL && sqm->sq_contains != NULL25.1M ) { Branch (2270:9): [True: 25.1M, False: 29]
Branch (2270:24): [True: 25.1M, False: 651]
|
2271 | int res = (*sqm->sq_contains)(seq, ob); |
2272 | assert(_Py_CheckSlotResult(seq, "__contains__", res >= 0)); |
2273 | return res; |
2274 | } |
2275 | Py_ssize_t result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
2276 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
2277 | } |
2278 | |
2279 | /* Backwards compatibility */ |
2280 | #undef PySequence_In |
2281 | int |
2282 | PySequence_In(PyObject *w, PyObject *v) |
2283 | { |
2284 | return PySequence_Contains(w, v); |
2285 | } |
2286 | |
2287 | Py_ssize_t |
2288 | PySequence_Index(PyObject *s, PyObject *o) |
2289 | { |
2290 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
2291 | } |
2292 | |
2293 | /* Operations on mappings */ |
2294 | |
2295 | int |
2296 | PyMapping_Check(PyObject *o) |
2297 | { |
2298 | return o && Py_TYPE(o)->tp_as_mapping && Branch (2298:12): [True: 4.29M, False: 0]
Branch (2298:17): [True: 3.63M, False: 660k]
|
2299 | Py_TYPE3.63M (o)->tp_as_mapping->mp_subscript3.63M ; Branch (2299:9): [True: 3.63M, False: 853]
|
2300 | } |
2301 | |
2302 | Py_ssize_t |
2303 | PyMapping_Size(PyObject *o) |
2304 | { |
2305 | if (o == NULL) { Branch (2305:9): [True: 0, False: 1.48M]
|
2306 | null_error(); |
2307 | return -1; |
2308 | } |
2309 | |
2310 | PyMappingMethods *m = Py_TYPE(o)->tp_as_mapping; |
2311 | if (m && m->mp_length1.48M ) { Branch (2311:9): [True: 1.48M, False: 173]
Branch (2311:14): [True: 1.48M, False: 165]
|
2312 | Py_ssize_t len = m->mp_length(o); |
2313 | assert(_Py_CheckSlotResult(o, "__len__", len >= 0)); |
2314 | return len; |
2315 | } |
2316 | |
2317 | if (Py_TYPE(o)->tp_as_sequence && Py_TYPE165 (o)->tp_as_sequence->sq_length165 ) { Branch (2317:9): [True: 165, False: 173]
Branch (2317:39): [True: 0, False: 165]
|
2318 | type_error("%.200s is not a mapping", o); |
2319 | return -1; |
2320 | } |
2321 | /* PyMapping_Size() can be called from PyObject_Size(). */ |
2322 | type_error("object of type '%.200s' has no len()", o); |
2323 | return -1; |
2324 | } |
2325 | |
2326 | #undef PyMapping_Length |
2327 | Py_ssize_t |
2328 | PyMapping_Length(PyObject *o) |
2329 | { |
2330 | return PyMapping_Size(o); |
2331 | } |
2332 | #define PyMapping_Length PyMapping_Size |
2333 | |
2334 | PyObject * |
2335 | PyMapping_GetItemString(PyObject *o, const char *key) |
2336 | { |
2337 | PyObject *okey, *r; |
2338 | |
2339 | if (key == NULL) { Branch (2339:9): [True: 0, False: 2.53k]
|
2340 | return null_error(); |
2341 | } |
2342 | |
2343 | okey = PyUnicode_FromString(key); |
2344 | if (okey == NULL) Branch (2344:9): [True: 0, False: 2.53k]
|
2345 | return NULL; |
2346 | r = PyObject_GetItem(o, okey); |
2347 | Py_DECREF(okey); |
2348 | return r; |
2349 | } |
2350 | |
2351 | int |
2352 | PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value) |
2353 | { |
2354 | PyObject *okey; |
2355 | int r; |
2356 | |
2357 | if (key == NULL) { Branch (2357:9): [True: 0, False: 538]
|
2358 | null_error(); |
2359 | return -1; |
2360 | } |
2361 | |
2362 | okey = PyUnicode_FromString(key); |
2363 | if (okey == NULL) Branch (2363:9): [True: 0, False: 538]
|
2364 | return -1; |
2365 | r = PyObject_SetItem(o, okey, value); |
2366 | Py_DECREF(okey); |
2367 | return r; |
2368 | } |
2369 | |
2370 | int |
2371 | PyMapping_HasKeyString(PyObject *o, const char *key) |
2372 | { |
2373 | PyObject *v; |
2374 |
|
2375 | v = PyMapping_GetItemString(o, key); |
2376 | if (v) { Branch (2376:9): [True: 0, False: 0]
|
2377 | Py_DECREF(v); |
2378 | return 1; |
2379 | } |
2380 | PyErr_Clear(); |
2381 | return 0; |
2382 | } |
2383 | |
2384 | int |
2385 | PyMapping_HasKey(PyObject *o, PyObject *key) |
2386 | { |
2387 | PyObject *v; |
2388 |
|
2389 | v = PyObject_GetItem(o, key); |
2390 | if (v) { Branch (2390:9): [True: 0, False: 0]
|
2391 | Py_DECREF(v); |
2392 | return 1; |
2393 | } |
2394 | PyErr_Clear(); |
2395 | return 0; |
2396 | } |
2397 | |
2398 | /* This function is quite similar to PySequence_Fast(), but specialized to be |
2399 | a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values(). |
2400 | */ |
2401 | static PyObject * |
2402 | method_output_as_list(PyObject *o, PyObject *meth) |
2403 | { |
2404 | PyObject *it, *result, *meth_output; |
2405 | |
2406 | assert(o != NULL); |
2407 | meth_output = PyObject_CallMethodNoArgs(o, meth); |
2408 | if (meth_output == NULL || PyList_CheckExact76.3k (meth_output)) { Branch (2408:9): [True: 19, False: 76.3k]
|
2409 | return meth_output; |
2410 | } |
2411 | it = PyObject_GetIter(meth_output); |
2412 | if (it == NULL) { Branch (2412:9): [True: 4, False: 76.3k]
|
2413 | PyThreadState *tstate = _PyThreadState_GET(); |
2414 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) { Branch (2414:13): [True: 4, False: 0]
|
2415 | _PyErr_Format(tstate, PyExc_TypeError, |
2416 | "%.200s.%U() returned a non-iterable (type %.200s)", |
2417 | Py_TYPE(o)->tp_name, |
2418 | meth, |
2419 | Py_TYPE(meth_output)->tp_name); |
2420 | } |
2421 | Py_DECREF(meth_output); |
2422 | return NULL; |
2423 | } |
2424 | Py_DECREF(meth_output); |
2425 | result = PySequence_List(it); |
2426 | Py_DECREF(it); |
2427 | return result; |
2428 | } |
2429 | |
2430 | PyObject * |
2431 | PyMapping_Keys(PyObject *o) |
2432 | { |
2433 | if (o == NULL) { Branch (2433:9): [True: 0, False: 66.4k]
|
2434 | return null_error(); |
2435 | } |
2436 | if (PyDict_CheckExact(o)) { |
2437 | return PyDict_Keys(o); |
2438 | } |
2439 | return method_output_as_list(o, &_Py_ID(keys)); |
2440 | } |
2441 | |
2442 | PyObject * |
2443 | PyMapping_Items(PyObject *o) |
2444 | { |
2445 | if (o == NULL) { Branch (2445:9): [True: 0, False: 11.2k]
|
2446 | return null_error(); |
2447 | } |
2448 | if (PyDict_CheckExact(o)) { |
2449 | return PyDict_Items(o); |
2450 | } |
2451 | return method_output_as_list(o, &_Py_ID(items)); |
2452 | } |
2453 | |
2454 | PyObject * |
2455 | PyMapping_Values(PyObject *o) |
2456 | { |
2457 | if (o == NULL) { Branch (2457:9): [True: 0, False: 102]
|
2458 | return null_error(); |
2459 | } |
2460 | if (PyDict_CheckExact(o)) { |
2461 | return PyDict_Values(o); |
2462 | } |
2463 | return method_output_as_list(o, &_Py_ID(values)); |
2464 | } |
2465 | |
2466 | /* isinstance(), issubclass() */ |
2467 | |
2468 | /* abstract_get_bases() has logically 4 return states: |
2469 | * |
2470 | * 1. getattr(cls, '__bases__') could raise an AttributeError |
2471 | * 2. getattr(cls, '__bases__') could raise some other exception |
2472 | * 3. getattr(cls, '__bases__') could return a tuple |
2473 | * 4. getattr(cls, '__bases__') could return something other than a tuple |
2474 | * |
2475 | * Only state #3 is a non-error state and only it returns a non-NULL object |
2476 | * (it returns the retrieved tuple). |
2477 | * |
2478 | * Any raised AttributeErrors are masked by clearing the exception and |
2479 | * returning NULL. If an object other than a tuple comes out of __bases__, |
2480 | * then again, the return value is NULL. So yes, these two situations |
2481 | * produce exactly the same results: NULL is returned and no error is set. |
2482 | * |
2483 | * If some exception other than AttributeError is raised, then NULL is also |
2484 | * returned, but the exception is not cleared. That's because we want the |
2485 | * exception to be propagated along. |
2486 | * |
2487 | * Callers are expected to test for PyErr_Occurred() when the return value |
2488 | * is NULL to decide whether a valid exception should be propagated or not. |
2489 | * When there's no exception to propagate, it's customary for the caller to |
2490 | * set a TypeError. |
2491 | */ |
2492 | static PyObject * |
2493 | abstract_get_bases(PyObject *cls) |
2494 | { |
2495 | PyObject *bases; |
2496 | |
2497 | (void)_PyObject_LookupAttr(cls, &_Py_ID(__bases__), &bases); |
2498 | if (bases != NULL && !160 PyTuple_Check160 (bases)) { Branch (2498:9): [True: 160, False: 109]
Branch (2498:26): [True: 0, False: 160]
|
2499 | Py_DECREF(bases); |
2500 | return NULL; |
2501 | } |
2502 | return bases; |
2503 | } |
2504 | |
2505 | |
2506 | static int |
2507 | abstract_issubclass(PyObject *derived, PyObject *cls) |
2508 | { |
2509 | PyObject *bases = NULL; |
2510 | Py_ssize_t i, n; |
2511 | int r = 0; |
2512 | |
2513 | while (1) { Branch (2513:12): [Folded - Ignored]
|
2514 | if (derived == cls) { Branch (2514:13): [True: 7, False: 111]
|
2515 | Py_XDECREF(bases); /* See below comment */ |
2516 | return 1; |
2517 | } |
2518 | /* Use XSETREF to drop bases reference *after* finishing with |
2519 | derived; bases might be the only reference to it. |
2520 | XSETREF is used instead of SETREF, because bases is NULL on the |
2521 | first iteration of the loop. |
2522 | */ |
2523 | Py_XSETREF(bases, abstract_get_bases(derived)); |
2524 | if (bases == NULL) { Branch (2524:13): [True: 4, False: 107]
|
2525 | if (PyErr_Occurred()) Branch (2525:17): [True: 3, False: 1]
|
2526 | return -1; |
2527 | return 0; |
2528 | } |
2529 | n = PyTuple_GET_SIZE(bases); |
2530 | if (n == 0) { Branch (2530:13): [True: 10, False: 97]
|
2531 | Py_DECREF(bases); |
2532 | return 0; |
2533 | } |
2534 | /* Avoid recursivity in the single inheritance case */ |
2535 | if (n == 1) { Branch (2535:13): [True: 13, False: 84]
|
2536 | derived = PyTuple_GET_ITEM(bases, 0); |
2537 | continue; |
2538 | } |
2539 | break; |
2540 | } |
2541 | assert(n >= 2); |
2542 | if (_Py_EnterRecursiveCall(" in __issubclass__")) { Branch (2542:9): [True: 0, False: 84]
|
2543 | Py_DECREF(bases); |
2544 | return -1; |
2545 | } |
2546 | for (i = 0; i < n; i++0 ) { Branch (2546:17): [True: 84, False: 0]
|
2547 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
2548 | if (r != 0) { Branch (2548:13): [True: 84, False: 0]
|
2549 | break; |
2550 | } |
2551 | } |
2552 | _Py_LeaveRecursiveCall(); |
2553 | Py_DECREF(bases); |
2554 | return r; |
2555 | } |
2556 | |
2557 | static int |
2558 | check_class(PyObject *cls, const char *error) |
2559 | { |
2560 | PyObject *bases = abstract_get_bases(cls); |
2561 | if (bases == NULL) { Branch (2561:9): [True: 105, False: 53]
|
2562 | /* Do not mask errors. */ |
2563 | PyThreadState *tstate = _PyThreadState_GET(); |
2564 | if (!_PyErr_Occurred(tstate)) { Branch (2564:13): [True: 98, False: 7]
|
2565 | _PyErr_SetString(tstate, PyExc_TypeError, error); |
2566 | } |
2567 | return 0; |
2568 | } |
2569 | Py_DECREF(bases); |
2570 | return -1; |
2571 | } |
2572 | |
2573 | static int |
2574 | object_isinstance(PyObject *inst, PyObject *cls) |
2575 | { |
2576 | PyObject *icls; |
2577 | int retval; |
2578 | if (PyType_Check(cls)) { |
2579 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
2580 | if (retval == 0) { Branch (2580:13): [True: 13.0M, False: 610k]
|
2581 | retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls); |
2582 | if (icls != NULL) { Branch (2582:17): [True: 13.0M, False: 3]
|
2583 | if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check1.20k (icls)) { Branch (2583:21): [True: 1.20k, False: 13.0M]
|
2584 | retval = PyType_IsSubtype( |
2585 | (PyTypeObject *)icls, |
2586 | (PyTypeObject *)cls); |
2587 | } |
2588 | else { |
2589 | retval = 0; |
2590 | } |
2591 | Py_DECREF(icls); |
2592 | } |
2593 | } |
2594 | } |
2595 | else { |
2596 | if (!check_class(cls, Branch (2596:13): [True: 6, False: 9]
|
2597 | "isinstance() arg 2 must be a type, a tuple of types, or a union")) |
2598 | return -1; |
2599 | retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls); |
2600 | if (icls != NULL) { Branch (2600:13): [True: 9, False: 0]
|
2601 | retval = abstract_issubclass(icls, cls); |
2602 | Py_DECREF(icls); |
2603 | } |
2604 | } |
2605 | |
2606 | return retval; |
2607 | } |
2608 | |
2609 | static int |
2610 | object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls) |
2611 | { |
2612 | /* Quick test for an exact match */ |
2613 | if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) { |
2614 | return 1; |
2615 | } |
2616 | |
2617 | /* We know what type's __instancecheck__ does. */ |
2618 | if (PyType_CheckExact(cls)) { |
2619 | return object_isinstance(inst, cls); |
2620 | } |
2621 | |
2622 | if (_PyUnion_Check(cls)) { |
2623 | cls = _Py_union_args(cls); |
2624 | } |
2625 | |
2626 | if (PyTuple_Check(cls)) { |
2627 | /* Not a general sequence -- that opens up the road to |
2628 | recursion and stack overflow. */ |
2629 | if (_Py_EnterRecursiveCallTstate(tstate, " in __instancecheck__")) { Branch (2629:13): [True: 2, False: 3.21M]
|
2630 | return -1; |
2631 | } |
2632 | Py_ssize_t n = PyTuple_GET_SIZE(cls); |
2633 | int r = 0; |
2634 | for (Py_ssize_t i = 0; i < n; ++i737k ) { Branch (2634:32): [True: 3.71M, False: 245k]
|
2635 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
2636 | r = object_recursive_isinstance(tstate, inst, item); |
2637 | if (r != 0) { Branch (2637:17): [True: 2.97M, False: 737k]
|
2638 | /* either found it, or got an error */ |
2639 | break; |
2640 | } |
2641 | } |
2642 | _Py_LeaveRecursiveCallTstate(tstate); |
2643 | return r; |
2644 | } |
2645 | |
2646 | PyObject *checker = _PyObject_LookupSpecial(cls, &_Py_ID(__instancecheck__)); |
2647 | if (checker != NULL) { Branch (2647:9): [True: 719k, False: 16]
|
2648 | if (_Py_EnterRecursiveCallTstate(tstate, " in __instancecheck__")) { Branch (2648:13): [True: 0, False: 719k]
|
2649 | Py_DECREF(checker); |
2650 | return -1; |
2651 | } |
2652 | |
2653 | PyObject *res = PyObject_CallOneArg(checker, inst); |
2654 | _Py_LeaveRecursiveCallTstate(tstate); |
2655 | Py_DECREF(checker); |
2656 | |
2657 | if (res == NULL) { Branch (2657:13): [True: 50, False: 719k]
|
2658 | return -1; |
2659 | } |
2660 | int ok = PyObject_IsTrue(res); |
2661 | Py_DECREF(res); |
2662 | |
2663 | return ok; |
2664 | } |
2665 | else if (_PyErr_Occurred(tstate)) { Branch (2665:14): [True: 1, False: 15]
|
2666 | return -1; |
2667 | } |
2668 | |
2669 | /* cls has no __instancecheck__() method */ |
2670 | return object_isinstance(inst, cls); |
2671 | } |
2672 | |
2673 | |
2674 | int |
2675 | PyObject_IsInstance(PyObject *inst, PyObject *cls) |
2676 | { |
2677 | PyThreadState *tstate = _PyThreadState_GET(); |
2678 | return object_recursive_isinstance(tstate, inst, cls); |
2679 | } |
2680 | |
2681 | |
2682 | static int |
2683 | recursive_issubclass(PyObject *derived, PyObject *cls) |
2684 | { |
2685 | if (PyType_Check(cls) && PyType_Check1.72M (derived)) { |
2686 | /* Fast path (non-recursive) */ |
2687 | return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls); |
2688 | } |
2689 | if (!check_class(derived, Branch (2689:9): [True: 79, False: 32]
|
2690 | "issubclass() arg 1 must be a class")) |
2691 | return -1; |
2692 | |
2693 | if (!_PyUnion_Check(cls) && !check_class(cls, Branch (2693:9): [True: 32, False: 0]
Branch (2693:33): [True: 20, False: 12]
|
2694 | "issubclass() arg 2 must be a class," |
2695 | " a tuple of classes, or a union")) { |
2696 | return -1; |
2697 | } |
2698 | |
2699 | return abstract_issubclass(derived, cls); |
2700 | } |
2701 | |
2702 | static int |
2703 | object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls) |
2704 | { |
2705 | PyObject *checker; |
2706 | |
2707 | /* We know what type's __subclasscheck__ does. */ |
2708 | if (PyType_CheckExact(cls)) { |
2709 | /* Quick test for an exact match */ |
2710 | if (derived == cls) Branch (2710:13): [True: 1.50M, False: 467k]
|
2711 | return 1; |
2712 | return recursive_issubclass(derived, cls); |
2713 | } |
2714 | |
2715 | if (_PyUnion_Check(cls)) { |
2716 | cls = _Py_union_args(cls); |
2717 | } |
2718 | |
2719 | if (PyTuple_Check(cls)) { |
2720 | |
2721 | if (_Py_EnterRecursiveCallTstate(tstate, " in __subclasscheck__")) { Branch (2721:13): [True: 1, False: 33.7k]
|
2722 | return -1; |
2723 | } |
2724 | Py_ssize_t n = PyTuple_GET_SIZE(cls); |
2725 | int r = 0; |
2726 | for (Py_ssize_t i = 0; i < n; ++i24.5k ) { Branch (2726:32): [True: 58.2k, False: 38]
|
2727 | PyObject *item = PyTuple_GET_ITEM(cls, i); |
2728 | r = object_issubclass(tstate, derived, item); |
2729 | if (r != 0) Branch (2729:17): [True: 33.7k, False: 24.5k]
|
2730 | /* either found it, or got an error */ |
2731 | break; |
2732 | } |
2733 | _Py_LeaveRecursiveCallTstate(tstate); |
2734 | return r; |
2735 | } |
2736 | |
2737 | checker = _PyObject_LookupSpecial(cls, &_Py_ID(__subclasscheck__)); |
2738 | if (checker != NULL) { Branch (2738:9): [True: 125k, False: 29]
|
2739 | int ok = -1; |
2740 | if (_Py_EnterRecursiveCallTstate(tstate, " in __subclasscheck__")) { Branch (2740:13): [True: 0, False: 125k]
|
2741 | Py_DECREF(checker); |
2742 | return ok; |
2743 | } |
2744 | PyObject *res = PyObject_CallOneArg(checker, derived); |
2745 | _Py_LeaveRecursiveCallTstate(tstate); |
2746 | Py_DECREF(checker); |
2747 | if (res != NULL) { Branch (2747:13): [True: 125k, False: 69]
|
2748 | ok = PyObject_IsTrue(res); |
2749 | Py_DECREF(res); |
2750 | } |
2751 | return ok; |
2752 | } |
2753 | else if (_PyErr_Occurred(tstate)) { Branch (2753:14): [True: 1, False: 28]
|
2754 | return -1; |
2755 | } |
2756 | |
2757 | /* Probably never reached anymore. */ |
2758 | return recursive_issubclass(derived, cls); |
2759 | } |
2760 | |
2761 | |
2762 | int |
2763 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
2764 | { |
2765 | PyThreadState *tstate = _PyThreadState_GET(); |
2766 | return object_issubclass(tstate, derived, cls); |
2767 | } |
2768 | |
2769 | |
2770 | int |
2771 | _PyObject_RealIsInstance(PyObject *inst, PyObject *cls) |
2772 | { |
2773 | return object_isinstance(inst, cls); |
2774 | } |
2775 | |
2776 | int |
2777 | _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls) |
2778 | { |
2779 | return recursive_issubclass(derived, cls); |
2780 | } |
2781 | |
2782 | |
2783 | PyObject * |
2784 | PyObject_GetIter(PyObject *o) |
2785 | { |
2786 | PyTypeObject *t = Py_TYPE(o); |
2787 | getiterfunc f; |
2788 | |
2789 | f = t->tp_iter; |
2790 | if (f == NULL) { Branch (2790:9): [True: 93.1k, False: 37.6M]
|
2791 | if (PySequence_Check(o)) Branch (2791:13): [True: 29.2k, False: 63.8k]
|
2792 | return PySeqIter_New(o); |
2793 | return type_error("'%.200s' object is not iterable", o); |
2794 | } |
2795 | else { |
2796 | PyObject *res = (*f)(o); |
2797 | if (res != NULL && !PyIter_Check(res)37.6M ) { Branch (2797:13): [True: 37.6M, False: 153]
Branch (2797:28): [True: 199, False: 37.6M]
|
2798 | PyErr_Format(PyExc_TypeError, |
2799 | "iter() returned non-iterator " |
2800 | "of type '%.100s'", |
2801 | Py_TYPE(res)->tp_name); |
2802 | Py_DECREF(res); |
2803 | res = NULL; |
2804 | } |
2805 | return res; |
2806 | } |
2807 | } |
2808 | |
2809 | PyObject * |
2810 | PyObject_GetAIter(PyObject *o) { |
2811 | PyTypeObject *t = Py_TYPE(o); |
2812 | unaryfunc f; |
2813 | |
2814 | if (t->tp_as_async == NULL || t->tp_as_async->am_aiter == NULL4 ) { Branch (2814:9): [True: 1, False: 4]
Branch (2814:35): [True: 0, False: 4]
|
2815 | return type_error("'%.200s' object is not an async iterable", o); |
2816 | } |
2817 | f = t->tp_as_async->am_aiter; |
2818 | PyObject *it = (*f)(o); |
2819 | if (it != NULL && !PyAIter_Check(it)) { Branch (2819:9): [True: 4, False: 0]
Branch (2819:23): [True: 0, False: 4]
|
2820 | PyErr_Format(PyExc_TypeError, |
2821 | "aiter() returned not an async iterator of type '%.100s'", |
2822 | Py_TYPE(it)->tp_name); |
2823 | Py_DECREF(it); |
2824 | it = NULL; |
2825 | } |
2826 | return it; |
2827 | } |
2828 | |
2829 | int |
2830 | PyIter_Check(PyObject *obj) |
2831 | { |
2832 | PyTypeObject *tp = Py_TYPE(obj); |
2833 | return (tp->tp_iternext != NULL && Branch (2833:13): [True: 41.5M, False: 166]
|
2834 | tp->tp_iternext != &_PyObject_NextNotImplemented41.5M ); Branch (2834:13): [True: 41.5M, False: 207]
|
2835 | } |
2836 | |
2837 | int |
2838 | PyAIter_Check(PyObject *obj) |
2839 | { |
2840 | PyTypeObject *tp = Py_TYPE(obj); |
2841 | return (tp->tp_as_async != NULL && Branch (2841:13): [True: 4, False: 0]
|
2842 | tp->tp_as_async->am_anext != NULL && Branch (2842:13): [True: 4, False: 0]
|
2843 | tp->tp_as_async->am_anext != &_PyObject_NextNotImplemented); Branch (2843:13): [True: 4, False: 0]
|
2844 | } |
2845 | |
2846 | /* Return next item. |
2847 | * If an error occurs, return NULL. PyErr_Occurred() will be true. |
2848 | * If the iteration terminates normally, return NULL and clear the |
2849 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred() |
2850 | * will be false. |
2851 | * Else return the next object. PyErr_Occurred() will be false. |
2852 | */ |
2853 | PyObject * |
2854 | PyIter_Next(PyObject *iter) |
2855 | { |
2856 | PyObject *result; |
2857 | result = (*Py_TYPE(iter)->tp_iternext)(iter); |
2858 | if (result == NULL) { Branch (2858:9): [True: 8.87M, False: 84.8M]
|
2859 | PyThreadState *tstate = _PyThreadState_GET(); |
2860 | if (_PyErr_Occurred(tstate) Branch (2860:13): [True: 880, False: 8.87M]
|
2861 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)880 ) Branch (2861:16): [True: 308, False: 572]
|
2862 | { |
2863 | _PyErr_Clear(tstate); |
2864 | } |
2865 | } |
2866 | return result; |
2867 | } |
2868 | |
2869 | PySendResult |
2870 | PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result) |
2871 | { |
2872 | assert(arg != NULL); |
2873 | assert(result != NULL); |
2874 | if (Py_TYPE(iter)->tp_as_async && Py_TYPE1.53M (iter)->tp_as_async->am_send1.53M ) { Branch (2874:9): [True: 1.53M, False: 1.06M]
Branch (2874:39): [True: 1.52M, False: 716]
|
2875 | PySendResult res = Py_TYPE(iter)->tp_as_async->am_send(iter, arg, result); |
2876 | assert(_Py_CheckSlotResult(iter, "am_send", res != PYGEN_ERROR)); |
2877 | return res; |
2878 | } |
2879 | if (arg == Py_None && PyIter_Check(iter)1.06M ) { Branch (2879:9): [True: 1.06M, False: 5]
Branch (2879:27): [True: 1.06M, False: 8]
|
2880 | *result = Py_TYPE(iter)->tp_iternext(iter); |
2881 | } |
2882 | else { |
2883 | *result = PyObject_CallMethodOneArg(iter, &_Py_ID(send), arg); |
2884 | } |
2885 | if (*result != NULL) { Branch (2885:9): [True: 1.06M, False: 5.58k]
|
2886 | return PYGEN_NEXT; |
2887 | } |
2888 | if (_PyGen_FetchStopIterationValue(result) == 0) { Branch (2888:9): [True: 5.40k, False: 179]
|
2889 | return PYGEN_RETURN; |
2890 | } |
2891 | return PYGEN_ERROR; |
2892 | } |
2893 | |
2894 | /* |
2895 | * Flatten a sequence of bytes() objects into a C array of |
2896 | * NULL terminated string pointers with a NULL char* terminating the array. |
2897 | * (ie: an argv or env list) |
2898 | * |
2899 | * Memory allocated for the returned list is allocated using PyMem_Malloc() |
2900 | * and MUST be freed by _Py_FreeCharPArray(). |
2901 | */ |
2902 | char *const * |
2903 | _PySequence_BytesToCharpArray(PyObject* self) |
2904 | { |
2905 | char **array; |
2906 | Py_ssize_t i, argc; |
2907 | PyObject *item = NULL; |
2908 | Py_ssize_t size; |
2909 | |
2910 | argc = PySequence_Size(self); |
2911 | if (argc == -1) Branch (2911:9): [True: 2, False: 10.7k]
|
2912 | return NULL; |
2913 | |
2914 | assert(argc >= 0); |
2915 | |
2916 | if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) { Branch (2916:9): [True: 1, False: 10.7k]
|
2917 | PyErr_NoMemory(); |
2918 | return NULL; |
2919 | } |
2920 | |
2921 | array = PyMem_Malloc((argc + 1) * sizeof(char *)); |
2922 | if (array == NULL) { Branch (2922:9): [True: 0, False: 10.7k]
|
2923 | PyErr_NoMemory(); |
2924 | return NULL; |
2925 | } |
2926 | for (i = 0; 10.7k i < argc; ++i182k ) { Branch (2926:17): [True: 182k, False: 10.7k]
|
2927 | char *data; |
2928 | item = PySequence_GetItem(self, i); |
2929 | if (item == NULL) { Branch (2929:13): [True: 1, False: 182k]
|
2930 | /* NULL terminate before freeing. */ |
2931 | array[i] = NULL; |
2932 | goto fail; |
2933 | } |
2934 | /* check for embedded null bytes */ |
2935 | if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) { Branch (2935:13): [True: 6, False: 182k]
|
2936 | /* NULL terminate before freeing. */ |
2937 | array[i] = NULL; |
2938 | goto fail; |
2939 | } |
2940 | size = PyBytes_GET_SIZE(item) + 1; |
2941 | array[i] = PyMem_Malloc(size); |
2942 | if (!array[i]) { Branch (2942:13): [True: 0, False: 182k]
|
2943 | PyErr_NoMemory(); |
2944 | goto fail; |
2945 | } |
2946 | memcpy(array[i], data, size); |
2947 | Py_DECREF(item); |
2948 | } |
2949 | array[argc] = NULL; |
2950 | |
2951 | return array; |
2952 | |
2953 | fail: |
2954 | Py_XDECREF(item); |
2955 | _Py_FreeCharPArray(array); |
2956 | return NULL; |
2957 | } |
2958 | |
2959 | |
2960 | /* Free's a NULL terminated char** array of C strings. */ |
2961 | void |
2962 | _Py_FreeCharPArray(char *const array[]) |
2963 | { |
2964 | Py_ssize_t i; |
2965 | for (i = 0; array[i] != NULL; ++i182k ) { Branch (2965:17): [True: 182k, False: 10.7k]
|
2966 | PyMem_Free(array[i]); |
2967 | } |
2968 | PyMem_Free((void*)array); |
2969 | } |