/home/mdboom/Work/builds/cpython/Objects/object.c
Line | Count | Source (jump to first uncovered line) |
1 | |
2 | /* Generic object operations; and implementation of None */ |
3 | |
4 | #include "Python.h" |
5 | #include "pycore_call.h" // _PyObject_CallNoArgs() |
6 | #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate() |
7 | #include "pycore_context.h" // _PyContextTokenMissing_Type |
8 | #include "pycore_dict.h" // _PyObject_MakeDictFromInstanceAttributes() |
9 | #include "pycore_floatobject.h" // _PyFloat_DebugMallocStats() |
10 | #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() |
11 | #include "pycore_namespace.h" // _PyNamespace_Type |
12 | #include "pycore_object.h" // _PyType_CheckConsistency(), _Py_FatalRefcountError() |
13 | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
14 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
15 | #include "pycore_pystate.h" // _PyThreadState_GET() |
16 | #include "pycore_symtable.h" // PySTEntry_Type |
17 | #include "pycore_typeobject.h" // _PyTypes_InitSlotDefs() |
18 | #include "pycore_unionobject.h" // _PyUnion_Type |
19 | #include "pycore_interpreteridobject.h" // _PyInterpreterID_Type |
20 | |
21 | #ifdef Py_LIMITED_API |
22 | // Prevent recursive call _Py_IncRef() <=> Py_INCREF() |
23 | # error "Py_LIMITED_API macro must not be defined" |
24 | #endif |
25 | |
26 | #ifdef __cplusplus |
27 | extern "C" { |
28 | #endif |
29 | |
30 | /* Defined in tracemalloc.c */ |
31 | extern void _PyMem_DumpTraceback(int fd, const void *ptr); |
32 | |
33 | |
34 | int |
35 | _PyObject_CheckConsistency(PyObject *op, int check_content) |
36 | { |
37 | #define CHECK(expr) \ |
38 | do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0) |
39 |
|
40 | CHECK(!_PyObject_IsFreed(op)); |
41 | CHECK(Py_REFCNT(op) >= 1); |
42 | |
43 | _PyType_CheckConsistency(Py_TYPE(op)); |
44 |
|
45 | if (PyUnicode_Check(op)) { |
46 | _PyUnicode_CheckConsistency(op, check_content); |
47 | } |
48 | else if (PyDict_Check(op)) { |
49 | _PyDict_CheckConsistency(op, check_content); |
50 | } |
51 | return 1; |
52 |
|
53 | #undef CHECK |
54 | } |
55 | |
56 | |
57 | #ifdef Py_REF_DEBUG |
58 | Py_ssize_t _Py_RefTotal; |
59 | |
60 | Py_ssize_t |
61 | _Py_GetRefTotal(void) |
62 | { |
63 | return _Py_RefTotal; |
64 | } |
65 | |
66 | void |
67 | _PyDebug_PrintTotalRefs(void) { |
68 | fprintf(stderr, |
69 | "[%zd refs, %zd blocks]\n", |
70 | _Py_GetRefTotal(), _Py_GetAllocatedBlocks()); |
71 | } |
72 | #endif /* Py_REF_DEBUG */ |
73 | |
74 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
75 | These are used by the individual routines for object creation. |
76 | Do not call them otherwise, they do not initialize the object! */ |
77 | |
78 | #ifdef Py_TRACE_REFS |
79 | /* Head of circular doubly-linked list of all objects. These are linked |
80 | * together via the _ob_prev and _ob_next members of a PyObject, which |
81 | * exist only in a Py_TRACE_REFS build. |
82 | */ |
83 | static PyObject refchain = {&refchain, &refchain}; |
84 | |
85 | /* Insert op at the front of the list of all objects. If force is true, |
86 | * op is added even if _ob_prev and _ob_next are non-NULL already. If |
87 | * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. |
88 | * force should be true if and only if op points to freshly allocated, |
89 | * uninitialized memory, or you've unlinked op from the list and are |
90 | * relinking it into the front. |
91 | * Note that objects are normally added to the list via _Py_NewReference, |
92 | * which is called by PyObject_Init. Not all objects are initialized that |
93 | * way, though; exceptions include statically allocated type objects, and |
94 | * statically allocated singletons (like Py_True and Py_None). |
95 | */ |
96 | void |
97 | _Py_AddToAllObjects(PyObject *op, int force) |
98 | { |
99 | #ifdef Py_DEBUG |
100 | if (!force) { |
101 | /* If it's initialized memory, op must be in or out of |
102 | * the list unambiguously. |
103 | */ |
104 | _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
105 | } |
106 | #endif |
107 | if (force || op->_ob_prev == NULL) { |
108 | op->_ob_next = refchain._ob_next; |
109 | op->_ob_prev = &refchain; |
110 | refchain._ob_next->_ob_prev = op; |
111 | refchain._ob_next = op; |
112 | } |
113 | } |
114 | #endif /* Py_TRACE_REFS */ |
115 | |
116 | #ifdef Py_REF_DEBUG |
117 | /* Log a fatal error; doesn't return. */ |
118 | void |
119 | _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) |
120 | { |
121 | _PyObject_AssertFailed(op, NULL, "object has negative ref count", |
122 | filename, lineno, __func__); |
123 | } |
124 | |
125 | #endif /* Py_REF_DEBUG */ |
126 | |
127 | void |
128 | Py_IncRef(PyObject *o) |
129 | { |
130 | Py_XINCREF(o); |
131 | } |
132 | |
133 | void |
134 | Py_DecRef(PyObject *o) |
135 | { |
136 | Py_XDECREF(o); |
137 | } |
138 | |
139 | void |
140 | _Py_IncRef(PyObject *o) |
141 | { |
142 | Py_INCREF(o); |
143 | } |
144 | |
145 | void |
146 | _Py_DecRef(PyObject *o) |
147 | { |
148 | Py_DECREF(o); |
149 | } |
150 | |
151 | PyObject * |
152 | PyObject_Init(PyObject *op, PyTypeObject *tp) |
153 | { |
154 | if (op == NULL) { Branch (154:9): [True: 0, False: 0]
|
155 | return PyErr_NoMemory(); |
156 | } |
157 | |
158 | _PyObject_Init(op, tp); |
159 | return op; |
160 | } |
161 | |
162 | PyVarObject * |
163 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
164 | { |
165 | if (op == NULL) { Branch (165:9): [True: 0, False: 0]
|
166 | return (PyVarObject *) PyErr_NoMemory(); |
167 | } |
168 | |
169 | _PyObject_InitVar(op, tp, size); |
170 | return op; |
171 | } |
172 | |
173 | PyObject * |
174 | _PyObject_New(PyTypeObject *tp) |
175 | { |
176 | PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp)); |
177 | if (op == NULL) { Branch (177:9): [True: 0, False: 2.83M]
|
178 | return PyErr_NoMemory(); |
179 | } |
180 | _PyObject_Init(op, tp); |
181 | return op; |
182 | } |
183 | |
184 | PyVarObject * |
185 | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
186 | { |
187 | PyVarObject *op; |
188 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
189 | op = (PyVarObject *) PyObject_Malloc(size); |
190 | if (op == NULL) { Branch (190:9): [True: 0, False: 263k]
|
191 | return (PyVarObject *)PyErr_NoMemory(); |
192 | } |
193 | _PyObject_InitVar(op, tp, nitems); |
194 | return op; |
195 | } |
196 | |
197 | void |
198 | PyObject_CallFinalizer(PyObject *self) |
199 | { |
200 | PyTypeObject *tp = Py_TYPE(self); |
201 | |
202 | if (tp->tp_finalize == NULL) Branch (202:9): [True: 0, False: 4.45M]
|
203 | return; |
204 | /* tp_finalize should only be called once. */ |
205 | if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self)4.27M ) Branch (205:30): [True: 215k, False: 4.05M]
|
206 | return; |
207 | |
208 | tp->tp_finalize(self); |
209 | if (_PyType_IS_GC(tp)) { |
210 | _PyGC_SET_FINALIZED(self); |
211 | } |
212 | } |
213 | |
214 | int |
215 | PyObject_CallFinalizerFromDealloc(PyObject *self) |
216 | { |
217 | if (Py_REFCNT(self) != 0) { Branch (217:9): [True: 0, False: 4.45M]
|
218 | _PyObject_ASSERT_FAILED_MSG(self, |
219 | "PyObject_CallFinalizerFromDealloc called " |
220 | "on object with a non-zero refcount"); |
221 | } |
222 | |
223 | /* Temporarily resurrect the object. */ |
224 | Py_SET_REFCNT(self, 1); |
225 | |
226 | PyObject_CallFinalizer(self); |
227 | |
228 | _PyObject_ASSERT_WITH_MSG(self, |
229 | Py_REFCNT(self) > 0, |
230 | "refcount is too small"); |
231 | |
232 | /* Undo the temporary resurrection; can't use DECREF here, it would |
233 | * cause a recursive call. */ |
234 | Py_SET_REFCNT(self, Py_REFCNT(self) - 1); |
235 | if (Py_REFCNT(self) == 0) { Branch (235:9): [True: 4.45M, False: 101]
|
236 | return 0; /* this is the normal path out */ |
237 | } |
238 | |
239 | /* tp_finalize resurrected it! Make it look like the original Py_DECREF |
240 | * never happened. */ |
241 | Py_ssize_t refcnt = Py_REFCNT(self); |
242 | _Py_NewReference(self); |
243 | Py_SET_REFCNT(self, refcnt); |
244 | |
245 | _PyObject_ASSERT(self, |
246 | (!_PyType_IS_GC(Py_TYPE(self)) |
247 | || _PyObject_GC_IS_TRACKED(self))); |
248 | /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased |
249 | _Py_RefTotal, so we need to undo that. */ |
250 | #ifdef Py_REF_DEBUG |
251 | _Py_RefTotal--; |
252 | #endif |
253 | return -1; |
254 | } |
255 | |
256 | int |
257 | PyObject_Print(PyObject *op, FILE *fp, int flags) |
258 | { |
259 | int ret = 0; |
260 | if (PyErr_CheckSignals()) Branch (260:9): [True: 0, False: 0]
|
261 | return -1; |
262 | #ifdef USE_STACKCHECK |
263 | if (PyOS_CheckStack()) { |
264 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
265 | return -1; |
266 | } |
267 | #endif |
268 | clearerr(fp); /* Clear any previous error condition */ |
269 | if (op == NULL) { Branch (269:9): [True: 0, False: 0]
|
270 | Py_BEGIN_ALLOW_THREADS |
271 | fprintf(fp, "<nil>"); |
272 | Py_END_ALLOW_THREADS |
273 | } |
274 | else { |
275 | if (Py_REFCNT(op) <= 0) { Branch (275:13): [True: 0, False: 0]
|
276 | Py_BEGIN_ALLOW_THREADS |
277 | fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op); |
278 | Py_END_ALLOW_THREADS |
279 | } |
280 | else { |
281 | PyObject *s; |
282 | if (flags & Py_PRINT_RAW) Branch (282:17): [True: 0, False: 0]
|
283 | s = PyObject_Str(op); |
284 | else |
285 | s = PyObject_Repr(op); |
286 | if (s == NULL) Branch (286:17): [True: 0, False: 0]
|
287 | ret = -1; |
288 | else if (PyBytes_Check(s)) { |
289 | fwrite(PyBytes_AS_STRING(s), 1, |
290 | PyBytes_GET_SIZE(s), fp); |
291 | } |
292 | else if (PyUnicode_Check(s)) { |
293 | PyObject *t; |
294 | t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); |
295 | if (t == NULL) { Branch (295:21): [True: 0, False: 0]
|
296 | ret = -1; |
297 | } |
298 | else { |
299 | fwrite(PyBytes_AS_STRING(t), 1, |
300 | PyBytes_GET_SIZE(t), fp); |
301 | Py_DECREF(t); |
302 | } |
303 | } |
304 | else { |
305 | PyErr_Format(PyExc_TypeError, |
306 | "str() or repr() returned '%.100s'", |
307 | Py_TYPE(s)->tp_name); |
308 | ret = -1; |
309 | } |
310 | Py_XDECREF(s); |
311 | } |
312 | } |
313 | if (ret == 0) { Branch (313:9): [True: 0, False: 0]
|
314 | if (ferror(fp)) { Branch (314:13): [True: 0, False: 0]
|
315 | PyErr_SetFromErrno(PyExc_OSError); |
316 | clearerr(fp); |
317 | ret = -1; |
318 | } |
319 | } |
320 | return ret; |
321 | } |
322 | |
323 | /* For debugging convenience. Set a breakpoint here and call it from your DLL */ |
324 | void |
325 | _Py_BreakPoint(void) |
326 | { |
327 | } |
328 | |
329 | |
330 | /* Heuristic checking if the object memory is uninitialized or deallocated. |
331 | Rely on the debug hooks on Python memory allocators: |
332 | see _PyMem_IsPtrFreed(). |
333 | |
334 | The function can be used to prevent segmentation fault on dereferencing |
335 | pointers like 0xDDDDDDDDDDDDDDDD. */ |
336 | int |
337 | _PyObject_IsFreed(PyObject *op) |
338 | { |
339 | if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) { Branch (339:9): [True: 0, False: 0]
Branch (339:34): [True: 0, False: 0]
|
340 | return 1; |
341 | } |
342 | /* ignore op->ob_ref: its value can have be modified |
343 | by Py_INCREF() and Py_DECREF(). */ |
344 | #ifdef Py_TRACE_REFS |
345 | if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) { |
346 | return 1; |
347 | } |
348 | if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) { |
349 | return 1; |
350 | } |
351 | #endif |
352 | return 0; |
353 | } |
354 | |
355 | |
356 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
357 | void |
358 | _PyObject_Dump(PyObject* op) |
359 | { |
360 | if (_PyObject_IsFreed(op)) { Branch (360:9): [True: 0, False: 0]
|
361 | /* It seems like the object memory has been freed: |
362 | don't access it to prevent a segmentation fault. */ |
363 | fprintf(stderr, "<object at %p is freed>\n", op); |
364 | fflush(stderr); |
365 | return; |
366 | } |
367 | |
368 | /* first, write fields which are the least likely to crash */ |
369 | fprintf(stderr, "object address : %p\n", (void *)op); |
370 | fprintf(stderr, "object refcount : %zd\n", Py_REFCNT(op)); |
371 | fflush(stderr); |
372 |
|
373 | PyTypeObject *type = Py_TYPE(op); |
374 | fprintf(stderr, "object type : %p\n", type); |
375 | fprintf(stderr, "object type name: %s\n", |
376 | type==NULL ? "NULL" : type->tp_name); Branch (376:13): [True: 0, False: 0]
|
377 | |
378 | /* the most dangerous part */ |
379 | fprintf(stderr, "object repr : "); |
380 | fflush(stderr); |
381 |
|
382 | PyGILState_STATE gil = PyGILState_Ensure(); |
383 | PyObject *error_type, *error_value, *error_traceback; |
384 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
385 |
|
386 | (void)PyObject_Print(op, stderr, 0); |
387 | fflush(stderr); |
388 |
|
389 | PyErr_Restore(error_type, error_value, error_traceback); |
390 | PyGILState_Release(gil); |
391 |
|
392 | fprintf(stderr, "\n"); |
393 | fflush(stderr); |
394 | } |
395 | |
396 | PyObject * |
397 | PyObject_Repr(PyObject *v) |
398 | { |
399 | PyObject *res; |
400 | if (PyErr_CheckSignals()) Branch (400:9): [True: 0, False: 4.14M]
|
401 | return NULL; |
402 | #ifdef USE_STACKCHECK |
403 | if (PyOS_CheckStack()) { |
404 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
405 | return NULL; |
406 | } |
407 | #endif |
408 | if (v == NULL) Branch (408:9): [True: 1, False: 4.14M]
|
409 | return PyUnicode_FromString("<NULL>"); |
410 | if (Py_TYPE(v)->tp_repr == NULL) Branch (410:9): [True: 0, False: 4.14M]
|
411 | return PyUnicode_FromFormat("<%s object at %p>", |
412 | Py_TYPE(v)->tp_name, v); |
413 | |
414 | PyThreadState *tstate = _PyThreadState_GET(); |
415 | #ifdef Py_DEBUG |
416 | /* PyObject_Repr() must not be called with an exception set, |
417 | because it can clear it (directly or indirectly) and so the |
418 | caller loses its exception */ |
419 | assert(!_PyErr_Occurred(tstate)); |
420 | #endif |
421 | |
422 | /* It is possible for a type to have a tp_repr representation that loops |
423 | infinitely. */ |
424 | if (_Py_EnterRecursiveCallTstate(tstate, Branch (424:9): [True: 3, False: 4.14M]
|
425 | " while getting the repr of an object")) { |
426 | return NULL; |
427 | } |
428 | res = (*Py_TYPE(v)->tp_repr)(v); |
429 | _Py_LeaveRecursiveCallTstate(tstate); |
430 | |
431 | if (res == NULL) { Branch (431:9): [True: 6.17k, False: 4.13M]
|
432 | return NULL; |
433 | } |
434 | if (!PyUnicode_Check(res)) { Branch (434:9): [True: 3, False: 4.13M]
|
435 | _PyErr_Format(tstate, PyExc_TypeError, |
436 | "__repr__ returned non-string (type %.200s)", |
437 | Py_TYPE(res)->tp_name); |
438 | Py_DECREF(res); |
439 | return NULL; |
440 | } |
441 | #ifndef Py_DEBUG |
442 | if (PyUnicode_READY(res) < 0) { Branch (442:9): [True: 0, False: 4.13M]
|
443 | return NULL; |
444 | } |
445 | #endif |
446 | return res; |
447 | } |
448 | |
449 | PyObject * |
450 | PyObject_Str(PyObject *v) |
451 | { |
452 | PyObject *res; |
453 | if (PyErr_CheckSignals()) Branch (453:9): [True: 0, False: 1.86M]
|
454 | return NULL; |
455 | #ifdef USE_STACKCHECK |
456 | if (PyOS_CheckStack()) { |
457 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
458 | return NULL; |
459 | } |
460 | #endif |
461 | if (v == NULL) Branch (461:9): [True: 1, False: 1.86M]
|
462 | return PyUnicode_FromString("<NULL>"); |
463 | if (PyUnicode_CheckExact(v)) { |
464 | #ifndef Py_DEBUG |
465 | if (PyUnicode_READY(v) < 0) Branch (465:13): [True: 0, False: 738k]
|
466 | return NULL; |
467 | #endif |
468 | Py_INCREF(v); |
469 | return v; |
470 | } |
471 | if (Py_TYPE(v)->tp_str == NULL) Branch (471:9): [True: 0, False: 1.12M]
|
472 | return PyObject_Repr(v); |
473 | |
474 | PyThreadState *tstate = _PyThreadState_GET(); |
475 | #ifdef Py_DEBUG |
476 | /* PyObject_Str() must not be called with an exception set, |
477 | because it can clear it (directly or indirectly) and so the |
478 | caller loses its exception */ |
479 | assert(!_PyErr_Occurred(tstate)); |
480 | #endif |
481 | |
482 | /* It is possible for a type to have a tp_str representation that loops |
483 | infinitely. */ |
484 | if (_Py_EnterRecursiveCallTstate(tstate, " while getting the str of an object")) { Branch (484:9): [True: 0, False: 1.12M]
|
485 | return NULL; |
486 | } |
487 | res = (*Py_TYPE(v)->tp_str)(v); |
488 | _Py_LeaveRecursiveCallTstate(tstate); |
489 | |
490 | if (res == NULL) { Branch (490:9): [True: 29, False: 1.12M]
|
491 | return NULL; |
492 | } |
493 | if (!PyUnicode_Check(res)) { Branch (493:9): [True: 1, False: 1.12M]
|
494 | _PyErr_Format(tstate, PyExc_TypeError, |
495 | "__str__ returned non-string (type %.200s)", |
496 | Py_TYPE(res)->tp_name); |
497 | Py_DECREF(res); |
498 | return NULL; |
499 | } |
500 | #ifndef Py_DEBUG |
501 | if (PyUnicode_READY(res) < 0) { Branch (501:9): [True: 0, False: 1.12M]
|
502 | return NULL; |
503 | } |
504 | #endif |
505 | assert(_PyUnicode_CheckConsistency(res, 1)); |
506 | return res; |
507 | } |
508 | |
509 | PyObject * |
510 | PyObject_ASCII(PyObject *v) |
511 | { |
512 | PyObject *repr, *ascii, *res; |
513 | |
514 | repr = PyObject_Repr(v); |
515 | if (repr == NULL) Branch (515:9): [True: 1, False: 5.84k]
|
516 | return NULL; |
517 | |
518 | if (PyUnicode_IS_ASCII(repr)) |
519 | return repr; |
520 | |
521 | /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
522 | ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); |
523 | Py_DECREF(repr); |
524 | if (ascii == NULL) Branch (524:9): [True: 0, False: 3.57k]
|
525 | return NULL; |
526 | |
527 | res = PyUnicode_DecodeASCII( |
528 | PyBytes_AS_STRING(ascii), |
529 | PyBytes_GET_SIZE(ascii), |
530 | NULL); |
531 | |
532 | Py_DECREF(ascii); |
533 | return res; |
534 | } |
535 | |
536 | PyObject * |
537 | PyObject_Bytes(PyObject *v) |
538 | { |
539 | PyObject *result, *func; |
540 | |
541 | if (v == NULL) Branch (541:9): [True: 1, False: 188k]
|
542 | return PyBytes_FromString("<NULL>"); |
543 | |
544 | if (PyBytes_CheckExact(v)) { |
545 | Py_INCREF(v); |
546 | return v; |
547 | } |
548 | |
549 | func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__)); |
550 | if (func != NULL) { Branch (550:9): [True: 0, False: 1.40k]
|
551 | result = _PyObject_CallNoArgs(func); |
552 | Py_DECREF(func); |
553 | if (result == NULL) Branch (553:13): [True: 0, False: 0]
|
554 | return NULL; |
555 | if (!PyBytes_Check(result)) { Branch (555:13): [True: 0, False: 0]
|
556 | PyErr_Format(PyExc_TypeError, |
557 | "__bytes__ returned non-bytes (type %.200s)", |
558 | Py_TYPE(result)->tp_name); |
559 | Py_DECREF(result); |
560 | return NULL; |
561 | } |
562 | return result; |
563 | } |
564 | else if (PyErr_Occurred()) Branch (564:14): [True: 0, False: 1.40k]
|
565 | return NULL; |
566 | return PyBytes_FromObject(v); |
567 | } |
568 | |
569 | |
570 | /* |
571 | def _PyObject_FunctionStr(x): |
572 | try: |
573 | qualname = x.__qualname__ |
574 | except AttributeError: |
575 | return str(x) |
576 | try: |
577 | mod = x.__module__ |
578 | if mod is not None and mod != 'builtins': |
579 | return f"{x.__module__}.{qualname}()" |
580 | except AttributeError: |
581 | pass |
582 | return qualname |
583 | */ |
584 | PyObject * |
585 | _PyObject_FunctionStr(PyObject *x) |
586 | { |
587 | assert(!PyErr_Occurred()); |
588 | PyObject *qualname; |
589 | int ret = _PyObject_LookupAttr(x, &_Py_ID(__qualname__), &qualname); |
590 | if (qualname == NULL) { Branch (590:9): [True: 2, False: 550]
|
591 | if (ret < 0) { Branch (591:13): [True: 0, False: 2]
|
592 | return NULL; |
593 | } |
594 | return PyObject_Str(x); |
595 | } |
596 | PyObject *module; |
597 | PyObject *result = NULL; |
598 | ret = _PyObject_LookupAttr(x, &_Py_ID(__module__), &module); |
599 | if (module != NULL && module != 528 Py_None528 ) { Branch (599:9): [True: 528, False: 22]
Branch (599:27): [True: 147, False: 381]
|
600 | ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE); |
601 | if (ret < 0) { Branch (601:13): [True: 0, False: 147]
|
602 | // error |
603 | goto done; |
604 | } |
605 | if (ret > 0) { Branch (605:13): [True: 128, False: 19]
|
606 | result = PyUnicode_FromFormat("%S.%S()", module, qualname); |
607 | goto done; |
608 | } |
609 | } |
610 | else if (ret < 0) { Branch (610:14): [True: 0, False: 403]
|
611 | goto done; |
612 | } |
613 | result = PyUnicode_FromFormat("%S()", qualname); |
614 | done: |
615 | Py_DECREF(qualname); |
616 | Py_XDECREF(module); |
617 | return result; |
618 | } |
619 | |
620 | /* For Python 3.0.1 and later, the old three-way comparison has been |
621 | completely removed in favour of rich comparisons. PyObject_Compare() and |
622 | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
623 | The old tp_compare slot has been renamed to tp_as_async, and should no |
624 | longer be used. Use tp_richcompare instead. |
625 | |
626 | See (*) below for practical amendments. |
627 | |
628 | tp_richcompare gets called with a first argument of the appropriate type |
629 | and a second object of an arbitrary type. We never do any kind of |
630 | coercion. |
631 | |
632 | The tp_richcompare slot should return an object, as follows: |
633 | |
634 | NULL if an exception occurred |
635 | NotImplemented if the requested comparison is not implemented |
636 | any other false value if the requested comparison is false |
637 | any other true value if the requested comparison is true |
638 | |
639 | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
640 | NotImplemented. |
641 | |
642 | (*) Practical amendments: |
643 | |
644 | - If rich comparison returns NotImplemented, == and != are decided by |
645 | comparing the object pointer (i.e. falling back to the base object |
646 | implementation). |
647 | |
648 | */ |
649 | |
650 | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
651 | int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
652 | |
653 | static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
654 | |
655 | /* Perform a rich comparison, raising TypeError when the requested comparison |
656 | operator is not supported. */ |
657 | static PyObject * |
658 | do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op) |
659 | { |
660 | richcmpfunc f; |
661 | PyObject *res; |
662 | int checked_reverse_op = 0; |
663 | |
664 | if (!Py_IS_TYPE(v, Py_TYPE(w)) && Branch (664:9): [True: 3.31M, False: 95.9M]
|
665 | PyType_IsSubtype(3.31M Py_TYPE3.31M (w), Py_TYPE3.31M (v)) && Branch (665:9): [True: 115k, False: 3.20M]
|
666 | (f = 115k Py_TYPE115k (w)->tp_richcompare) != NULL) { Branch (666:9): [True: 115k, False: 0]
|
667 | checked_reverse_op = 1; |
668 | res = (*f)(w, v, _Py_SwappedOp[op]); |
669 | if (res != Py_NotImplemented) Branch (669:13): [True: 21.8k, False: 93.1k]
|
670 | return res; |
671 | Py_DECREF(res); |
672 | } |
673 | if ((f = Py_TYPE(v)->tp_richcompare) != NULL) { Branch (673:9): [True: 99.2M, False: 215]
|
674 | res = (*f)(v, w, op); |
675 | if (res != Py_NotImplemented) Branch (675:13): [True: 94.6M, False: 4.62M]
|
676 | return res; |
677 | Py_DECREF(res); |
678 | } |
679 | if (!checked_reverse_op && (f = 4.53M Py_TYPE4.53M (w)->tp_richcompare) != NULL) { Branch (679:9): [True: 4.53M, False: 93.1k]
Branch (679:32): [True: 4.53M, False: 207]
|
680 | res = (*f)(w, v, _Py_SwappedOp[op]); |
681 | if (res != Py_NotImplemented) Branch (681:13): [True: 209k, False: 4.32M]
|
682 | return res; |
683 | Py_DECREF(res); |
684 | } |
685 | /* If neither object implements it, provide a sensible default |
686 | for == and !=, but raise an exception for ordering. */ |
687 | switch (op) { |
688 | case Py_EQ: Branch (688:5): [True: 4.39M, False: 22.4k]
|
689 | res = (v == w) ? Py_True : Py_False; Branch (689:15): [True: 0, False: 4.39M]
|
690 | break; |
691 | case Py_NE: Branch (691:5): [True: 20.1k, False: 4.39M]
|
692 | res = (v != w) ? Py_True : Py_False; Branch (692:15): [True: 20.1k, False: 0]
|
693 | break; |
694 | default: Branch (694:5): [True: 2.32k, False: 4.41M]
|
695 | _PyErr_Format(tstate, PyExc_TypeError, |
696 | "'%s' not supported between instances of '%.100s' and '%.100s'", |
697 | opstrings[op], |
698 | Py_TYPE(v)->tp_name, |
699 | Py_TYPE(w)->tp_name); |
700 | return NULL; |
701 | } |
702 | Py_INCREF(res); |
703 | return res; |
704 | } |
705 | |
706 | /* Perform a rich comparison with object result. This wraps do_richcompare() |
707 | with a check for NULL arguments and a recursion check. */ |
708 | |
709 | PyObject * |
710 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
711 | { |
712 | PyThreadState *tstate = _PyThreadState_GET(); |
713 | |
714 | assert(Py_LT <= op && op <= Py_GE); |
715 | if (v == NULL || w == NULL) { Branch (715:9): [True: 0, False: 99.2M]
Branch (715:22): [True: 0, False: 99.2M]
|
716 | if (!_PyErr_Occurred(tstate)) { Branch (716:13): [True: 0, False: 0]
|
717 | PyErr_BadInternalCall(); |
718 | } |
719 | return NULL; |
720 | } |
721 | if (_Py_EnterRecursiveCallTstate(tstate, " in comparison")) { Branch (721:9): [True: 15, False: 99.2M]
|
722 | return NULL; |
723 | } |
724 | PyObject *res = do_richcompare(tstate, v, w, op); |
725 | _Py_LeaveRecursiveCallTstate(tstate); |
726 | return res; |
727 | } |
728 | |
729 | /* Perform a rich comparison with integer result. This wraps |
730 | PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
731 | int |
732 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
733 | { |
734 | PyObject *res; |
735 | int ok; |
736 | |
737 | /* Quick result when objects are the same. |
738 | Guarantees that identity implies equality. */ |
739 | if (v == w) { Branch (739:9): [True: 18.9M, False: 65.4M]
|
740 | if (op == Py_EQ) Branch (740:13): [True: 18.6M, False: 222k]
|
741 | return 1; |
742 | else if (op == Py_NE) Branch (742:18): [True: 8, False: 222k]
|
743 | return 0; |
744 | } |
745 | |
746 | res = PyObject_RichCompare(v, w, op); |
747 | if (res == NULL) Branch (747:9): [True: 20.0k, False: 65.6M]
|
748 | return -1; |
749 | if (PyBool_Check(res)) |
750 | ok = (res == Py_True); |
751 | else |
752 | ok = PyObject_IsTrue(res); |
753 | Py_DECREF(res); |
754 | return ok; |
755 | } |
756 | |
757 | Py_hash_t |
758 | PyObject_HashNotImplemented(PyObject *v) |
759 | { |
760 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
761 | Py_TYPE(v)->tp_name); |
762 | return -1; |
763 | } |
764 | |
765 | Py_hash_t |
766 | PyObject_Hash(PyObject *v) |
767 | { |
768 | PyTypeObject *tp = Py_TYPE(v); |
769 | if (tp->tp_hash != NULL) Branch (769:9): [True: 94.8M, False: 1]
|
770 | return (*tp->tp_hash)(v); |
771 | /* To keep to the general practice that inheriting |
772 | * solely from object in C code should work without |
773 | * an explicit call to PyType_Ready, we implicitly call |
774 | * PyType_Ready here and then check the tp_hash slot again |
775 | */ |
776 | if (tp->tp_dict == NULL) { Branch (776:9): [True: 1, False: 0]
|
777 | if (PyType_Ready(tp) < 0) Branch (777:13): [True: 0, False: 1]
|
778 | return -1; |
779 | if (tp->tp_hash != NULL) Branch (779:13): [True: 1, False: 0]
|
780 | return (*tp->tp_hash)(v); |
781 | } |
782 | /* Otherwise, the object can't be hashed */ |
783 | return PyObject_HashNotImplemented(v); |
784 | } |
785 | |
786 | PyObject * |
787 | PyObject_GetAttrString(PyObject *v, const char *name) |
788 | { |
789 | PyObject *w, *res; |
790 | |
791 | if (Py_TYPE(v)->tp_getattr != NULL) Branch (791:9): [True: 0, False: 2.31M]
|
792 | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
793 | w = PyUnicode_FromString(name); |
794 | if (w == NULL) Branch (794:9): [True: 0, False: 2.31M]
|
795 | return NULL; |
796 | res = PyObject_GetAttr(v, w); |
797 | Py_DECREF(w); |
798 | return res; |
799 | } |
800 | |
801 | int |
802 | PyObject_HasAttrString(PyObject *v, const char *name) |
803 | { |
804 | PyObject *res = PyObject_GetAttrString(v, name); |
805 | if (res != NULL) { Branch (805:9): [True: 0, False: 0]
|
806 | Py_DECREF(res); |
807 | return 1; |
808 | } |
809 | PyErr_Clear(); |
810 | return 0; |
811 | } |
812 | |
813 | int |
814 | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
815 | { |
816 | PyObject *s; |
817 | int res; |
818 | |
819 | if (Py_TYPE(v)->tp_setattr != NULL) Branch (819:9): [True: 0, False: 115k]
|
820 | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
821 | s = PyUnicode_InternFromString(name); |
822 | if (s == NULL) Branch (822:9): [True: 0, False: 115k]
|
823 | return -1; |
824 | res = PyObject_SetAttr(v, s, w); |
825 | Py_XDECREF(s); |
826 | return res; |
827 | } |
828 | |
829 | int |
830 | _PyObject_IsAbstract(PyObject *obj) |
831 | { |
832 | int res; |
833 | PyObject* isabstract; |
834 | |
835 | if (obj == NULL) Branch (835:9): [True: 406, False: 110k]
|
836 | return 0; |
837 | |
838 | res = _PyObject_LookupAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract); |
839 | if (res > 0) { Branch (839:9): [True: 21.2k, False: 89.6k]
|
840 | res = PyObject_IsTrue(isabstract); |
841 | Py_DECREF(isabstract); |
842 | } |
843 | return res; |
844 | } |
845 | |
846 | PyObject * |
847 | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) |
848 | { |
849 | PyObject *result; |
850 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
851 | if (!oname) Branch (851:9): [True: 0, False: 32.2k]
|
852 | return NULL; |
853 | result = PyObject_GetAttr(v, oname); |
854 | return result; |
855 | } |
856 | |
857 | int |
858 | _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w) |
859 | { |
860 | int result; |
861 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
862 | if (!oname) Branch (862:9): [True: 0, False: 643]
|
863 | return -1; |
864 | result = PyObject_SetAttr(v, oname, w); |
865 | return result; |
866 | } |
867 | |
868 | static inline int |
869 | set_attribute_error_context(PyObject* v, PyObject* name) |
870 | { |
871 | assert(PyErr_Occurred()); |
872 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)){ Branch (872:9): [True: 707, False: 407k]
|
873 | return 0; |
874 | } |
875 | // Intercept AttributeError exceptions and augment them to offer suggestions later. |
876 | PyObject *type, *value, *traceback; |
877 | PyErr_Fetch(&type, &value, &traceback); |
878 | PyErr_NormalizeException(&type, &value, &traceback); |
879 | // Check if the normalized exception is indeed an AttributeError |
880 | if (!PyErr_GivenExceptionMatches(value, PyExc_AttributeError)) { Branch (880:9): [True: 0, False: 407k]
|
881 | goto restore; |
882 | } |
883 | PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) value; |
884 | // Check if this exception was already augmented |
885 | if (the_exc->name || the_exc->obj406k ) { Branch (885:9): [True: 129, False: 406k]
Branch (885:26): [True: 0, False: 406k]
|
886 | goto restore; |
887 | } |
888 | // Augment the exception with the name and object |
889 | if (PyObject_SetAttr(value, &_Py_ID(name), name) || Branch (889:9): [True: 0, False: 406k]
|
890 | PyObject_SetAttr(value, &_Py_ID(obj), v)) { Branch (890:9): [True: 0, False: 406k]
|
891 | return 1; |
892 | } |
893 | restore: |
894 | PyErr_Restore(type, value, traceback); |
895 | return 0; |
896 | } |
897 | |
898 | PyObject * |
899 | PyObject_GetAttr(PyObject *v, PyObject *name) |
900 | { |
901 | PyTypeObject *tp = Py_TYPE(v); |
902 | if (!PyUnicode_Check(name)) { Branch (902:9): [True: 4, False: 96.7M]
|
903 | PyErr_Format(PyExc_TypeError, |
904 | "attribute name must be string, not '%.200s'", |
905 | Py_TYPE(name)->tp_name); |
906 | return NULL; |
907 | } |
908 | |
909 | PyObject* result = NULL; |
910 | if (tp->tp_getattro != NULL) { Branch (910:9): [True: 96.7M, False: 0]
|
911 | result = (*tp->tp_getattro)(v, name); |
912 | } |
913 | else if (tp->tp_getattr != NULL) { Branch (913:14): [True: 0, False: 0]
|
914 | const char *name_str = PyUnicode_AsUTF8(name); |
915 | if (name_str == NULL) { Branch (915:13): [True: 0, False: 0]
|
916 | return NULL; |
917 | } |
918 | result = (*tp->tp_getattr)(v, (char *)name_str); |
919 | } |
920 | else { |
921 | PyErr_Format(PyExc_AttributeError, |
922 | "'%.50s' object has no attribute '%U'", |
923 | tp->tp_name, name); |
924 | } |
925 | |
926 | if (result == NULL) { Branch (926:9): [True: 395k, False: 96.3M]
|
927 | set_attribute_error_context(v, name); |
928 | } |
929 | return result; |
930 | } |
931 | |
932 | int |
933 | _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result) |
934 | { |
935 | PyTypeObject *tp = Py_TYPE(v); |
936 | |
937 | if (!PyUnicode_Check(name)) { Branch (937:9): [True: 2, False: 33.3M]
|
938 | PyErr_Format(PyExc_TypeError, |
939 | "attribute name must be string, not '%.200s'", |
940 | Py_TYPE(name)->tp_name); |
941 | *result = NULL; |
942 | return -1; |
943 | } |
944 | |
945 | if (tp->tp_getattro == PyObject_GenericGetAttr) { Branch (945:9): [True: 28.0M, False: 5.27M]
|
946 | *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1); |
947 | if (*result != NULL) { Branch (947:13): [True: 19.5M, False: 8.50M]
|
948 | return 1; |
949 | } |
950 | if (PyErr_Occurred()) { Branch (950:13): [True: 602, False: 8.50M]
|
951 | return -1; |
952 | } |
953 | return 0; |
954 | } |
955 | if (tp->tp_getattro != NULL) { Branch (955:9): [True: 5.27M, False: 0]
|
956 | *result = (*tp->tp_getattro)(v, name); |
957 | } |
958 | else if (tp->tp_getattr != NULL) { Branch (958:14): [True: 0, False: 0]
|
959 | const char *name_str = PyUnicode_AsUTF8(name); |
960 | if (name_str == NULL) { Branch (960:13): [True: 0, False: 0]
|
961 | *result = NULL; |
962 | return -1; |
963 | } |
964 | *result = (*tp->tp_getattr)(v, (char *)name_str); |
965 | } |
966 | else { |
967 | *result = NULL; |
968 | return 0; |
969 | } |
970 | |
971 | if (*result != NULL) { Branch (971:9): [True: 2.12M, False: 3.15M]
|
972 | return 1; |
973 | } |
974 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { Branch (974:9): [True: 26, False: 3.15M]
|
975 | return -1; |
976 | } |
977 | PyErr_Clear(); |
978 | return 0; |
979 | } |
980 | |
981 | int |
982 | _PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result) |
983 | { |
984 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
985 | if (!oname) { Branch (985:9): [True: 0, False: 6.86k]
|
986 | *result = NULL; |
987 | return -1; |
988 | } |
989 | return _PyObject_LookupAttr(v, oname, result); |
990 | } |
991 | |
992 | int |
993 | PyObject_HasAttr(PyObject *v, PyObject *name) |
994 | { |
995 | PyObject *res; |
996 | if (_PyObject_LookupAttr(v, name, &res) < 0) { Branch (996:9): [True: 0, False: 722]
|
997 | PyErr_Clear(); |
998 | return 0; |
999 | } |
1000 | if (res == NULL) { Branch (1000:9): [True: 670, False: 52]
|
1001 | return 0; |
1002 | } |
1003 | Py_DECREF(res); |
1004 | return 1; |
1005 | } |
1006 | |
1007 | int |
1008 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
1009 | { |
1010 | PyTypeObject *tp = Py_TYPE(v); |
1011 | int err; |
1012 | |
1013 | if (!PyUnicode_Check(name)) { Branch (1013:9): [True: 4, False: 16.3M]
|
1014 | PyErr_Format(PyExc_TypeError, |
1015 | "attribute name must be string, not '%.200s'", |
1016 | Py_TYPE(name)->tp_name); |
1017 | return -1; |
1018 | } |
1019 | Py_INCREF(name); |
1020 | |
1021 | PyUnicode_InternInPlace(&name); |
1022 | if (tp->tp_setattro != NULL) { Branch (1022:9): [True: 16.3M, False: 5]
|
1023 | err = (*tp->tp_setattro)(v, name, value); |
1024 | Py_DECREF(name); |
1025 | return err; |
1026 | } |
1027 | if (tp->tp_setattr != NULL) { Branch (1027:9): [True: 5, False: 0]
|
1028 | const char *name_str = PyUnicode_AsUTF8(name); |
1029 | if (name_str == NULL) { Branch (1029:13): [True: 0, False: 5]
|
1030 | Py_DECREF(name); |
1031 | return -1; |
1032 | } |
1033 | err = (*tp->tp_setattr)(v, (char *)name_str, value); |
1034 | Py_DECREF(name); |
1035 | return err; |
1036 | } |
1037 | Py_DECREF(name); |
1038 | _PyObject_ASSERT(name, Py_REFCNT(name) >= 1); |
1039 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) Branch (1039:9): [True: 0, False: 0]
Branch (1039:35): [True: 0, False: 0]
|
1040 | PyErr_Format(PyExc_TypeError, |
1041 | "'%.100s' object has no attributes " |
1042 | "(%s .%U)", |
1043 | tp->tp_name, |
1044 | value==NULL ? "del" : "assign to", Branch (1044:22): [True: 0, False: 0]
|
1045 | name); |
1046 | else |
1047 | PyErr_Format(PyExc_TypeError, |
1048 | "'%.100s' object has only read-only attributes " |
1049 | "(%s .%U)", |
1050 | tp->tp_name, |
1051 | value==NULL ? "del" : "assign to", Branch (1051:22): [True: 0, False: 0]
|
1052 | name); |
1053 | return -1; |
1054 | } |
1055 | |
1056 | PyObject ** |
1057 | _PyObject_DictPointer(PyObject *obj) |
1058 | { |
1059 | Py_ssize_t dictoffset; |
1060 | PyTypeObject *tp = Py_TYPE(obj); |
1061 | |
1062 | if (tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (1062:9): [True: 266M, False: 81.8M]
|
1063 | return _PyObject_ManagedDictPointer(obj); |
1064 | } |
1065 | dictoffset = tp->tp_dictoffset; |
1066 | if (dictoffset == 0) Branch (1066:9): [True: 21.0M, False: 60.8M]
|
1067 | return NULL; |
1068 | if (dictoffset < 0) { Branch (1068:9): [True: 23.1M, False: 37.6M]
|
1069 | Py_ssize_t tsize = Py_SIZE(obj); |
1070 | if (tsize < 0) { Branch (1070:13): [True: 48.9k, False: 23.1M]
|
1071 | tsize = -tsize; |
1072 | } |
1073 | size_t size = _PyObject_VAR_SIZE(tp, tsize); |
1074 | |
1075 | dictoffset += (long)size; |
1076 | _PyObject_ASSERT(obj, dictoffset > 0); |
1077 | _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0); |
1078 | } |
1079 | return (PyObject **) ((char *)obj + dictoffset); |
1080 | } |
1081 | |
1082 | /* Helper to get a pointer to an object's __dict__ slot, if any. |
1083 | * Creates the dict from inline attributes if necessary. |
1084 | * Does not set an exception. */ |
1085 | PyObject ** |
1086 | _PyObject_GetDictPtr(PyObject *obj) |
1087 | { |
1088 | if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) { Branch (1088:9): [True: 45, False: 801]
|
1089 | return _PyObject_DictPointer(obj); |
1090 | } |
1091 | PyObject **dict_ptr = _PyObject_ManagedDictPointer(obj); |
1092 | PyDictValues **values_ptr = _PyObject_ValuesPointer(obj); |
1093 | if (*values_ptr == NULL) { Branch (1093:9): [True: 540, False: 261]
|
1094 | return dict_ptr; |
1095 | } |
1096 | assert(*dict_ptr == NULL); |
1097 | PyObject *dict = _PyObject_MakeDictFromInstanceAttributes(obj, *values_ptr); |
1098 | if (dict == NULL) { Branch (1098:9): [True: 0, False: 261]
|
1099 | PyErr_Clear(); |
1100 | return NULL; |
1101 | } |
1102 | *values_ptr = NULL; |
1103 | *dict_ptr = dict; |
1104 | return dict_ptr; |
1105 | } |
1106 | |
1107 | PyObject * |
1108 | PyObject_SelfIter(PyObject *obj) |
1109 | { |
1110 | Py_INCREF(obj); |
1111 | return obj; |
1112 | } |
1113 | |
1114 | /* Helper used when the __next__ method is removed from a type: |
1115 | tp_iternext is never NULL and can be safely called without checking |
1116 | on every iteration. |
1117 | */ |
1118 | |
1119 | PyObject * |
1120 | _PyObject_NextNotImplemented(PyObject *self) |
1121 | { |
1122 | PyErr_Format(PyExc_TypeError, |
1123 | "'%.200s' object is not iterable", |
1124 | Py_TYPE(self)->tp_name); |
1125 | return NULL; |
1126 | } |
1127 | |
1128 | |
1129 | /* Specialized version of _PyObject_GenericGetAttrWithDict |
1130 | specifically for the LOAD_METHOD opcode. |
1131 | |
1132 | Return 1 if a method is found, 0 if it's a regular attribute |
1133 | from __dict__ or something returned by using a descriptor |
1134 | protocol. |
1135 | |
1136 | `method` will point to the resolved attribute or NULL. In the |
1137 | latter case, an error will be set. |
1138 | */ |
1139 | int |
1140 | _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) |
1141 | { |
1142 | int meth_found = 0; |
1143 | |
1144 | assert(*method == NULL); |
1145 | |
1146 | PyTypeObject *tp = Py_TYPE(obj); |
1147 | if (!_PyType_IsReady(tp)) { Branch (1147:9): [True: 0, False: 36.5M]
|
1148 | if (PyType_Ready(tp) < 0) { Branch (1148:13): [True: 0, False: 0]
|
1149 | return 0; |
1150 | } |
1151 | } |
1152 | |
1153 | if (tp->tp_getattro != PyObject_GenericGetAttr || !30.9M PyUnicode_CheckExact30.9M (name)) { Branch (1153:9): [True: 5.61M, False: 30.9M]
Branch (1153:55): [True: 0, False: 30.9M]
|
1154 | *method = PyObject_GetAttr(obj, name); |
1155 | return 0; |
1156 | } |
1157 | |
1158 | PyObject *descr = _PyType_Lookup(tp, name); |
1159 | descrgetfunc f = NULL; |
1160 | if (descr != NULL) { Branch (1160:9): [True: 30.3M, False: 573k]
|
1161 | Py_INCREF(descr); |
1162 | if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { Branch (1162:13): [True: 29.6M, False: 649k]
|
1163 | meth_found = 1; |
1164 | } else { |
1165 | f = Py_TYPE(descr)->tp_descr_get; |
1166 | if (f != NULL && PyDescr_IsData(descr)263k ) { Branch (1166:17): [True: 263k, False: 385k]
Branch (1166:30): [True: 3.35k, False: 260k]
|
1167 | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1168 | Py_DECREF(descr); |
1169 | return 0; |
1170 | } |
1171 | } |
1172 | } |
1173 | PyDictValues *values; |
1174 | if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) && Branch (1174:9): [True: 14.9M, False: 15.9M]
|
1175 | (values = *_PyObject_ValuesPointer(obj))14.9M ) Branch (1175:9): [True: 5.27M, False: 9.71M]
|
1176 | { |
1177 | assert(*_PyObject_DictPointer(obj) == NULL); |
1178 | PyObject *attr = _PyObject_GetInstanceAttribute(obj, values, name); |
1179 | if (attr != NULL) { Branch (1179:13): [True: 47.1k, False: 5.22M]
|
1180 | *method = attr; |
1181 | Py_XDECREF(descr); |
1182 | return 0; |
1183 | } |
1184 | } |
1185 | else { |
1186 | PyObject **dictptr = _PyObject_DictPointer(obj); |
1187 | PyObject *dict; |
1188 | if (dictptr != NULL && (dict = *dictptr) != NULL18.6M ) { Branch (1188:13): [True: 18.6M, False: 7.01M]
Branch (1188:32): [True: 14.5M, False: 4.07M]
|
1189 | Py_INCREF(dict); |
1190 | PyObject *attr = PyDict_GetItemWithError(dict, name); |
1191 | if (attr != NULL) { Branch (1191:17): [True: 1.17M, False: 13.3M]
|
1192 | *method = Py_NewRef(attr); |
1193 | Py_DECREF(dict); |
1194 | Py_XDECREF(descr); |
1195 | return 0; |
1196 | } |
1197 | Py_DECREF(dict); |
1198 | |
1199 | if (PyErr_Occurred()) { Branch (1199:17): [True: 0, False: 13.3M]
|
1200 | Py_XDECREF(descr); |
1201 | return 0; |
1202 | } |
1203 | } |
1204 | } |
1205 | |
1206 | if (meth_found) { Branch (1206:9): [True: 29.0M, False: 658k]
|
1207 | *method = descr; |
1208 | return 1; |
1209 | } |
1210 | |
1211 | if (f != NULL) { Branch (1211:9): [True: 260k, False: 397k]
|
1212 | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1213 | Py_DECREF(descr); |
1214 | return 0; |
1215 | } |
1216 | |
1217 | if (descr != NULL) { Branch (1217:9): [True: 385k, False: 12.1k]
|
1218 | *method = descr; |
1219 | return 0; |
1220 | } |
1221 | |
1222 | PyErr_Format(PyExc_AttributeError, |
1223 | "'%.50s' object has no attribute '%U'", |
1224 | tp->tp_name, name); |
1225 | |
1226 | set_attribute_error_context(obj, name); |
1227 | return 0; |
1228 | } |
1229 | |
1230 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ |
1231 | |
1232 | PyObject * |
1233 | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, |
1234 | PyObject *dict, int suppress) |
1235 | { |
1236 | /* Make sure the logic of _PyObject_GetMethod is in sync with |
1237 | this method. |
1238 | |
1239 | When suppress=1, this function suppresses AttributeError. |
1240 | */ |
1241 | |
1242 | PyTypeObject *tp = Py_TYPE(obj); |
1243 | PyObject *descr = NULL; |
1244 | PyObject *res = NULL; |
1245 | descrgetfunc f; |
1246 | PyObject **dictptr; |
1247 | |
1248 | if (!PyUnicode_Check(name)){ Branch (1248:9): [True: 0, False: 117M]
|
1249 | PyErr_Format(PyExc_TypeError, |
1250 | "attribute name must be string, not '%.200s'", |
1251 | Py_TYPE(name)->tp_name); |
1252 | return NULL; |
1253 | } |
1254 | Py_INCREF(name); |
1255 | |
1256 | if (tp->tp_dict == NULL) { Branch (1256:9): [True: 1, False: 117M]
|
1257 | if (PyType_Ready(tp) < 0) Branch (1257:13): [True: 0, False: 1]
|
1258 | goto done; |
1259 | } |
1260 | |
1261 | descr = _PyType_Lookup(tp, name); |
1262 | |
1263 | f = NULL; |
1264 | if (descr != NULL) { Branch (1264:9): [True: 69.3M, False: 48.6M]
|
1265 | Py_INCREF(descr); |
1266 | f = Py_TYPE(descr)->tp_descr_get; |
1267 | if (f != NULL && PyDescr_IsData(descr)58.1M ) { Branch (1267:13): [True: 58.1M, False: 11.2M]
Branch (1267:26): [True: 29.9M, False: 28.1M]
|
1268 | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1269 | if (res == NULL && suppress110k && Branch (1269:17): [True: 110k, False: 29.8M]
Branch (1269:32): [True: 72.2k, False: 38.3k]
|
1270 | PyErr_ExceptionMatches(PyExc_AttributeError)72.2k ) { Branch (1270:21): [True: 71.6k, False: 602]
|
1271 | PyErr_Clear(); |
1272 | } |
1273 | goto done; |
1274 | } |
1275 | } |
1276 | if (dict == NULL) { Branch (1276:9): [True: 88.0M, False: 9.67k]
|
1277 | if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) && Branch (1277:13): [True: 51.6M, False: 36.4M]
|
1278 | *_PyObject_ValuesPointer(obj)51.6M ) Branch (1278:13): [True: 35.5M, False: 16.0M]
|
1279 | { |
1280 | PyDictValues **values_ptr = _PyObject_ValuesPointer(obj); |
1281 | if (PyUnicode_CheckExact(name)) { |
1282 | assert(*_PyObject_DictPointer(obj) == NULL); |
1283 | res = _PyObject_GetInstanceAttribute(obj, *values_ptr, name); |
1284 | if (res != NULL) { Branch (1284:21): [True: 17.5M, False: 18.0M]
|
1285 | goto done; |
1286 | } |
1287 | } |
1288 | else { |
1289 | dictptr = _PyObject_DictPointer(obj); |
1290 | assert(dictptr != NULL && *dictptr == NULL); |
1291 | *dictptr = dict = _PyObject_MakeDictFromInstanceAttributes(obj, *values_ptr); |
1292 | if (dict == NULL) { Branch (1292:21): [True: 0, False: 2]
|
1293 | res = NULL; |
1294 | goto done; |
1295 | } |
1296 | *values_ptr = NULL; |
1297 | } |
1298 | } |
1299 | else { |
1300 | dictptr = _PyObject_DictPointer(obj); |
1301 | if (dictptr) { Branch (1301:17): [True: 38.4M, False: 14.0M]
|
1302 | dict = *dictptr; |
1303 | } |
1304 | } |
1305 | } |
1306 | if (dict != NULL) { Branch (1306:9): [True: 33.0M, False: 37.4M]
|
1307 | Py_INCREF(dict); |
1308 | res = PyDict_GetItemWithError(dict, name); |
1309 | if (res != NULL) { Branch (1309:13): [True: 21.8M, False: 11.2M]
|
1310 | Py_INCREF(res); |
1311 | Py_DECREF(dict); |
1312 | goto done; |
1313 | } |
1314 | else { |
1315 | Py_DECREF(dict); |
1316 | if (PyErr_Occurred()) { Branch (1316:17): [True: 0, False: 11.2M]
|
1317 | if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) { Branch (1317:21): [True: 0, False: 0]
Branch (1317:33): [True: 0, False: 0]
|
1318 | PyErr_Clear(); |
1319 | } |
1320 | else { |
1321 | goto done; |
1322 | } |
1323 | } |
1324 | } |
1325 | } |
1326 | |
1327 | if (f != NULL) { Branch (1327:9): [True: 28.1M, False: 20.5M]
|
1328 | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1329 | if (res == NULL && suppress1.46k && Branch (1329:13): [True: 1.46k, False: 28.1M]
Branch (1329:28): [True: 0, False: 1.46k]
|
1330 | PyErr_ExceptionMatches(PyExc_AttributeError)0 ) { Branch (1330:17): [True: 0, False: 0]
|
1331 | PyErr_Clear(); |
1332 | } |
1333 | goto done; |
1334 | } |
1335 | |
1336 | if (descr != NULL) { Branch (1336:9): [True: 8.73M, False: 11.7M]
|
1337 | res = descr; |
1338 | descr = NULL; |
1339 | goto done; |
1340 | } |
1341 | |
1342 | if (!suppress) { Branch (1342:9): [True: 3.35M, False: 8.43M]
|
1343 | PyErr_Format(PyExc_AttributeError, |
1344 | "'%.50s' object has no attribute '%U'", |
1345 | tp->tp_name, name); |
1346 | } |
1347 | done: |
1348 | Py_XDECREF(descr); |
1349 | Py_DECREF(name); |
1350 | return res; |
1351 | } |
1352 | |
1353 | PyObject * |
1354 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
1355 | { |
1356 | return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0); |
1357 | } |
1358 | |
1359 | int |
1360 | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
1361 | PyObject *value, PyObject *dict) |
1362 | { |
1363 | PyTypeObject *tp = Py_TYPE(obj); |
1364 | PyObject *descr; |
1365 | descrsetfunc f; |
1366 | int res = -1; |
1367 | |
1368 | if (!PyUnicode_Check(name)){ Branch (1368:9): [True: 2, False: 16.3M]
|
1369 | PyErr_Format(PyExc_TypeError, |
1370 | "attribute name must be string, not '%.200s'", |
1371 | Py_TYPE(name)->tp_name); |
1372 | return -1; |
1373 | } |
1374 | |
1375 | if (tp->tp_dict == NULL && PyType_Ready(tp) < 00 ) Branch (1375:9): [True: 0, False: 16.3M]
Branch (1375:32): [True: 0, False: 0]
|
1376 | return -1; |
1377 | |
1378 | Py_INCREF(name); |
1379 | Py_INCREF(tp); |
1380 | descr = _PyType_Lookup(tp, name); |
1381 | |
1382 | if (descr != NULL) { Branch (1382:9): [True: 5.58M, False: 10.7M]
|
1383 | Py_INCREF(descr); |
1384 | f = Py_TYPE(descr)->tp_descr_set; |
1385 | if (f != NULL) { Branch (1385:13): [True: 1.34M, False: 4.24M]
|
1386 | res = f(descr, obj, value); |
1387 | goto done; |
1388 | } |
1389 | } |
1390 | |
1391 | if (dict == NULL) { Branch (1391:9): [True: 14.9M, False: 14.1k]
|
1392 | if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) && *_PyObject_ValuesPointer(obj)6.57M ) { Branch (1392:13): [True: 6.57M, False: 8.38M]
Branch (1392:57): [True: 4.69M, False: 1.87M]
|
1393 | res = _PyObject_StoreInstanceAttribute(obj, *_PyObject_ValuesPointer(obj), name, value); |
1394 | } |
1395 | else { |
1396 | PyObject **dictptr = _PyObject_DictPointer(obj); |
1397 | if (dictptr == NULL) { Branch (1397:17): [True: 97, False: 10.2M]
|
1398 | if (descr == NULL) { Branch (1398:21): [True: 90, False: 7]
|
1399 | PyErr_Format(PyExc_AttributeError, |
1400 | "'%.100s' object has no attribute '%U'", |
1401 | tp->tp_name, name); |
1402 | } |
1403 | else { |
1404 | PyErr_Format(PyExc_AttributeError, |
1405 | "'%.50s' object attribute '%U' is read-only", |
1406 | tp->tp_name, name); |
1407 | } |
1408 | goto done; |
1409 | } |
1410 | else { |
1411 | res = _PyObjectDict_SetItem(tp, dictptr, name, value); |
1412 | } |
1413 | } |
1414 | } |
1415 | else { |
1416 | Py_INCREF(dict); |
1417 | if (value == NULL) Branch (1417:13): [True: 155, False: 14.0k]
|
1418 | res = PyDict_DelItem(dict, name); |
1419 | else |
1420 | res = PyDict_SetItem(dict, name, value); |
1421 | Py_DECREF(dict); |
1422 | } |
1423 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)114 ) { Branch (1423:9): [True: 114, False: 14.9M]
Branch (1423:20): [True: 39, False: 75]
|
1424 | if (PyType_IsSubtype(tp, &PyType_Type)) { Branch (1424:13): [True: 16, False: 23]
|
1425 | PyErr_Format(PyExc_AttributeError, |
1426 | "type object '%.50s' has no attribute '%U'", |
1427 | ((PyTypeObject*)obj)->tp_name, name); |
1428 | } |
1429 | else { |
1430 | PyErr_Format(PyExc_AttributeError, |
1431 | "'%.100s' object has no attribute '%U'", |
1432 | tp->tp_name, name); |
1433 | } |
1434 | } |
1435 | done: |
1436 | Py_XDECREF(descr); |
1437 | Py_DECREF(tp); |
1438 | Py_DECREF(name); |
1439 | return res; |
1440 | } |
1441 | |
1442 | int |
1443 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
1444 | { |
1445 | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
1446 | } |
1447 | |
1448 | int |
1449 | PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context) |
1450 | { |
1451 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
1452 | if (dictptr == NULL) { Branch (1452:9): [True: 0, False: 18]
|
1453 | if (_PyType_HasFeature(Py_TYPE(obj), Py_TPFLAGS_MANAGED_DICT) && Branch (1453:13): [True: 0, False: 0]
|
1454 | *_PyObject_ValuesPointer(obj) != NULL) Branch (1454:13): [True: 0, False: 0]
|
1455 | { |
1456 | /* Was unable to convert to dict */ |
1457 | PyErr_NoMemory(); |
1458 | } |
1459 | else { |
1460 | PyErr_SetString(PyExc_AttributeError, |
1461 | "This object has no __dict__"); |
1462 | } |
1463 | return -1; |
1464 | } |
1465 | if (value == NULL) { Branch (1465:9): [True: 11, False: 7]
|
1466 | PyErr_SetString(PyExc_TypeError, "cannot delete __dict__"); |
1467 | return -1; |
1468 | } |
1469 | if (!PyDict_Check(value)) { Branch (1469:9): [True: 2, False: 5]
|
1470 | PyErr_Format(PyExc_TypeError, |
1471 | "__dict__ must be set to a dictionary, " |
1472 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
1473 | return -1; |
1474 | } |
1475 | Py_INCREF(value); |
1476 | Py_XSETREF(*dictptr, value); |
1477 | return 0; |
1478 | } |
1479 | |
1480 | |
1481 | /* Test a value used as condition, e.g., in a while or if statement. |
1482 | Return -1 if an error occurred */ |
1483 | |
1484 | int |
1485 | PyObject_IsTrue(PyObject *v) |
1486 | { |
1487 | Py_ssize_t res; |
1488 | if (v == Py_True) Branch (1488:9): [True: 3.60M, False: 78.5M]
|
1489 | return 1; |
1490 | if (v == Py_False) Branch (1490:9): [True: 6.85M, False: 71.6M]
|
1491 | return 0; |
1492 | if (v == Py_None) Branch (1492:9): [True: 28.6M, False: 43.0M]
|
1493 | return 0; |
1494 | else if (Py_TYPE(v)->tp_as_number != NULL && Branch (1494:14): [True: 29.2M, False: 13.8M]
|
1495 | Py_TYPE29.2M (v)->tp_as_number->nb_bool != NULL29.2M ) Branch (1495:14): [True: 11.3M, False: 17.9M]
|
1496 | res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v); |
1497 | else if (Py_TYPE(v)->tp_as_mapping != NULL && Branch (1497:14): [True: 24.8M, False: 6.92M]
|
1498 | Py_TYPE24.8M (v)->tp_as_mapping->mp_length != NULL24.8M ) Branch (1498:14): [True: 21.5M, False: 3.23M]
|
1499 | res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v); |
1500 | else if (Py_TYPE(v)->tp_as_sequence != NULL && Branch (1500:14): [True: 5.31M, False: 4.83M]
|
1501 | Py_TYPE5.31M (v)->tp_as_sequence->sq_length != NULL5.31M ) Branch (1501:14): [True: 2.08M, False: 3.23M]
|
1502 | res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v); |
1503 | else |
1504 | return 1; |
1505 | /* if it is negative, it should be either -1 or -2 */ |
1506 | return (res > 0) ? 124.7M : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); Branch (1506:12): [True: 24.7M, False: 10.1M]
|
1507 | } |
1508 | |
1509 | /* equivalent of 'not v' |
1510 | Return -1 if an error occurred */ |
1511 | |
1512 | int |
1513 | PyObject_Not(PyObject *v) |
1514 | { |
1515 | int res; |
1516 | res = PyObject_IsTrue(v); |
1517 | if (res < 0) Branch (1517:9): [True: 1, False: 9.64k]
|
1518 | return res; |
1519 | return res == 0; |
1520 | } |
1521 | |
1522 | /* Test whether an object can be called */ |
1523 | |
1524 | int |
1525 | PyCallable_Check(PyObject *x) |
1526 | { |
1527 | if (x == NULL) Branch (1527:9): [True: 0, False: 1.14M]
|
1528 | return 0; |
1529 | return Py_TYPE(x)->tp_call != NULL; |
1530 | } |
1531 | |
1532 | |
1533 | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
1534 | static PyObject * |
1535 | _dir_locals(void) |
1536 | { |
1537 | PyObject *names; |
1538 | PyObject *locals; |
1539 | |
1540 | locals = PyEval_GetLocals(); |
1541 | if (locals == NULL) Branch (1541:9): [True: 0, False: 20]
|
1542 | return NULL; |
1543 | |
1544 | names = PyMapping_Keys(locals); |
1545 | if (!names) Branch (1545:9): [True: 1, False: 19]
|
1546 | return NULL; |
1547 | if (!PyList_Check(names)) { Branch (1547:9): [True: 0, False: 19]
|
1548 | PyErr_Format(PyExc_TypeError, |
1549 | "dir(): expected keys() of locals to be a list, " |
1550 | "not '%.200s'", Py_TYPE(names)->tp_name); |
1551 | Py_DECREF(names); |
1552 | return NULL; |
1553 | } |
1554 | if (PyList_Sort(names)) { Branch (1554:9): [True: 0, False: 19]
|
1555 | Py_DECREF(names); |
1556 | return NULL; |
1557 | } |
1558 | /* the locals don't need to be DECREF'd */ |
1559 | return names; |
1560 | } |
1561 | |
1562 | /* Helper for PyObject_Dir: object introspection. */ |
1563 | static PyObject * |
1564 | _dir_object(PyObject *obj) |
1565 | { |
1566 | PyObject *result, *sorted; |
1567 | PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__)); |
1568 | |
1569 | assert(obj != NULL); |
1570 | if (dirfunc == NULL) { Branch (1570:9): [True: 1, False: 19.7k]
|
1571 | if (!PyErr_Occurred()) Branch (1571:13): [True: 0, False: 1]
|
1572 | PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); |
1573 | return NULL; |
1574 | } |
1575 | /* use __dir__ */ |
1576 | result = _PyObject_CallNoArgs(dirfunc); |
1577 | Py_DECREF(dirfunc); |
1578 | if (result == NULL) Branch (1578:9): [True: 5, False: 19.7k]
|
1579 | return NULL; |
1580 | /* return sorted(result) */ |
1581 | sorted = PySequence_List(result); |
1582 | Py_DECREF(result); |
1583 | if (sorted == NULL) Branch (1583:9): [True: 1, False: 19.7k]
|
1584 | return NULL; |
1585 | if (PyList_Sort(sorted)) { Branch (1585:9): [True: 0, False: 19.7k]
|
1586 | Py_DECREF(sorted); |
1587 | return NULL; |
1588 | } |
1589 | return sorted; |
1590 | } |
1591 | |
1592 | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
1593 | (local) scope. Otherwise, performs introspection of the object: returns a |
1594 | sorted list of attribute names (supposedly) accessible from the object |
1595 | */ |
1596 | PyObject * |
1597 | PyObject_Dir(PyObject *obj) |
1598 | { |
1599 | return (obj == NULL) ? _dir_locals()20 : _dir_object(obj)19.7k ; Branch (1599:12): [True: 20, False: 19.7k]
|
1600 | } |
1601 | |
1602 | /* |
1603 | None is a non-NULL undefined value. |
1604 | There is (and should be!) no way to create other objects of this type, |
1605 | so there is exactly one (which is indestructible, by the way). |
1606 | */ |
1607 | |
1608 | /* ARGSUSED */ |
1609 | static PyObject * |
1610 | none_repr(PyObject *op) |
1611 | { |
1612 | return PyUnicode_FromString("None"); |
1613 | } |
1614 | |
1615 | static void _Py_NO_RETURN |
1616 | none_dealloc(PyObject* Py_UNUSED(ignore)) |
1617 | { |
1618 | _Py_FatalRefcountError("deallocating None"); |
1619 | } |
1620 | |
1621 | static PyObject * |
1622 | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
1623 | { |
1624 | if (PyTuple_GET_SIZE(args) || (2 kwargs2 && PyDict_GET_SIZE1 (kwargs))) { Branch (1624:36): [True: 1, False: 1]
|
1625 | PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); |
1626 | return NULL; |
1627 | } |
1628 | Py_RETURN_NONE1 ; |
1629 | } |
1630 | |
1631 | static int |
1632 | none_bool(PyObject *v) |
1633 | { |
1634 | return 0; |
1635 | } |
1636 | |
1637 | static PyNumberMethods none_as_number = { |
1638 | 0, /* nb_add */ |
1639 | 0, /* nb_subtract */ |
1640 | 0, /* nb_multiply */ |
1641 | 0, /* nb_remainder */ |
1642 | 0, /* nb_divmod */ |
1643 | 0, /* nb_power */ |
1644 | 0, /* nb_negative */ |
1645 | 0, /* nb_positive */ |
1646 | 0, /* nb_absolute */ |
1647 | (inquiry)none_bool, /* nb_bool */ |
1648 | 0, /* nb_invert */ |
1649 | 0, /* nb_lshift */ |
1650 | 0, /* nb_rshift */ |
1651 | 0, /* nb_and */ |
1652 | 0, /* nb_xor */ |
1653 | 0, /* nb_or */ |
1654 | 0, /* nb_int */ |
1655 | 0, /* nb_reserved */ |
1656 | 0, /* nb_float */ |
1657 | 0, /* nb_inplace_add */ |
1658 | 0, /* nb_inplace_subtract */ |
1659 | 0, /* nb_inplace_multiply */ |
1660 | 0, /* nb_inplace_remainder */ |
1661 | 0, /* nb_inplace_power */ |
1662 | 0, /* nb_inplace_lshift */ |
1663 | 0, /* nb_inplace_rshift */ |
1664 | 0, /* nb_inplace_and */ |
1665 | 0, /* nb_inplace_xor */ |
1666 | 0, /* nb_inplace_or */ |
1667 | 0, /* nb_floor_divide */ |
1668 | 0, /* nb_true_divide */ |
1669 | 0, /* nb_inplace_floor_divide */ |
1670 | 0, /* nb_inplace_true_divide */ |
1671 | 0, /* nb_index */ |
1672 | }; |
1673 | |
1674 | PyTypeObject _PyNone_Type = { |
1675 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1676 | "NoneType", |
1677 | 0, |
1678 | 0, |
1679 | none_dealloc, /*tp_dealloc*/ /*never called*/ |
1680 | 0, /*tp_vectorcall_offset*/ |
1681 | 0, /*tp_getattr*/ |
1682 | 0, /*tp_setattr*/ |
1683 | 0, /*tp_as_async*/ |
1684 | none_repr, /*tp_repr*/ |
1685 | &none_as_number, /*tp_as_number*/ |
1686 | 0, /*tp_as_sequence*/ |
1687 | 0, /*tp_as_mapping*/ |
1688 | 0, /*tp_hash */ |
1689 | 0, /*tp_call */ |
1690 | 0, /*tp_str */ |
1691 | 0, /*tp_getattro */ |
1692 | 0, /*tp_setattro */ |
1693 | 0, /*tp_as_buffer */ |
1694 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
1695 | 0, /*tp_doc */ |
1696 | 0, /*tp_traverse */ |
1697 | 0, /*tp_clear */ |
1698 | 0, /*tp_richcompare */ |
1699 | 0, /*tp_weaklistoffset */ |
1700 | 0, /*tp_iter */ |
1701 | 0, /*tp_iternext */ |
1702 | 0, /*tp_methods */ |
1703 | 0, /*tp_members */ |
1704 | 0, /*tp_getset */ |
1705 | 0, /*tp_base */ |
1706 | 0, /*tp_dict */ |
1707 | 0, /*tp_descr_get */ |
1708 | 0, /*tp_descr_set */ |
1709 | 0, /*tp_dictoffset */ |
1710 | 0, /*tp_init */ |
1711 | 0, /*tp_alloc */ |
1712 | none_new, /*tp_new */ |
1713 | }; |
1714 | |
1715 | PyObject _Py_NoneStruct = { |
1716 | _PyObject_EXTRA_INIT |
1717 | 1, &_PyNone_Type |
1718 | }; |
1719 | |
1720 | /* NotImplemented is an object that can be used to signal that an |
1721 | operation is not implemented for the given type combination. */ |
1722 | |
1723 | static PyObject * |
1724 | NotImplemented_repr(PyObject *op) |
1725 | { |
1726 | return PyUnicode_FromString("NotImplemented"); |
1727 | } |
1728 | |
1729 | static PyObject * |
1730 | NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
1731 | { |
1732 | return PyUnicode_FromString("NotImplemented"); |
1733 | } |
1734 | |
1735 | static PyMethodDef notimplemented_methods[] = { |
1736 | {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL}, |
1737 | {NULL, NULL} |
1738 | }; |
1739 | |
1740 | static PyObject * |
1741 | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
1742 | { |
1743 | if (PyTuple_GET_SIZE(args) || (2 kwargs2 && PyDict_GET_SIZE1 (kwargs))) { Branch (1743:36): [True: 1, False: 1]
|
1744 | PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); |
1745 | return NULL; |
1746 | } |
1747 | Py_RETURN_NOTIMPLEMENTED1 ; |
1748 | } |
1749 | |
1750 | static void _Py_NO_RETURN |
1751 | notimplemented_dealloc(PyObject* ignore) |
1752 | { |
1753 | /* This should never get called, but we also don't want to SEGV if |
1754 | * we accidentally decref NotImplemented out of existence. |
1755 | */ |
1756 | Py_FatalError("deallocating NotImplemented"); |
1757 | } |
1758 | |
1759 | static int |
1760 | notimplemented_bool(PyObject *v) |
1761 | { |
1762 | if (PyErr_WarnEx(PyExc_DeprecationWarning, Branch (1762:9): [True: 0, False: 3]
|
1763 | "NotImplemented should not be used in a boolean context", |
1764 | 1) < 0) |
1765 | { |
1766 | return -1; |
1767 | } |
1768 | return 1; |
1769 | } |
1770 | |
1771 | static PyNumberMethods notimplemented_as_number = { |
1772 | .nb_bool = notimplemented_bool, |
1773 | }; |
1774 | |
1775 | PyTypeObject _PyNotImplemented_Type = { |
1776 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1777 | "NotImplementedType", |
1778 | 0, |
1779 | 0, |
1780 | notimplemented_dealloc, /*tp_dealloc*/ /*never called*/ |
1781 | 0, /*tp_vectorcall_offset*/ |
1782 | 0, /*tp_getattr*/ |
1783 | 0, /*tp_setattr*/ |
1784 | 0, /*tp_as_async*/ |
1785 | NotImplemented_repr, /*tp_repr*/ |
1786 | ¬implemented_as_number, /*tp_as_number*/ |
1787 | 0, /*tp_as_sequence*/ |
1788 | 0, /*tp_as_mapping*/ |
1789 | 0, /*tp_hash */ |
1790 | 0, /*tp_call */ |
1791 | 0, /*tp_str */ |
1792 | 0, /*tp_getattro */ |
1793 | 0, /*tp_setattro */ |
1794 | 0, /*tp_as_buffer */ |
1795 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
1796 | 0, /*tp_doc */ |
1797 | 0, /*tp_traverse */ |
1798 | 0, /*tp_clear */ |
1799 | 0, /*tp_richcompare */ |
1800 | 0, /*tp_weaklistoffset */ |
1801 | 0, /*tp_iter */ |
1802 | 0, /*tp_iternext */ |
1803 | notimplemented_methods, /*tp_methods */ |
1804 | 0, /*tp_members */ |
1805 | 0, /*tp_getset */ |
1806 | 0, /*tp_base */ |
1807 | 0, /*tp_dict */ |
1808 | 0, /*tp_descr_get */ |
1809 | 0, /*tp_descr_set */ |
1810 | 0, /*tp_dictoffset */ |
1811 | 0, /*tp_init */ |
1812 | 0, /*tp_alloc */ |
1813 | notimplemented_new, /*tp_new */ |
1814 | }; |
1815 | |
1816 | PyObject _Py_NotImplementedStruct = { |
1817 | _PyObject_EXTRA_INIT |
1818 | 1, &_PyNotImplemented_Type |
1819 | }; |
1820 | |
1821 | PyStatus |
1822 | _PyTypes_InitState(PyInterpreterState *interp) |
1823 | { |
1824 | if (!_Py_IsMainInterpreter(interp)) { Branch (1824:9): [True: 171, False: 107]
|
1825 | return _PyStatus_OK(); |
1826 | } |
1827 | |
1828 | PyStatus status = _PyTypes_InitSlotDefs(); |
1829 | if (_PyStatus_EXCEPTION(status)) { |
1830 | return status; |
1831 | } |
1832 | |
1833 | return _PyStatus_OK(); |
1834 | } |
1835 | |
1836 | |
1837 | |
1838 | #ifdef MS_WINDOWS |
1839 | extern PyTypeObject PyHKEY_Type; |
1840 | #endif |
1841 | extern PyTypeObject _Py_GenericAliasIterType; |
1842 | extern PyTypeObject _PyMemoryIter_Type; |
1843 | extern PyTypeObject _PyLineIterator; |
1844 | extern PyTypeObject _PyPositionsIterator; |
1845 | |
1846 | static PyTypeObject* static_types[] = { |
1847 | // The two most important base types: must be initialized first and |
1848 | // deallocated last. |
1849 | &PyBaseObject_Type, |
1850 | &PyType_Type, |
1851 | |
1852 | // Static types with base=&PyBaseObject_Type |
1853 | &PyAsyncGen_Type, |
1854 | &PyByteArrayIter_Type, |
1855 | &PyByteArray_Type, |
1856 | &PyBytesIter_Type, |
1857 | &PyBytes_Type, |
1858 | &PyCFunction_Type, |
1859 | &PyCallIter_Type, |
1860 | &PyCapsule_Type, |
1861 | &PyCell_Type, |
1862 | &PyClassMethodDescr_Type, |
1863 | &PyClassMethod_Type, |
1864 | &PyCode_Type, |
1865 | &PyComplex_Type, |
1866 | &PyContextToken_Type, |
1867 | &PyContextVar_Type, |
1868 | &PyContext_Type, |
1869 | &PyCoro_Type, |
1870 | &PyDictItems_Type, |
1871 | &PyDictIterItem_Type, |
1872 | &PyDictIterKey_Type, |
1873 | &PyDictIterValue_Type, |
1874 | &PyDictKeys_Type, |
1875 | &PyDictProxy_Type, |
1876 | &PyDictRevIterItem_Type, |
1877 | &PyDictRevIterKey_Type, |
1878 | &PyDictRevIterValue_Type, |
1879 | &PyDictValues_Type, |
1880 | &PyDict_Type, |
1881 | &PyEllipsis_Type, |
1882 | &PyEnum_Type, |
1883 | &PyFilter_Type, |
1884 | &PyFloat_Type, |
1885 | &PyFrame_Type, |
1886 | &PyFrozenSet_Type, |
1887 | &PyFunction_Type, |
1888 | &PyGen_Type, |
1889 | &PyGetSetDescr_Type, |
1890 | #ifdef MS_WINDOWS |
1891 | &PyHKEY_Type, |
1892 | #endif |
1893 | &PyInstanceMethod_Type, |
1894 | &PyListIter_Type, |
1895 | &PyListRevIter_Type, |
1896 | &PyList_Type, |
1897 | &PyLongRangeIter_Type, |
1898 | &PyLong_Type, |
1899 | &PyMap_Type, |
1900 | &PyMemberDescr_Type, |
1901 | &PyMemoryView_Type, |
1902 | &PyMethodDescr_Type, |
1903 | &PyMethod_Type, |
1904 | &PyModuleDef_Type, |
1905 | &PyModule_Type, |
1906 | &PyODictIter_Type, |
1907 | &PyPickleBuffer_Type, |
1908 | &PyProperty_Type, |
1909 | &PyRangeIter_Type, |
1910 | &PyRange_Type, |
1911 | &PyReversed_Type, |
1912 | &PySTEntry_Type, |
1913 | &PySeqIter_Type, |
1914 | &PySetIter_Type, |
1915 | &PySet_Type, |
1916 | &PySlice_Type, |
1917 | &PyStaticMethod_Type, |
1918 | &PyStdPrinter_Type, |
1919 | &PySuper_Type, |
1920 | &PyTraceBack_Type, |
1921 | &PyTupleIter_Type, |
1922 | &PyTuple_Type, |
1923 | &PyUnicodeIter_Type, |
1924 | &PyUnicode_Type, |
1925 | &PyWrapperDescr_Type, |
1926 | &PyZip_Type, |
1927 | &Py_GenericAliasType, |
1928 | &_PyAnextAwaitable_Type, |
1929 | &_PyAsyncGenASend_Type, |
1930 | &_PyAsyncGenAThrow_Type, |
1931 | &_PyAsyncGenWrappedValue_Type, |
1932 | &_PyContextTokenMissing_Type, |
1933 | &_PyCoroWrapper_Type, |
1934 | &_Py_GenericAliasIterType, |
1935 | &_PyHamtItems_Type, |
1936 | &_PyHamtKeys_Type, |
1937 | &_PyHamtValues_Type, |
1938 | &_PyHamt_ArrayNode_Type, |
1939 | &_PyHamt_BitmapNode_Type, |
1940 | &_PyHamt_CollisionNode_Type, |
1941 | &_PyHamt_Type, |
1942 | &_PyInterpreterID_Type, |
1943 | &_PyLineIterator, |
1944 | &_PyManagedBuffer_Type, |
1945 | &_PyMemoryIter_Type, |
1946 | &_PyMethodWrapper_Type, |
1947 | &_PyNamespace_Type, |
1948 | &_PyNone_Type, |
1949 | &_PyNotImplemented_Type, |
1950 | &_PyPositionsIterator, |
1951 | &_PyUnicodeASCIIIter_Type, |
1952 | &_PyUnion_Type, |
1953 | &_PyWeakref_CallableProxyType, |
1954 | &_PyWeakref_ProxyType, |
1955 | &_PyWeakref_RefType, |
1956 | |
1957 | // subclasses: _PyTypes_FiniTypes() deallocates them before their base |
1958 | // class |
1959 | &PyBool_Type, // base=&PyLong_Type |
1960 | &PyCMethod_Type, // base=&PyCFunction_Type |
1961 | &PyODictItems_Type, // base=&PyDictItems_Type |
1962 | &PyODictKeys_Type, // base=&PyDictKeys_Type |
1963 | &PyODictValues_Type, // base=&PyDictValues_Type |
1964 | &PyODict_Type, // base=&PyDict_Type |
1965 | }; |
1966 | |
1967 | |
1968 | PyStatus |
1969 | _PyTypes_InitTypes(PyInterpreterState *interp) |
1970 | { |
1971 | if (!_Py_IsMainInterpreter(interp)) { Branch (1971:9): [True: 171, False: 107]
|
1972 | return _PyStatus_OK(); |
1973 | } |
1974 | |
1975 | // All other static types (unless initialized elsewhere) |
1976 | for (size_t i=0; 107 i < Py_ARRAY_LENGTH(static_types); i++11.5k ) { Branch (1976:22): [True: 11.5k, False: 107]
|
1977 | PyTypeObject *type = static_types[i]; |
1978 | if (PyType_Ready(type) < 0) { Branch (1978:13): [True: 0, False: 11.5k]
|
1979 | return _PyStatus_ERR("Can't initialize types"); |
1980 | } |
1981 | if (type == &PyType_Type) { Branch (1981:13): [True: 107, False: 11.4k]
|
1982 | // Sanitify checks of the two most important types |
1983 | assert(PyBaseObject_Type.tp_base == NULL); |
1984 | assert(PyType_Type.tp_base == &PyBaseObject_Type); |
1985 | } |
1986 | } |
1987 | |
1988 | return _PyStatus_OK(); |
1989 | } |
1990 | |
1991 | |
1992 | // Best-effort function clearing static types. |
1993 | // |
1994 | // Don't deallocate a type if it still has subclasses. If a Py_Finalize() |
1995 | // sub-function is interrupted by CTRL+C or fails with MemoryError, some |
1996 | // subclasses are not cleared properly. Leave the static type unchanged in this |
1997 | // case. |
1998 | void |
1999 | _PyTypes_FiniTypes(PyInterpreterState *interp) |
2000 | { |
2001 | if (!_Py_IsMainInterpreter(interp)) { Branch (2001:9): [True: 169, False: 103]
|
2002 | return; |
2003 | } |
2004 | |
2005 | // Deallocate types in the reverse order to deallocate subclasses before |
2006 | // their base classes. |
2007 | for (Py_ssize_t i=103 Py_ARRAY_LENGTH103 (static_types)-1; i>=0; i--11.1k ) { Branch (2007:56): [True: 11.1k, False: 103]
|
2008 | PyTypeObject *type = static_types[i]; |
2009 | _PyStaticType_Dealloc(type); |
2010 | } |
2011 | } |
2012 | |
2013 | |
2014 | void |
2015 | _Py_NewReference(PyObject *op) |
2016 | { |
2017 | if (_Py_tracemalloc_config.tracing) { Branch (2017:9): [True: 552k, False: 674M]
|
2018 | _PyTraceMalloc_NewReference(op); |
2019 | } |
2020 | #ifdef Py_REF_DEBUG |
2021 | _Py_RefTotal++; |
2022 | #endif |
2023 | Py_SET_REFCNT(op, 1); |
2024 | #ifdef Py_TRACE_REFS |
2025 | _Py_AddToAllObjects(op, 1); |
2026 | #endif |
2027 | } |
2028 | |
2029 | |
2030 | #ifdef Py_TRACE_REFS |
2031 | void |
2032 | _Py_ForgetReference(PyObject *op) |
2033 | { |
2034 | if (Py_REFCNT(op) < 0) { |
2035 | _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt"); |
2036 | } |
2037 | |
2038 | if (op == &refchain || |
2039 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
2040 | { |
2041 | _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain"); |
2042 | } |
2043 | |
2044 | #ifdef SLOW_UNREF_CHECK |
2045 | PyObject *p; |
2046 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
2047 | if (p == op) { |
2048 | break; |
2049 | } |
2050 | } |
2051 | if (p == &refchain) { |
2052 | /* Not found */ |
2053 | _PyObject_ASSERT_FAILED_MSG(op, |
2054 | "object not found in the objects list"); |
2055 | } |
2056 | #endif |
2057 | |
2058 | op->_ob_next->_ob_prev = op->_ob_prev; |
2059 | op->_ob_prev->_ob_next = op->_ob_next; |
2060 | op->_ob_next = op->_ob_prev = NULL; |
2061 | } |
2062 | |
2063 | /* Print all live objects. Because PyObject_Print is called, the |
2064 | * interpreter must be in a healthy state. |
2065 | */ |
2066 | void |
2067 | _Py_PrintReferences(FILE *fp) |
2068 | { |
2069 | PyObject *op; |
2070 | fprintf(fp, "Remaining objects:\n"); |
2071 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
2072 | fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op)); |
2073 | if (PyObject_Print(op, fp, 0) != 0) { |
2074 | PyErr_Clear(); |
2075 | } |
2076 | putc('\n', fp); |
2077 | } |
2078 | } |
2079 | |
2080 | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
2081 | * doesn't make any calls to the Python C API, so is always safe to call. |
2082 | */ |
2083 | void |
2084 | _Py_PrintReferenceAddresses(FILE *fp) |
2085 | { |
2086 | PyObject *op; |
2087 | fprintf(fp, "Remaining object addresses:\n"); |
2088 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
2089 | fprintf(fp, "%p [%zd] %s\n", (void *)op, |
2090 | Py_REFCNT(op), Py_TYPE(op)->tp_name); |
2091 | } |
2092 | |
2093 | PyObject * |
2094 | _Py_GetObjects(PyObject *self, PyObject *args) |
2095 | { |
2096 | int i, n; |
2097 | PyObject *t = NULL; |
2098 | PyObject *res, *op; |
2099 | |
2100 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
2101 | return NULL; |
2102 | op = refchain._ob_next; |
2103 | res = PyList_New(0); |
2104 | if (res == NULL) |
2105 | return NULL; |
2106 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
2107 | while (op == self || op == args || op == res || op == t || |
2108 | (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) { |
2109 | op = op->_ob_next; |
2110 | if (op == &refchain) |
2111 | return res; |
2112 | } |
2113 | if (PyList_Append(res, op) < 0) { |
2114 | Py_DECREF(res); |
2115 | return NULL; |
2116 | } |
2117 | op = op->_ob_next; |
2118 | } |
2119 | return res; |
2120 | } |
2121 | |
2122 | #endif |
2123 | |
2124 | |
2125 | /* Hack to force loading of abstract.o */ |
2126 | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
2127 | |
2128 | |
2129 | void |
2130 | _PyObject_DebugTypeStats(FILE *out) |
2131 | { |
2132 | _PyDict_DebugMallocStats(out); |
2133 | _PyFloat_DebugMallocStats(out); |
2134 | _PyList_DebugMallocStats(out); |
2135 | _PyTuple_DebugMallocStats(out); |
2136 | } |
2137 | |
2138 | /* These methods are used to control infinite recursion in repr, str, print, |
2139 | etc. Container objects that may recursively contain themselves, |
2140 | e.g. builtin dictionaries and lists, should use Py_ReprEnter() and |
2141 | Py_ReprLeave() to avoid infinite recursion. |
2142 | |
2143 | Py_ReprEnter() returns 0 the first time it is called for a particular |
2144 | object and 1 every time thereafter. It returns -1 if an exception |
2145 | occurred. Py_ReprLeave() has no return value. |
2146 | |
2147 | See dictobject.c and listobject.c for examples of use. |
2148 | */ |
2149 | |
2150 | int |
2151 | Py_ReprEnter(PyObject *obj) |
2152 | { |
2153 | PyObject *dict; |
2154 | PyObject *list; |
2155 | Py_ssize_t i; |
2156 | |
2157 | dict = PyThreadState_GetDict(); |
2158 | /* Ignore a missing thread-state, so that this function can be called |
2159 | early on startup. */ |
2160 | if (dict == NULL) Branch (2160:9): [True: 0, False: 52.2k]
|
2161 | return 0; |
2162 | list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); |
2163 | if (list == NULL) { Branch (2163:9): [True: 84, False: 52.1k]
|
2164 | if (PyErr_Occurred()) { Branch (2164:13): [True: 0, False: 84]
|
2165 | return -1; |
2166 | } |
2167 | list = PyList_New(0); |
2168 | if (list == NULL) Branch (2168:13): [True: 0, False: 84]
|
2169 | return -1; |
2170 | if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0) Branch (2170:13): [True: 0, False: 84]
|
2171 | return -1; |
2172 | Py_DECREF(list); |
2173 | } |
2174 | i = PyList_GET_SIZE(list); |
2175 | while (--i >= 0) { Branch (2175:12): [True: 1.50M, False: 52.2k]
|
2176 | if (PyList_GET_ITEM(list, i) == obj) Branch (2176:13): [True: 48, False: 1.50M]
|
2177 | return 1; |
2178 | } |
2179 | if (PyList_Append(list, obj) < 0) Branch (2179:9): [True: 0, False: 52.2k]
|
2180 | return -1; |
2181 | return 0; |
2182 | } |
2183 | |
2184 | void |
2185 | Py_ReprLeave(PyObject *obj) |
2186 | { |
2187 | PyObject *dict; |
2188 | PyObject *list; |
2189 | Py_ssize_t i; |
2190 | PyObject *error_type, *error_value, *error_traceback; |
2191 | |
2192 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
2193 | |
2194 | dict = PyThreadState_GetDict(); |
2195 | if (dict == NULL) Branch (2195:9): [True: 0, False: 52.2k]
|
2196 | goto finally; |
2197 | |
2198 | list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); |
2199 | if (list == NULL || !PyList_Check(list)) Branch (2199:9): [True: 0, False: 52.2k]
Branch (2199:25): [True: 0, False: 52.2k]
|
2200 | goto finally; |
2201 | |
2202 | i = PyList_GET_SIZE(list); |
2203 | /* Count backwards because we always expect obj to be list[-1] */ |
2204 | while (--i >= 0) { Branch (2204:12): [True: 52.2k, False: 1]
|
2205 | if (PyList_GET_ITEM(list, i) == obj) { Branch (2205:13): [True: 52.2k, False: 0]
|
2206 | PyList_SetSlice(list, i, i + 1, NULL); |
2207 | break; |
2208 | } |
2209 | } |
2210 | |
2211 | finally: |
2212 | /* ignore exceptions because there is no way to report them. */ |
2213 | PyErr_Restore(error_type, error_value, error_traceback); |
2214 | } |
2215 | |
2216 | /* Trashcan support. */ |
2217 | |
2218 | #define _PyTrash_UNWIND_LEVEL 50 |
2219 | |
2220 | /* Add op to the gcstate->trash_delete_later list. Called when the current |
2221 | * call-stack depth gets large. op must be a currently untracked gc'ed |
2222 | * object, with refcount 0. Py_DECREF must already have been called on it. |
2223 | */ |
2224 | static void |
2225 | _PyTrash_thread_deposit_object(PyObject *op) |
2226 | { |
2227 | PyThreadState *tstate = _PyThreadState_GET(); |
2228 | _PyObject_ASSERT(op, _PyObject_IS_GC(op)); |
2229 | _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op)); |
2230 | _PyObject_ASSERT(op, Py_REFCNT(op) == 0); |
2231 | _PyGCHead_SET_PREV(_Py_AS_GC(op), (PyGC_Head*)tstate->trash_delete_later); |
2232 | tstate->trash_delete_later = op; |
2233 | } |
2234 | |
2235 | /* Deallocate all the objects in the gcstate->trash_delete_later list. |
2236 | * Called when the call-stack unwinds again. */ |
2237 | static void |
2238 | _PyTrash_thread_destroy_chain(void) |
2239 | { |
2240 | PyThreadState *tstate = _PyThreadState_GET(); |
2241 | /* We need to increase trash_delete_nesting here, otherwise, |
2242 | _PyTrash_thread_destroy_chain will be called recursively |
2243 | and then possibly crash. An example that may crash without |
2244 | increase: |
2245 | N = 500000 # need to be large enough |
2246 | ob = object() |
2247 | tups = [(ob,) for i in range(N)] |
2248 | for i in range(49): |
2249 | tups = [(tup,) for tup in tups] |
2250 | del tups |
2251 | */ |
2252 | assert(tstate->trash_delete_nesting == 0); |
2253 | ++tstate->trash_delete_nesting; |
2254 | while (tstate->trash_delete_later) { Branch (2254:12): [True: 60.0k, False: 584]
|
2255 | PyObject *op = tstate->trash_delete_later; |
2256 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
2257 | |
2258 | tstate->trash_delete_later = |
2259 | (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op)); |
2260 | |
2261 | /* Call the deallocator directly. This used to try to |
2262 | * fool Py_DECREF into calling it indirectly, but |
2263 | * Py_DECREF was already called on this object, and in |
2264 | * assorted non-release builds calling Py_DECREF again ends |
2265 | * up distorting allocation statistics. |
2266 | */ |
2267 | _PyObject_ASSERT(op, Py_REFCNT(op) == 0); |
2268 | (*dealloc)(op); |
2269 | assert(tstate->trash_delete_nesting == 1); |
2270 | } |
2271 | --tstate->trash_delete_nesting; |
2272 | } |
2273 | |
2274 | |
2275 | int |
2276 | _PyTrash_begin(PyThreadState *tstate, PyObject *op) |
2277 | { |
2278 | if (tstate->trash_delete_nesting >= _PyTrash_UNWIND_LEVEL) { Branch (2278:9): [True: 60.0k, False: 214M]
|
2279 | /* Store the object (to be deallocated later) and jump past |
2280 | * Py_TRASHCAN_END, skipping the body of the deallocator */ |
2281 | _PyTrash_thread_deposit_object(op); |
2282 | return 1; |
2283 | } |
2284 | ++tstate->trash_delete_nesting; |
2285 | return 0; |
2286 | } |
2287 | |
2288 | |
2289 | void |
2290 | _PyTrash_end(PyThreadState *tstate) |
2291 | { |
2292 | --tstate->trash_delete_nesting; |
2293 | if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 03.01M ) { Branch (2293:9): [True: 3.01M, False: 211M]
Branch (2293:39): [True: 584, False: 3.01M]
|
2294 | _PyTrash_thread_destroy_chain(); |
2295 | } |
2296 | } |
2297 | |
2298 | |
2299 | /* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide |
2300 | implementation details. */ |
2301 | int |
2302 | _PyTrash_cond(PyObject *op, destructor dealloc) |
2303 | { |
2304 | return Py_TYPE(op)->tp_dealloc == dealloc; |
2305 | } |
2306 | |
2307 | |
2308 | void _Py_NO_RETURN |
2309 | _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, |
2310 | const char *file, int line, const char *function) |
2311 | { |
2312 | fprintf(stderr, "%s:%d: ", file, line); |
2313 | if (function) { Branch (2313:9): [True: 0, False: 0]
|
2314 | fprintf(stderr, "%s: ", function); |
2315 | } |
2316 | fflush(stderr); |
2317 |
|
2318 | if (expr) { Branch (2318:9): [True: 0, False: 0]
|
2319 | fprintf(stderr, "Assertion \"%s\" failed", expr); |
2320 | } |
2321 | else { |
2322 | fprintf(stderr, "Assertion failed"); |
2323 | } |
2324 | fflush(stderr); |
2325 |
|
2326 | if (msg) { Branch (2326:9): [True: 0, False: 0]
|
2327 | fprintf(stderr, ": %s", msg); |
2328 | } |
2329 | fprintf(stderr, "\n"); |
2330 | fflush(stderr); |
2331 |
|
2332 | if (_PyObject_IsFreed(obj)) { Branch (2332:9): [True: 0, False: 0]
|
2333 | /* It seems like the object memory has been freed: |
2334 | don't access it to prevent a segmentation fault. */ |
2335 | fprintf(stderr, "<object at %p is freed>\n", obj); |
2336 | fflush(stderr); |
2337 | } |
2338 | else { |
2339 | /* Display the traceback where the object has been allocated. |
2340 | Do it before dumping repr(obj), since repr() is more likely |
2341 | to crash than dumping the traceback. */ |
2342 | void *ptr; |
2343 | PyTypeObject *type = Py_TYPE(obj); |
2344 | if (_PyType_IS_GC(type)) { |
2345 | ptr = (void *)((char *)obj - sizeof(PyGC_Head)); |
2346 | } |
2347 | else { |
2348 | ptr = (void *)obj; |
2349 | } |
2350 | _PyMem_DumpTraceback(fileno(stderr), ptr); |
2351 | |
2352 | /* This might succeed or fail, but we're about to abort, so at least |
2353 | try to provide any extra info we can: */ |
2354 | _PyObject_Dump(obj); |
2355 |
|
2356 | fprintf(stderr, "\n"); |
2357 | fflush(stderr); |
2358 | } |
2359 |
|
2360 | Py_FatalError("_PyObject_AssertFailed"); |
2361 | } |
2362 | |
2363 | |
2364 | void |
2365 | _Py_Dealloc(PyObject *op) |
2366 | { |
2367 | PyTypeObject *type = Py_TYPE(op); |
2368 | destructor dealloc = type->tp_dealloc; |
2369 | #ifdef Py_DEBUG |
2370 | PyThreadState *tstate = _PyThreadState_GET(); |
2371 | PyObject *old_exc_type = tstate->curexc_type; |
2372 | // Keep the old exception type alive to prevent undefined behavior |
2373 | // on (tstate->curexc_type != old_exc_type) below |
2374 | Py_XINCREF(old_exc_type); |
2375 | // Make sure that type->tp_name remains valid |
2376 | Py_INCREF(type); |
2377 | #endif |
2378 | |
2379 | #ifdef Py_TRACE_REFS |
2380 | _Py_ForgetReference(op); |
2381 | #endif |
2382 | (*dealloc)(op); |
2383 | |
2384 | #ifdef Py_DEBUG |
2385 | // gh-89373: The tp_dealloc function must leave the current exception |
2386 | // unchanged. |
2387 | if (tstate->curexc_type != old_exc_type) { |
2388 | const char *err; |
2389 | if (old_exc_type == NULL) { |
2390 | err = "Deallocator of type '%s' raised an exception"; |
2391 | } |
2392 | else if (tstate->curexc_type == NULL) { |
2393 | err = "Deallocator of type '%s' cleared the current exception"; |
2394 | } |
2395 | else { |
2396 | // It can happen if dealloc() normalized the current exception. |
2397 | // A deallocator function must not change the current exception, |
2398 | // not even normalize it. |
2399 | err = "Deallocator of type '%s' overrode the current exception"; |
2400 | } |
2401 | _Py_FatalErrorFormat(__func__, err, type->tp_name); |
2402 | } |
2403 | Py_XDECREF(old_exc_type); |
2404 | Py_DECREF(type); |
2405 | #endif |
2406 | } |
2407 | |
2408 | |
2409 | PyObject ** |
2410 | PyObject_GET_WEAKREFS_LISTPTR(PyObject *op) |
2411 | { |
2412 | return _PyObject_GET_WEAKREFS_LISTPTR(op); |
2413 | } |
2414 | |
2415 | |
2416 | #undef Py_NewRef |
2417 | #undef Py_XNewRef |
2418 | |
2419 | // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI. |
2420 | PyObject* |
2421 | Py_NewRef(PyObject *obj) |
2422 | { |
2423 | return _Py_NewRef(obj); |
2424 | } |
2425 | |
2426 | PyObject* |
2427 | Py_XNewRef(PyObject *obj) |
2428 | { |
2429 | return _Py_XNewRef(obj); |
2430 | } |
2431 | |
2432 | #undef Py_Is |
2433 | #undef Py_IsNone |
2434 | #undef Py_IsTrue |
2435 | #undef Py_IsFalse |
2436 | |
2437 | // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions |
2438 | // for the stable ABI. |
2439 | int Py_Is(PyObject *x, PyObject *y) |
2440 | { |
2441 | return (x == y); |
2442 | } |
2443 | |
2444 | int Py_IsNone(PyObject *x) |
2445 | { |
2446 | return Py_Is(x, Py_None); |
2447 | } |
2448 | |
2449 | int Py_IsTrue(PyObject *x) |
2450 | { |
2451 | return Py_Is(x, Py_True); |
2452 | } |
2453 | |
2454 | int Py_IsFalse(PyObject *x) |
2455 | { |
2456 | return Py_Is(x, Py_False); |
2457 | } |
2458 | |
2459 | #ifdef __cplusplus |
2460 | } |
2461 | #endif |