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