The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Objects / object.c
blob69ad23d2f94573e204c8f66d3651cdb00d66d237
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Generic object operations; and implementation of None (NoObject) */
34 #include "Python.h"
36 #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
37 DL_IMPORT(long) _Py_RefTotal;
38 #endif
40 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
41 These are used by the individual routines for object creation.
42 Do not call them otherwise, they do not initialize the object! */
44 #ifdef COUNT_ALLOCS
45 static PyTypeObject *type_list;
46 extern int tuple_zero_allocs, fast_tuple_allocs;
47 extern int quick_int_allocs, quick_neg_int_allocs;
48 extern int null_strings, one_strings;
49 void
50 dump_counts()
52 PyTypeObject *tp;
54 for (tp = type_list; tp; tp = tp->tp_next)
55 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
56 tp->tp_name, tp->tp_alloc, tp->tp_free,
57 tp->tp_maxalloc);
58 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
59 fast_tuple_allocs, tuple_zero_allocs);
60 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
61 quick_int_allocs, quick_neg_int_allocs);
62 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
63 null_strings, one_strings);
66 PyObject *
67 get_counts()
69 PyTypeObject *tp;
70 PyObject *result;
71 PyObject *v;
73 result = PyList_New(0);
74 if (result == NULL)
75 return NULL;
76 for (tp = type_list; tp; tp = tp->tp_next) {
77 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
78 tp->tp_free, tp->tp_maxalloc);
79 if (v == NULL) {
80 Py_DECREF(result);
81 return NULL;
83 if (PyList_Append(result, v) < 0) {
84 Py_DECREF(v);
85 Py_DECREF(result);
86 return NULL;
88 Py_DECREF(v);
90 return result;
93 void
94 inc_count(tp)
95 PyTypeObject *tp;
97 if (tp->tp_alloc == 0) {
98 /* first time; insert in linked list */
99 if (tp->tp_next != NULL) /* sanity check */
100 Py_FatalError("XXX inc_count sanity check");
101 tp->tp_next = type_list;
102 type_list = tp;
104 tp->tp_alloc++;
105 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
106 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
108 #endif
110 #ifndef MS_COREDLL
111 PyObject *
112 _PyObject_New(tp)
113 PyTypeObject *tp;
114 #else
115 PyObject *
116 _PyObject_New(tp,op)
117 PyTypeObject *tp;
118 PyObject *op;
119 #endif
121 #ifndef MS_COREDLL
122 PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
123 #endif
124 if (op == NULL)
125 return PyErr_NoMemory();
126 op->ob_type = tp;
127 _Py_NewReference(op);
128 return op;
131 #ifndef MS_COREDLL
132 PyVarObject *
133 _PyObject_NewVar(tp, size)
134 PyTypeObject *tp;
135 int size;
136 #else
137 PyVarObject *
138 _PyObject_NewVar(tp, size, op)
139 PyTypeObject *tp;
140 int size;
141 PyVarObject *op;
142 #endif
144 #ifndef MS_COREDLL
145 PyVarObject *op = (PyVarObject *)
146 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
147 #endif
148 if (op == NULL)
149 return (PyVarObject *)PyErr_NoMemory();
150 op->ob_type = tp;
151 op->ob_size = size;
152 _Py_NewReference((PyObject *)op);
153 return op;
157 PyObject_Print(op, fp, flags)
158 PyObject *op;
159 FILE *fp;
160 int flags;
162 int ret = 0;
163 if (PyErr_CheckSignals())
164 return -1;
165 #ifdef USE_STACKCHECK
166 if (PyOS_CheckStack()) {
167 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
168 return -1;
170 #endif
171 clearerr(fp); /* Clear any previous error condition */
172 if (op == NULL) {
173 fprintf(fp, "<nil>");
175 else {
176 if (op->ob_refcnt <= 0)
177 fprintf(fp, "<refcnt %u at %lx>",
178 op->ob_refcnt, (long)op);
179 else if (op->ob_type->tp_print == NULL) {
180 if (op->ob_type->tp_repr == NULL) {
181 fprintf(fp, "<%s object at %lx>",
182 op->ob_type->tp_name, (long)op);
184 else {
185 PyObject *s;
186 if (flags & Py_PRINT_RAW)
187 s = PyObject_Str(op);
188 else
189 s = PyObject_Repr(op);
190 if (s == NULL)
191 ret = -1;
192 else if (!PyString_Check(s)) {
193 PyErr_SetString(PyExc_TypeError,
194 "repr not string");
195 ret = -1;
197 else {
198 ret = PyObject_Print(s, fp,
199 Py_PRINT_RAW);
201 Py_XDECREF(s);
204 else
205 ret = (*op->ob_type->tp_print)(op, fp, flags);
207 if (ret == 0) {
208 if (ferror(fp)) {
209 PyErr_SetFromErrno(PyExc_IOError);
210 clearerr(fp);
211 ret = -1;
214 return ret;
217 PyObject *
218 PyObject_Repr(v)
219 PyObject *v;
221 if (PyErr_CheckSignals())
222 return NULL;
223 #ifdef USE_STACKCHECK
224 if (PyOS_CheckStack()) {
225 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
226 return NULL;
228 #endif
229 if (v == NULL)
230 return PyString_FromString("<NULL>");
231 else if (v->ob_type->tp_repr == NULL) {
232 char buf[120];
233 sprintf(buf, "<%.80s object at %lx>",
234 v->ob_type->tp_name, (long)v);
235 return PyString_FromString(buf);
237 else
238 return (*v->ob_type->tp_repr)(v);
241 PyObject *
242 PyObject_Str(v)
243 PyObject *v;
245 if (v == NULL)
246 return PyString_FromString("<NULL>");
247 else if (PyString_Check(v)) {
248 Py_INCREF(v);
249 return v;
251 else if (v->ob_type->tp_str != NULL)
252 return (*v->ob_type->tp_str)(v);
253 else {
254 PyObject *func;
255 PyObject *res;
256 if (!PyInstance_Check(v) ||
257 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
258 PyErr_Clear();
259 return PyObject_Repr(v);
261 res = PyEval_CallObject(func, (PyObject *)NULL);
262 Py_DECREF(func);
263 return res;
267 static PyObject *
268 do_cmp(v, w)
269 PyObject *v, *w;
271 long c;
272 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
273 because the check in cmpobject() reverses the objects first.
274 This is intentional -- it makes no sense to define cmp(x,y)
275 different than -cmp(y,x). */
276 if (PyInstance_Check(v) || PyInstance_Check(w))
277 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
278 c = PyObject_Compare(v, w);
279 if (c && PyErr_Occurred())
280 return NULL;
281 return PyInt_FromLong(c);
285 PyObject_Compare(v, w)
286 PyObject *v, *w;
288 PyTypeObject *vtp, *wtp;
289 if (v == NULL || w == NULL) {
290 PyErr_BadInternalCall();
291 return -1;
293 if (v == w)
294 return 0;
295 if (PyInstance_Check(v) || PyInstance_Check(w)) {
296 PyObject *res;
297 int c;
298 if (!PyInstance_Check(v))
299 return -PyObject_Compare(w, v);
300 res = do_cmp(v, w);
301 if (res == NULL)
302 return -1;
303 if (!PyInt_Check(res)) {
304 Py_DECREF(res);
305 PyErr_SetString(PyExc_TypeError,
306 "comparison did not return an int");
307 return -1;
309 c = PyInt_AsLong(res);
310 Py_DECREF(res);
311 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
313 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
314 char *vname = vtp->tp_name;
315 char *wname = wtp->tp_name;
316 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
317 int err;
318 err = PyNumber_CoerceEx(&v, &w);
319 if (err < 0)
320 return -1;
321 else if (err == 0) {
322 int cmp;
323 vtp = v->ob_type;
324 if (vtp->tp_compare == NULL)
325 cmp = (v < w) ? -1 : 1;
326 else
327 cmp = (*vtp->tp_compare)(v, w);
328 Py_DECREF(v);
329 Py_DECREF(w);
330 return cmp;
333 else if (vtp->tp_as_number != NULL)
334 vname = "";
335 else if (wtp->tp_as_number != NULL)
336 wname = "";
337 /* Numerical types compare smaller than all other types */
338 return strcmp(vname, wname);
340 if (vtp->tp_compare == NULL)
341 return (v < w) ? -1 : 1;
342 return (*vtp->tp_compare)(v, w);
345 long
346 PyObject_Hash(v)
347 PyObject *v;
349 PyTypeObject *tp = v->ob_type;
350 if (tp->tp_hash != NULL)
351 return (*tp->tp_hash)(v);
352 if (tp->tp_compare == NULL)
353 return (long) v; /* Use address as hash value */
354 /* If there's a cmp but no hash defined, the object can't be hashed */
355 PyErr_SetString(PyExc_TypeError, "unhashable type");
356 return -1;
359 PyObject *
360 PyObject_GetAttrString(v, name)
361 PyObject *v;
362 char *name;
364 if (v->ob_type->tp_getattro != NULL) {
365 PyObject *w, *res;
366 w = PyString_InternFromString(name);
367 if (w == NULL)
368 return NULL;
369 res = (*v->ob_type->tp_getattro)(v, w);
370 Py_XDECREF(w);
371 return res;
374 if (v->ob_type->tp_getattr == NULL) {
375 PyErr_Format(PyExc_AttributeError,
376 "'%.50s' object has no attribute '%.400s'",
377 v->ob_type->tp_name,
378 name);
379 return NULL;
381 else {
382 return (*v->ob_type->tp_getattr)(v, name);
387 PyObject_HasAttrString(v, name)
388 PyObject *v;
389 char *name;
391 PyObject *res = PyObject_GetAttrString(v, name);
392 if (res != NULL) {
393 Py_DECREF(res);
394 return 1;
396 PyErr_Clear();
397 return 0;
401 PyObject_SetAttrString(v, name, w)
402 PyObject *v;
403 char *name;
404 PyObject *w;
406 if (v->ob_type->tp_setattro != NULL) {
407 PyObject *s;
408 int res;
409 s = PyString_InternFromString(name);
410 if (s == NULL)
411 return -1;
412 res = (*v->ob_type->tp_setattro)(v, s, w);
413 Py_XDECREF(s);
414 return res;
417 if (v->ob_type->tp_setattr == NULL) {
418 if (v->ob_type->tp_getattr == NULL)
419 PyErr_SetString(PyExc_TypeError,
420 "attribute-less object (assign or del)");
421 else
422 PyErr_SetString(PyExc_TypeError,
423 "object has read-only attributes");
424 return -1;
426 else {
427 return (*v->ob_type->tp_setattr)(v, name, w);
431 PyObject *
432 PyObject_GetAttr(v, name)
433 PyObject *v;
434 PyObject *name;
436 if (v->ob_type->tp_getattro != NULL)
437 return (*v->ob_type->tp_getattro)(v, name);
438 else
439 return PyObject_GetAttrString(v, PyString_AsString(name));
443 PyObject_HasAttr(v, name)
444 PyObject *v;
445 PyObject *name;
447 PyObject *res = PyObject_GetAttr(v, name);
448 if (res != NULL) {
449 Py_DECREF(res);
450 return 1;
452 PyErr_Clear();
453 return 0;
457 PyObject_SetAttr(v, name, value)
458 PyObject *v;
459 PyObject *name;
460 PyObject *value;
462 int err;
463 Py_INCREF(name);
464 PyString_InternInPlace(&name);
465 if (v->ob_type->tp_setattro != NULL)
466 err = (*v->ob_type->tp_setattro)(v, name, value);
467 else
468 err = PyObject_SetAttrString(
469 v, PyString_AsString(name), value);
470 Py_DECREF(name);
471 return err;
474 /* Test a value used as condition, e.g., in a for or if statement.
475 Return -1 if an error occurred */
478 PyObject_IsTrue(v)
479 PyObject *v;
481 int res;
482 if (v == Py_None)
483 res = 0;
484 else if (v->ob_type->tp_as_number != NULL &&
485 v->ob_type->tp_as_number->nb_nonzero != NULL)
486 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
487 else if (v->ob_type->tp_as_mapping != NULL &&
488 v->ob_type->tp_as_mapping->mp_length != NULL)
489 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
490 else if (v->ob_type->tp_as_sequence != NULL &&
491 v->ob_type->tp_as_sequence->sq_length != NULL)
492 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
493 else
494 res = 1;
495 if (res > 0)
496 res = 1;
497 return res;
500 /* equivalent of 'not v'
501 Return -1 if an error occurred */
504 PyObject_Not(v)
505 PyObject *v;
507 int res;
508 res = PyObject_IsTrue(v);
509 if (res < 0)
510 return res;
511 return res == 0;
514 /* Coerce two numeric types to the "larger" one.
515 Increment the reference count on each argument.
516 Return -1 and raise an exception if no coercion is possible
517 (and then no reference count is incremented).
521 PyNumber_CoerceEx(pv, pw)
522 PyObject **pv, **pw;
524 register PyObject *v = *pv;
525 register PyObject *w = *pw;
526 int res;
528 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
529 Py_INCREF(v);
530 Py_INCREF(w);
531 return 0;
533 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
534 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
535 if (res <= 0)
536 return res;
538 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
539 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
540 if (res <= 0)
541 return res;
543 return 1;
547 PyNumber_Coerce(pv, pw)
548 PyObject **pv, **pw;
550 int err = PyNumber_CoerceEx(pv, pw);
551 if (err <= 0)
552 return err;
553 PyErr_SetString(PyExc_TypeError, "number coercion failed");
554 return -1;
558 /* Test whether an object can be called */
561 PyCallable_Check(x)
562 PyObject *x;
564 if (x == NULL)
565 return 0;
566 if (x->ob_type->tp_call != NULL ||
567 PyFunction_Check(x) ||
568 PyMethod_Check(x) ||
569 PyCFunction_Check(x) ||
570 PyClass_Check(x))
571 return 1;
572 if (PyInstance_Check(x)) {
573 PyObject *call = PyObject_GetAttrString(x, "__call__");
574 if (call == NULL) {
575 PyErr_Clear();
576 return 0;
578 /* Could test recursively but don't, for fear of endless
579 recursion if some joker sets self.__call__ = self */
580 Py_DECREF(call);
581 return 1;
583 return 0;
588 NoObject is usable as a non-NULL undefined value, used by the macro None.
589 There is (and should be!) no way to create other objects of this type,
590 so there is exactly one (which is indestructible, by the way).
593 /* ARGSUSED */
594 static PyObject *
595 none_repr(op)
596 PyObject *op;
598 return PyString_FromString("None");
601 static PyTypeObject PyNothing_Type = {
602 PyObject_HEAD_INIT(&PyType_Type)
604 "None",
607 0, /*tp_dealloc*/ /*never called*/
608 0, /*tp_print*/
609 0, /*tp_getattr*/
610 0, /*tp_setattr*/
611 0, /*tp_compare*/
612 (reprfunc)none_repr, /*tp_repr*/
613 0, /*tp_as_number*/
614 0, /*tp_as_sequence*/
615 0, /*tp_as_mapping*/
616 0, /*tp_hash */
619 PyObject _Py_NoneStruct = {
620 PyObject_HEAD_INIT(&PyNothing_Type)
624 #ifdef Py_TRACE_REFS
626 static PyObject refchain = {&refchain, &refchain};
628 void
629 _Py_ResetReferences()
631 refchain._ob_prev = refchain._ob_next = &refchain;
632 _Py_RefTotal = 0;
635 void
636 _Py_NewReference(op)
637 PyObject *op;
639 _Py_RefTotal++;
640 op->ob_refcnt = 1;
641 op->_ob_next = refchain._ob_next;
642 op->_ob_prev = &refchain;
643 refchain._ob_next->_ob_prev = op;
644 refchain._ob_next = op;
645 #ifdef COUNT_ALLOCS
646 inc_count(op->ob_type);
647 #endif
650 void
651 _Py_ForgetReference(op)
652 register PyObject *op;
654 #ifdef SLOW_UNREF_CHECK
655 register PyObject *p;
656 #endif
657 if (op->ob_refcnt < 0)
658 Py_FatalError("UNREF negative refcnt");
659 if (op == &refchain ||
660 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
661 Py_FatalError("UNREF invalid object");
662 #ifdef SLOW_UNREF_CHECK
663 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
664 if (p == op)
665 break;
667 if (p == &refchain) /* Not found */
668 Py_FatalError("UNREF unknown object");
669 #endif
670 op->_ob_next->_ob_prev = op->_ob_prev;
671 op->_ob_prev->_ob_next = op->_ob_next;
672 op->_ob_next = op->_ob_prev = NULL;
673 #ifdef COUNT_ALLOCS
674 op->ob_type->tp_free++;
675 #endif
678 void
679 _Py_Dealloc(op)
680 PyObject *op;
682 destructor dealloc = op->ob_type->tp_dealloc;
683 _Py_ForgetReference(op);
684 op->ob_type = NULL;
685 (*dealloc)(op);
688 void
689 _Py_PrintReferences(fp)
690 FILE *fp;
692 PyObject *op;
693 fprintf(fp, "Remaining objects:\n");
694 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
695 fprintf(fp, "[%d] ", op->ob_refcnt);
696 if (PyObject_Print(op, fp, 0) != 0)
697 PyErr_Clear();
698 putc('\n', fp);
702 PyObject *
703 _Py_GetObjects(self, args)
704 PyObject *self;
705 PyObject *args;
707 int i, n;
708 PyObject *t = NULL;
709 PyObject *res, *op;
711 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
712 return NULL;
713 op = refchain._ob_next;
714 res = PyList_New(0);
715 if (res == NULL)
716 return NULL;
717 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
718 while (op == self || op == args || op == res || op == t ||
719 t != NULL && op->ob_type != (PyTypeObject *) t) {
720 op = op->_ob_next;
721 if (op == &refchain)
722 return res;
724 if (PyList_Append(res, op) < 0) {
725 Py_DECREF(res);
726 return NULL;
728 op = op->_ob_next;
730 return res;
733 #endif
736 /* Hack to force loading of cobject.o */
737 PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
740 /* Hack to force loading of abstract.o */
741 int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
744 /* Malloc wrappers (see mymalloc.h) */
746 /* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
748 ANY *
749 Py_Malloc(nbytes)
750 size_t nbytes;
752 ANY *p;
753 #if _PyMem_EXTRA > 0
754 if (nbytes == 0)
755 nbytes = _PyMem_EXTRA;
756 #endif
757 p = malloc(nbytes);
758 if (p != NULL)
759 return p;
760 else {
761 PyErr_NoMemory();
762 return NULL;
766 ANY *
767 Py_Realloc(p, nbytes)
768 ANY *p;
769 size_t nbytes;
771 #if _PyMem_EXTRA > 0
772 if (nbytes == 0)
773 nbytes = _PyMem_EXTRA;
774 #endif
775 p = realloc(p, nbytes);
776 if (p != NULL)
777 return p;
778 else {
779 PyErr_NoMemory();
780 return NULL;
784 void
785 Py_Free(p)
786 ANY *p;
788 free(p);
791 /* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
793 ANY *
794 PyMem_Malloc(nbytes)
795 size_t nbytes;
797 #if _PyMem_EXTRA > 0
798 if (nbytes == 0)
799 nbytes = _PyMem_EXTRA;
800 #endif
801 return malloc(nbytes);
804 ANY *
805 PyMem_Realloc(p, nbytes)
806 ANY *p;
807 size_t nbytes;
809 #if _PyMem_EXTRA > 0
810 if (nbytes == 0)
811 nbytes = _PyMem_EXTRA;
812 #endif
813 return realloc(p, nbytes);
816 void
817 PyMem_Free(p)
818 ANY *p;
820 free(p);
824 /* These methods are used to control infinite recursion in repr, str, print,
825 etc. Container objects that may recursively contain themselves,
826 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
827 Py_ReprLeave() to avoid infinite recursion.
829 Py_ReprEnter() returns 0 the first time it is called for a particular
830 object and 1 every time thereafter. It returns -1 if an exception
831 occurred. Py_ReprLeave() has no return value.
833 See dictobject.c and listobject.c for examples of use.
836 #define KEY "Py_Repr"
839 Py_ReprEnter(obj)
840 PyObject *obj;
842 PyObject *dict;
843 PyObject *list;
844 int i;
846 dict = PyThreadState_GetDict();
847 if (dict == NULL)
848 return -1;
849 list = PyDict_GetItemString(dict, KEY);
850 if (list == NULL) {
851 list = PyList_New(0);
852 if (list == NULL)
853 return -1;
854 if (PyDict_SetItemString(dict, KEY, list) < 0)
855 return -1;
856 Py_DECREF(list);
858 i = PyList_GET_SIZE(list);
859 while (--i >= 0) {
860 if (PyList_GET_ITEM(list, i) == obj)
861 return 1;
863 PyList_Append(list, obj);
864 return 0;
867 void
868 Py_ReprLeave(obj)
869 PyObject *obj;
871 PyObject *dict;
872 PyObject *list;
873 int i;
875 dict = PyThreadState_GetDict();
876 if (dict == NULL)
877 return;
878 list = PyDict_GetItemString(dict, KEY);
879 if (list == NULL || !PyList_Check(list))
880 return;
881 i = PyList_GET_SIZE(list);
882 /* Count backwards because we always expect obj to be list[-1] */
883 while (--i >= 0) {
884 if (PyList_GET_ITEM(list, i) == obj) {
885 PyList_SetSlice(list, i, i + 1, NULL);
886 break;