1
2 /* Generic object operations; and implementation of None (NoObject) */
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
_Py_GetRefTotal(void)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 int Py_DivisionWarningFlag;
33 int Py_Py3kWarningFlag;
34
35 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
36 These are used by the individual routines for object creation.
37 Do not call them otherwise, they do not initialize the object! */
38
39 #ifdef Py_TRACE_REFS
40 /* Head of circular doubly-linked list of all objects. These are linked
41 * together via the _ob_prev and _ob_next members of a PyObject, which
42 * exist only in a Py_TRACE_REFS build.
43 */
44 static PyObject refchain = {&refchain, &refchain};
45
46 /* Insert op at the front of the list of all objects. If force is true,
47 * op is added even if _ob_prev and _ob_next are non-NULL already. If
48 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
49 * force should be true if and only if op points to freshly allocated,
50 * uninitialized memory, or you've unlinked op from the list and are
51 * relinking it into the front.
52 * Note that objects are normally added to the list via _Py_NewReference,
53 * which is called by PyObject_Init. Not all objects are initialized that
54 * way, though; exceptions include statically allocated type objects, and
55 * statically allocated singletons (like Py_True and Py_None).
56 */
57 void
_Py_AddToAllObjects(PyObject * op,int force)58 _Py_AddToAllObjects(PyObject *op, int force)
59 {
60 #ifdef Py_DEBUG
61 if (!force) {
62 /* If it's initialized memory, op must be in or out of
63 * the list unambiguously.
64 */
65 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
66 }
67 #endif
68 if (force || op->_ob_prev == NULL) {
69 op->_ob_next = refchain._ob_next;
70 op->_ob_prev = &refchain;
71 refchain._ob_next->_ob_prev = op;
72 refchain._ob_next = op;
73 }
74 }
75 #endif /* Py_TRACE_REFS */
76
77 #ifdef COUNT_ALLOCS
78 static PyTypeObject *type_list;
79 /* All types are added to type_list, at least when
80 they get one object created. That makes them
81 immortal, which unfortunately contributes to
82 garbage itself. If unlist_types_without_objects
83 is set, they will be removed from the type_list
84 once the last object is deallocated. */
85 static int unlist_types_without_objects;
86 extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
87 extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
88 extern Py_ssize_t null_strings, one_strings;
89 void
dump_counts(FILE * f)90 dump_counts(FILE* f)
91 {
92 PyTypeObject *tp;
93
94 for (tp = type_list; tp; tp = tp->tp_next)
95 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
96 "freed: %" PY_FORMAT_SIZE_T "d, "
97 "max in use: %" PY_FORMAT_SIZE_T "d\n",
98 tp->tp_name, tp->tp_allocs, tp->tp_frees,
99 tp->tp_maxalloc);
100 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
101 "empty: %" PY_FORMAT_SIZE_T "d\n",
102 fast_tuple_allocs, tuple_zero_allocs);
103 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
104 "neg: %" PY_FORMAT_SIZE_T "d\n",
105 quick_int_allocs, quick_neg_int_allocs);
106 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
107 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
108 null_strings, one_strings);
109 }
110
111 PyObject *
get_counts(void)112 get_counts(void)
113 {
114 PyTypeObject *tp;
115 PyObject *result;
116 PyObject *v;
117
118 result = PyList_New(0);
119 if (result == NULL)
120 return NULL;
121 for (tp = type_list; tp; tp = tp->tp_next) {
122 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
123 tp->tp_frees, tp->tp_maxalloc);
124 if (v == NULL) {
125 Py_DECREF(result);
126 return NULL;
127 }
128 if (PyList_Append(result, v) < 0) {
129 Py_DECREF(v);
130 Py_DECREF(result);
131 return NULL;
132 }
133 Py_DECREF(v);
134 }
135 return result;
136 }
137
138 void
inc_count(PyTypeObject * tp)139 inc_count(PyTypeObject *tp)
140 {
141 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
142 /* first time; insert in linked list */
143 if (tp->tp_next != NULL) /* sanity check */
144 Py_FatalError("XXX inc_count sanity check");
145 if (type_list)
146 type_list->tp_prev = tp;
147 tp->tp_next = type_list;
148 /* Note that as of Python 2.2, heap-allocated type objects
149 * can go away, but this code requires that they stay alive
150 * until program exit. That's why we're careful with
151 * refcounts here. type_list gets a new reference to tp,
152 * while ownership of the reference type_list used to hold
153 * (if any) was transferred to tp->tp_next in the line above.
154 * tp is thus effectively immortal after this.
155 */
156 Py_INCREF(tp);
157 type_list = tp;
158 #ifdef Py_TRACE_REFS
159 /* Also insert in the doubly-linked list of all objects,
160 * if not already there.
161 */
162 _Py_AddToAllObjects((PyObject *)tp, 0);
163 #endif
164 }
165 tp->tp_allocs++;
166 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
167 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
168 }
169
dec_count(PyTypeObject * tp)170 void dec_count(PyTypeObject *tp)
171 {
172 tp->tp_frees++;
173 if (unlist_types_without_objects &&
174 tp->tp_allocs == tp->tp_frees) {
175 /* unlink the type from type_list */
176 if (tp->tp_prev)
177 tp->tp_prev->tp_next = tp->tp_next;
178 else
179 type_list = tp->tp_next;
180 if (tp->tp_next)
181 tp->tp_next->tp_prev = tp->tp_prev;
182 tp->tp_next = tp->tp_prev = NULL;
183 Py_DECREF(tp);
184 }
185 }
186
187 #endif
188
189 #ifdef Py_REF_DEBUG
190 /* Log a fatal error; doesn't return. */
191 void
_Py_NegativeRefcount(const char * fname,int lineno,PyObject * op)192 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
193 {
194 char buf[300];
195
196 PyOS_snprintf(buf, sizeof(buf),
197 "%s:%i object at %p has negative ref count "
198 "%" PY_FORMAT_SIZE_T "d",
199 fname, lineno, op, op->ob_refcnt);
200 Py_FatalError(buf);
201 }
202
203 #endif /* Py_REF_DEBUG */
204
205 void
Py_IncRef(PyObject * o)206 Py_IncRef(PyObject *o)
207 {
208 Py_XINCREF(o);
209 }
210
211 void
Py_DecRef(PyObject * o)212 Py_DecRef(PyObject *o)
213 {
214 Py_XDECREF(o);
215 }
216
217 PyObject *
PyObject_Init(PyObject * op,PyTypeObject * tp)218 PyObject_Init(PyObject *op, PyTypeObject *tp)
219 {
220 if (op == NULL)
221 return PyErr_NoMemory();
222 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
223 Py_TYPE(op) = tp;
224 _Py_NewReference(op);
225 return op;
226 }
227
228 PyVarObject *
PyObject_InitVar(PyVarObject * op,PyTypeObject * tp,Py_ssize_t size)229 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
230 {
231 if (op == NULL)
232 return (PyVarObject *) PyErr_NoMemory();
233 /* Any changes should be reflected in PyObject_INIT_VAR */
234 op->ob_size = size;
235 Py_TYPE(op) = tp;
236 _Py_NewReference((PyObject *)op);
237 return op;
238 }
239
240 PyObject *
_PyObject_New(PyTypeObject * tp)241 _PyObject_New(PyTypeObject *tp)
242 {
243 PyObject *op;
244 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
245 if (op == NULL)
246 return PyErr_NoMemory();
247 return PyObject_INIT(op, tp);
248 }
249
250 PyVarObject *
_PyObject_NewVar(PyTypeObject * tp,Py_ssize_t nitems)251 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
252 {
253 PyVarObject *op;
254 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
255 op = (PyVarObject *) PyObject_MALLOC(size);
256 if (op == NULL)
257 return (PyVarObject *)PyErr_NoMemory();
258 return PyObject_INIT_VAR(op, tp, nitems);
259 }
260
261 /* for binary compatibility with 2.2 */
262 #undef _PyObject_Del
263 void
_PyObject_Del(PyObject * op)264 _PyObject_Del(PyObject *op)
265 {
266 PyObject_FREE(op);
267 }
268
269 /* Implementation of PyObject_Print with recursion checking */
270 static int
internal_print(PyObject * op,FILE * fp,int flags,int nesting)271 internal_print(PyObject *op, FILE *fp, int flags, int nesting)
272 {
273 int ret = 0;
274 if (nesting > 10) {
275 PyErr_SetString(PyExc_RuntimeError, "print recursion");
276 return -1;
277 }
278 if (PyErr_CheckSignals())
279 return -1;
280 #ifdef USE_STACKCHECK
281 if (PyOS_CheckStack()) {
282 PyErr_SetString(PyExc_MemoryError, "stack overflow");
283 return -1;
284 }
285 #endif
286 clearerr(fp); /* Clear any previous error condition */
287 if (op == NULL) {
288 Py_BEGIN_ALLOW_THREADS
289 fprintf(fp, "<nil>");
290 Py_END_ALLOW_THREADS
291 }
292 else {
293 if (op->ob_refcnt <= 0)
294 /* XXX(twouters) cast refcount to long until %zd is
295 universally available */
296 Py_BEGIN_ALLOW_THREADS
297 fprintf(fp, "<refcnt %ld at %p>",
298 (long)op->ob_refcnt, op);
299 Py_END_ALLOW_THREADS
300 else if (Py_TYPE(op)->tp_print == NULL) {
301 PyObject *s;
302 if (flags & Py_PRINT_RAW)
303 s = PyObject_Str(op);
304 else
305 s = PyObject_Repr(op);
306 if (s == NULL)
307 ret = -1;
308 else {
309 ret = internal_print(s, fp, Py_PRINT_RAW,
310 nesting+1);
311 }
312 Py_XDECREF(s);
313 }
314 else
315 ret = (*Py_TYPE(op)->tp_print)(op, fp, flags);
316 }
317 if (ret == 0) {
318 if (ferror(fp)) {
319 PyErr_SetFromErrno(PyExc_IOError);
320 clearerr(fp);
321 ret = -1;
322 }
323 }
324 return ret;
325 }
326
327 int
PyObject_Print(PyObject * op,FILE * fp,int flags)328 PyObject_Print(PyObject *op, FILE *fp, int flags)
329 {
330 return internal_print(op, fp, flags, 0);
331 }
332
333
334 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
_PyObject_Dump(PyObject * op)335 void _PyObject_Dump(PyObject* op)
336 {
337 if (op == NULL)
338 fprintf(stderr, "NULL\n");
339 else {
340 #ifdef WITH_THREAD
341 PyGILState_STATE gil;
342 #endif
343 fprintf(stderr, "object : ");
344 #ifdef WITH_THREAD
345 gil = PyGILState_Ensure();
346 #endif
347 (void)PyObject_Print(op, stderr, 0);
348 #ifdef WITH_THREAD
349 PyGILState_Release(gil);
350 #endif
351 /* XXX(twouters) cast refcount to long until %zd is
352 universally available */
353 fprintf(stderr, "\n"
354 "type : %s\n"
355 "refcount: %ld\n"
356 "address : %p\n",
357 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
358 (long)op->ob_refcnt,
359 op);
360 }
361 }
362
363 PyObject *
PyObject_Repr(PyObject * v)364 PyObject_Repr(PyObject *v)
365 {
366 if (PyErr_CheckSignals())
367 return NULL;
368 #ifdef USE_STACKCHECK
369 if (PyOS_CheckStack()) {
370 PyErr_SetString(PyExc_MemoryError, "stack overflow");
371 return NULL;
372 }
373 #endif
374 if (v == NULL)
375 return PyString_FromString("<NULL>");
376 else if (Py_TYPE(v)->tp_repr == NULL)
377 return PyString_FromFormat("<%s object at %p>",
378 Py_TYPE(v)->tp_name, v);
379 else {
380 PyObject *res;
381 /* It is possible for a type to have a tp_repr representation that
382 loops infinitely. */
383 if (Py_EnterRecursiveCall(" while getting the repr of an object"))
384 return NULL;
385 res = (*Py_TYPE(v)->tp_repr)(v);
386 Py_LeaveRecursiveCall();
387 if (res == NULL)
388 return NULL;
389 #ifdef Py_USING_UNICODE
390 if (PyUnicode_Check(res)) {
391 PyObject* str;
392 str = PyUnicode_AsEncodedString(res, NULL, NULL);
393 Py_DECREF(res);
394 if (str)
395 res = str;
396 else
397 return NULL;
398 }
399 #endif
400 if (!PyString_Check(res)) {
401 PyErr_Format(PyExc_TypeError,
402 "__repr__ returned non-string (type %.200s)",
403 Py_TYPE(res)->tp_name);
404 Py_DECREF(res);
405 return NULL;
406 }
407 return res;
408 }
409 }
410
411 PyObject *
_PyObject_Str(PyObject * v)412 _PyObject_Str(PyObject *v)
413 {
414 PyObject *res;
415 int type_ok;
416 if (v == NULL)
417 return PyString_FromString("<NULL>");
418 if (PyString_CheckExact(v)) {
419 Py_INCREF(v);
420 return v;
421 }
422 #ifdef Py_USING_UNICODE
423 if (PyUnicode_CheckExact(v)) {
424 Py_INCREF(v);
425 return v;
426 }
427 #endif
428 if (Py_TYPE(v)->tp_str == NULL)
429 return PyObject_Repr(v);
430
431 /* It is possible for a type to have a tp_str representation that loops
432 infinitely. */
433 if (Py_EnterRecursiveCall(" while getting the str of an object"))
434 return NULL;
435 res = (*Py_TYPE(v)->tp_str)(v);
436 Py_LeaveRecursiveCall();
437 if (res == NULL)
438 return NULL;
439 type_ok = PyString_Check(res);
440 #ifdef Py_USING_UNICODE
441 type_ok = type_ok || PyUnicode_Check(res);
442 #endif
443 if (!type_ok) {
444 PyErr_Format(PyExc_TypeError,
445 "__str__ returned non-string (type %.200s)",
446 Py_TYPE(res)->tp_name);
447 Py_DECREF(res);
448 return NULL;
449 }
450 return res;
451 }
452
453 PyObject *
PyObject_Str(PyObject * v)454 PyObject_Str(PyObject *v)
455 {
456 PyObject *res = _PyObject_Str(v);
457 if (res == NULL)
458 return NULL;
459 #ifdef Py_USING_UNICODE
460 if (PyUnicode_Check(res)) {
461 PyObject* str;
462 str = PyUnicode_AsEncodedString(res, NULL, NULL);
463 Py_DECREF(res);
464 if (str)
465 res = str;
466 else
467 return NULL;
468 }
469 #endif
470 assert(PyString_Check(res));
471 return res;
472 }
473
474 #ifdef Py_USING_UNICODE
475 PyObject *
PyObject_Unicode(PyObject * v)476 PyObject_Unicode(PyObject *v)
477 {
478 PyObject *res;
479 PyObject *func;
480 PyObject *str;
481 int unicode_method_found = 0;
482 static PyObject *unicodestr = NULL;
483
484 if (v == NULL) {
485 res = PyString_FromString("<NULL>");
486 if (res == NULL)
487 return NULL;
488 str = PyUnicode_FromEncodedObject(res, NULL, "strict");
489 Py_DECREF(res);
490 return str;
491 } else if (PyUnicode_CheckExact(v)) {
492 Py_INCREF(v);
493 return v;
494 }
495
496 if (PyInstance_Check(v)) {
497 /* We're an instance of a classic class */
498 /* Try __unicode__ from the instance -- alas we have no type */
499 if (!unicodestr) {
500 unicodestr = PyString_InternFromString("__unicode__");
501 if (!unicodestr)
502 return NULL;
503 }
504 func = PyObject_GetAttr(v, unicodestr);
505 if (func != NULL) {
506 unicode_method_found = 1;
507 res = PyObject_CallFunctionObjArgs(func, NULL);
508 Py_DECREF(func);
509 }
510 else {
511 PyErr_Clear();
512 }
513 }
514 else {
515 /* Not a classic class instance, try __unicode__. */
516 func = _PyObject_LookupSpecial(v, "__unicode__", &unicodestr);
517 if (func != NULL) {
518 unicode_method_found = 1;
519 res = PyObject_CallFunctionObjArgs(func, NULL);
520 Py_DECREF(func);
521 }
522 else if (PyErr_Occurred())
523 return NULL;
524 }
525
526 /* Didn't find __unicode__ */
527 if (!unicode_method_found) {
528 if (PyUnicode_Check(v)) {
529 /* For a Unicode subtype that's didn't overwrite __unicode__,
530 return a true Unicode object with the same data. */
531 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
532 PyUnicode_GET_SIZE(v));
533 }
534 if (PyString_CheckExact(v)) {
535 Py_INCREF(v);
536 res = v;
537 }
538 else {
539 if (Py_TYPE(v)->tp_str != NULL)
540 res = (*Py_TYPE(v)->tp_str)(v);
541 else
542 res = PyObject_Repr(v);
543 }
544 }
545
546 if (res == NULL)
547 return NULL;
548 if (!PyUnicode_Check(res)) {
549 str = PyUnicode_FromEncodedObject(res, NULL, "strict");
550 Py_DECREF(res);
551 res = str;
552 }
553 return res;
554 }
555 #endif
556
557
558 /* Helper to warn about deprecated tp_compare return values. Return:
559 -2 for an exception;
560 -1 if v < w;
561 0 if v == w;
562 1 if v > w.
563 (This function cannot return 2.)
564 */
565 static int
adjust_tp_compare(int c)566 adjust_tp_compare(int c)
567 {
568 if (PyErr_Occurred()) {
569 if (c != -1 && c != -2) {
570 PyObject *t, *v, *tb;
571 PyErr_Fetch(&t, &v, &tb);
572 if (PyErr_Warn(PyExc_RuntimeWarning,
573 "tp_compare didn't return -1 or -2 "
574 "for exception") < 0) {
575 Py_XDECREF(t);
576 Py_XDECREF(v);
577 Py_XDECREF(tb);
578 }
579 else
580 PyErr_Restore(t, v, tb);
581 }
582 return -2;
583 }
584 else if (c < -1 || c > 1) {
585 if (PyErr_Warn(PyExc_RuntimeWarning,
586 "tp_compare didn't return -1, 0 or 1") < 0)
587 return -2;
588 else
589 return c < -1 ? -1 : 1;
590 }
591 else {
592 assert(c >= -1 && c <= 1);
593 return c;
594 }
595 }
596
597
598 /* Macro to get the tp_richcompare field of a type if defined */
599 #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
600 ? (t)->tp_richcompare : NULL)
601
602 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
603 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
604
605 /* Try a genuine rich comparison, returning an object. Return:
606 NULL for exception;
607 NotImplemented if this particular rich comparison is not implemented or
608 undefined;
609 some object not equal to NotImplemented if it is implemented
610 (this latter object may not be a Boolean).
611 */
612 static PyObject *
try_rich_compare(PyObject * v,PyObject * w,int op)613 try_rich_compare(PyObject *v, PyObject *w, int op)
614 {
615 richcmpfunc f;
616 PyObject *res;
617
618 if (v->ob_type != w->ob_type &&
619 PyType_IsSubtype(w->ob_type, v->ob_type) &&
620 (f = RICHCOMPARE(w->ob_type)) != NULL) {
621 res = (*f)(w, v, _Py_SwappedOp[op]);
622 if (res != Py_NotImplemented)
623 return res;
624 Py_DECREF(res);
625 }
626 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
627 res = (*f)(v, w, op);
628 if (res != Py_NotImplemented)
629 return res;
630 Py_DECREF(res);
631 }
632 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
633 return (*f)(w, v, _Py_SwappedOp[op]);
634 }
635 res = Py_NotImplemented;
636 Py_INCREF(res);
637 return res;
638 }
639
640 /* Try a genuine rich comparison, returning an int. Return:
641 -1 for exception (including the case where try_rich_compare() returns an
642 object that's not a Boolean);
643 0 if the outcome is false;
644 1 if the outcome is true;
645 2 if this particular rich comparison is not implemented or undefined.
646 */
647 static int
try_rich_compare_bool(PyObject * v,PyObject * w,int op)648 try_rich_compare_bool(PyObject *v, PyObject *w, int op)
649 {
650 PyObject *res;
651 int ok;
652
653 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
654 return 2; /* Shortcut, avoid INCREF+DECREF */
655 res = try_rich_compare(v, w, op);
656 if (res == NULL)
657 return -1;
658 if (res == Py_NotImplemented) {
659 Py_DECREF(res);
660 return 2;
661 }
662 ok = PyObject_IsTrue(res);
663 Py_DECREF(res);
664 return ok;
665 }
666
667 /* Try rich comparisons to determine a 3-way comparison. Return:
668 -2 for an exception;
669 -1 if v < w;
670 0 if v == w;
671 1 if v > w;
672 2 if this particular rich comparison is not implemented or undefined.
673 */
674 static int
try_rich_to_3way_compare(PyObject * v,PyObject * w)675 try_rich_to_3way_compare(PyObject *v, PyObject *w)
676 {
677 static struct { int op; int outcome; } tries[3] = {
678 /* Try this operator, and if it is true, use this outcome: */
679 {Py_EQ, 0},
680 {Py_LT, -1},
681 {Py_GT, 1},
682 };
683 int i;
684
685 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
686 return 2; /* Shortcut */
687
688 for (i = 0; i < 3; i++) {
689 switch (try_rich_compare_bool(v, w, tries[i].op)) {
690 case -1:
691 return -2;
692 case 1:
693 return tries[i].outcome;
694 }
695 }
696
697 return 2;
698 }
699
700 /* Try a 3-way comparison, returning an int. Return:
701 -2 for an exception;
702 -1 if v < w;
703 0 if v == w;
704 1 if v > w;
705 2 if this particular 3-way comparison is not implemented or undefined.
706 */
707 static int
try_3way_compare(PyObject * v,PyObject * w)708 try_3way_compare(PyObject *v, PyObject *w)
709 {
710 int c;
711 cmpfunc f;
712
713 /* Comparisons involving instances are given to instance_compare,
714 which has the same return conventions as this function. */
715
716 f = v->ob_type->tp_compare;
717 if (PyInstance_Check(v))
718 return (*f)(v, w);
719 if (PyInstance_Check(w))
720 return (*w->ob_type->tp_compare)(v, w);
721
722 /* If both have the same (non-NULL) tp_compare, use it. */
723 if (f != NULL && f == w->ob_type->tp_compare) {
724 c = (*f)(v, w);
725 return adjust_tp_compare(c);
726 }
727
728 /* If either tp_compare is _PyObject_SlotCompare, that's safe. */
729 if (f == _PyObject_SlotCompare ||
730 w->ob_type->tp_compare == _PyObject_SlotCompare)
731 return _PyObject_SlotCompare(v, w);
732
733 /* If we're here, v and w,
734 a) are not instances;
735 b) have different types or a type without tp_compare; and
736 c) don't have a user-defined tp_compare.
737 tp_compare implementations in C assume that both arguments
738 have their type, so we give up if the coercion fails or if
739 it yields types which are still incompatible (which can
740 happen with a user-defined nb_coerce).
741 */
742 c = PyNumber_CoerceEx(&v, &w);
743 if (c < 0)
744 return -2;
745 if (c > 0)
746 return 2;
747 f = v->ob_type->tp_compare;
748 if (f != NULL && f == w->ob_type->tp_compare) {
749 c = (*f)(v, w);
750 Py_DECREF(v);
751 Py_DECREF(w);
752 return adjust_tp_compare(c);
753 }
754
755 /* No comparison defined */
756 Py_DECREF(v);
757 Py_DECREF(w);
758 return 2;
759 }
760
761 /* Final fallback 3-way comparison, returning an int. Return:
762 -2 if an error occurred;
763 -1 if v < w;
764 0 if v == w;
765 1 if v > w.
766 */
767 static int
default_3way_compare(PyObject * v,PyObject * w)768 default_3way_compare(PyObject *v, PyObject *w)
769 {
770 int c;
771 const char *vname, *wname;
772
773 if (v->ob_type == w->ob_type) {
774 /* When comparing these pointers, they must be cast to
775 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
776 * uintptr_t). ANSI specifies that pointer compares other
777 * than == and != to non-related structures are undefined.
778 */
779 Py_uintptr_t vv = (Py_uintptr_t)v;
780 Py_uintptr_t ww = (Py_uintptr_t)w;
781 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
782 }
783
784 /* None is smaller than anything */
785 if (v == Py_None)
786 return -1;
787 if (w == Py_None)
788 return 1;
789
790 /* different type: compare type names; numbers are smaller */
791 if (PyNumber_Check(v))
792 vname = "";
793 else
794 vname = v->ob_type->tp_name;
795 if (PyNumber_Check(w))
796 wname = "";
797 else
798 wname = w->ob_type->tp_name;
799 c = strcmp(vname, wname);
800 if (c < 0)
801 return -1;
802 if (c > 0)
803 return 1;
804 /* Same type name, or (more likely) incomparable numeric types */
805 return ((Py_uintptr_t)(v->ob_type) < (
806 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
807 }
808
809 /* Do a 3-way comparison, by hook or by crook. Return:
810 -2 for an exception (but see below);
811 -1 if v < w;
812 0 if v == w;
813 1 if v > w;
814 BUT: if the object implements a tp_compare function, it returns
815 whatever this function returns (whether with an exception or not).
816 */
817 static int
do_cmp(PyObject * v,PyObject * w)818 do_cmp(PyObject *v, PyObject *w)
819 {
820 int c;
821 cmpfunc f;
822
823 if (v->ob_type == w->ob_type
824 && (f = v->ob_type->tp_compare) != NULL) {
825 c = (*f)(v, w);
826 if (PyInstance_Check(v)) {
827 /* Instance tp_compare has a different signature.
828 But if it returns undefined we fall through. */
829 if (c != 2)
830 return c;
831 /* Else fall through to try_rich_to_3way_compare() */
832 }
833 else
834 return adjust_tp_compare(c);
835 }
836 /* We only get here if one of the following is true:
837 a) v and w have different types
838 b) v and w have the same type, which doesn't have tp_compare
839 c) v and w are instances, and either __cmp__ is not defined or
840 __cmp__ returns NotImplemented
841 */
842 c = try_rich_to_3way_compare(v, w);
843 if (c < 2)
844 return c;
845 c = try_3way_compare(v, w);
846 if (c < 2)
847 return c;
848 return default_3way_compare(v, w);
849 }
850
851 /* Compare v to w. Return
852 -1 if v < w or exception (PyErr_Occurred() true in latter case).
853 0 if v == w.
854 1 if v > w.
855 XXX The docs (C API manual) say the return value is undefined in case
856 XXX of error.
857 */
858 int
PyObject_Compare(PyObject * v,PyObject * w)859 PyObject_Compare(PyObject *v, PyObject *w)
860 {
861 int result;
862
863 if (v == NULL || w == NULL) {
864 PyErr_BadInternalCall();
865 return -1;
866 }
867 if (v == w)
868 return 0;
869 if (Py_EnterRecursiveCall(" in cmp"))
870 return -1;
871 result = do_cmp(v, w);
872 Py_LeaveRecursiveCall();
873 return result < 0 ? -1 : result;
874 }
875
876 /* Return (new reference to) Py_True or Py_False. */
877 static PyObject *
convert_3way_to_object(int op,int c)878 convert_3way_to_object(int op, int c)
879 {
880 PyObject *result;
881 switch (op) {
882 case Py_LT: c = c < 0; break;
883 case Py_LE: c = c <= 0; break;
884 case Py_EQ: c = c == 0; break;
885 case Py_NE: c = c != 0; break;
886 case Py_GT: c = c > 0; break;
887 case Py_GE: c = c >= 0; break;
888 }
889 result = c ? Py_True : Py_False;
890 Py_INCREF(result);
891 return result;
892 }
893
894 /* We want a rich comparison but don't have one. Try a 3-way cmp instead.
895 Return
896 NULL if error
897 Py_True if v op w
898 Py_False if not (v op w)
899 */
900 static PyObject *
try_3way_to_rich_compare(PyObject * v,PyObject * w,int op)901 try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
902 {
903 int c;
904
905 c = try_3way_compare(v, w);
906 if (c >= 2) {
907
908 /* Py3K warning if types are not equal and comparison isn't == or != */
909 if (Py_Py3kWarningFlag &&
910 v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE &&
911 PyErr_WarnEx(PyExc_DeprecationWarning,
912 "comparing unequal types not supported "
913 "in 3.x", 1) < 0) {
914 return NULL;
915 }
916
917 c = default_3way_compare(v, w);
918 }
919 if (c <= -2)
920 return NULL;
921 return convert_3way_to_object(op, c);
922 }
923
924 /* Do rich comparison on v and w. Return
925 NULL if error
926 Else a new reference to an object other than Py_NotImplemented, usually(?):
927 Py_True if v op w
928 Py_False if not (v op w)
929 */
930 static PyObject *
do_richcmp(PyObject * v,PyObject * w,int op)931 do_richcmp(PyObject *v, PyObject *w, int op)
932 {
933 PyObject *res;
934
935 res = try_rich_compare(v, w, op);
936 if (res != Py_NotImplemented)
937 return res;
938 Py_DECREF(res);
939
940 return try_3way_to_rich_compare(v, w, op);
941 }
942
943 /* Return:
944 NULL for exception;
945 some object not equal to NotImplemented if it is implemented
946 (this latter object may not be a Boolean).
947 */
948 PyObject *
PyObject_RichCompare(PyObject * v,PyObject * w,int op)949 PyObject_RichCompare(PyObject *v, PyObject *w, int op)
950 {
951 PyObject *res;
952
953 assert(Py_LT <= op && op <= Py_GE);
954 if (Py_EnterRecursiveCall(" in cmp"))
955 return NULL;
956
957 /* If the types are equal, and not old-style instances, try to
958 get out cheap (don't bother with coercions etc.). */
959 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
960 cmpfunc fcmp;
961 richcmpfunc frich = RICHCOMPARE(v->ob_type);
962 /* If the type has richcmp, try it first. try_rich_compare
963 tries it two-sided, which is not needed since we've a
964 single type only. */
965 if (frich != NULL) {
966 res = (*frich)(v, w, op);
967 if (res != Py_NotImplemented)
968 goto Done;
969 Py_DECREF(res);
970 }
971 /* No richcmp, or this particular richmp not implemented.
972 Try 3-way cmp. */
973 fcmp = v->ob_type->tp_compare;
974 if (fcmp != NULL) {
975 int c = (*fcmp)(v, w);
976 c = adjust_tp_compare(c);
977 if (c == -2) {
978 res = NULL;
979 goto Done;
980 }
981 res = convert_3way_to_object(op, c);
982 goto Done;
983 }
984 }
985
986 /* Fast path not taken, or couldn't deliver a useful result. */
987 res = do_richcmp(v, w, op);
988 Done:
989 Py_LeaveRecursiveCall();
990 return res;
991 }
992
993 /* Return -1 if error; 1 if v op w; 0 if not (v op w). */
994 int
PyObject_RichCompareBool(PyObject * v,PyObject * w,int op)995 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
996 {
997 PyObject *res;
998 int ok;
999
1000 /* Quick result when objects are the same.
1001 Guarantees that identity implies equality. */
1002 if (v == w) {
1003 if (op == Py_EQ)
1004 return 1;
1005 else if (op == Py_NE)
1006 return 0;
1007 }
1008
1009 res = PyObject_RichCompare(v, w, op);
1010 if (res == NULL)
1011 return -1;
1012 if (PyBool_Check(res))
1013 ok = (res == Py_True);
1014 else
1015 ok = PyObject_IsTrue(res);
1016 Py_DECREF(res);
1017 return ok;
1018 }
1019
1020 /* Set of hash utility functions to help maintaining the invariant that
1021 if a==b then hash(a)==hash(b)
1022
1023 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
1024 */
1025
1026 long
_Py_HashDouble(double v)1027 _Py_HashDouble(double v)
1028 {
1029 double intpart, fractpart;
1030 int expo;
1031 long hipart;
1032 long x; /* the final hash value */
1033 /* This is designed so that Python numbers of different types
1034 * that compare equal hash to the same value; otherwise comparisons
1035 * of mapping keys will turn out weird.
1036 */
1037
1038 if (!Py_IS_FINITE(v)) {
1039 if (Py_IS_INFINITY(v))
1040 return v < 0 ? -271828 : 314159;
1041 else
1042 return 0;
1043 }
1044 fractpart = modf(v, &intpart);
1045 if (fractpart == 0.0) {
1046 /* This must return the same hash as an equal int or long. */
1047 if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) {
1048 /* Convert to long and use its hash. */
1049 PyObject *plong; /* converted to Python long */
1050 plong = PyLong_FromDouble(v);
1051 if (plong == NULL)
1052 return -1;
1053 x = PyObject_Hash(plong);
1054 Py_DECREF(plong);
1055 return x;
1056 }
1057 /* Fits in a C long == a Python int, so is its own hash. */
1058 x = (long)intpart;
1059 if (x == -1)
1060 x = -2;
1061 return x;
1062 }
1063 /* The fractional part is non-zero, so we don't have to worry about
1064 * making this match the hash of some other type.
1065 * Use frexp to get at the bits in the double.
1066 * Since the VAX D double format has 56 mantissa bits, which is the
1067 * most of any double format in use, each of these parts may have as
1068 * many as (but no more than) 56 significant bits.
1069 * So, assuming sizeof(long) >= 4, each part can be broken into two
1070 * longs; frexp and multiplication are used to do that.
1071 * Also, since the Cray double format has 15 exponent bits, which is
1072 * the most of any double format in use, shifting the exponent field
1073 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
1074 */
1075 v = frexp(v, &expo);
1076 v *= 2147483648.0; /* 2**31 */
1077 hipart = (long)v; /* take the top 32 bits */
1078 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
1079 x = hipart + (long)v + (expo << 15);
1080 if (x == -1)
1081 x = -2;
1082 return x;
1083 }
1084
1085 long
_Py_HashPointer(void * p)1086 _Py_HashPointer(void *p)
1087 {
1088 long x;
1089 size_t y = (size_t)p;
1090 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
1091 excessive hash collisions for dicts and sets */
1092 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
1093 x = (long)y;
1094 if (x == -1)
1095 x = -2;
1096 return x;
1097 }
1098
1099 long
PyObject_HashNotImplemented(PyObject * self)1100 PyObject_HashNotImplemented(PyObject *self)
1101 {
1102 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
1103 self->ob_type->tp_name);
1104 return -1;
1105 }
1106
1107 _Py_HashSecret_t _Py_HashSecret;
1108
1109 long
PyObject_Hash(PyObject * v)1110 PyObject_Hash(PyObject *v)
1111 {
1112 PyTypeObject *tp = v->ob_type;
1113 if (tp->tp_hash != NULL)
1114 return (*tp->tp_hash)(v);
1115 /* To keep to the general practice that inheriting
1116 * solely from object in C code should work without
1117 * an explicit call to PyType_Ready, we implicitly call
1118 * PyType_Ready here and then check the tp_hash slot again
1119 */
1120 if (tp->tp_dict == NULL) {
1121 if (PyType_Ready(tp) < 0)
1122 return -1;
1123 if (tp->tp_hash != NULL)
1124 return (*tp->tp_hash)(v);
1125 }
1126 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
1127 return _Py_HashPointer(v); /* Use address as hash value */
1128 }
1129 /* If there's a cmp but no hash defined, the object can't be hashed */
1130 return PyObject_HashNotImplemented(v);
1131 }
1132
1133 PyObject *
PyObject_GetAttrString(PyObject * v,const char * name)1134 PyObject_GetAttrString(PyObject *v, const char *name)
1135 {
1136 PyObject *w, *res;
1137
1138 if (Py_TYPE(v)->tp_getattr != NULL)
1139 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
1140 w = PyString_InternFromString(name);
1141 if (w == NULL)
1142 return NULL;
1143 res = PyObject_GetAttr(v, w);
1144 Py_XDECREF(w);
1145 return res;
1146 }
1147
1148 int
PyObject_HasAttrString(PyObject * v,const char * name)1149 PyObject_HasAttrString(PyObject *v, const char *name)
1150 {
1151 PyObject *res = PyObject_GetAttrString(v, name);
1152 if (res != NULL) {
1153 Py_DECREF(res);
1154 return 1;
1155 }
1156 PyErr_Clear();
1157 return 0;
1158 }
1159
1160 int
PyObject_SetAttrString(PyObject * v,const char * name,PyObject * w)1161 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
1162 {
1163 PyObject *s;
1164 int res;
1165
1166 if (Py_TYPE(v)->tp_setattr != NULL)
1167 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
1168 s = PyString_InternFromString(name);
1169 if (s == NULL)
1170 return -1;
1171 res = PyObject_SetAttr(v, s, w);
1172 Py_XDECREF(s);
1173 return res;
1174 }
1175
1176 PyObject *
PyObject_GetAttr(PyObject * v,PyObject * name)1177 PyObject_GetAttr(PyObject *v, PyObject *name)
1178 {
1179 PyTypeObject *tp = Py_TYPE(v);
1180
1181 if (!PyString_Check(name)) {
1182 #ifdef Py_USING_UNICODE
1183 /* The Unicode to string conversion is done here because the
1184 existing tp_getattro slots expect a string object as name
1185 and we wouldn't want to break those. */
1186 if (PyUnicode_Check(name)) {
1187 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1188 if (name == NULL)
1189 return NULL;
1190 }
1191 else
1192 #endif
1193 {
1194 PyErr_Format(PyExc_TypeError,
1195 "attribute name must be string, not '%.200s'",
1196 Py_TYPE(name)->tp_name);
1197 return NULL;
1198 }
1199 }
1200 if (tp->tp_getattro != NULL)
1201 return (*tp->tp_getattro)(v, name);
1202 if (tp->tp_getattr != NULL)
1203 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1204 PyErr_Format(PyExc_AttributeError,
1205 "'%.50s' object has no attribute '%.400s'",
1206 tp->tp_name, PyString_AS_STRING(name));
1207 return NULL;
1208 }
1209
1210 int
PyObject_HasAttr(PyObject * v,PyObject * name)1211 PyObject_HasAttr(PyObject *v, PyObject *name)
1212 {
1213 PyObject *res = PyObject_GetAttr(v, name);
1214 if (res != NULL) {
1215 Py_DECREF(res);
1216 return 1;
1217 }
1218 PyErr_Clear();
1219 return 0;
1220 }
1221
1222 int
PyObject_SetAttr(PyObject * v,PyObject * name,PyObject * value)1223 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
1224 {
1225 PyTypeObject *tp = Py_TYPE(v);
1226 int err;
1227
1228 if (!PyString_Check(name)){
1229 #ifdef Py_USING_UNICODE
1230 /* The Unicode to string conversion is done here because the
1231 existing tp_setattro slots expect a string object as name
1232 and we wouldn't want to break those. */
1233 if (PyUnicode_Check(name)) {
1234 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1235 if (name == NULL)
1236 return -1;
1237 }
1238 else
1239 #endif
1240 {
1241 PyErr_Format(PyExc_TypeError,
1242 "attribute name must be string, not '%.200s'",
1243 Py_TYPE(name)->tp_name);
1244 return -1;
1245 }
1246 }
1247 else
1248 Py_INCREF(name);
1249
1250 PyString_InternInPlace(&name);
1251 if (tp->tp_setattro != NULL) {
1252 err = (*tp->tp_setattro)(v, name, value);
1253 Py_DECREF(name);
1254 return err;
1255 }
1256 if (tp->tp_setattr != NULL) {
1257 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1258 Py_DECREF(name);
1259 return err;
1260 }
1261 Py_DECREF(name);
1262 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1263 PyErr_Format(PyExc_TypeError,
1264 "'%.100s' object has no attributes "
1265 "(%s .%.100s)",
1266 tp->tp_name,
1267 value==NULL ? "del" : "assign to",
1268 PyString_AS_STRING(name));
1269 else
1270 PyErr_Format(PyExc_TypeError,
1271 "'%.100s' object has only read-only attributes "
1272 "(%s .%.100s)",
1273 tp->tp_name,
1274 value==NULL ? "del" : "assign to",
1275 PyString_AS_STRING(name));
1276 return -1;
1277 }
1278
1279 /* Helper to get a pointer to an object's __dict__ slot, if any */
1280
1281 PyObject **
_PyObject_GetDictPtr(PyObject * obj)1282 _PyObject_GetDictPtr(PyObject *obj)
1283 {
1284 Py_ssize_t dictoffset;
1285 PyTypeObject *tp = Py_TYPE(obj);
1286
1287 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1288 return NULL;
1289 dictoffset = tp->tp_dictoffset;
1290 if (dictoffset == 0)
1291 return NULL;
1292 if (dictoffset < 0) {
1293 Py_ssize_t tsize;
1294 size_t size;
1295
1296 tsize = ((PyVarObject *)obj)->ob_size;
1297 if (tsize < 0)
1298 tsize = -tsize;
1299 size = _PyObject_VAR_SIZE(tp, tsize);
1300
1301 dictoffset += (long)size;
1302 assert(dictoffset > 0);
1303 assert(dictoffset % SIZEOF_VOID_P == 0);
1304 }
1305 return (PyObject **) ((char *)obj + dictoffset);
1306 }
1307
1308 PyObject *
PyObject_SelfIter(PyObject * obj)1309 PyObject_SelfIter(PyObject *obj)
1310 {
1311 Py_INCREF(obj);
1312 return obj;
1313 }
1314
1315 /* Helper used when the __next__ method is removed from a type:
1316 tp_iternext is never NULL and can be safely called without checking
1317 on every iteration.
1318 */
1319
1320 PyObject *
_PyObject_NextNotImplemented(PyObject * self)1321 _PyObject_NextNotImplemented(PyObject *self)
1322 {
1323 PyErr_Format(PyExc_TypeError,
1324 "'%.200s' object is not iterable",
1325 Py_TYPE(self)->tp_name);
1326 return NULL;
1327 }
1328
1329 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1330
1331 PyObject *
_PyObject_GenericGetAttrWithDict(PyObject * obj,PyObject * name,PyObject * dict)1332 _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
1333 {
1334 PyTypeObject *tp = Py_TYPE(obj);
1335 PyObject *descr = NULL;
1336 PyObject *res = NULL;
1337 descrgetfunc f;
1338 Py_ssize_t dictoffset;
1339 PyObject **dictptr;
1340
1341 if (!PyString_Check(name)){
1342 #ifdef Py_USING_UNICODE
1343 /* The Unicode to string conversion is done here because the
1344 existing tp_setattro slots expect a string object as name
1345 and we wouldn't want to break those. */
1346 if (PyUnicode_Check(name)) {
1347 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1348 if (name == NULL)
1349 return NULL;
1350 }
1351 else
1352 #endif
1353 {
1354 PyErr_Format(PyExc_TypeError,
1355 "attribute name must be string, not '%.200s'",
1356 Py_TYPE(name)->tp_name);
1357 return NULL;
1358 }
1359 }
1360 else
1361 Py_INCREF(name);
1362
1363 if (tp->tp_dict == NULL) {
1364 if (PyType_Ready(tp) < 0)
1365 goto done;
1366 }
1367
1368 #if 0 /* XXX this is not quite _PyType_Lookup anymore */
1369 /* Inline _PyType_Lookup */
1370 {
1371 Py_ssize_t i, n;
1372 PyObject *mro, *base, *dict;
1373
1374 /* Look in tp_dict of types in MRO */
1375 mro = tp->tp_mro;
1376 assert(mro != NULL);
1377 assert(PyTuple_Check(mro));
1378 n = PyTuple_GET_SIZE(mro);
1379 for (i = 0; i < n; i++) {
1380 base = PyTuple_GET_ITEM(mro, i);
1381 if (PyClass_Check(base))
1382 dict = ((PyClassObject *)base)->cl_dict;
1383 else {
1384 assert(PyType_Check(base));
1385 dict = ((PyTypeObject *)base)->tp_dict;
1386 }
1387 assert(dict && PyDict_Check(dict));
1388 descr = PyDict_GetItem(dict, name);
1389 if (descr != NULL)
1390 break;
1391 }
1392 }
1393 #else
1394 descr = _PyType_Lookup(tp, name);
1395 #endif
1396
1397 Py_XINCREF(descr);
1398
1399 f = NULL;
1400 if (descr != NULL &&
1401 PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
1402 f = descr->ob_type->tp_descr_get;
1403 if (f != NULL && PyDescr_IsData(descr)) {
1404 res = f(descr, obj, (PyObject *)obj->ob_type);
1405 Py_DECREF(descr);
1406 goto done;
1407 }
1408 }
1409
1410 if (dict == NULL) {
1411 /* Inline _PyObject_GetDictPtr */
1412 dictoffset = tp->tp_dictoffset;
1413 if (dictoffset != 0) {
1414 if (dictoffset < 0) {
1415 Py_ssize_t tsize;
1416 size_t size;
1417
1418 tsize = ((PyVarObject *)obj)->ob_size;
1419 if (tsize < 0)
1420 tsize = -tsize;
1421 size = _PyObject_VAR_SIZE(tp, tsize);
1422
1423 dictoffset += (long)size;
1424 assert(dictoffset > 0);
1425 assert(dictoffset % SIZEOF_VOID_P == 0);
1426 }
1427 dictptr = (PyObject **) ((char *)obj + dictoffset);
1428 dict = *dictptr;
1429 }
1430 }
1431 if (dict != NULL) {
1432 Py_INCREF(dict);
1433 res = PyDict_GetItem(dict, name);
1434 if (res != NULL) {
1435 Py_INCREF(res);
1436 Py_XDECREF(descr);
1437 Py_DECREF(dict);
1438 goto done;
1439 }
1440 Py_DECREF(dict);
1441 }
1442
1443 if (f != NULL) {
1444 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1445 Py_DECREF(descr);
1446 goto done;
1447 }
1448
1449 if (descr != NULL) {
1450 res = descr;
1451 /* descr was already increfed above */
1452 goto done;
1453 }
1454
1455 PyErr_Format(PyExc_AttributeError,
1456 "'%.50s' object has no attribute '%.400s'",
1457 tp->tp_name, PyString_AS_STRING(name));
1458 done:
1459 Py_DECREF(name);
1460 return res;
1461 }
1462
1463 PyObject *
PyObject_GenericGetAttr(PyObject * obj,PyObject * name)1464 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1465 {
1466 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1467 }
1468
1469 int
_PyObject_GenericSetAttrWithDict(PyObject * obj,PyObject * name,PyObject * value,PyObject * dict)1470 _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1471 PyObject *value, PyObject *dict)
1472 {
1473 PyTypeObject *tp = Py_TYPE(obj);
1474 PyObject *descr;
1475 descrsetfunc f;
1476 PyObject **dictptr;
1477 int res = -1;
1478
1479 if (!PyString_Check(name)){
1480 #ifdef Py_USING_UNICODE
1481 /* The Unicode to string conversion is done here because the
1482 existing tp_setattro slots expect a string object as name
1483 and we wouldn't want to break those. */
1484 if (PyUnicode_Check(name)) {
1485 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1486 if (name == NULL)
1487 return -1;
1488 }
1489 else
1490 #endif
1491 {
1492 PyErr_Format(PyExc_TypeError,
1493 "attribute name must be string, not '%.200s'",
1494 Py_TYPE(name)->tp_name);
1495 return -1;
1496 }
1497 }
1498 else
1499 Py_INCREF(name);
1500
1501 if (tp->tp_dict == NULL) {
1502 if (PyType_Ready(tp) < 0)
1503 goto done;
1504 }
1505
1506 descr = _PyType_Lookup(tp, name);
1507 f = NULL;
1508 if (descr != NULL &&
1509 PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
1510 f = descr->ob_type->tp_descr_set;
1511 if (f != NULL && PyDescr_IsData(descr)) {
1512 res = f(descr, obj, value);
1513 goto done;
1514 }
1515 }
1516
1517 if (dict == NULL) {
1518 dictptr = _PyObject_GetDictPtr(obj);
1519 if (dictptr != NULL) {
1520 dict = *dictptr;
1521 if (dict == NULL && value != NULL) {
1522 dict = PyDict_New();
1523 if (dict == NULL)
1524 goto done;
1525 *dictptr = dict;
1526 }
1527 }
1528 }
1529 if (dict != NULL) {
1530 Py_INCREF(dict);
1531 if (value == NULL)
1532 res = PyDict_DelItem(dict, name);
1533 else
1534 res = PyDict_SetItem(dict, name, value);
1535 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1536 PyErr_SetObject(PyExc_AttributeError, name);
1537 Py_DECREF(dict);
1538 goto done;
1539 }
1540
1541 if (f != NULL) {
1542 res = f(descr, obj, value);
1543 goto done;
1544 }
1545
1546 if (descr == NULL) {
1547 PyErr_Format(PyExc_AttributeError,
1548 "'%.100s' object has no attribute '%.200s'",
1549 tp->tp_name, PyString_AS_STRING(name));
1550 goto done;
1551 }
1552
1553 PyErr_Format(PyExc_AttributeError,
1554 "'%.50s' object attribute '%.400s' is read-only",
1555 tp->tp_name, PyString_AS_STRING(name));
1556 done:
1557 Py_DECREF(name);
1558 return res;
1559 }
1560
1561 int
PyObject_GenericSetAttr(PyObject * obj,PyObject * name,PyObject * value)1562 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1563 {
1564 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1565 }
1566
1567
1568 /* Test a value used as condition, e.g., in a for or if statement.
1569 Return -1 if an error occurred */
1570
1571 int
PyObject_IsTrue(PyObject * v)1572 PyObject_IsTrue(PyObject *v)
1573 {
1574 Py_ssize_t res;
1575 if (v == Py_True)
1576 return 1;
1577 if (v == Py_False)
1578 return 0;
1579 if (v == Py_None)
1580 return 0;
1581 else if (v->ob_type->tp_as_number != NULL &&
1582 v->ob_type->tp_as_number->nb_nonzero != NULL)
1583 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
1584 else if (v->ob_type->tp_as_mapping != NULL &&
1585 v->ob_type->tp_as_mapping->mp_length != NULL)
1586 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1587 else if (v->ob_type->tp_as_sequence != NULL &&
1588 v->ob_type->tp_as_sequence->sq_length != NULL)
1589 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1590 else
1591 return 1;
1592 /* if it is negative, it should be either -1 or -2 */
1593 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
1594 }
1595
1596 /* equivalent of 'not v'
1597 Return -1 if an error occurred */
1598
1599 int
PyObject_Not(PyObject * v)1600 PyObject_Not(PyObject *v)
1601 {
1602 int res;
1603 res = PyObject_IsTrue(v);
1604 if (res < 0)
1605 return res;
1606 return res == 0;
1607 }
1608
1609 /* Coerce two numeric types to the "larger" one.
1610 Increment the reference count on each argument.
1611 Return value:
1612 -1 if an error occurred;
1613 0 if the coercion succeeded (and then the reference counts are increased);
1614 1 if no coercion is possible (and no error is raised).
1615 */
1616 int
PyNumber_CoerceEx(PyObject ** pv,PyObject ** pw)1617 PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
1618 {
1619 register PyObject *v = *pv;
1620 register PyObject *w = *pw;
1621 int res;
1622
1623 /* Shortcut only for old-style types */
1624 if (v->ob_type == w->ob_type &&
1625 !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES))
1626 {
1627 Py_INCREF(v);
1628 Py_INCREF(w);
1629 return 0;
1630 }
1631 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1632 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1633 if (res <= 0)
1634 return res;
1635 }
1636 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1637 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1638 if (res <= 0)
1639 return res;
1640 }
1641 return 1;
1642 }
1643
1644 /* Coerce two numeric types to the "larger" one.
1645 Increment the reference count on each argument.
1646 Return -1 and raise an exception if no coercion is possible
1647 (and then no reference count is incremented).
1648 */
1649 int
PyNumber_Coerce(PyObject ** pv,PyObject ** pw)1650 PyNumber_Coerce(PyObject **pv, PyObject **pw)
1651 {
1652 int err = PyNumber_CoerceEx(pv, pw);
1653 if (err <= 0)
1654 return err;
1655 PyErr_SetString(PyExc_TypeError, "number coercion failed");
1656 return -1;
1657 }
1658
1659
1660 /* Test whether an object can be called */
1661
1662 int
PyCallable_Check(PyObject * x)1663 PyCallable_Check(PyObject *x)
1664 {
1665 if (x == NULL)
1666 return 0;
1667 if (PyInstance_Check(x)) {
1668 PyObject *call = PyObject_GetAttrString(x, "__call__");
1669 if (call == NULL) {
1670 PyErr_Clear();
1671 return 0;
1672 }
1673 /* Could test recursively but don't, for fear of endless
1674 recursion if some joker sets self.__call__ = self */
1675 Py_DECREF(call);
1676 return 1;
1677 }
1678 else {
1679 return x->ob_type->tp_call != NULL;
1680 }
1681 }
1682
1683 /* ------------------------- PyObject_Dir() helpers ------------------------- */
1684
1685 /* Helper for PyObject_Dir.
1686 Merge the __dict__ of aclass into dict, and recursively also all
1687 the __dict__s of aclass's base classes. The order of merging isn't
1688 defined, as it's expected that only the final set of dict keys is
1689 interesting.
1690 Return 0 on success, -1 on error.
1691 */
1692
1693 static int
merge_class_dict(PyObject * dict,PyObject * aclass)1694 merge_class_dict(PyObject* dict, PyObject* aclass)
1695 {
1696 PyObject *classdict;
1697 PyObject *bases;
1698
1699 assert(PyDict_Check(dict));
1700 assert(aclass);
1701
1702 /* Merge in the type's dict (if any). */
1703 classdict = PyObject_GetAttrString(aclass, "__dict__");
1704 if (classdict == NULL)
1705 PyErr_Clear();
1706 else {
1707 int status = PyDict_Update(dict, classdict);
1708 Py_DECREF(classdict);
1709 if (status < 0)
1710 return -1;
1711 }
1712
1713 /* Recursively merge in the base types' (if any) dicts. */
1714 bases = PyObject_GetAttrString(aclass, "__bases__");
1715 if (bases == NULL)
1716 PyErr_Clear();
1717 else {
1718 /* We have no guarantee that bases is a real tuple */
1719 Py_ssize_t i, n;
1720 n = PySequence_Size(bases); /* This better be right */
1721 if (n < 0)
1722 PyErr_Clear();
1723 else {
1724 for (i = 0; i < n; i++) {
1725 int status;
1726 PyObject *base = PySequence_GetItem(bases, i);
1727 if (base == NULL) {
1728 Py_DECREF(bases);
1729 return -1;
1730 }
1731 status = merge_class_dict(dict, base);
1732 Py_DECREF(base);
1733 if (status < 0) {
1734 Py_DECREF(bases);
1735 return -1;
1736 }
1737 }
1738 }
1739 Py_DECREF(bases);
1740 }
1741 return 0;
1742 }
1743
1744 /* Helper for PyObject_Dir.
1745 If obj has an attr named attrname that's a list, merge its string
1746 elements into keys of dict.
1747 Return 0 on success, -1 on error. Errors due to not finding the attr,
1748 or the attr not being a list, are suppressed.
1749 */
1750
1751 static int
merge_list_attr(PyObject * dict,PyObject * obj,const char * attrname)1752 merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname)
1753 {
1754 PyObject *list;
1755 int result = 0;
1756
1757 assert(PyDict_Check(dict));
1758 assert(obj);
1759 assert(attrname);
1760
1761 list = PyObject_GetAttrString(obj, attrname);
1762 if (list == NULL)
1763 PyErr_Clear();
1764
1765 else if (PyList_Check(list)) {
1766 int i;
1767 for (i = 0; i < PyList_GET_SIZE(list); ++i) {
1768 PyObject *item = PyList_GET_ITEM(list, i);
1769 if (PyString_Check(item)) {
1770 result = PyDict_SetItem(dict, item, Py_None);
1771 if (result < 0)
1772 break;
1773 }
1774 }
1775 if (Py_Py3kWarningFlag &&
1776 (strcmp(attrname, "__members__") == 0 ||
1777 strcmp(attrname, "__methods__") == 0)) {
1778 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1779 "__members__ and __methods__ not "
1780 "supported in 3.x", 1) < 0) {
1781 Py_XDECREF(list);
1782 return -1;
1783 }
1784 }
1785 }
1786
1787 Py_XDECREF(list);
1788 return result;
1789 }
1790
1791 /* Helper for PyObject_Dir without arguments: returns the local scope. */
1792 static PyObject *
_dir_locals(void)1793 _dir_locals(void)
1794 {
1795 PyObject *names;
1796 PyObject *locals = PyEval_GetLocals();
1797
1798 if (locals == NULL) {
1799 PyErr_SetString(PyExc_SystemError, "frame does not exist");
1800 return NULL;
1801 }
1802
1803 names = PyMapping_Keys(locals);
1804 if (!names)
1805 return NULL;
1806 if (!PyList_Check(names)) {
1807 PyErr_Format(PyExc_TypeError,
1808 "dir(): expected keys() of locals to be a list, "
1809 "not '%.200s'", Py_TYPE(names)->tp_name);
1810 Py_DECREF(names);
1811 return NULL;
1812 }
1813 /* the locals don't need to be DECREF'd */
1814 return names;
1815 }
1816
1817 /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
1818 We deliberately don't suck up its __class__, as methods belonging to the
1819 metaclass would probably be more confusing than helpful.
1820 */
1821 static PyObject *
_specialized_dir_type(PyObject * obj)1822 _specialized_dir_type(PyObject *obj)
1823 {
1824 PyObject *result = NULL;
1825 PyObject *dict = PyDict_New();
1826
1827 if (dict != NULL && merge_class_dict(dict, obj) == 0)
1828 result = PyDict_Keys(dict);
1829
1830 Py_XDECREF(dict);
1831 return result;
1832 }
1833
1834 /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */
1835 static PyObject *
_specialized_dir_module(PyObject * obj)1836 _specialized_dir_module(PyObject *obj)
1837 {
1838 PyObject *result = NULL;
1839 PyObject *dict = PyObject_GetAttrString(obj, "__dict__");
1840
1841 if (dict != NULL) {
1842 if (PyDict_Check(dict))
1843 result = PyDict_Keys(dict);
1844 else {
1845 char *name = PyModule_GetName(obj);
1846 if (name)
1847 PyErr_Format(PyExc_TypeError,
1848 "%.200s.__dict__ is not a dictionary",
1849 name);
1850 }
1851 }
1852
1853 Py_XDECREF(dict);
1854 return result;
1855 }
1856
1857 /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__,
1858 and recursively up the __class__.__bases__ chain.
1859 */
1860 static PyObject *
_generic_dir(PyObject * obj)1861 _generic_dir(PyObject *obj)
1862 {
1863 PyObject *result = NULL;
1864 PyObject *dict = NULL;
1865 PyObject *itsclass = NULL;
1866
1867 /* Get __dict__ (which may or may not be a real dict...) */
1868 dict = PyObject_GetAttrString(obj, "__dict__");
1869 if (dict == NULL) {
1870 PyErr_Clear();
1871 dict = PyDict_New();
1872 }
1873 else if (!PyDict_Check(dict)) {
1874 Py_DECREF(dict);
1875 dict = PyDict_New();
1876 }
1877 else {
1878 /* Copy __dict__ to avoid mutating it. */
1879 PyObject *temp = PyDict_Copy(dict);
1880 Py_DECREF(dict);
1881 dict = temp;
1882 }
1883
1884 if (dict == NULL)
1885 goto error;
1886
1887 /* Merge in __members__ and __methods__ (if any).
1888 * This is removed in Python 3000. */
1889 if (merge_list_attr(dict, obj, "__members__") < 0)
1890 goto error;
1891 if (merge_list_attr(dict, obj, "__methods__") < 0)
1892 goto error;
1893
1894 /* Merge in attrs reachable from its class. */
1895 itsclass = PyObject_GetAttrString(obj, "__class__");
1896 if (itsclass == NULL)
1897 /* XXX(tomer): Perhaps fall back to obj->ob_type if no
1898 __class__ exists? */
1899 PyErr_Clear();
1900 else {
1901 if (merge_class_dict(dict, itsclass) != 0)
1902 goto error;
1903 }
1904
1905 result = PyDict_Keys(dict);
1906 /* fall through */
1907 error:
1908 Py_XDECREF(itsclass);
1909 Py_XDECREF(dict);
1910 return result;
1911 }
1912
1913 /* Helper for PyObject_Dir: object introspection.
1914 This calls one of the above specialized versions if no __dir__ method
1915 exists. */
1916 static PyObject *
_dir_object(PyObject * obj)1917 _dir_object(PyObject *obj)
1918 {
1919 PyObject *result = NULL;
1920 static PyObject *dir_str = NULL;
1921 PyObject *dirfunc;
1922
1923 assert(obj);
1924 if (PyInstance_Check(obj)) {
1925 dirfunc = PyObject_GetAttrString(obj, "__dir__");
1926 if (dirfunc == NULL) {
1927 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1928 PyErr_Clear();
1929 else
1930 return NULL;
1931 }
1932 }
1933 else {
1934 dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str);
1935 if (PyErr_Occurred())
1936 return NULL;
1937 }
1938 if (dirfunc == NULL) {
1939 /* use default implementation */
1940 if (PyModule_Check(obj))
1941 result = _specialized_dir_module(obj);
1942 else if (PyType_Check(obj) || PyClass_Check(obj))
1943 result = _specialized_dir_type(obj);
1944 else
1945 result = _generic_dir(obj);
1946 }
1947 else {
1948 /* use __dir__ */
1949 result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
1950 Py_DECREF(dirfunc);
1951 if (result == NULL)
1952 return NULL;
1953
1954 /* result must be a list */
1955 /* XXX(gbrandl): could also check if all items are strings */
1956 if (!PyList_Check(result)) {
1957 PyErr_Format(PyExc_TypeError,
1958 "__dir__() must return a list, not %.200s",
1959 Py_TYPE(result)->tp_name);
1960 Py_DECREF(result);
1961 result = NULL;
1962 }
1963 }
1964
1965 return result;
1966 }
1967
1968 /* Implementation of dir() -- if obj is NULL, returns the names in the current
1969 (local) scope. Otherwise, performs introspection of the object: returns a
1970 sorted list of attribute names (supposedly) accessible from the object
1971 */
1972 PyObject *
PyObject_Dir(PyObject * obj)1973 PyObject_Dir(PyObject *obj)
1974 {
1975 PyObject * result;
1976
1977 if (obj == NULL)
1978 /* no object -- introspect the locals */
1979 result = _dir_locals();
1980 else
1981 /* object -- introspect the object */
1982 result = _dir_object(obj);
1983
1984 assert(result == NULL || PyList_Check(result));
1985
1986 if (result != NULL && PyList_Sort(result) != 0) {
1987 /* sorting the list failed */
1988 Py_DECREF(result);
1989 result = NULL;
1990 }
1991
1992 return result;
1993 }
1994
1995 /*
1996 NoObject is usable as a non-NULL undefined value, used by the macro None.
1997 There is (and should be!) no way to create other objects of this type,
1998 so there is exactly one (which is indestructible, by the way).
1999 (XXX This type and the type of NotImplemented below should be unified.)
2000 */
2001
2002 /* ARGSUSED */
2003 static PyObject *
none_repr(PyObject * op)2004 none_repr(PyObject *op)
2005 {
2006 return PyString_FromString("None");
2007 }
2008
2009 /* ARGUSED */
2010 static void
none_dealloc(PyObject * ignore)2011 none_dealloc(PyObject* ignore)
2012 {
2013 /* This should never get called, but we also don't want to SEGV if
2014 * we accidentally decref None out of existence.
2015 */
2016 Py_FatalError("deallocating None");
2017 }
2018
2019
2020 static PyTypeObject PyNone_Type = {
2021 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2022 "NoneType",
2023 0,
2024 0,
2025 none_dealloc, /*tp_dealloc*/ /*never called*/
2026 0, /*tp_print*/
2027 0, /*tp_getattr*/
2028 0, /*tp_setattr*/
2029 0, /*tp_compare*/
2030 none_repr, /*tp_repr*/
2031 0, /*tp_as_number*/
2032 0, /*tp_as_sequence*/
2033 0, /*tp_as_mapping*/
2034 (hashfunc)_Py_HashPointer, /*tp_hash */
2035 };
2036
2037 PyObject _Py_NoneStruct = {
2038 _PyObject_EXTRA_INIT
2039 1, &PyNone_Type
2040 };
2041
2042 /* NotImplemented is an object that can be used to signal that an
2043 operation is not implemented for the given type combination. */
2044
2045 static PyObject *
NotImplemented_repr(PyObject * op)2046 NotImplemented_repr(PyObject *op)
2047 {
2048 return PyString_FromString("NotImplemented");
2049 }
2050
2051 static PyTypeObject PyNotImplemented_Type = {
2052 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2053 "NotImplementedType",
2054 0,
2055 0,
2056 none_dealloc, /*tp_dealloc*/ /*never called*/
2057 0, /*tp_print*/
2058 0, /*tp_getattr*/
2059 0, /*tp_setattr*/
2060 0, /*tp_compare*/
2061 NotImplemented_repr, /*tp_repr*/
2062 0, /*tp_as_number*/
2063 0, /*tp_as_sequence*/
2064 0, /*tp_as_mapping*/
2065 0, /*tp_hash */
2066 };
2067
2068 PyObject _Py_NotImplementedStruct = {
2069 _PyObject_EXTRA_INIT
2070 1, &PyNotImplemented_Type
2071 };
2072
2073 void
_Py_ReadyTypes(void)2074 _Py_ReadyTypes(void)
2075 {
2076 if (PyType_Ready(&PyType_Type) < 0)
2077 Py_FatalError("Can't initialize type type");
2078
2079 if (PyType_Ready(&_PyWeakref_RefType) < 0)
2080 Py_FatalError("Can't initialize weakref type");
2081
2082 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
2083 Py_FatalError("Can't initialize callable weakref proxy type");
2084
2085 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
2086 Py_FatalError("Can't initialize weakref proxy type");
2087
2088 if (PyType_Ready(&PyBool_Type) < 0)
2089 Py_FatalError("Can't initialize bool type");
2090
2091 if (PyType_Ready(&PyString_Type) < 0)
2092 Py_FatalError("Can't initialize str type");
2093
2094 if (PyType_Ready(&PyByteArray_Type) < 0)
2095 Py_FatalError("Can't initialize bytearray type");
2096
2097 if (PyType_Ready(&PyList_Type) < 0)
2098 Py_FatalError("Can't initialize list type");
2099
2100 if (PyType_Ready(&PyNone_Type) < 0)
2101 Py_FatalError("Can't initialize None type");
2102
2103 if (PyType_Ready(&PyNotImplemented_Type) < 0)
2104 Py_FatalError("Can't initialize NotImplemented type");
2105
2106 if (PyType_Ready(&PyTraceBack_Type) < 0)
2107 Py_FatalError("Can't initialize traceback type");
2108
2109 if (PyType_Ready(&PySuper_Type) < 0)
2110 Py_FatalError("Can't initialize super type");
2111
2112 if (PyType_Ready(&PyBaseObject_Type) < 0)
2113 Py_FatalError("Can't initialize object type");
2114
2115 if (PyType_Ready(&PyRange_Type) < 0)
2116 Py_FatalError("Can't initialize xrange type");
2117
2118 if (PyType_Ready(&PyDict_Type) < 0)
2119 Py_FatalError("Can't initialize dict type");
2120
2121 if (PyType_Ready(&PySet_Type) < 0)
2122 Py_FatalError("Can't initialize set type");
2123
2124 #ifdef Py_USING_UNICODE
2125 if (PyType_Ready(&PyUnicode_Type) < 0)
2126 Py_FatalError("Can't initialize unicode type");
2127 #endif
2128
2129 if (PyType_Ready(&PySlice_Type) < 0)
2130 Py_FatalError("Can't initialize slice type");
2131
2132 if (PyType_Ready(&PyStaticMethod_Type) < 0)
2133 Py_FatalError("Can't initialize static method type");
2134
2135 #ifndef WITHOUT_COMPLEX
2136 if (PyType_Ready(&PyComplex_Type) < 0)
2137 Py_FatalError("Can't initialize complex type");
2138 #endif
2139
2140 if (PyType_Ready(&PyFloat_Type) < 0)
2141 Py_FatalError("Can't initialize float type");
2142
2143 if (PyType_Ready(&PyBuffer_Type) < 0)
2144 Py_FatalError("Can't initialize buffer type");
2145
2146 if (PyType_Ready(&PyLong_Type) < 0)
2147 Py_FatalError("Can't initialize long type");
2148
2149 if (PyType_Ready(&PyInt_Type) < 0)
2150 Py_FatalError("Can't initialize int type");
2151
2152 if (PyType_Ready(&PyFrozenSet_Type) < 0)
2153 Py_FatalError("Can't initialize frozenset type");
2154
2155 if (PyType_Ready(&PyProperty_Type) < 0)
2156 Py_FatalError("Can't initialize property type");
2157
2158 if (PyType_Ready(&PyMemoryView_Type) < 0)
2159 Py_FatalError("Can't initialize memoryview type");
2160
2161 if (PyType_Ready(&PyTuple_Type) < 0)
2162 Py_FatalError("Can't initialize tuple type");
2163
2164 if (PyType_Ready(&PyEnum_Type) < 0)
2165 Py_FatalError("Can't initialize enumerate type");
2166
2167 if (PyType_Ready(&PyReversed_Type) < 0)
2168 Py_FatalError("Can't initialize reversed type");
2169
2170 if (PyType_Ready(&PyCode_Type) < 0)
2171 Py_FatalError("Can't initialize code type");
2172
2173 if (PyType_Ready(&PyFrame_Type) < 0)
2174 Py_FatalError("Can't initialize frame type");
2175
2176 if (PyType_Ready(&PyCFunction_Type) < 0)
2177 Py_FatalError("Can't initialize builtin function type");
2178
2179 if (PyType_Ready(&PyMethod_Type) < 0)
2180 Py_FatalError("Can't initialize method type");
2181
2182 if (PyType_Ready(&PyFunction_Type) < 0)
2183 Py_FatalError("Can't initialize function type");
2184
2185 if (PyType_Ready(&PyClass_Type) < 0)
2186 Py_FatalError("Can't initialize class type");
2187
2188 if (PyType_Ready(&PyDictProxy_Type) < 0)
2189 Py_FatalError("Can't initialize dict proxy type");
2190
2191 if (PyType_Ready(&PyGen_Type) < 0)
2192 Py_FatalError("Can't initialize generator type");
2193
2194 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
2195 Py_FatalError("Can't initialize get-set descriptor type");
2196
2197 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
2198 Py_FatalError("Can't initialize wrapper type");
2199
2200 if (PyType_Ready(&PyInstance_Type) < 0)
2201 Py_FatalError("Can't initialize instance type");
2202
2203 if (PyType_Ready(&PyEllipsis_Type) < 0)
2204 Py_FatalError("Can't initialize ellipsis type");
2205
2206 if (PyType_Ready(&PyMemberDescr_Type) < 0)
2207 Py_FatalError("Can't initialize member descriptor type");
2208
2209 if (PyType_Ready(&PyFile_Type) < 0)
2210 Py_FatalError("Can't initialize file type");
2211
2212 if (PyType_Ready(&PyCapsule_Type) < 0)
2213 Py_FatalError("Can't initialize capsule type");
2214
2215 if (PyType_Ready(&PyCell_Type) < 0)
2216 Py_FatalError("Can't initialize cell type");
2217
2218 if (PyType_Ready(&PyCallIter_Type) < 0)
2219 Py_FatalError("Can't initialize call iter type");
2220
2221 if (PyType_Ready(&PySeqIter_Type) < 0)
2222 Py_FatalError("Can't initialize sequence iterator type");
2223 }
2224
2225
2226 #ifdef Py_TRACE_REFS
2227
2228 void
_Py_NewReference(PyObject * op)2229 _Py_NewReference(PyObject *op)
2230 {
2231 _Py_INC_REFTOTAL;
2232 op->ob_refcnt = 1;
2233 _Py_AddToAllObjects(op, 1);
2234 _Py_INC_TPALLOCS(op);
2235 }
2236
2237 void
_Py_ForgetReference(register PyObject * op)2238 _Py_ForgetReference(register PyObject *op)
2239 {
2240 #ifdef SLOW_UNREF_CHECK
2241 register PyObject *p;
2242 #endif
2243 if (op->ob_refcnt < 0)
2244 Py_FatalError("UNREF negative refcnt");
2245 if (op == &refchain ||
2246 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
2247 Py_FatalError("UNREF invalid object");
2248 #ifdef SLOW_UNREF_CHECK
2249 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
2250 if (p == op)
2251 break;
2252 }
2253 if (p == &refchain) /* Not found */
2254 Py_FatalError("UNREF unknown object");
2255 #endif
2256 op->_ob_next->_ob_prev = op->_ob_prev;
2257 op->_ob_prev->_ob_next = op->_ob_next;
2258 op->_ob_next = op->_ob_prev = NULL;
2259 _Py_INC_TPFREES(op);
2260 }
2261
2262 void
_Py_Dealloc(PyObject * op)2263 _Py_Dealloc(PyObject *op)
2264 {
2265 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2266 _Py_ForgetReference(op);
2267 (*dealloc)(op);
2268 }
2269
2270 /* Print all live objects. Because PyObject_Print is called, the
2271 * interpreter must be in a healthy state.
2272 */
2273 void
_Py_PrintReferences(FILE * fp)2274 _Py_PrintReferences(FILE *fp)
2275 {
2276 PyObject *op;
2277 fprintf(fp, "Remaining objects:\n");
2278 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
2279 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
2280 if (PyObject_Print(op, fp, 0) != 0)
2281 PyErr_Clear();
2282 putc('\n', fp);
2283 }
2284 }
2285
2286 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
2287 * doesn't make any calls to the Python C API, so is always safe to call.
2288 */
2289 void
_Py_PrintReferenceAddresses(FILE * fp)2290 _Py_PrintReferenceAddresses(FILE *fp)
2291 {
2292 PyObject *op;
2293 fprintf(fp, "Remaining object addresses:\n");
2294 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
2295 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
2296 op->ob_refcnt, Py_TYPE(op)->tp_name);
2297 }
2298
2299 PyObject *
_Py_GetObjects(PyObject * self,PyObject * args)2300 _Py_GetObjects(PyObject *self, PyObject *args)
2301 {
2302 int i, n;
2303 PyObject *t = NULL;
2304 PyObject *res, *op;
2305
2306 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
2307 return NULL;
2308 op = refchain._ob_next;
2309 res = PyList_New(0);
2310 if (res == NULL)
2311 return NULL;
2312 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
2313 while (op == self || op == args || op == res || op == t ||
2314 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
2315 op = op->_ob_next;
2316 if (op == &refchain)
2317 return res;
2318 }
2319 if (PyList_Append(res, op) < 0) {
2320 Py_DECREF(res);
2321 return NULL;
2322 }
2323 op = op->_ob_next;
2324 }
2325 return res;
2326 }
2327
2328 #endif
2329
2330
2331 /* Hack to force loading of capsule.o */
2332 PyTypeObject *_Py_capsule_hack = &PyCapsule_Type;
2333
2334
2335 /* Hack to force loading of cobject.o */
2336 PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
2337
2338
2339 /* Hack to force loading of abstract.o */
2340 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
2341
2342
2343 /* Python's malloc wrappers (see pymem.h) */
2344
2345 void *
PyMem_Malloc(size_t nbytes)2346 PyMem_Malloc(size_t nbytes)
2347 {
2348 return PyMem_MALLOC(nbytes);
2349 }
2350
2351 void *
PyMem_Realloc(void * p,size_t nbytes)2352 PyMem_Realloc(void *p, size_t nbytes)
2353 {
2354 return PyMem_REALLOC(p, nbytes);
2355 }
2356
2357 void
PyMem_Free(void * p)2358 PyMem_Free(void *p)
2359 {
2360 PyMem_FREE(p);
2361 }
2362
2363
2364 /* These methods are used to control infinite recursion in repr, str, print,
2365 etc. Container objects that may recursively contain themselves,
2366 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
2367 Py_ReprLeave() to avoid infinite recursion.
2368
2369 Py_ReprEnter() returns 0 the first time it is called for a particular
2370 object and 1 every time thereafter. It returns -1 if an exception
2371 occurred. Py_ReprLeave() has no return value.
2372
2373 See dictobject.c and listobject.c for examples of use.
2374 */
2375
2376 #define KEY "Py_Repr"
2377
2378 int
Py_ReprEnter(PyObject * obj)2379 Py_ReprEnter(PyObject *obj)
2380 {
2381 PyObject *dict;
2382 PyObject *list;
2383 Py_ssize_t i;
2384
2385 dict = PyThreadState_GetDict();
2386 if (dict == NULL)
2387 return 0;
2388 list = PyDict_GetItemString(dict, KEY);
2389 if (list == NULL) {
2390 list = PyList_New(0);
2391 if (list == NULL)
2392 return -1;
2393 if (PyDict_SetItemString(dict, KEY, list) < 0)
2394 return -1;
2395 Py_DECREF(list);
2396 }
2397 i = PyList_GET_SIZE(list);
2398 while (--i >= 0) {
2399 if (PyList_GET_ITEM(list, i) == obj)
2400 return 1;
2401 }
2402 PyList_Append(list, obj);
2403 return 0;
2404 }
2405
2406 void
Py_ReprLeave(PyObject * obj)2407 Py_ReprLeave(PyObject *obj)
2408 {
2409 PyObject *dict;
2410 PyObject *list;
2411 Py_ssize_t i;
2412
2413 dict = PyThreadState_GetDict();
2414 if (dict == NULL)
2415 return;
2416 list = PyDict_GetItemString(dict, KEY);
2417 if (list == NULL || !PyList_Check(list))
2418 return;
2419 i = PyList_GET_SIZE(list);
2420 /* Count backwards because we always expect obj to be list[-1] */
2421 while (--i >= 0) {
2422 if (PyList_GET_ITEM(list, i) == obj) {
2423 PyList_SetSlice(list, i, i + 1, NULL);
2424 break;
2425 }
2426 }
2427 }
2428
2429 /* Trashcan support. */
2430
2431 /* Current call-stack depth of tp_dealloc calls. */
2432 int _PyTrash_delete_nesting = 0;
2433
2434 /* List of objects that still need to be cleaned up, singly linked via their
2435 * gc headers' gc_prev pointers.
2436 */
2437 PyObject *_PyTrash_delete_later = NULL;
2438
2439 /* Add op to the _PyTrash_delete_later list. Called when the current
2440 * call-stack depth gets large. op must be a currently untracked gc'ed
2441 * object, with refcount 0. Py_DECREF must already have been called on it.
2442 */
2443 void
_PyTrash_deposit_object(PyObject * op)2444 _PyTrash_deposit_object(PyObject *op)
2445 {
2446 assert(PyObject_IS_GC(op));
2447 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
2448 assert(op->ob_refcnt == 0);
2449 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
2450 _PyTrash_delete_later = op;
2451 }
2452
2453 /* The equivalent API, using per-thread state recursion info */
2454 void
_PyTrash_thread_deposit_object(PyObject * op)2455 _PyTrash_thread_deposit_object(PyObject *op)
2456 {
2457 PyThreadState *tstate = PyThreadState_GET();
2458 assert(PyObject_IS_GC(op));
2459 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
2460 assert(op->ob_refcnt == 0);
2461 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2462 tstate->trash_delete_later = op;
2463 }
2464
2465 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2466 * the call-stack unwinds again.
2467 */
2468 void
_PyTrash_destroy_chain(void)2469 _PyTrash_destroy_chain(void)
2470 {
2471 while (_PyTrash_delete_later) {
2472 PyObject *op = _PyTrash_delete_later;
2473 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2474
2475 _PyTrash_delete_later =
2476 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2477
2478 /* Call the deallocator directly. This used to try to
2479 * fool Py_DECREF into calling it indirectly, but
2480 * Py_DECREF was already called on this object, and in
2481 * assorted non-release builds calling Py_DECREF again ends
2482 * up distorting allocation statistics.
2483 */
2484 assert(op->ob_refcnt == 0);
2485 ++_PyTrash_delete_nesting;
2486 (*dealloc)(op);
2487 --_PyTrash_delete_nesting;
2488 }
2489 }
2490
2491 /* The equivalent API, using per-thread state recursion info */
2492 void
_PyTrash_thread_destroy_chain(void)2493 _PyTrash_thread_destroy_chain(void)
2494 {
2495 PyThreadState *tstate = PyThreadState_GET();
2496 while (tstate->trash_delete_later) {
2497 PyObject *op = tstate->trash_delete_later;
2498 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2499
2500 tstate->trash_delete_later =
2501 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2502
2503 /* Call the deallocator directly. This used to try to
2504 * fool Py_DECREF into calling it indirectly, but
2505 * Py_DECREF was already called on this object, and in
2506 * assorted non-release builds calling Py_DECREF again ends
2507 * up distorting allocation statistics.
2508 */
2509 assert(op->ob_refcnt == 0);
2510 ++tstate->trash_delete_nesting;
2511 (*dealloc)(op);
2512 --tstate->trash_delete_nesting;
2513 }
2514 }
2515
2516 #ifdef __cplusplus
2517 }
2518 #endif
2519