/src/Python-3.8.3/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_initconfig.h" |
6 | | #include "pycore_object.h" |
7 | | #include "pycore_pystate.h" |
8 | | #include "pycore_context.h" |
9 | | #include "frameobject.h" |
10 | | #include "interpreteridobject.h" |
11 | | |
12 | | #ifdef __cplusplus |
13 | | extern "C" { |
14 | | #endif |
15 | | |
16 | | /* Defined in tracemalloc.c */ |
17 | | extern void _PyMem_DumpTraceback(int fd, const void *ptr); |
18 | | |
19 | | _Py_IDENTIFIER(Py_Repr); |
20 | | _Py_IDENTIFIER(__bytes__); |
21 | | _Py_IDENTIFIER(__dir__); |
22 | | _Py_IDENTIFIER(__isabstractmethod__); |
23 | | |
24 | | |
25 | | int |
26 | | _PyObject_CheckConsistency(PyObject *op, int check_content) |
27 | 0 | { |
28 | 0 | #define CHECK(expr) \ |
29 | 0 | do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0) |
30 | |
|
31 | 0 | CHECK(!_PyObject_IsFreed(op)); |
32 | 0 | CHECK(Py_REFCNT(op) >= 1); |
33 | |
|
34 | 0 | CHECK(op->ob_type != NULL); |
35 | 0 | _PyType_CheckConsistency(op->ob_type); |
36 | |
|
37 | 0 | if (PyUnicode_Check(op)) { |
38 | 0 | _PyUnicode_CheckConsistency(op, check_content); |
39 | 0 | } |
40 | 0 | else if (PyDict_Check(op)) { |
41 | 0 | _PyDict_CheckConsistency(op, check_content); |
42 | 0 | } |
43 | 0 | return 1; |
44 | |
|
45 | 0 | #undef CHECK |
46 | 0 | } |
47 | | |
48 | | |
49 | | #ifdef Py_REF_DEBUG |
50 | | Py_ssize_t _Py_RefTotal; |
51 | | |
52 | | Py_ssize_t |
53 | | _Py_GetRefTotal(void) |
54 | | { |
55 | | PyObject *o; |
56 | | Py_ssize_t total = _Py_RefTotal; |
57 | | o = _PySet_Dummy; |
58 | | if (o != NULL) |
59 | | total -= o->ob_refcnt; |
60 | | return total; |
61 | | } |
62 | | |
63 | | void |
64 | | _PyDebug_PrintTotalRefs(void) { |
65 | | fprintf(stderr, |
66 | | "[%" PY_FORMAT_SIZE_T "d refs, " |
67 | | "%" PY_FORMAT_SIZE_T "d blocks]\n", |
68 | | _Py_GetRefTotal(), _Py_GetAllocatedBlocks()); |
69 | | } |
70 | | #endif /* Py_REF_DEBUG */ |
71 | | |
72 | | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
73 | | These are used by the individual routines for object creation. |
74 | | Do not call them otherwise, they do not initialize the object! */ |
75 | | |
76 | | #ifdef Py_TRACE_REFS |
77 | | /* Head of circular doubly-linked list of all objects. These are linked |
78 | | * together via the _ob_prev and _ob_next members of a PyObject, which |
79 | | * exist only in a Py_TRACE_REFS build. |
80 | | */ |
81 | | static PyObject refchain = {&refchain, &refchain}; |
82 | | |
83 | | /* Insert op at the front of the list of all objects. If force is true, |
84 | | * op is added even if _ob_prev and _ob_next are non-NULL already. If |
85 | | * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. |
86 | | * force should be true if and only if op points to freshly allocated, |
87 | | * uninitialized memory, or you've unlinked op from the list and are |
88 | | * relinking it into the front. |
89 | | * Note that objects are normally added to the list via _Py_NewReference, |
90 | | * which is called by PyObject_Init. Not all objects are initialized that |
91 | | * way, though; exceptions include statically allocated type objects, and |
92 | | * statically allocated singletons (like Py_True and Py_None). |
93 | | */ |
94 | | void |
95 | | _Py_AddToAllObjects(PyObject *op, int force) |
96 | | { |
97 | | #ifdef Py_DEBUG |
98 | | if (!force) { |
99 | | /* If it's initialized memory, op must be in or out of |
100 | | * the list unambiguously. |
101 | | */ |
102 | | _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
103 | | } |
104 | | #endif |
105 | | if (force || op->_ob_prev == NULL) { |
106 | | op->_ob_next = refchain._ob_next; |
107 | | op->_ob_prev = &refchain; |
108 | | refchain._ob_next->_ob_prev = op; |
109 | | refchain._ob_next = op; |
110 | | } |
111 | | } |
112 | | #endif /* Py_TRACE_REFS */ |
113 | | |
114 | | #ifdef COUNT_ALLOCS |
115 | | static PyTypeObject *type_list; |
116 | | /* All types are added to type_list, at least when |
117 | | they get one object created. That makes them |
118 | | immortal, which unfortunately contributes to |
119 | | garbage itself. If unlist_types_without_objects |
120 | | is set, they will be removed from the type_list |
121 | | once the last object is deallocated. */ |
122 | | static int unlist_types_without_objects; |
123 | | extern Py_ssize_t _Py_tuple_zero_allocs, _Py_fast_tuple_allocs; |
124 | | extern Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs; |
125 | | extern Py_ssize_t _Py_null_strings, _Py_one_strings; |
126 | | void |
127 | | _Py_dump_counts(FILE* f) |
128 | | { |
129 | | PyInterpreterState *interp = _PyInterpreterState_Get(); |
130 | | if (!interp->config.show_alloc_count) { |
131 | | return; |
132 | | } |
133 | | |
134 | | PyTypeObject *tp; |
135 | | for (tp = type_list; tp; tp = tp->tp_next) |
136 | | fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, " |
137 | | "freed: %" PY_FORMAT_SIZE_T "d, " |
138 | | "max in use: %" PY_FORMAT_SIZE_T "d\n", |
139 | | tp->tp_name, tp->tp_allocs, tp->tp_frees, |
140 | | tp->tp_maxalloc); |
141 | | fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " |
142 | | "empty: %" PY_FORMAT_SIZE_T "d\n", |
143 | | _Py_fast_tuple_allocs, _Py_tuple_zero_allocs); |
144 | | fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " |
145 | | "neg: %" PY_FORMAT_SIZE_T "d\n", |
146 | | _Py_quick_int_allocs, _Py_quick_neg_int_allocs); |
147 | | fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " |
148 | | "1-strings: %" PY_FORMAT_SIZE_T "d\n", |
149 | | _Py_null_strings, _Py_one_strings); |
150 | | } |
151 | | |
152 | | PyObject * |
153 | | _Py_get_counts(void) |
154 | | { |
155 | | PyTypeObject *tp; |
156 | | PyObject *result; |
157 | | PyObject *v; |
158 | | |
159 | | result = PyList_New(0); |
160 | | if (result == NULL) |
161 | | return NULL; |
162 | | for (tp = type_list; tp; tp = tp->tp_next) { |
163 | | v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
164 | | tp->tp_frees, tp->tp_maxalloc); |
165 | | if (v == NULL) { |
166 | | Py_DECREF(result); |
167 | | return NULL; |
168 | | } |
169 | | if (PyList_Append(result, v) < 0) { |
170 | | Py_DECREF(v); |
171 | | Py_DECREF(result); |
172 | | return NULL; |
173 | | } |
174 | | Py_DECREF(v); |
175 | | } |
176 | | return result; |
177 | | } |
178 | | |
179 | | void |
180 | | _Py_inc_count(PyTypeObject *tp) |
181 | | { |
182 | | if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
183 | | /* first time; insert in linked list */ |
184 | | if (tp->tp_next != NULL) /* sanity check */ |
185 | | Py_FatalError("XXX _Py_inc_count sanity check"); |
186 | | if (type_list) |
187 | | type_list->tp_prev = tp; |
188 | | tp->tp_next = type_list; |
189 | | /* Note that as of Python 2.2, heap-allocated type objects |
190 | | * can go away, but this code requires that they stay alive |
191 | | * until program exit. That's why we're careful with |
192 | | * refcounts here. type_list gets a new reference to tp, |
193 | | * while ownership of the reference type_list used to hold |
194 | | * (if any) was transferred to tp->tp_next in the line above. |
195 | | * tp is thus effectively immortal after this. |
196 | | */ |
197 | | Py_INCREF(tp); |
198 | | type_list = tp; |
199 | | #ifdef Py_TRACE_REFS |
200 | | /* Also insert in the doubly-linked list of all objects, |
201 | | * if not already there. |
202 | | */ |
203 | | _Py_AddToAllObjects((PyObject *)tp, 0); |
204 | | #endif |
205 | | } |
206 | | tp->tp_allocs++; |
207 | | if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
208 | | tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
209 | | } |
210 | | |
211 | | void _Py_dec_count(PyTypeObject *tp) |
212 | | { |
213 | | tp->tp_frees++; |
214 | | if (unlist_types_without_objects && |
215 | | tp->tp_allocs == tp->tp_frees) { |
216 | | /* unlink the type from type_list */ |
217 | | if (tp->tp_prev) |
218 | | tp->tp_prev->tp_next = tp->tp_next; |
219 | | else |
220 | | type_list = tp->tp_next; |
221 | | if (tp->tp_next) |
222 | | tp->tp_next->tp_prev = tp->tp_prev; |
223 | | tp->tp_next = tp->tp_prev = NULL; |
224 | | Py_DECREF(tp); |
225 | | } |
226 | | } |
227 | | |
228 | | #endif |
229 | | |
230 | | #ifdef Py_REF_DEBUG |
231 | | /* Log a fatal error; doesn't return. */ |
232 | | void |
233 | | _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) |
234 | | { |
235 | | _PyObject_AssertFailed(op, NULL, "object has negative ref count", |
236 | | filename, lineno, __func__); |
237 | | } |
238 | | |
239 | | #endif /* Py_REF_DEBUG */ |
240 | | |
241 | | void |
242 | | Py_IncRef(PyObject *o) |
243 | 0 | { |
244 | 0 | Py_XINCREF(o); |
245 | 0 | } |
246 | | |
247 | | void |
248 | | Py_DecRef(PyObject *o) |
249 | 0 | { |
250 | 0 | Py_XDECREF(o); |
251 | 0 | } |
252 | | |
253 | | PyObject * |
254 | | PyObject_Init(PyObject *op, PyTypeObject *tp) |
255 | 11.0k | { |
256 | 11.0k | if (op == NULL) |
257 | 0 | return PyErr_NoMemory(); |
258 | | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
259 | 11.0k | Py_TYPE(op) = tp; |
260 | 11.0k | if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) { |
261 | 0 | Py_INCREF(tp); |
262 | 0 | } |
263 | 11.0k | _Py_NewReference(op); |
264 | 11.0k | return op; |
265 | 11.0k | } |
266 | | |
267 | | PyVarObject * |
268 | | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
269 | 12 | { |
270 | 12 | if (op == NULL) |
271 | 0 | return (PyVarObject *) PyErr_NoMemory(); |
272 | | /* Any changes should be reflected in PyObject_INIT_VAR */ |
273 | 12 | Py_SIZE(op) = size; |
274 | 12 | PyObject_Init((PyObject *)op, tp); |
275 | 12 | return op; |
276 | 12 | } |
277 | | |
278 | | PyObject * |
279 | | _PyObject_New(PyTypeObject *tp) |
280 | 127k | { |
281 | 127k | PyObject *op; |
282 | 127k | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
283 | 127k | if (op == NULL) |
284 | 0 | return PyErr_NoMemory(); |
285 | 127k | return PyObject_INIT(op, tp); |
286 | 127k | } |
287 | | |
288 | | PyVarObject * |
289 | | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
290 | 0 | { |
291 | 0 | PyVarObject *op; |
292 | 0 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
293 | 0 | op = (PyVarObject *) PyObject_MALLOC(size); |
294 | 0 | if (op == NULL) |
295 | 0 | return (PyVarObject *)PyErr_NoMemory(); |
296 | 0 | return PyObject_INIT_VAR(op, tp, nitems); |
297 | 0 | } |
298 | | |
299 | | void |
300 | | PyObject_CallFinalizer(PyObject *self) |
301 | 701 | { |
302 | 701 | PyTypeObject *tp = Py_TYPE(self); |
303 | | |
304 | 701 | if (tp->tp_finalize == NULL) |
305 | 0 | return; |
306 | | /* tp_finalize should only be called once. */ |
307 | 701 | if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self)) |
308 | 0 | return; |
309 | | |
310 | 701 | tp->tp_finalize(self); |
311 | 701 | if (PyType_IS_GC(tp)) { |
312 | 701 | _PyGC_SET_FINALIZED(self); |
313 | 701 | } |
314 | 701 | } |
315 | | |
316 | | int |
317 | | PyObject_CallFinalizerFromDealloc(PyObject *self) |
318 | 701 | { |
319 | 701 | Py_ssize_t refcnt; |
320 | | |
321 | | /* Temporarily resurrect the object. */ |
322 | 701 | if (self->ob_refcnt != 0) { |
323 | 0 | Py_FatalError("PyObject_CallFinalizerFromDealloc called on " |
324 | 0 | "object with a non-zero refcount"); |
325 | 0 | } |
326 | 701 | self->ob_refcnt = 1; |
327 | | |
328 | 701 | PyObject_CallFinalizer(self); |
329 | | |
330 | | /* Undo the temporary resurrection; can't use DECREF here, it would |
331 | | * cause a recursive call. |
332 | | */ |
333 | 701 | _PyObject_ASSERT_WITH_MSG(self, |
334 | 701 | self->ob_refcnt > 0, |
335 | 701 | "refcount is too small"); |
336 | 701 | if (--self->ob_refcnt == 0) |
337 | 701 | return 0; /* this is the normal path out */ |
338 | | |
339 | | /* tp_finalize resurrected it! Make it look like the original Py_DECREF |
340 | | * never happened. |
341 | | */ |
342 | 0 | refcnt = self->ob_refcnt; |
343 | 0 | _Py_NewReference(self); |
344 | 0 | self->ob_refcnt = refcnt; |
345 | |
|
346 | 0 | _PyObject_ASSERT(self, |
347 | 0 | (!PyType_IS_GC(Py_TYPE(self)) |
348 | 0 | || _PyObject_GC_IS_TRACKED(self))); |
349 | | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
350 | | * we need to undo that. */ |
351 | 0 | _Py_DEC_REFTOTAL; |
352 | | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
353 | | * chain, so no more to do there. |
354 | | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
355 | | * _Py_NewReference bumped tp_allocs: both of those need to be |
356 | | * undone. |
357 | | */ |
358 | | #ifdef COUNT_ALLOCS |
359 | | --Py_TYPE(self)->tp_frees; |
360 | | --Py_TYPE(self)->tp_allocs; |
361 | | #endif |
362 | 0 | return -1; |
363 | 701 | } |
364 | | |
365 | | int |
366 | | PyObject_Print(PyObject *op, FILE *fp, int flags) |
367 | 0 | { |
368 | 0 | int ret = 0; |
369 | 0 | if (PyErr_CheckSignals()) |
370 | 0 | return -1; |
371 | | #ifdef USE_STACKCHECK |
372 | | if (PyOS_CheckStack()) { |
373 | | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
374 | | return -1; |
375 | | } |
376 | | #endif |
377 | 0 | clearerr(fp); /* Clear any previous error condition */ |
378 | 0 | if (op == NULL) { |
379 | 0 | Py_BEGIN_ALLOW_THREADS |
380 | 0 | fprintf(fp, "<nil>"); |
381 | 0 | Py_END_ALLOW_THREADS |
382 | 0 | } |
383 | 0 | else { |
384 | 0 | if (op->ob_refcnt <= 0) { |
385 | | /* XXX(twouters) cast refcount to long until %zd is |
386 | | universally available */ |
387 | 0 | Py_BEGIN_ALLOW_THREADS |
388 | 0 | fprintf(fp, "<refcnt %ld at %p>", |
389 | 0 | (long)op->ob_refcnt, (void *)op); |
390 | 0 | Py_END_ALLOW_THREADS |
391 | 0 | } |
392 | 0 | else { |
393 | 0 | PyObject *s; |
394 | 0 | if (flags & Py_PRINT_RAW) |
395 | 0 | s = PyObject_Str(op); |
396 | 0 | else |
397 | 0 | s = PyObject_Repr(op); |
398 | 0 | if (s == NULL) |
399 | 0 | ret = -1; |
400 | 0 | else if (PyBytes_Check(s)) { |
401 | 0 | fwrite(PyBytes_AS_STRING(s), 1, |
402 | 0 | PyBytes_GET_SIZE(s), fp); |
403 | 0 | } |
404 | 0 | else if (PyUnicode_Check(s)) { |
405 | 0 | PyObject *t; |
406 | 0 | t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); |
407 | 0 | if (t == NULL) { |
408 | 0 | ret = -1; |
409 | 0 | } |
410 | 0 | else { |
411 | 0 | fwrite(PyBytes_AS_STRING(t), 1, |
412 | 0 | PyBytes_GET_SIZE(t), fp); |
413 | 0 | Py_DECREF(t); |
414 | 0 | } |
415 | 0 | } |
416 | 0 | else { |
417 | 0 | PyErr_Format(PyExc_TypeError, |
418 | 0 | "str() or repr() returned '%.100s'", |
419 | 0 | s->ob_type->tp_name); |
420 | 0 | ret = -1; |
421 | 0 | } |
422 | 0 | Py_XDECREF(s); |
423 | 0 | } |
424 | 0 | } |
425 | 0 | if (ret == 0) { |
426 | 0 | if (ferror(fp)) { |
427 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
428 | 0 | clearerr(fp); |
429 | 0 | ret = -1; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | return ret; |
433 | 0 | } |
434 | | |
435 | | /* For debugging convenience. Set a breakpoint here and call it from your DLL */ |
436 | | void |
437 | | _Py_BreakPoint(void) |
438 | 0 | { |
439 | 0 | } |
440 | | |
441 | | |
442 | | /* Heuristic checking if the object memory is uninitialized or deallocated. |
443 | | Rely on the debug hooks on Python memory allocators: |
444 | | see _PyMem_IsPtrFreed(). |
445 | | |
446 | | The function can be used to prevent segmentation fault on dereferencing |
447 | | pointers like 0xDDDDDDDDDDDDDDDD. */ |
448 | | int |
449 | | _PyObject_IsFreed(PyObject *op) |
450 | 0 | { |
451 | 0 | if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(op->ob_type)) { |
452 | 0 | return 1; |
453 | 0 | } |
454 | | /* ignore op->ob_ref: its value can have be modified |
455 | | by Py_INCREF() and Py_DECREF(). */ |
456 | | #ifdef Py_TRACE_REFS |
457 | | if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) { |
458 | | return 1; |
459 | | } |
460 | | if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) { |
461 | | return 1; |
462 | | } |
463 | | #endif |
464 | 0 | return 0; |
465 | 0 | } |
466 | | |
467 | | |
468 | | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
469 | | void |
470 | | _PyObject_Dump(PyObject* op) |
471 | 0 | { |
472 | 0 | if (_PyObject_IsFreed(op)) { |
473 | | /* It seems like the object memory has been freed: |
474 | | don't access it to prevent a segmentation fault. */ |
475 | 0 | fprintf(stderr, "<object at %p is freed>\n", op); |
476 | 0 | fflush(stderr); |
477 | 0 | return; |
478 | 0 | } |
479 | | |
480 | | /* first, write fields which are the least likely to crash */ |
481 | 0 | fprintf(stderr, "object address : %p\n", (void *)op); |
482 | | /* XXX(twouters) cast refcount to long until %zd is |
483 | | universally available */ |
484 | 0 | fprintf(stderr, "object refcount : %ld\n", (long)op->ob_refcnt); |
485 | 0 | fflush(stderr); |
486 | |
|
487 | 0 | PyTypeObject *type = Py_TYPE(op); |
488 | 0 | fprintf(stderr, "object type : %p\n", type); |
489 | 0 | fprintf(stderr, "object type name: %s\n", |
490 | 0 | type==NULL ? "NULL" : type->tp_name); |
491 | | |
492 | | /* the most dangerous part */ |
493 | 0 | fprintf(stderr, "object repr : "); |
494 | 0 | fflush(stderr); |
495 | |
|
496 | 0 | PyGILState_STATE gil = PyGILState_Ensure(); |
497 | 0 | PyObject *error_type, *error_value, *error_traceback; |
498 | 0 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
499 | |
|
500 | 0 | (void)PyObject_Print(op, stderr, 0); |
501 | 0 | fflush(stderr); |
502 | |
|
503 | 0 | PyErr_Restore(error_type, error_value, error_traceback); |
504 | 0 | PyGILState_Release(gil); |
505 | |
|
506 | 0 | fprintf(stderr, "\n"); |
507 | 0 | fflush(stderr); |
508 | 0 | } |
509 | | |
510 | | PyObject * |
511 | | PyObject_Repr(PyObject *v) |
512 | 74 | { |
513 | 74 | PyObject *res; |
514 | 74 | if (PyErr_CheckSignals()) |
515 | 0 | return NULL; |
516 | | #ifdef USE_STACKCHECK |
517 | | if (PyOS_CheckStack()) { |
518 | | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
519 | | return NULL; |
520 | | } |
521 | | #endif |
522 | 74 | if (v == NULL) |
523 | 0 | return PyUnicode_FromString("<NULL>"); |
524 | 74 | if (Py_TYPE(v)->tp_repr == NULL) |
525 | 0 | return PyUnicode_FromFormat("<%s object at %p>", |
526 | 0 | v->ob_type->tp_name, v); |
527 | | |
528 | | #ifdef Py_DEBUG |
529 | | /* PyObject_Repr() must not be called with an exception set, |
530 | | because it can clear it (directly or indirectly) and so the |
531 | | caller loses its exception */ |
532 | | assert(!PyErr_Occurred()); |
533 | | #endif |
534 | | |
535 | | /* It is possible for a type to have a tp_repr representation that loops |
536 | | infinitely. */ |
537 | 74 | if (Py_EnterRecursiveCall(" while getting the repr of an object")) |
538 | 0 | return NULL; |
539 | 74 | res = (*v->ob_type->tp_repr)(v); |
540 | 74 | Py_LeaveRecursiveCall(); |
541 | 74 | if (res == NULL) |
542 | 0 | return NULL; |
543 | 74 | if (!PyUnicode_Check(res)) { |
544 | 0 | PyErr_Format(PyExc_TypeError, |
545 | 0 | "__repr__ returned non-string (type %.200s)", |
546 | 0 | res->ob_type->tp_name); |
547 | 0 | Py_DECREF(res); |
548 | 0 | return NULL; |
549 | 0 | } |
550 | 74 | #ifndef Py_DEBUG |
551 | 74 | if (PyUnicode_READY(res) < 0) |
552 | 0 | return NULL; |
553 | 74 | #endif |
554 | 74 | return res; |
555 | 74 | } |
556 | | |
557 | | PyObject * |
558 | | PyObject_Str(PyObject *v) |
559 | 922 | { |
560 | 922 | PyObject *res; |
561 | 922 | if (PyErr_CheckSignals()) |
562 | 0 | return NULL; |
563 | | #ifdef USE_STACKCHECK |
564 | | if (PyOS_CheckStack()) { |
565 | | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
566 | | return NULL; |
567 | | } |
568 | | #endif |
569 | 922 | if (v == NULL) |
570 | 0 | return PyUnicode_FromString("<NULL>"); |
571 | 922 | if (PyUnicode_CheckExact(v)) { |
572 | 871 | #ifndef Py_DEBUG |
573 | 871 | if (PyUnicode_READY(v) < 0) |
574 | 0 | return NULL; |
575 | 871 | #endif |
576 | 871 | Py_INCREF(v); |
577 | 871 | return v; |
578 | 871 | } |
579 | 51 | if (Py_TYPE(v)->tp_str == NULL) |
580 | 0 | return PyObject_Repr(v); |
581 | | |
582 | | #ifdef Py_DEBUG |
583 | | /* PyObject_Str() must not be called with an exception set, |
584 | | because it can clear it (directly or indirectly) and so the |
585 | | caller loses its exception */ |
586 | | assert(!PyErr_Occurred()); |
587 | | #endif |
588 | | |
589 | | /* It is possible for a type to have a tp_str representation that loops |
590 | | infinitely. */ |
591 | 51 | if (Py_EnterRecursiveCall(" while getting the str of an object")) |
592 | 0 | return NULL; |
593 | 51 | res = (*Py_TYPE(v)->tp_str)(v); |
594 | 51 | Py_LeaveRecursiveCall(); |
595 | 51 | if (res == NULL) |
596 | 0 | return NULL; |
597 | 51 | if (!PyUnicode_Check(res)) { |
598 | 0 | PyErr_Format(PyExc_TypeError, |
599 | 0 | "__str__ returned non-string (type %.200s)", |
600 | 0 | Py_TYPE(res)->tp_name); |
601 | 0 | Py_DECREF(res); |
602 | 0 | return NULL; |
603 | 0 | } |
604 | 51 | #ifndef Py_DEBUG |
605 | 51 | if (PyUnicode_READY(res) < 0) |
606 | 0 | return NULL; |
607 | 51 | #endif |
608 | 51 | assert(_PyUnicode_CheckConsistency(res, 1)); |
609 | 51 | return res; |
610 | 51 | } |
611 | | |
612 | | PyObject * |
613 | | PyObject_ASCII(PyObject *v) |
614 | 0 | { |
615 | 0 | PyObject *repr, *ascii, *res; |
616 | |
|
617 | 0 | repr = PyObject_Repr(v); |
618 | 0 | if (repr == NULL) |
619 | 0 | return NULL; |
620 | | |
621 | 0 | if (PyUnicode_IS_ASCII(repr)) |
622 | 0 | return repr; |
623 | | |
624 | | /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
625 | 0 | ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); |
626 | 0 | Py_DECREF(repr); |
627 | 0 | if (ascii == NULL) |
628 | 0 | return NULL; |
629 | | |
630 | 0 | res = PyUnicode_DecodeASCII( |
631 | 0 | PyBytes_AS_STRING(ascii), |
632 | 0 | PyBytes_GET_SIZE(ascii), |
633 | 0 | NULL); |
634 | |
|
635 | 0 | Py_DECREF(ascii); |
636 | 0 | return res; |
637 | 0 | } |
638 | | |
639 | | PyObject * |
640 | | PyObject_Bytes(PyObject *v) |
641 | 719 | { |
642 | 719 | PyObject *result, *func; |
643 | | |
644 | 719 | if (v == NULL) |
645 | 0 | return PyBytes_FromString("<NULL>"); |
646 | | |
647 | 719 | if (PyBytes_CheckExact(v)) { |
648 | 719 | Py_INCREF(v); |
649 | 719 | return v; |
650 | 719 | } |
651 | | |
652 | 0 | func = _PyObject_LookupSpecial(v, &PyId___bytes__); |
653 | 0 | if (func != NULL) { |
654 | 0 | result = _PyObject_CallNoArg(func); |
655 | 0 | Py_DECREF(func); |
656 | 0 | if (result == NULL) |
657 | 0 | return NULL; |
658 | 0 | if (!PyBytes_Check(result)) { |
659 | 0 | PyErr_Format(PyExc_TypeError, |
660 | 0 | "__bytes__ returned non-bytes (type %.200s)", |
661 | 0 | Py_TYPE(result)->tp_name); |
662 | 0 | Py_DECREF(result); |
663 | 0 | return NULL; |
664 | 0 | } |
665 | 0 | return result; |
666 | 0 | } |
667 | 0 | else if (PyErr_Occurred()) |
668 | 0 | return NULL; |
669 | 0 | return PyBytes_FromObject(v); |
670 | 0 | } |
671 | | |
672 | | /* For Python 3.0.1 and later, the old three-way comparison has been |
673 | | completely removed in favour of rich comparisons. PyObject_Compare() and |
674 | | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
675 | | The old tp_compare slot has been renamed to tp_as_async, and should no |
676 | | longer be used. Use tp_richcompare instead. |
677 | | |
678 | | See (*) below for practical amendments. |
679 | | |
680 | | tp_richcompare gets called with a first argument of the appropriate type |
681 | | and a second object of an arbitrary type. We never do any kind of |
682 | | coercion. |
683 | | |
684 | | The tp_richcompare slot should return an object, as follows: |
685 | | |
686 | | NULL if an exception occurred |
687 | | NotImplemented if the requested comparison is not implemented |
688 | | any other false value if the requested comparison is false |
689 | | any other true value if the requested comparison is true |
690 | | |
691 | | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
692 | | NotImplemented. |
693 | | |
694 | | (*) Practical amendments: |
695 | | |
696 | | - If rich comparison returns NotImplemented, == and != are decided by |
697 | | comparing the object pointer (i.e. falling back to the base object |
698 | | implementation). |
699 | | |
700 | | */ |
701 | | |
702 | | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
703 | | int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
704 | | |
705 | | static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
706 | | |
707 | | /* Perform a rich comparison, raising TypeError when the requested comparison |
708 | | operator is not supported. */ |
709 | | static PyObject * |
710 | | do_richcompare(PyObject *v, PyObject *w, int op) |
711 | 125k | { |
712 | 125k | richcmpfunc f; |
713 | 125k | PyObject *res; |
714 | 125k | int checked_reverse_op = 0; |
715 | | |
716 | 125k | if (v->ob_type != w->ob_type && |
717 | 125k | PyType_IsSubtype(w->ob_type, v->ob_type) && |
718 | 125k | (f = w->ob_type->tp_richcompare) != NULL) { |
719 | 4 | checked_reverse_op = 1; |
720 | 4 | res = (*f)(w, v, _Py_SwappedOp[op]); |
721 | 4 | if (res != Py_NotImplemented) |
722 | 4 | return res; |
723 | 0 | Py_DECREF(res); |
724 | 0 | } |
725 | 125k | if ((f = v->ob_type->tp_richcompare) != NULL) { |
726 | 125k | res = (*f)(v, w, op); |
727 | 125k | if (res != Py_NotImplemented) |
728 | 124k | return res; |
729 | 228 | Py_DECREF(res); |
730 | 228 | } |
731 | 228 | if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { |
732 | 228 | res = (*f)(w, v, _Py_SwappedOp[op]); |
733 | 228 | if (res != Py_NotImplemented) |
734 | 0 | return res; |
735 | 228 | Py_DECREF(res); |
736 | 228 | } |
737 | | /* If neither object implements it, provide a sensible default |
738 | | for == and !=, but raise an exception for ordering. */ |
739 | 228 | switch (op) { |
740 | 228 | case Py_EQ: |
741 | 228 | res = (v == w) ? Py_True : Py_False; |
742 | 228 | break; |
743 | 0 | case Py_NE: |
744 | 0 | res = (v != w) ? Py_True : Py_False; |
745 | 0 | break; |
746 | 0 | default: |
747 | 0 | PyErr_Format(PyExc_TypeError, |
748 | 0 | "'%s' not supported between instances of '%.100s' and '%.100s'", |
749 | 0 | opstrings[op], |
750 | 0 | v->ob_type->tp_name, |
751 | 0 | w->ob_type->tp_name); |
752 | 0 | return NULL; |
753 | 228 | } |
754 | 228 | Py_INCREF(res); |
755 | 228 | return res; |
756 | 228 | } |
757 | | |
758 | | /* Perform a rich comparison with object result. This wraps do_richcompare() |
759 | | with a check for NULL arguments and a recursion check. */ |
760 | | |
761 | | PyObject * |
762 | | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
763 | 125k | { |
764 | 125k | PyObject *res; |
765 | | |
766 | 125k | assert(Py_LT <= op && op <= Py_GE); |
767 | 125k | if (v == NULL || w == NULL) { |
768 | 0 | if (!PyErr_Occurred()) |
769 | 0 | PyErr_BadInternalCall(); |
770 | 0 | return NULL; |
771 | 0 | } |
772 | 125k | if (Py_EnterRecursiveCall(" in comparison")) |
773 | 0 | return NULL; |
774 | 125k | res = do_richcompare(v, w, op); |
775 | 125k | Py_LeaveRecursiveCall(); |
776 | 125k | return res; |
777 | 125k | } |
778 | | |
779 | | /* Perform a rich comparison with integer result. This wraps |
780 | | PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
781 | | int |
782 | | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
783 | 108k | { |
784 | 108k | PyObject *res; |
785 | 108k | int ok; |
786 | | |
787 | | /* Quick result when objects are the same. |
788 | | Guarantees that identity implies equality. */ |
789 | 108k | if (v == w) { |
790 | 336 | if (op == Py_EQ) |
791 | 310 | return 1; |
792 | 26 | else if (op == Py_NE) |
793 | 0 | return 0; |
794 | 336 | } |
795 | | |
796 | 108k | res = PyObject_RichCompare(v, w, op); |
797 | 108k | if (res == NULL) |
798 | 0 | return -1; |
799 | 108k | if (PyBool_Check(res)) |
800 | 108k | ok = (res == Py_True); |
801 | 0 | else |
802 | 0 | ok = PyObject_IsTrue(res); |
803 | 108k | Py_DECREF(res); |
804 | 108k | return ok; |
805 | 108k | } |
806 | | |
807 | | Py_hash_t |
808 | | PyObject_HashNotImplemented(PyObject *v) |
809 | 0 | { |
810 | 0 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
811 | 0 | Py_TYPE(v)->tp_name); |
812 | 0 | return -1; |
813 | 0 | } |
814 | | |
815 | | Py_hash_t |
816 | | PyObject_Hash(PyObject *v) |
817 | 321k | { |
818 | 321k | PyTypeObject *tp = Py_TYPE(v); |
819 | 321k | if (tp->tp_hash != NULL) |
820 | 321k | return (*tp->tp_hash)(v); |
821 | | /* To keep to the general practice that inheriting |
822 | | * solely from object in C code should work without |
823 | | * an explicit call to PyType_Ready, we implicitly call |
824 | | * PyType_Ready here and then check the tp_hash slot again |
825 | | */ |
826 | 0 | if (tp->tp_dict == NULL) { |
827 | 0 | if (PyType_Ready(tp) < 0) |
828 | 0 | return -1; |
829 | 0 | if (tp->tp_hash != NULL) |
830 | 0 | return (*tp->tp_hash)(v); |
831 | 0 | } |
832 | | /* Otherwise, the object can't be hashed */ |
833 | 0 | return PyObject_HashNotImplemented(v); |
834 | 0 | } |
835 | | |
836 | | PyObject * |
837 | | PyObject_GetAttrString(PyObject *v, const char *name) |
838 | 819 | { |
839 | 819 | PyObject *w, *res; |
840 | | |
841 | 819 | if (Py_TYPE(v)->tp_getattr != NULL) |
842 | 0 | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
843 | 819 | w = PyUnicode_FromString(name); |
844 | 819 | if (w == NULL) |
845 | 0 | return NULL; |
846 | 819 | res = PyObject_GetAttr(v, w); |
847 | 819 | Py_DECREF(w); |
848 | 819 | return res; |
849 | 819 | } |
850 | | |
851 | | int |
852 | | PyObject_HasAttrString(PyObject *v, const char *name) |
853 | 0 | { |
854 | 0 | PyObject *res = PyObject_GetAttrString(v, name); |
855 | 0 | if (res != NULL) { |
856 | 0 | Py_DECREF(res); |
857 | 0 | return 1; |
858 | 0 | } |
859 | 0 | PyErr_Clear(); |
860 | 0 | return 0; |
861 | 0 | } |
862 | | |
863 | | int |
864 | | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
865 | 5.40k | { |
866 | 5.40k | PyObject *s; |
867 | 5.40k | int res; |
868 | | |
869 | 5.40k | if (Py_TYPE(v)->tp_setattr != NULL) |
870 | 0 | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
871 | 5.40k | s = PyUnicode_InternFromString(name); |
872 | 5.40k | if (s == NULL) |
873 | 0 | return -1; |
874 | 5.40k | res = PyObject_SetAttr(v, s, w); |
875 | 5.40k | Py_XDECREF(s); |
876 | 5.40k | return res; |
877 | 5.40k | } |
878 | | |
879 | | int |
880 | | _PyObject_IsAbstract(PyObject *obj) |
881 | 4.10k | { |
882 | 4.10k | int res; |
883 | 4.10k | PyObject* isabstract; |
884 | | |
885 | 4.10k | if (obj == NULL) |
886 | 2 | return 0; |
887 | | |
888 | 4.10k | res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract); |
889 | 4.10k | if (res > 0) { |
890 | 913 | res = PyObject_IsTrue(isabstract); |
891 | 913 | Py_DECREF(isabstract); |
892 | 913 | } |
893 | 4.10k | return res; |
894 | 4.10k | } |
895 | | |
896 | | PyObject * |
897 | | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) |
898 | 10.1k | { |
899 | 10.1k | PyObject *result; |
900 | 10.1k | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
901 | 10.1k | if (!oname) |
902 | 0 | return NULL; |
903 | 10.1k | result = PyObject_GetAttr(v, oname); |
904 | 10.1k | return result; |
905 | 10.1k | } |
906 | | |
907 | | int |
908 | | _PyObject_HasAttrId(PyObject *v, _Py_Identifier *name) |
909 | 0 | { |
910 | 0 | int result; |
911 | 0 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
912 | 0 | if (!oname) |
913 | 0 | return -1; |
914 | 0 | result = PyObject_HasAttr(v, oname); |
915 | 0 | return result; |
916 | 0 | } |
917 | | |
918 | | int |
919 | | _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w) |
920 | 1.71k | { |
921 | 1.71k | int result; |
922 | 1.71k | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
923 | 1.71k | if (!oname) |
924 | 0 | return -1; |
925 | 1.71k | result = PyObject_SetAttr(v, oname, w); |
926 | 1.71k | return result; |
927 | 1.71k | } |
928 | | |
929 | | PyObject * |
930 | | PyObject_GetAttr(PyObject *v, PyObject *name) |
931 | 87.1k | { |
932 | 87.1k | PyTypeObject *tp = Py_TYPE(v); |
933 | | |
934 | 87.1k | if (!PyUnicode_Check(name)) { |
935 | 0 | PyErr_Format(PyExc_TypeError, |
936 | 0 | "attribute name must be string, not '%.200s'", |
937 | 0 | name->ob_type->tp_name); |
938 | 0 | return NULL; |
939 | 0 | } |
940 | 87.1k | if (tp->tp_getattro != NULL) |
941 | 87.1k | return (*tp->tp_getattro)(v, name); |
942 | 0 | if (tp->tp_getattr != NULL) { |
943 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
944 | 0 | if (name_str == NULL) |
945 | 0 | return NULL; |
946 | 0 | return (*tp->tp_getattr)(v, (char *)name_str); |
947 | 0 | } |
948 | 0 | PyErr_Format(PyExc_AttributeError, |
949 | 0 | "'%.50s' object has no attribute '%U'", |
950 | 0 | tp->tp_name, name); |
951 | 0 | return NULL; |
952 | 0 | } |
953 | | |
954 | | int |
955 | | _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result) |
956 | 15.9k | { |
957 | 15.9k | PyTypeObject *tp = Py_TYPE(v); |
958 | | |
959 | 15.9k | if (!PyUnicode_Check(name)) { |
960 | 0 | PyErr_Format(PyExc_TypeError, |
961 | 0 | "attribute name must be string, not '%.200s'", |
962 | 0 | name->ob_type->tp_name); |
963 | 0 | *result = NULL; |
964 | 0 | return -1; |
965 | 0 | } |
966 | | |
967 | 15.9k | if (tp->tp_getattro == PyObject_GenericGetAttr) { |
968 | 8.98k | *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1); |
969 | 8.98k | if (*result != NULL) { |
970 | 5.00k | return 1; |
971 | 5.00k | } |
972 | 3.97k | if (PyErr_Occurred()) { |
973 | 0 | return -1; |
974 | 0 | } |
975 | 3.97k | return 0; |
976 | 3.97k | } |
977 | 6.98k | if (tp->tp_getattro != NULL) { |
978 | 6.98k | *result = (*tp->tp_getattro)(v, name); |
979 | 6.98k | } |
980 | 0 | else if (tp->tp_getattr != NULL) { |
981 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
982 | 0 | if (name_str == NULL) { |
983 | 0 | *result = NULL; |
984 | 0 | return -1; |
985 | 0 | } |
986 | 0 | *result = (*tp->tp_getattr)(v, (char *)name_str); |
987 | 0 | } |
988 | 0 | else { |
989 | 0 | *result = NULL; |
990 | 0 | return 0; |
991 | 0 | } |
992 | | |
993 | 6.98k | if (*result != NULL) { |
994 | 5.27k | return 1; |
995 | 5.27k | } |
996 | 1.70k | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
997 | 0 | return -1; |
998 | 0 | } |
999 | 1.70k | PyErr_Clear(); |
1000 | 1.70k | return 0; |
1001 | 1.70k | } |
1002 | | |
1003 | | int |
1004 | | _PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result) |
1005 | 8.87k | { |
1006 | 8.87k | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
1007 | 8.87k | if (!oname) { |
1008 | 0 | *result = NULL; |
1009 | 0 | return -1; |
1010 | 0 | } |
1011 | 8.87k | return _PyObject_LookupAttr(v, oname, result); |
1012 | 8.87k | } |
1013 | | |
1014 | | int |
1015 | | PyObject_HasAttr(PyObject *v, PyObject *name) |
1016 | 0 | { |
1017 | 0 | PyObject *res; |
1018 | 0 | if (_PyObject_LookupAttr(v, name, &res) < 0) { |
1019 | 0 | PyErr_Clear(); |
1020 | 0 | return 0; |
1021 | 0 | } |
1022 | 0 | if (res == NULL) { |
1023 | 0 | return 0; |
1024 | 0 | } |
1025 | 0 | Py_DECREF(res); |
1026 | 0 | return 1; |
1027 | 0 | } |
1028 | | |
1029 | | int |
1030 | | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
1031 | 24.5k | { |
1032 | 24.5k | PyTypeObject *tp = Py_TYPE(v); |
1033 | 24.5k | int err; |
1034 | | |
1035 | 24.5k | if (!PyUnicode_Check(name)) { |
1036 | 0 | PyErr_Format(PyExc_TypeError, |
1037 | 0 | "attribute name must be string, not '%.200s'", |
1038 | 0 | name->ob_type->tp_name); |
1039 | 0 | return -1; |
1040 | 0 | } |
1041 | 24.5k | Py_INCREF(name); |
1042 | | |
1043 | 24.5k | PyUnicode_InternInPlace(&name); |
1044 | 24.5k | if (tp->tp_setattro != NULL) { |
1045 | 24.5k | err = (*tp->tp_setattro)(v, name, value); |
1046 | 24.5k | Py_DECREF(name); |
1047 | 24.5k | return err; |
1048 | 24.5k | } |
1049 | 0 | if (tp->tp_setattr != NULL) { |
1050 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
1051 | 0 | if (name_str == NULL) { |
1052 | 0 | Py_DECREF(name); |
1053 | 0 | return -1; |
1054 | 0 | } |
1055 | 0 | err = (*tp->tp_setattr)(v, (char *)name_str, value); |
1056 | 0 | Py_DECREF(name); |
1057 | 0 | return err; |
1058 | 0 | } |
1059 | 0 | Py_DECREF(name); |
1060 | 0 | _PyObject_ASSERT(name, name->ob_refcnt >= 1); |
1061 | 0 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
1062 | 0 | PyErr_Format(PyExc_TypeError, |
1063 | 0 | "'%.100s' object has no attributes " |
1064 | 0 | "(%s .%U)", |
1065 | 0 | tp->tp_name, |
1066 | 0 | value==NULL ? "del" : "assign to", |
1067 | 0 | name); |
1068 | 0 | else |
1069 | 0 | PyErr_Format(PyExc_TypeError, |
1070 | 0 | "'%.100s' object has only read-only attributes " |
1071 | 0 | "(%s .%U)", |
1072 | 0 | tp->tp_name, |
1073 | 0 | value==NULL ? "del" : "assign to", |
1074 | 0 | name); |
1075 | 0 | return -1; |
1076 | 0 | } |
1077 | | |
1078 | | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
1079 | | |
1080 | | PyObject ** |
1081 | | _PyObject_GetDictPtr(PyObject *obj) |
1082 | 48.3k | { |
1083 | 48.3k | Py_ssize_t dictoffset; |
1084 | 48.3k | PyTypeObject *tp = Py_TYPE(obj); |
1085 | | |
1086 | 48.3k | dictoffset = tp->tp_dictoffset; |
1087 | 48.3k | if (dictoffset == 0) |
1088 | 14.4k | return NULL; |
1089 | 33.9k | if (dictoffset < 0) { |
1090 | 570 | Py_ssize_t tsize; |
1091 | 570 | size_t size; |
1092 | | |
1093 | 570 | tsize = ((PyVarObject *)obj)->ob_size; |
1094 | 570 | if (tsize < 0) |
1095 | 0 | tsize = -tsize; |
1096 | 570 | size = _PyObject_VAR_SIZE(tp, tsize); |
1097 | | |
1098 | 570 | dictoffset += (long)size; |
1099 | 570 | _PyObject_ASSERT(obj, dictoffset > 0); |
1100 | 570 | _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0); |
1101 | 570 | } |
1102 | 33.9k | return (PyObject **) ((char *)obj + dictoffset); |
1103 | 48.3k | } |
1104 | | |
1105 | | PyObject * |
1106 | | PyObject_SelfIter(PyObject *obj) |
1107 | 920 | { |
1108 | 920 | Py_INCREF(obj); |
1109 | 920 | return obj; |
1110 | 920 | } |
1111 | | |
1112 | | /* Helper used when the __next__ method is removed from a type: |
1113 | | tp_iternext is never NULL and can be safely called without checking |
1114 | | on every iteration. |
1115 | | */ |
1116 | | |
1117 | | PyObject * |
1118 | | _PyObject_NextNotImplemented(PyObject *self) |
1119 | 0 | { |
1120 | 0 | PyErr_Format(PyExc_TypeError, |
1121 | 0 | "'%.200s' object is not iterable", |
1122 | 0 | Py_TYPE(self)->tp_name); |
1123 | 0 | return NULL; |
1124 | 0 | } |
1125 | | |
1126 | | |
1127 | | /* Specialized version of _PyObject_GenericGetAttrWithDict |
1128 | | specifically for the LOAD_METHOD opcode. |
1129 | | |
1130 | | Return 1 if a method is found, 0 if it's a regular attribute |
1131 | | from __dict__ or something returned by using a descriptor |
1132 | | protocol. |
1133 | | |
1134 | | `method` will point to the resolved attribute or NULL. In the |
1135 | | latter case, an error will be set. |
1136 | | */ |
1137 | | int |
1138 | | _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) |
1139 | 37.8k | { |
1140 | 37.8k | PyTypeObject *tp = Py_TYPE(obj); |
1141 | 37.8k | PyObject *descr; |
1142 | 37.8k | descrgetfunc f = NULL; |
1143 | 37.8k | PyObject **dictptr, *dict; |
1144 | 37.8k | PyObject *attr; |
1145 | 37.8k | int meth_found = 0; |
1146 | | |
1147 | 37.8k | assert(*method == NULL); |
1148 | | |
1149 | 37.8k | if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr |
1150 | 37.8k | || !PyUnicode_Check(name)) { |
1151 | 17.1k | *method = PyObject_GetAttr(obj, name); |
1152 | 17.1k | return 0; |
1153 | 17.1k | } |
1154 | | |
1155 | 20.7k | if (tp->tp_dict == NULL && PyType_Ready(tp) < 0) |
1156 | 0 | return 0; |
1157 | | |
1158 | 20.7k | descr = _PyType_Lookup(tp, name); |
1159 | 20.7k | if (descr != NULL) { |
1160 | 20.6k | Py_INCREF(descr); |
1161 | 20.6k | if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { |
1162 | 20.6k | meth_found = 1; |
1163 | 20.6k | } else { |
1164 | 17 | f = descr->ob_type->tp_descr_get; |
1165 | 17 | if (f != NULL && PyDescr_IsData(descr)) { |
1166 | 14 | *method = f(descr, obj, (PyObject *)obj->ob_type); |
1167 | 14 | Py_DECREF(descr); |
1168 | 14 | return 0; |
1169 | 14 | } |
1170 | 17 | } |
1171 | 20.6k | } |
1172 | | |
1173 | 20.7k | dictptr = _PyObject_GetDictPtr(obj); |
1174 | 20.7k | if (dictptr != NULL && (dict = *dictptr) != NULL) { |
1175 | 5.43k | Py_INCREF(dict); |
1176 | 5.43k | attr = PyDict_GetItemWithError(dict, name); |
1177 | 5.43k | if (attr != NULL) { |
1178 | 53 | Py_INCREF(attr); |
1179 | 53 | *method = attr; |
1180 | 53 | Py_DECREF(dict); |
1181 | 53 | Py_XDECREF(descr); |
1182 | 53 | return 0; |
1183 | 53 | } |
1184 | 5.38k | else { |
1185 | 5.38k | Py_DECREF(dict); |
1186 | 5.38k | if (PyErr_Occurred()) { |
1187 | 0 | Py_XDECREF(descr); |
1188 | 0 | return 0; |
1189 | 0 | } |
1190 | 5.38k | } |
1191 | 5.43k | } |
1192 | | |
1193 | 20.6k | if (meth_found) { |
1194 | 20.6k | *method = descr; |
1195 | 20.6k | return 1; |
1196 | 20.6k | } |
1197 | | |
1198 | 3 | if (f != NULL) { |
1199 | 0 | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1200 | 0 | Py_DECREF(descr); |
1201 | 0 | return 0; |
1202 | 0 | } |
1203 | | |
1204 | 3 | if (descr != NULL) { |
1205 | 3 | *method = descr; |
1206 | 3 | return 0; |
1207 | 3 | } |
1208 | | |
1209 | 0 | PyErr_Format(PyExc_AttributeError, |
1210 | 0 | "'%.50s' object has no attribute '%U'", |
1211 | 0 | tp->tp_name, name); |
1212 | 0 | return 0; |
1213 | 3 | } |
1214 | | |
1215 | | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ |
1216 | | |
1217 | | PyObject * |
1218 | | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, |
1219 | | PyObject *dict, int suppress) |
1220 | 88.5k | { |
1221 | | /* Make sure the logic of _PyObject_GetMethod is in sync with |
1222 | | this method. |
1223 | | |
1224 | | When suppress=1, this function suppress AttributeError. |
1225 | | */ |
1226 | | |
1227 | 88.5k | PyTypeObject *tp = Py_TYPE(obj); |
1228 | 88.5k | PyObject *descr = NULL; |
1229 | 88.5k | PyObject *res = NULL; |
1230 | 88.5k | descrgetfunc f; |
1231 | 88.5k | Py_ssize_t dictoffset; |
1232 | 88.5k | PyObject **dictptr; |
1233 | | |
1234 | 88.5k | if (!PyUnicode_Check(name)){ |
1235 | 0 | PyErr_Format(PyExc_TypeError, |
1236 | 0 | "attribute name must be string, not '%.200s'", |
1237 | 0 | name->ob_type->tp_name); |
1238 | 0 | return NULL; |
1239 | 0 | } |
1240 | 88.5k | Py_INCREF(name); |
1241 | | |
1242 | 88.5k | if (tp->tp_dict == NULL) { |
1243 | 0 | if (PyType_Ready(tp) < 0) |
1244 | 0 | goto done; |
1245 | 0 | } |
1246 | | |
1247 | 88.5k | descr = _PyType_Lookup(tp, name); |
1248 | | |
1249 | 88.5k | f = NULL; |
1250 | 88.5k | if (descr != NULL) { |
1251 | 14.8k | Py_INCREF(descr); |
1252 | 14.8k | f = descr->ob_type->tp_descr_get; |
1253 | 14.8k | if (f != NULL && PyDescr_IsData(descr)) { |
1254 | 11.6k | res = f(descr, obj, (PyObject *)obj->ob_type); |
1255 | 11.6k | if (res == NULL && suppress && |
1256 | 11.6k | PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1257 | 0 | PyErr_Clear(); |
1258 | 0 | } |
1259 | 11.6k | goto done; |
1260 | 11.6k | } |
1261 | 14.8k | } |
1262 | | |
1263 | 76.9k | if (dict == NULL) { |
1264 | | /* Inline _PyObject_GetDictPtr */ |
1265 | 76.9k | dictoffset = tp->tp_dictoffset; |
1266 | 76.9k | if (dictoffset != 0) { |
1267 | 73.9k | if (dictoffset < 0) { |
1268 | 579 | Py_ssize_t tsize; |
1269 | 579 | size_t size; |
1270 | | |
1271 | 579 | tsize = ((PyVarObject *)obj)->ob_size; |
1272 | 579 | if (tsize < 0) |
1273 | 0 | tsize = -tsize; |
1274 | 579 | size = _PyObject_VAR_SIZE(tp, tsize); |
1275 | 579 | _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX); |
1276 | | |
1277 | 579 | dictoffset += (Py_ssize_t)size; |
1278 | 579 | _PyObject_ASSERT(obj, dictoffset > 0); |
1279 | 579 | _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0); |
1280 | 579 | } |
1281 | 73.9k | dictptr = (PyObject **) ((char *)obj + dictoffset); |
1282 | 73.9k | dict = *dictptr; |
1283 | 73.9k | } |
1284 | 76.9k | } |
1285 | 76.9k | if (dict != NULL) { |
1286 | 71.9k | Py_INCREF(dict); |
1287 | 71.9k | res = PyDict_GetItemWithError(dict, name); |
1288 | 71.9k | if (res != NULL) { |
1289 | 66.5k | Py_INCREF(res); |
1290 | 66.5k | Py_DECREF(dict); |
1291 | 66.5k | goto done; |
1292 | 66.5k | } |
1293 | 5.31k | else { |
1294 | 5.31k | Py_DECREF(dict); |
1295 | 5.31k | if (PyErr_Occurred()) { |
1296 | 0 | if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1297 | 0 | PyErr_Clear(); |
1298 | 0 | } |
1299 | 0 | else { |
1300 | 0 | goto done; |
1301 | 0 | } |
1302 | 0 | } |
1303 | 5.31k | } |
1304 | 71.9k | } |
1305 | | |
1306 | 10.3k | if (f != NULL) { |
1307 | 3.09k | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1308 | 3.09k | if (res == NULL && suppress && |
1309 | 3.09k | PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1310 | 0 | PyErr_Clear(); |
1311 | 0 | } |
1312 | 3.09k | goto done; |
1313 | 3.09k | } |
1314 | | |
1315 | 7.25k | if (descr != NULL) { |
1316 | 88 | res = descr; |
1317 | 88 | descr = NULL; |
1318 | 88 | goto done; |
1319 | 88 | } |
1320 | | |
1321 | 7.16k | if (!suppress) { |
1322 | 3.19k | PyErr_Format(PyExc_AttributeError, |
1323 | 3.19k | "'%.50s' object has no attribute '%U'", |
1324 | 3.19k | tp->tp_name, name); |
1325 | 3.19k | } |
1326 | 88.5k | done: |
1327 | 88.5k | Py_XDECREF(descr); |
1328 | 88.5k | Py_DECREF(name); |
1329 | 88.5k | return res; |
1330 | 7.16k | } |
1331 | | |
1332 | | PyObject * |
1333 | | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
1334 | 79.5k | { |
1335 | 79.5k | return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0); |
1336 | 79.5k | } |
1337 | | |
1338 | | int |
1339 | | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
1340 | | PyObject *value, PyObject *dict) |
1341 | 24.5k | { |
1342 | 24.5k | PyTypeObject *tp = Py_TYPE(obj); |
1343 | 24.5k | PyObject *descr; |
1344 | 24.5k | descrsetfunc f; |
1345 | 24.5k | PyObject **dictptr; |
1346 | 24.5k | int res = -1; |
1347 | | |
1348 | 24.5k | if (!PyUnicode_Check(name)){ |
1349 | 0 | PyErr_Format(PyExc_TypeError, |
1350 | 0 | "attribute name must be string, not '%.200s'", |
1351 | 0 | name->ob_type->tp_name); |
1352 | 0 | return -1; |
1353 | 0 | } |
1354 | | |
1355 | 24.5k | if (tp->tp_dict == NULL && PyType_Ready(tp) < 0) |
1356 | 0 | return -1; |
1357 | | |
1358 | 24.5k | Py_INCREF(name); |
1359 | | |
1360 | 24.5k | descr = _PyType_Lookup(tp, name); |
1361 | | |
1362 | 24.5k | if (descr != NULL) { |
1363 | 1.52k | Py_INCREF(descr); |
1364 | 1.52k | f = descr->ob_type->tp_descr_set; |
1365 | 1.52k | if (f != NULL) { |
1366 | 1.30k | res = f(descr, obj, value); |
1367 | 1.30k | goto done; |
1368 | 1.30k | } |
1369 | 1.52k | } |
1370 | | |
1371 | | /* XXX [Steve Dower] These are really noisy - worth it? */ |
1372 | | /*if (PyType_Check(obj) || PyModule_Check(obj)) { |
1373 | | if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0) |
1374 | | return -1; |
1375 | | if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0) |
1376 | | return -1; |
1377 | | }*/ |
1378 | | |
1379 | 23.2k | if (dict == NULL) { |
1380 | 23.2k | dictptr = _PyObject_GetDictPtr(obj); |
1381 | 23.2k | if (dictptr == NULL) { |
1382 | 0 | if (descr == NULL) { |
1383 | 0 | PyErr_Format(PyExc_AttributeError, |
1384 | 0 | "'%.100s' object has no attribute '%U'", |
1385 | 0 | tp->tp_name, name); |
1386 | 0 | } |
1387 | 0 | else { |
1388 | 0 | PyErr_Format(PyExc_AttributeError, |
1389 | 0 | "'%.50s' object attribute '%U' is read-only", |
1390 | 0 | tp->tp_name, name); |
1391 | 0 | } |
1392 | 0 | goto done; |
1393 | 0 | } |
1394 | 23.2k | res = _PyObjectDict_SetItem(tp, dictptr, name, value); |
1395 | 23.2k | } |
1396 | 0 | else { |
1397 | 0 | Py_INCREF(dict); |
1398 | 0 | if (value == NULL) |
1399 | 0 | res = PyDict_DelItem(dict, name); |
1400 | 0 | else |
1401 | 0 | res = PyDict_SetItem(dict, name, value); |
1402 | 0 | Py_DECREF(dict); |
1403 | 0 | } |
1404 | 23.2k | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
1405 | 0 | PyErr_SetObject(PyExc_AttributeError, name); |
1406 | | |
1407 | 24.5k | done: |
1408 | 24.5k | Py_XDECREF(descr); |
1409 | 24.5k | Py_DECREF(name); |
1410 | 24.5k | return res; |
1411 | 23.2k | } |
1412 | | |
1413 | | int |
1414 | | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
1415 | 23.5k | { |
1416 | 23.5k | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
1417 | 23.5k | } |
1418 | | |
1419 | | int |
1420 | | PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context) |
1421 | 0 | { |
1422 | 0 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
1423 | 0 | if (dictptr == NULL) { |
1424 | 0 | PyErr_SetString(PyExc_AttributeError, |
1425 | 0 | "This object has no __dict__"); |
1426 | 0 | return -1; |
1427 | 0 | } |
1428 | 0 | if (value == NULL) { |
1429 | 0 | PyErr_SetString(PyExc_TypeError, "cannot delete __dict__"); |
1430 | 0 | return -1; |
1431 | 0 | } |
1432 | 0 | if (!PyDict_Check(value)) { |
1433 | 0 | PyErr_Format(PyExc_TypeError, |
1434 | 0 | "__dict__ must be set to a dictionary, " |
1435 | 0 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
1436 | 0 | return -1; |
1437 | 0 | } |
1438 | 0 | Py_INCREF(value); |
1439 | 0 | Py_XSETREF(*dictptr, value); |
1440 | 0 | return 0; |
1441 | 0 | } |
1442 | | |
1443 | | |
1444 | | /* Test a value used as condition, e.g., in a for or if statement. |
1445 | | Return -1 if an error occurred */ |
1446 | | |
1447 | | int |
1448 | | PyObject_IsTrue(PyObject *v) |
1449 | 14.9k | { |
1450 | 14.9k | Py_ssize_t res; |
1451 | 14.9k | if (v == Py_True) |
1452 | 1.59k | return 1; |
1453 | 13.3k | if (v == Py_False) |
1454 | 2.57k | return 0; |
1455 | 10.7k | if (v == Py_None) |
1456 | 589 | return 0; |
1457 | 10.1k | else if (v->ob_type->tp_as_number != NULL && |
1458 | 10.1k | v->ob_type->tp_as_number->nb_bool != NULL) |
1459 | 1.71k | res = (*v->ob_type->tp_as_number->nb_bool)(v); |
1460 | 8.44k | else if (v->ob_type->tp_as_mapping != NULL && |
1461 | 8.44k | v->ob_type->tp_as_mapping->mp_length != NULL) |
1462 | 7.72k | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
1463 | 722 | else if (v->ob_type->tp_as_sequence != NULL && |
1464 | 722 | v->ob_type->tp_as_sequence->sq_length != NULL) |
1465 | 462 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
1466 | 260 | else |
1467 | 260 | return 1; |
1468 | | /* if it is negative, it should be either -1 or -2 */ |
1469 | 9.89k | return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
1470 | 10.7k | } |
1471 | | |
1472 | | /* equivalent of 'not v' |
1473 | | Return -1 if an error occurred */ |
1474 | | |
1475 | | int |
1476 | | PyObject_Not(PyObject *v) |
1477 | 0 | { |
1478 | 0 | int res; |
1479 | 0 | res = PyObject_IsTrue(v); |
1480 | 0 | if (res < 0) |
1481 | 0 | return res; |
1482 | 0 | return res == 0; |
1483 | 0 | } |
1484 | | |
1485 | | /* Test whether an object can be called */ |
1486 | | |
1487 | | int |
1488 | | PyCallable_Check(PyObject *x) |
1489 | 2.21k | { |
1490 | 2.21k | if (x == NULL) |
1491 | 0 | return 0; |
1492 | 2.21k | return x->ob_type->tp_call != NULL; |
1493 | 2.21k | } |
1494 | | |
1495 | | |
1496 | | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
1497 | | static PyObject * |
1498 | | _dir_locals(void) |
1499 | 0 | { |
1500 | 0 | PyObject *names; |
1501 | 0 | PyObject *locals; |
1502 | |
|
1503 | 0 | locals = PyEval_GetLocals(); |
1504 | 0 | if (locals == NULL) |
1505 | 0 | return NULL; |
1506 | | |
1507 | 0 | names = PyMapping_Keys(locals); |
1508 | 0 | if (!names) |
1509 | 0 | return NULL; |
1510 | 0 | if (!PyList_Check(names)) { |
1511 | 0 | PyErr_Format(PyExc_TypeError, |
1512 | 0 | "dir(): expected keys() of locals to be a list, " |
1513 | 0 | "not '%.200s'", Py_TYPE(names)->tp_name); |
1514 | 0 | Py_DECREF(names); |
1515 | 0 | return NULL; |
1516 | 0 | } |
1517 | 0 | if (PyList_Sort(names)) { |
1518 | 0 | Py_DECREF(names); |
1519 | 0 | return NULL; |
1520 | 0 | } |
1521 | | /* the locals don't need to be DECREF'd */ |
1522 | 0 | return names; |
1523 | 0 | } |
1524 | | |
1525 | | /* Helper for PyObject_Dir: object introspection. */ |
1526 | | static PyObject * |
1527 | | _dir_object(PyObject *obj) |
1528 | 14 | { |
1529 | 14 | PyObject *result, *sorted; |
1530 | 14 | PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__); |
1531 | | |
1532 | 14 | assert(obj != NULL); |
1533 | 14 | if (dirfunc == NULL) { |
1534 | 0 | if (!PyErr_Occurred()) |
1535 | 0 | PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); |
1536 | 0 | return NULL; |
1537 | 0 | } |
1538 | | /* use __dir__ */ |
1539 | 14 | result = _PyObject_CallNoArg(dirfunc); |
1540 | 14 | Py_DECREF(dirfunc); |
1541 | 14 | if (result == NULL) |
1542 | 0 | return NULL; |
1543 | | /* return sorted(result) */ |
1544 | 14 | sorted = PySequence_List(result); |
1545 | 14 | Py_DECREF(result); |
1546 | 14 | if (sorted == NULL) |
1547 | 0 | return NULL; |
1548 | 14 | if (PyList_Sort(sorted)) { |
1549 | 0 | Py_DECREF(sorted); |
1550 | 0 | return NULL; |
1551 | 0 | } |
1552 | 14 | return sorted; |
1553 | 14 | } |
1554 | | |
1555 | | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
1556 | | (local) scope. Otherwise, performs introspection of the object: returns a |
1557 | | sorted list of attribute names (supposedly) accessible from the object |
1558 | | */ |
1559 | | PyObject * |
1560 | | PyObject_Dir(PyObject *obj) |
1561 | 14 | { |
1562 | 14 | return (obj == NULL) ? _dir_locals() : _dir_object(obj); |
1563 | 14 | } |
1564 | | |
1565 | | /* |
1566 | | None is a non-NULL undefined value. |
1567 | | There is (and should be!) no way to create other objects of this type, |
1568 | | so there is exactly one (which is indestructible, by the way). |
1569 | | */ |
1570 | | |
1571 | | /* ARGSUSED */ |
1572 | | static PyObject * |
1573 | | none_repr(PyObject *op) |
1574 | 0 | { |
1575 | 0 | return PyUnicode_FromString("None"); |
1576 | 0 | } |
1577 | | |
1578 | | /* ARGUSED */ |
1579 | | static void |
1580 | | none_dealloc(PyObject* ignore) |
1581 | 0 | { |
1582 | | /* This should never get called, but we also don't want to SEGV if |
1583 | | * we accidentally decref None out of existence. |
1584 | | */ |
1585 | 0 | Py_FatalError("deallocating None"); |
1586 | 0 | } |
1587 | | |
1588 | | static PyObject * |
1589 | | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
1590 | 0 | { |
1591 | 0 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
1592 | 0 | PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); |
1593 | 0 | return NULL; |
1594 | 0 | } |
1595 | 0 | Py_RETURN_NONE; |
1596 | 0 | } |
1597 | | |
1598 | | static int |
1599 | | none_bool(PyObject *v) |
1600 | 0 | { |
1601 | 0 | return 0; |
1602 | 0 | } |
1603 | | |
1604 | | static PyNumberMethods none_as_number = { |
1605 | | 0, /* nb_add */ |
1606 | | 0, /* nb_subtract */ |
1607 | | 0, /* nb_multiply */ |
1608 | | 0, /* nb_remainder */ |
1609 | | 0, /* nb_divmod */ |
1610 | | 0, /* nb_power */ |
1611 | | 0, /* nb_negative */ |
1612 | | 0, /* nb_positive */ |
1613 | | 0, /* nb_absolute */ |
1614 | | (inquiry)none_bool, /* nb_bool */ |
1615 | | 0, /* nb_invert */ |
1616 | | 0, /* nb_lshift */ |
1617 | | 0, /* nb_rshift */ |
1618 | | 0, /* nb_and */ |
1619 | | 0, /* nb_xor */ |
1620 | | 0, /* nb_or */ |
1621 | | 0, /* nb_int */ |
1622 | | 0, /* nb_reserved */ |
1623 | | 0, /* nb_float */ |
1624 | | 0, /* nb_inplace_add */ |
1625 | | 0, /* nb_inplace_subtract */ |
1626 | | 0, /* nb_inplace_multiply */ |
1627 | | 0, /* nb_inplace_remainder */ |
1628 | | 0, /* nb_inplace_power */ |
1629 | | 0, /* nb_inplace_lshift */ |
1630 | | 0, /* nb_inplace_rshift */ |
1631 | | 0, /* nb_inplace_and */ |
1632 | | 0, /* nb_inplace_xor */ |
1633 | | 0, /* nb_inplace_or */ |
1634 | | 0, /* nb_floor_divide */ |
1635 | | 0, /* nb_true_divide */ |
1636 | | 0, /* nb_inplace_floor_divide */ |
1637 | | 0, /* nb_inplace_true_divide */ |
1638 | | 0, /* nb_index */ |
1639 | | }; |
1640 | | |
1641 | | PyTypeObject _PyNone_Type = { |
1642 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1643 | | "NoneType", |
1644 | | 0, |
1645 | | 0, |
1646 | | none_dealloc, /*tp_dealloc*/ /*never called*/ |
1647 | | 0, /*tp_vectorcall_offset*/ |
1648 | | 0, /*tp_getattr*/ |
1649 | | 0, /*tp_setattr*/ |
1650 | | 0, /*tp_as_async*/ |
1651 | | none_repr, /*tp_repr*/ |
1652 | | &none_as_number, /*tp_as_number*/ |
1653 | | 0, /*tp_as_sequence*/ |
1654 | | 0, /*tp_as_mapping*/ |
1655 | | 0, /*tp_hash */ |
1656 | | 0, /*tp_call */ |
1657 | | 0, /*tp_str */ |
1658 | | 0, /*tp_getattro */ |
1659 | | 0, /*tp_setattro */ |
1660 | | 0, /*tp_as_buffer */ |
1661 | | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
1662 | | 0, /*tp_doc */ |
1663 | | 0, /*tp_traverse */ |
1664 | | 0, /*tp_clear */ |
1665 | | 0, /*tp_richcompare */ |
1666 | | 0, /*tp_weaklistoffset */ |
1667 | | 0, /*tp_iter */ |
1668 | | 0, /*tp_iternext */ |
1669 | | 0, /*tp_methods */ |
1670 | | 0, /*tp_members */ |
1671 | | 0, /*tp_getset */ |
1672 | | 0, /*tp_base */ |
1673 | | 0, /*tp_dict */ |
1674 | | 0, /*tp_descr_get */ |
1675 | | 0, /*tp_descr_set */ |
1676 | | 0, /*tp_dictoffset */ |
1677 | | 0, /*tp_init */ |
1678 | | 0, /*tp_alloc */ |
1679 | | none_new, /*tp_new */ |
1680 | | }; |
1681 | | |
1682 | | PyObject _Py_NoneStruct = { |
1683 | | _PyObject_EXTRA_INIT |
1684 | | 1, &_PyNone_Type |
1685 | | }; |
1686 | | |
1687 | | /* NotImplemented is an object that can be used to signal that an |
1688 | | operation is not implemented for the given type combination. */ |
1689 | | |
1690 | | static PyObject * |
1691 | | NotImplemented_repr(PyObject *op) |
1692 | 0 | { |
1693 | 0 | return PyUnicode_FromString("NotImplemented"); |
1694 | 0 | } |
1695 | | |
1696 | | static PyObject * |
1697 | | NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
1698 | 0 | { |
1699 | 0 | return PyUnicode_FromString("NotImplemented"); |
1700 | 0 | } |
1701 | | |
1702 | | static PyMethodDef notimplemented_methods[] = { |
1703 | | {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL}, |
1704 | | {NULL, NULL} |
1705 | | }; |
1706 | | |
1707 | | static PyObject * |
1708 | | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
1709 | 0 | { |
1710 | 0 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
1711 | 0 | PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); |
1712 | 0 | return NULL; |
1713 | 0 | } |
1714 | 0 | Py_RETURN_NOTIMPLEMENTED; |
1715 | 0 | } |
1716 | | |
1717 | | static void |
1718 | | notimplemented_dealloc(PyObject* ignore) |
1719 | 0 | { |
1720 | | /* This should never get called, but we also don't want to SEGV if |
1721 | | * we accidentally decref NotImplemented out of existence. |
1722 | | */ |
1723 | 0 | Py_FatalError("deallocating NotImplemented"); |
1724 | 0 | } |
1725 | | |
1726 | | PyTypeObject _PyNotImplemented_Type = { |
1727 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1728 | | "NotImplementedType", |
1729 | | 0, |
1730 | | 0, |
1731 | | notimplemented_dealloc, /*tp_dealloc*/ /*never called*/ |
1732 | | 0, /*tp_vectorcall_offset*/ |
1733 | | 0, /*tp_getattr*/ |
1734 | | 0, /*tp_setattr*/ |
1735 | | 0, /*tp_as_async*/ |
1736 | | NotImplemented_repr, /*tp_repr*/ |
1737 | | 0, /*tp_as_number*/ |
1738 | | 0, /*tp_as_sequence*/ |
1739 | | 0, /*tp_as_mapping*/ |
1740 | | 0, /*tp_hash */ |
1741 | | 0, /*tp_call */ |
1742 | | 0, /*tp_str */ |
1743 | | 0, /*tp_getattro */ |
1744 | | 0, /*tp_setattro */ |
1745 | | 0, /*tp_as_buffer */ |
1746 | | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
1747 | | 0, /*tp_doc */ |
1748 | | 0, /*tp_traverse */ |
1749 | | 0, /*tp_clear */ |
1750 | | 0, /*tp_richcompare */ |
1751 | | 0, /*tp_weaklistoffset */ |
1752 | | 0, /*tp_iter */ |
1753 | | 0, /*tp_iternext */ |
1754 | | notimplemented_methods, /*tp_methods */ |
1755 | | 0, /*tp_members */ |
1756 | | 0, /*tp_getset */ |
1757 | | 0, /*tp_base */ |
1758 | | 0, /*tp_dict */ |
1759 | | 0, /*tp_descr_get */ |
1760 | | 0, /*tp_descr_set */ |
1761 | | 0, /*tp_dictoffset */ |
1762 | | 0, /*tp_init */ |
1763 | | 0, /*tp_alloc */ |
1764 | | notimplemented_new, /*tp_new */ |
1765 | | }; |
1766 | | |
1767 | | PyObject _Py_NotImplementedStruct = { |
1768 | | _PyObject_EXTRA_INIT |
1769 | | 1, &_PyNotImplemented_Type |
1770 | | }; |
1771 | | |
1772 | | PyStatus |
1773 | | _PyTypes_Init(void) |
1774 | 14 | { |
1775 | 14 | #define INIT_TYPE(TYPE, NAME) \ |
1776 | 924 | do { \ |
1777 | 924 | if (PyType_Ready(TYPE) < 0) { \ |
1778 | 0 | return _PyStatus_ERR("Can't initialize " NAME " type"); \ |
1779 | 0 | } \ |
1780 | 924 | } while (0) |
1781 | | |
1782 | 14 | INIT_TYPE(&PyBaseObject_Type, "object"); |
1783 | 14 | INIT_TYPE(&PyType_Type, "type"); |
1784 | 14 | INIT_TYPE(&_PyWeakref_RefType, "weakref"); |
1785 | 14 | INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy"); |
1786 | 14 | INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy"); |
1787 | 14 | INIT_TYPE(&PyLong_Type, "int"); |
1788 | 14 | INIT_TYPE(&PyBool_Type, "bool"); |
1789 | 14 | INIT_TYPE(&PyByteArray_Type, "bytearray"); |
1790 | 14 | INIT_TYPE(&PyBytes_Type, "str"); |
1791 | 14 | INIT_TYPE(&PyList_Type, "list"); |
1792 | 14 | INIT_TYPE(&_PyNone_Type, "None"); |
1793 | 14 | INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented"); |
1794 | 14 | INIT_TYPE(&PyTraceBack_Type, "traceback"); |
1795 | 14 | INIT_TYPE(&PySuper_Type, "super"); |
1796 | 14 | INIT_TYPE(&PyRange_Type, "range"); |
1797 | 14 | INIT_TYPE(&PyDict_Type, "dict"); |
1798 | 14 | INIT_TYPE(&PyDictKeys_Type, "dict keys"); |
1799 | 14 | INIT_TYPE(&PyDictValues_Type, "dict values"); |
1800 | 14 | INIT_TYPE(&PyDictItems_Type, "dict items"); |
1801 | 14 | INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys"); |
1802 | 14 | INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values"); |
1803 | 14 | INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items"); |
1804 | 14 | INIT_TYPE(&PyODict_Type, "OrderedDict"); |
1805 | 14 | INIT_TYPE(&PyODictKeys_Type, "odict_keys"); |
1806 | 14 | INIT_TYPE(&PyODictItems_Type, "odict_items"); |
1807 | 14 | INIT_TYPE(&PyODictValues_Type, "odict_values"); |
1808 | 14 | INIT_TYPE(&PyODictIter_Type, "odict_keyiterator"); |
1809 | 14 | INIT_TYPE(&PySet_Type, "set"); |
1810 | 14 | INIT_TYPE(&PyUnicode_Type, "str"); |
1811 | 14 | INIT_TYPE(&PySlice_Type, "slice"); |
1812 | 14 | INIT_TYPE(&PyStaticMethod_Type, "static method"); |
1813 | 14 | INIT_TYPE(&PyComplex_Type, "complex"); |
1814 | 14 | INIT_TYPE(&PyFloat_Type, "float"); |
1815 | 14 | INIT_TYPE(&PyFrozenSet_Type, "frozenset"); |
1816 | 14 | INIT_TYPE(&PyProperty_Type, "property"); |
1817 | 14 | INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer"); |
1818 | 14 | INIT_TYPE(&PyMemoryView_Type, "memoryview"); |
1819 | 14 | INIT_TYPE(&PyTuple_Type, "tuple"); |
1820 | 14 | INIT_TYPE(&PyEnum_Type, "enumerate"); |
1821 | 14 | INIT_TYPE(&PyReversed_Type, "reversed"); |
1822 | 14 | INIT_TYPE(&PyStdPrinter_Type, "StdPrinter"); |
1823 | 14 | INIT_TYPE(&PyCode_Type, "code"); |
1824 | 14 | INIT_TYPE(&PyFrame_Type, "frame"); |
1825 | 14 | INIT_TYPE(&PyCFunction_Type, "builtin function"); |
1826 | 14 | INIT_TYPE(&PyMethod_Type, "method"); |
1827 | 14 | INIT_TYPE(&PyFunction_Type, "function"); |
1828 | 14 | INIT_TYPE(&PyDictProxy_Type, "dict proxy"); |
1829 | 14 | INIT_TYPE(&PyGen_Type, "generator"); |
1830 | 14 | INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor"); |
1831 | 14 | INIT_TYPE(&PyWrapperDescr_Type, "wrapper"); |
1832 | 14 | INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper"); |
1833 | 14 | INIT_TYPE(&PyEllipsis_Type, "ellipsis"); |
1834 | 14 | INIT_TYPE(&PyMemberDescr_Type, "member descriptor"); |
1835 | 14 | INIT_TYPE(&_PyNamespace_Type, "namespace"); |
1836 | 14 | INIT_TYPE(&PyCapsule_Type, "capsule"); |
1837 | 14 | INIT_TYPE(&PyLongRangeIter_Type, "long range iterator"); |
1838 | 14 | INIT_TYPE(&PyCell_Type, "cell"); |
1839 | 14 | INIT_TYPE(&PyInstanceMethod_Type, "instance method"); |
1840 | 14 | INIT_TYPE(&PyClassMethodDescr_Type, "class method descr"); |
1841 | 14 | INIT_TYPE(&PyMethodDescr_Type, "method descr"); |
1842 | 14 | INIT_TYPE(&PyCallIter_Type, "call iter"); |
1843 | 14 | INIT_TYPE(&PySeqIter_Type, "sequence iterator"); |
1844 | 14 | INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer"); |
1845 | 14 | INIT_TYPE(&PyCoro_Type, "coroutine"); |
1846 | 14 | INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper"); |
1847 | 14 | INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID"); |
1848 | 14 | return _PyStatus_OK(); |
1849 | | |
1850 | 14 | #undef INIT_TYPE |
1851 | 14 | } |
1852 | | |
1853 | | |
1854 | | #ifdef Py_TRACE_REFS |
1855 | | |
1856 | | void |
1857 | | _Py_NewReference(PyObject *op) |
1858 | | { |
1859 | | if (_Py_tracemalloc_config.tracing) { |
1860 | | _PyTraceMalloc_NewReference(op); |
1861 | | } |
1862 | | _Py_INC_REFTOTAL; |
1863 | | op->ob_refcnt = 1; |
1864 | | _Py_AddToAllObjects(op, 1); |
1865 | | _Py_INC_TPALLOCS(op); |
1866 | | } |
1867 | | |
1868 | | void |
1869 | | _Py_ForgetReference(PyObject *op) |
1870 | | { |
1871 | | #ifdef SLOW_UNREF_CHECK |
1872 | | PyObject *p; |
1873 | | #endif |
1874 | | if (op->ob_refcnt < 0) |
1875 | | Py_FatalError("UNREF negative refcnt"); |
1876 | | if (op == &refchain || |
1877 | | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
1878 | | fprintf(stderr, "* ob\n"); |
1879 | | _PyObject_Dump(op); |
1880 | | fprintf(stderr, "* op->_ob_prev->_ob_next\n"); |
1881 | | _PyObject_Dump(op->_ob_prev->_ob_next); |
1882 | | fprintf(stderr, "* op->_ob_next->_ob_prev\n"); |
1883 | | _PyObject_Dump(op->_ob_next->_ob_prev); |
1884 | | Py_FatalError("UNREF invalid object"); |
1885 | | } |
1886 | | #ifdef SLOW_UNREF_CHECK |
1887 | | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
1888 | | if (p == op) |
1889 | | break; |
1890 | | } |
1891 | | if (p == &refchain) /* Not found */ |
1892 | | Py_FatalError("UNREF unknown object"); |
1893 | | #endif |
1894 | | op->_ob_next->_ob_prev = op->_ob_prev; |
1895 | | op->_ob_prev->_ob_next = op->_ob_next; |
1896 | | op->_ob_next = op->_ob_prev = NULL; |
1897 | | _Py_INC_TPFREES(op); |
1898 | | } |
1899 | | |
1900 | | /* Print all live objects. Because PyObject_Print is called, the |
1901 | | * interpreter must be in a healthy state. |
1902 | | */ |
1903 | | void |
1904 | | _Py_PrintReferences(FILE *fp) |
1905 | | { |
1906 | | PyObject *op; |
1907 | | fprintf(fp, "Remaining objects:\n"); |
1908 | | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
1909 | | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, op->ob_refcnt); |
1910 | | if (PyObject_Print(op, fp, 0) != 0) |
1911 | | PyErr_Clear(); |
1912 | | putc('\n', fp); |
1913 | | } |
1914 | | } |
1915 | | |
1916 | | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
1917 | | * doesn't make any calls to the Python C API, so is always safe to call. |
1918 | | */ |
1919 | | void |
1920 | | _Py_PrintReferenceAddresses(FILE *fp) |
1921 | | { |
1922 | | PyObject *op; |
1923 | | fprintf(fp, "Remaining object addresses:\n"); |
1924 | | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
1925 | | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op, |
1926 | | op->ob_refcnt, Py_TYPE(op)->tp_name); |
1927 | | } |
1928 | | |
1929 | | PyObject * |
1930 | | _Py_GetObjects(PyObject *self, PyObject *args) |
1931 | | { |
1932 | | int i, n; |
1933 | | PyObject *t = NULL; |
1934 | | PyObject *res, *op; |
1935 | | |
1936 | | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
1937 | | return NULL; |
1938 | | op = refchain._ob_next; |
1939 | | res = PyList_New(0); |
1940 | | if (res == NULL) |
1941 | | return NULL; |
1942 | | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
1943 | | while (op == self || op == args || op == res || op == t || |
1944 | | (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
1945 | | op = op->_ob_next; |
1946 | | if (op == &refchain) |
1947 | | return res; |
1948 | | } |
1949 | | if (PyList_Append(res, op) < 0) { |
1950 | | Py_DECREF(res); |
1951 | | return NULL; |
1952 | | } |
1953 | | op = op->_ob_next; |
1954 | | } |
1955 | | return res; |
1956 | | } |
1957 | | |
1958 | | #endif |
1959 | | |
1960 | | |
1961 | | /* Hack to force loading of abstract.o */ |
1962 | | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
1963 | | |
1964 | | |
1965 | | void |
1966 | | _PyObject_DebugTypeStats(FILE *out) |
1967 | 0 | { |
1968 | 0 | _PyCFunction_DebugMallocStats(out); |
1969 | 0 | _PyDict_DebugMallocStats(out); |
1970 | 0 | _PyFloat_DebugMallocStats(out); |
1971 | 0 | _PyFrame_DebugMallocStats(out); |
1972 | 0 | _PyList_DebugMallocStats(out); |
1973 | 0 | _PyMethod_DebugMallocStats(out); |
1974 | 0 | _PyTuple_DebugMallocStats(out); |
1975 | 0 | } |
1976 | | |
1977 | | /* These methods are used to control infinite recursion in repr, str, print, |
1978 | | etc. Container objects that may recursively contain themselves, |
1979 | | e.g. builtin dictionaries and lists, should use Py_ReprEnter() and |
1980 | | Py_ReprLeave() to avoid infinite recursion. |
1981 | | |
1982 | | Py_ReprEnter() returns 0 the first time it is called for a particular |
1983 | | object and 1 every time thereafter. It returns -1 if an exception |
1984 | | occurred. Py_ReprLeave() has no return value. |
1985 | | |
1986 | | See dictobject.c and listobject.c for examples of use. |
1987 | | */ |
1988 | | |
1989 | | int |
1990 | | Py_ReprEnter(PyObject *obj) |
1991 | 2 | { |
1992 | 2 | PyObject *dict; |
1993 | 2 | PyObject *list; |
1994 | 2 | Py_ssize_t i; |
1995 | | |
1996 | 2 | dict = PyThreadState_GetDict(); |
1997 | | /* Ignore a missing thread-state, so that this function can be called |
1998 | | early on startup. */ |
1999 | 2 | if (dict == NULL) |
2000 | 0 | return 0; |
2001 | 2 | list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); |
2002 | 2 | if (list == NULL) { |
2003 | 1 | if (PyErr_Occurred()) { |
2004 | 0 | return -1; |
2005 | 0 | } |
2006 | 1 | list = PyList_New(0); |
2007 | 1 | if (list == NULL) |
2008 | 0 | return -1; |
2009 | 1 | if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0) |
2010 | 0 | return -1; |
2011 | 1 | Py_DECREF(list); |
2012 | 1 | } |
2013 | 2 | i = PyList_GET_SIZE(list); |
2014 | 2 | while (--i >= 0) { |
2015 | 0 | if (PyList_GET_ITEM(list, i) == obj) |
2016 | 0 | return 1; |
2017 | 0 | } |
2018 | 2 | if (PyList_Append(list, obj) < 0) |
2019 | 0 | return -1; |
2020 | 2 | return 0; |
2021 | 2 | } |
2022 | | |
2023 | | void |
2024 | | Py_ReprLeave(PyObject *obj) |
2025 | 2 | { |
2026 | 2 | PyObject *dict; |
2027 | 2 | PyObject *list; |
2028 | 2 | Py_ssize_t i; |
2029 | 2 | PyObject *error_type, *error_value, *error_traceback; |
2030 | | |
2031 | 2 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
2032 | | |
2033 | 2 | dict = PyThreadState_GetDict(); |
2034 | 2 | if (dict == NULL) |
2035 | 0 | goto finally; |
2036 | | |
2037 | 2 | list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); |
2038 | 2 | if (list == NULL || !PyList_Check(list)) |
2039 | 0 | goto finally; |
2040 | | |
2041 | 2 | i = PyList_GET_SIZE(list); |
2042 | | /* Count backwards because we always expect obj to be list[-1] */ |
2043 | 2 | while (--i >= 0) { |
2044 | 2 | if (PyList_GET_ITEM(list, i) == obj) { |
2045 | 2 | PyList_SetSlice(list, i, i + 1, NULL); |
2046 | 2 | break; |
2047 | 2 | } |
2048 | 2 | } |
2049 | | |
2050 | 2 | finally: |
2051 | | /* ignore exceptions because there is no way to report them. */ |
2052 | 2 | PyErr_Restore(error_type, error_value, error_traceback); |
2053 | 2 | } |
2054 | | |
2055 | | /* Trashcan support. */ |
2056 | | |
2057 | | /* Add op to the _PyTrash_delete_later list. Called when the current |
2058 | | * call-stack depth gets large. op must be a currently untracked gc'ed |
2059 | | * object, with refcount 0. Py_DECREF must already have been called on it. |
2060 | | */ |
2061 | | void |
2062 | | _PyTrash_deposit_object(PyObject *op) |
2063 | 0 | { |
2064 | 0 | _PyObject_ASSERT(op, PyObject_IS_GC(op)); |
2065 | 0 | _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op)); |
2066 | 0 | _PyObject_ASSERT(op, op->ob_refcnt == 0); |
2067 | 0 | _PyGCHead_SET_PREV(_Py_AS_GC(op), _PyRuntime.gc.trash_delete_later); |
2068 | 0 | _PyRuntime.gc.trash_delete_later = op; |
2069 | 0 | } |
2070 | | |
2071 | | /* The equivalent API, using per-thread state recursion info */ |
2072 | | void |
2073 | | _PyTrash_thread_deposit_object(PyObject *op) |
2074 | 0 | { |
2075 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2076 | 0 | _PyObject_ASSERT(op, PyObject_IS_GC(op)); |
2077 | 0 | _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op)); |
2078 | 0 | _PyObject_ASSERT(op, op->ob_refcnt == 0); |
2079 | 0 | _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later); |
2080 | 0 | tstate->trash_delete_later = op; |
2081 | 0 | } |
2082 | | |
2083 | | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
2084 | | * the call-stack unwinds again. |
2085 | | */ |
2086 | | void |
2087 | | _PyTrash_destroy_chain(void) |
2088 | 0 | { |
2089 | 0 | while (_PyRuntime.gc.trash_delete_later) { |
2090 | 0 | PyObject *op = _PyRuntime.gc.trash_delete_later; |
2091 | 0 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
2092 | |
|
2093 | 0 | _PyRuntime.gc.trash_delete_later = |
2094 | 0 | (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op)); |
2095 | | |
2096 | | /* Call the deallocator directly. This used to try to |
2097 | | * fool Py_DECREF into calling it indirectly, but |
2098 | | * Py_DECREF was already called on this object, and in |
2099 | | * assorted non-release builds calling Py_DECREF again ends |
2100 | | * up distorting allocation statistics. |
2101 | | */ |
2102 | 0 | _PyObject_ASSERT(op, op->ob_refcnt == 0); |
2103 | 0 | ++_PyRuntime.gc.trash_delete_nesting; |
2104 | 0 | (*dealloc)(op); |
2105 | 0 | --_PyRuntime.gc.trash_delete_nesting; |
2106 | 0 | } |
2107 | 0 | } |
2108 | | |
2109 | | /* The equivalent API, using per-thread state recursion info */ |
2110 | | void |
2111 | | _PyTrash_thread_destroy_chain(void) |
2112 | 0 | { |
2113 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2114 | | /* We need to increase trash_delete_nesting here, otherwise, |
2115 | | _PyTrash_thread_destroy_chain will be called recursively |
2116 | | and then possibly crash. An example that may crash without |
2117 | | increase: |
2118 | | N = 500000 # need to be large enough |
2119 | | ob = object() |
2120 | | tups = [(ob,) for i in range(N)] |
2121 | | for i in range(49): |
2122 | | tups = [(tup,) for tup in tups] |
2123 | | del tups |
2124 | | */ |
2125 | 0 | assert(tstate->trash_delete_nesting == 0); |
2126 | 0 | ++tstate->trash_delete_nesting; |
2127 | 0 | while (tstate->trash_delete_later) { |
2128 | 0 | PyObject *op = tstate->trash_delete_later; |
2129 | 0 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
2130 | |
|
2131 | 0 | tstate->trash_delete_later = |
2132 | 0 | (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op)); |
2133 | | |
2134 | | /* Call the deallocator directly. This used to try to |
2135 | | * fool Py_DECREF into calling it indirectly, but |
2136 | | * Py_DECREF was already called on this object, and in |
2137 | | * assorted non-release builds calling Py_DECREF again ends |
2138 | | * up distorting allocation statistics. |
2139 | | */ |
2140 | 0 | _PyObject_ASSERT(op, op->ob_refcnt == 0); |
2141 | 0 | (*dealloc)(op); |
2142 | 0 | assert(tstate->trash_delete_nesting == 1); |
2143 | 0 | } |
2144 | 0 | --tstate->trash_delete_nesting; |
2145 | 0 | } |
2146 | | |
2147 | | |
2148 | | void |
2149 | | _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, |
2150 | | const char *file, int line, const char *function) |
2151 | 0 | { |
2152 | 0 | fprintf(stderr, "%s:%d: ", file, line); |
2153 | 0 | if (function) { |
2154 | 0 | fprintf(stderr, "%s: ", function); |
2155 | 0 | } |
2156 | 0 | fflush(stderr); |
2157 | |
|
2158 | 0 | if (expr) { |
2159 | 0 | fprintf(stderr, "Assertion \"%s\" failed", expr); |
2160 | 0 | } |
2161 | 0 | else { |
2162 | 0 | fprintf(stderr, "Assertion failed"); |
2163 | 0 | } |
2164 | 0 | fflush(stderr); |
2165 | |
|
2166 | 0 | if (msg) { |
2167 | 0 | fprintf(stderr, ": %s", msg); |
2168 | 0 | } |
2169 | 0 | fprintf(stderr, "\n"); |
2170 | 0 | fflush(stderr); |
2171 | |
|
2172 | 0 | if (_PyObject_IsFreed(obj)) { |
2173 | | /* It seems like the object memory has been freed: |
2174 | | don't access it to prevent a segmentation fault. */ |
2175 | 0 | fprintf(stderr, "<object at %p is freed>\n", obj); |
2176 | 0 | fflush(stderr); |
2177 | 0 | } |
2178 | 0 | else { |
2179 | | /* Display the traceback where the object has been allocated. |
2180 | | Do it before dumping repr(obj), since repr() is more likely |
2181 | | to crash than dumping the traceback. */ |
2182 | 0 | void *ptr; |
2183 | 0 | PyTypeObject *type = Py_TYPE(obj); |
2184 | 0 | if (PyType_IS_GC(type)) { |
2185 | 0 | ptr = (void *)((char *)obj - sizeof(PyGC_Head)); |
2186 | 0 | } |
2187 | 0 | else { |
2188 | 0 | ptr = (void *)obj; |
2189 | 0 | } |
2190 | 0 | _PyMem_DumpTraceback(fileno(stderr), ptr); |
2191 | | |
2192 | | /* This might succeed or fail, but we're about to abort, so at least |
2193 | | try to provide any extra info we can: */ |
2194 | 0 | _PyObject_Dump(obj); |
2195 | |
|
2196 | 0 | fprintf(stderr, "\n"); |
2197 | 0 | fflush(stderr); |
2198 | 0 | } |
2199 | |
|
2200 | 0 | Py_FatalError("_PyObject_AssertFailed"); |
2201 | 0 | } |
2202 | | |
2203 | | |
2204 | | #undef _Py_Dealloc |
2205 | | |
2206 | | void |
2207 | | _Py_Dealloc(PyObject *op) |
2208 | 536k | { |
2209 | 536k | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
2210 | | #ifdef Py_TRACE_REFS |
2211 | | _Py_ForgetReference(op); |
2212 | | #else |
2213 | 536k | _Py_INC_TPFREES(op); |
2214 | 536k | #endif |
2215 | 536k | (*dealloc)(op); |
2216 | 536k | } |
2217 | | |
2218 | | #ifdef __cplusplus |
2219 | | } |
2220 | | #endif |