1991-02-19 20:39:46 +08:00
|
|
|
/***********************************************************
|
2000-07-01 07:50:40 +08:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
1996-10-25 22:44:06 +08:00
|
|
|
|
2000-07-01 07:50:40 +08:00
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
1991-02-19 20:39:46 +08:00
|
|
|
******************************************************************/
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
/* Generic object operations; and implementation of None (NoObject) */
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
2000-08-23 05:52:51 +08:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
1990-10-14 20:07:46 +08:00
|
|
|
|
2000-07-12 05:47:20 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
#include "macglue.h"
|
|
|
|
#endif
|
|
|
|
|
2000-04-24 23:40:53 +08:00
|
|
|
/* just for trashcan: */
|
|
|
|
#include "compile.h"
|
|
|
|
#include "frameobject.h"
|
|
|
|
#include "traceback.h"
|
|
|
|
|
1995-03-30 00:57:48 +08:00
|
|
|
#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
|
2000-01-21 06:32:56 +08:00
|
|
|
DL_IMPORT(long) _Py_RefTotal;
|
1990-12-20 23:06:42 +08:00
|
|
|
#endif
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
|
|
|
|
These are used by the individual routines for object creation.
|
|
|
|
Do not call them otherwise, they do not initialize the object! */
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1993-10-11 20:54:31 +08:00
|
|
|
#ifdef COUNT_ALLOCS
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyTypeObject *type_list;
|
1993-10-16 00:18:48 +08:00
|
|
|
extern int tuple_zero_allocs, fast_tuple_allocs;
|
|
|
|
extern int quick_int_allocs, quick_neg_int_allocs;
|
1993-10-22 20:04:32 +08:00
|
|
|
extern int null_strings, one_strings;
|
1993-10-11 20:54:31 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
dump_counts(void)
|
1993-10-11 20:54:31 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject *tp;
|
1993-10-11 20:54:31 +08:00
|
|
|
|
|
|
|
for (tp = type_list; tp; tp = tp->tp_next)
|
1993-10-25 16:40:52 +08:00
|
|
|
fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
|
|
|
|
tp->tp_name, tp->tp_alloc, tp->tp_free,
|
|
|
|
tp->tp_maxalloc);
|
|
|
|
fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
|
|
|
|
fast_tuple_allocs, tuple_zero_allocs);
|
|
|
|
fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
|
|
|
|
quick_int_allocs, quick_neg_int_allocs);
|
|
|
|
fprintf(stderr, "null strings: %d, 1-strings: %d\n",
|
|
|
|
null_strings, one_strings);
|
1993-10-11 20:54:31 +08:00
|
|
|
}
|
|
|
|
|
1995-08-29 17:18:14 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
get_counts(void)
|
1995-08-29 17:18:14 +08:00
|
|
|
{
|
|
|
|
PyTypeObject *tp;
|
|
|
|
PyObject *result;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
result = PyList_New(0);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (tp = type_list; tp; tp = tp->tp_next) {
|
|
|
|
v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
|
|
|
|
tp->tp_free, tp->tp_maxalloc);
|
|
|
|
if (v == NULL) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyList_Append(result, v) < 0) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1993-10-11 20:54:31 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
inc_count(PyTypeObject *tp)
|
1993-10-11 20:54:31 +08:00
|
|
|
{
|
|
|
|
if (tp->tp_alloc == 0) {
|
1995-04-06 22:46:26 +08:00
|
|
|
/* first time; insert in linked list */
|
1993-10-11 20:54:31 +08:00
|
|
|
if (tp->tp_next != NULL) /* sanity check */
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_FatalError("XXX inc_count sanity check");
|
1993-10-11 20:54:31 +08:00
|
|
|
tp->tp_next = type_list;
|
|
|
|
type_list = tp;
|
|
|
|
}
|
|
|
|
tp->tp_alloc++;
|
|
|
|
if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
|
|
|
|
tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Init(PyObject *op, PyTypeObject *tp)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
if (op == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"NULL object passed to PyObject_Init");
|
|
|
|
return op;
|
|
|
|
}
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifdef WITH_CYCLE_GC
|
|
|
|
if (PyType_IS_GC(tp))
|
|
|
|
op = (PyObject *) PyObject_FROM_GC(op);
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
|
1990-10-14 20:07:46 +08:00
|
|
|
op->ob_type = tp;
|
1997-05-02 11:12:38 +08:00
|
|
|
_Py_NewReference(op);
|
1990-10-14 20:07:46 +08:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
1997-05-16 05:31:03 +08:00
|
|
|
PyVarObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
|
2000-05-04 07:44:39 +08:00
|
|
|
{
|
|
|
|
if (op == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"NULL object passed to PyObject_InitVar");
|
|
|
|
return op;
|
|
|
|
}
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifdef WITH_CYCLE_GC
|
|
|
|
if (PyType_IS_GC(tp))
|
|
|
|
op = (PyVarObject *) PyObject_FROM_GC(op);
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
/* Any changes should be reflected in PyObject_INIT_VAR */
|
|
|
|
op->ob_size = size;
|
|
|
|
op->ob_type = tp;
|
|
|
|
_Py_NewReference((PyObject *)op);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
_PyObject_New(PyTypeObject *tp)
|
2000-05-04 07:44:39 +08:00
|
|
|
{
|
|
|
|
PyObject *op;
|
|
|
|
op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
|
|
|
|
if (op == NULL)
|
|
|
|
return PyErr_NoMemory();
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifdef WITH_CYCLE_GC
|
|
|
|
if (PyType_IS_GC(tp))
|
|
|
|
op = (PyObject *) PyObject_FROM_GC(op);
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyObject_INIT(op, tp);
|
|
|
|
}
|
|
|
|
|
1997-05-16 05:31:03 +08:00
|
|
|
PyVarObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
_PyObject_NewVar(PyTypeObject *tp, int size)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
PyVarObject *op;
|
|
|
|
op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
|
1990-10-14 20:07:46 +08:00
|
|
|
if (op == NULL)
|
1997-05-16 05:31:03 +08:00
|
|
|
return (PyVarObject *)PyErr_NoMemory();
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifdef WITH_CYCLE_GC
|
|
|
|
if (PyType_IS_GC(tp))
|
|
|
|
op = (PyVarObject *) PyObject_FROM_GC(op);
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyObject_INIT_VAR(op, tp, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_PyObject_Del(PyObject *op)
|
2000-05-04 07:44:39 +08:00
|
|
|
{
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifdef WITH_CYCLE_GC
|
2000-07-01 09:00:38 +08:00
|
|
|
if (op && PyType_IS_GC(op->ob_type)) {
|
|
|
|
op = (PyObject *) PyObject_AS_GC(op);
|
2000-06-30 13:02:53 +08:00
|
|
|
}
|
2000-07-01 09:00:38 +08:00
|
|
|
#endif
|
|
|
|
PyObject_FREE(op);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifndef WITH_CYCLE_GC
|
|
|
|
/* extension modules might need these */
|
|
|
|
void _PyGC_Insert(PyObject *op) { }
|
|
|
|
void _PyGC_Remove(PyObject *op) { }
|
|
|
|
#endif
|
|
|
|
|
1991-06-08 01:10:43 +09:00
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Print(PyObject *op, FILE *fp, int flags)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1991-07-28 06:40:24 +09:00
|
|
|
int ret = 0;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (PyErr_CheckSignals())
|
1991-06-08 01:10:43 +09:00
|
|
|
return -1;
|
1998-04-29 00:06:54 +08:00
|
|
|
#ifdef USE_STACKCHECK
|
|
|
|
if (PyOS_CheckStack()) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2000-01-13 00:28:58 +08:00
|
|
|
clearerr(fp); /* Clear any previous error condition */
|
1991-06-08 01:10:43 +09:00
|
|
|
if (op == NULL) {
|
|
|
|
fprintf(fp, "<nil>");
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
1991-06-08 01:10:43 +09:00
|
|
|
else {
|
|
|
|
if (op->ob_refcnt <= 0)
|
2000-06-30 23:01:00 +08:00
|
|
|
fprintf(fp, "<refcnt %u at %p>",
|
|
|
|
op->ob_refcnt, op);
|
1992-09-04 04:32:55 +08:00
|
|
|
else if (op->ob_type->tp_print == NULL) {
|
|
|
|
if (op->ob_type->tp_repr == NULL) {
|
2000-06-30 23:01:00 +08:00
|
|
|
fprintf(fp, "<%s object at %p>",
|
|
|
|
op->ob_type->tp_name, op);
|
1992-09-04 04:32:55 +08:00
|
|
|
}
|
|
|
|
else {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *s;
|
|
|
|
if (flags & Py_PRINT_RAW)
|
|
|
|
s = PyObject_Str(op);
|
1993-11-05 18:22:19 +08:00
|
|
|
else
|
1997-05-02 11:12:38 +08:00
|
|
|
s = PyObject_Repr(op);
|
1992-09-04 04:32:55 +08:00
|
|
|
if (s == NULL)
|
|
|
|
ret = -1;
|
|
|
|
else {
|
1998-04-22 06:25:01 +08:00
|
|
|
ret = PyObject_Print(s, fp,
|
|
|
|
Py_PRINT_RAW);
|
1992-09-04 04:32:55 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_XDECREF(s);
|
1992-09-04 04:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
1991-06-08 01:10:43 +09:00
|
|
|
else
|
1991-07-28 06:40:24 +09:00
|
|
|
ret = (*op->ob_type->tp_print)(op, fp, flags);
|
1991-06-08 01:10:43 +09:00
|
|
|
}
|
1991-07-28 06:40:24 +09:00
|
|
|
if (ret == 0) {
|
|
|
|
if (ferror(fp)) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1991-07-28 06:40:24 +09:00
|
|
|
clearerr(fp);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Repr(PyObject *v)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
if (PyErr_CheckSignals())
|
1991-06-08 01:10:43 +09:00
|
|
|
return NULL;
|
1998-04-29 00:06:54 +08:00
|
|
|
#ifdef USE_STACKCHECK
|
|
|
|
if (PyOS_CheckStack()) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1991-06-08 01:10:43 +09:00
|
|
|
if (v == NULL)
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString("<NULL>");
|
1991-06-08 01:10:43 +09:00
|
|
|
else if (v->ob_type->tp_repr == NULL) {
|
|
|
|
char buf[120];
|
2000-06-30 23:01:00 +08:00
|
|
|
sprintf(buf, "<%.80s object at %p>",
|
|
|
|
v->ob_type->tp_name, v);
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString(buf);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
2000-03-11 06:55:18 +08:00
|
|
|
else {
|
|
|
|
PyObject *res;
|
|
|
|
res = (*v->ob_type->tp_repr)(v);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2000-07-01 22:31:09 +08:00
|
|
|
if (PyUnicode_Check(res)) {
|
|
|
|
PyObject* str;
|
2000-07-09 01:43:32 +08:00
|
|
|
str = PyUnicode_AsUnicodeEscapeString(res);
|
2000-07-03 17:57:53 +08:00
|
|
|
Py_DECREF(res);
|
|
|
|
if (str)
|
2000-07-01 22:31:09 +08:00
|
|
|
res = str;
|
2000-07-03 17:57:53 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
2000-07-01 22:31:09 +08:00
|
|
|
}
|
2000-03-11 06:55:18 +08:00
|
|
|
if (!PyString_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-10 20:46:51 +08:00
|
|
|
"__repr__ returned non-string (type %.200s)",
|
2000-03-11 06:55:18 +08:00
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Str(PyObject *v)
|
1993-11-05 18:22:19 +08:00
|
|
|
{
|
2000-03-11 06:55:18 +08:00
|
|
|
PyObject *res;
|
|
|
|
|
1993-11-05 18:22:19 +08:00
|
|
|
if (v == NULL)
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString("<NULL>");
|
|
|
|
else if (PyString_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
1993-11-05 18:22:19 +08:00
|
|
|
return v;
|
|
|
|
}
|
1995-01-18 00:35:13 +08:00
|
|
|
else if (v->ob_type->tp_str != NULL)
|
2000-03-11 06:55:18 +08:00
|
|
|
res = (*v->ob_type->tp_str)(v);
|
1993-11-05 18:22:19 +08:00
|
|
|
else {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *func;
|
|
|
|
if (!PyInstance_Check(v) ||
|
|
|
|
(func = PyObject_GetAttrString(v, "__str__")) == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return PyObject_Repr(v);
|
1993-11-05 18:22:19 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1993-11-05 18:22:19 +08:00
|
|
|
}
|
2000-03-11 06:55:18 +08:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2000-07-03 17:57:53 +08:00
|
|
|
if (PyUnicode_Check(res)) {
|
|
|
|
PyObject* str;
|
|
|
|
str = PyUnicode_AsEncodedString(res, NULL, NULL);
|
|
|
|
Py_DECREF(res);
|
|
|
|
if (str)
|
|
|
|
res = str;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-11 06:55:18 +08:00
|
|
|
if (!PyString_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-10 20:46:51 +08:00
|
|
|
"__str__ returned non-string (type %.200s)",
|
2000-03-11 06:55:18 +08:00
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
1993-11-05 18:22:19 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
do_cmp(PyObject *v, PyObject *w)
|
1995-01-12 19:26:10 +08:00
|
|
|
{
|
1997-05-23 08:06:51 +08:00
|
|
|
long c;
|
1995-01-12 19:26:10 +08:00
|
|
|
/* __rcmp__ actually won't be called unless __cmp__ isn't defined,
|
|
|
|
because the check in cmpobject() reverses the objects first.
|
1997-05-02 11:12:38 +08:00
|
|
|
This is intentional -- it makes no sense to define cmp(x,y)
|
|
|
|
different than -cmp(y,x). */
|
|
|
|
if (PyInstance_Check(v) || PyInstance_Check(w))
|
|
|
|
return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
|
1997-05-23 08:06:51 +08:00
|
|
|
c = PyObject_Compare(v, w);
|
|
|
|
if (c && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(c);
|
1995-01-12 19:26:10 +08:00
|
|
|
}
|
|
|
|
|
2000-04-15 03:13:24 +08:00
|
|
|
PyObject *_PyCompareState_Key;
|
|
|
|
|
2000-07-16 20:04:32 +08:00
|
|
|
/* _PyCompareState_nesting is incremented before calling compare (for
|
2000-04-15 03:13:24 +08:00
|
|
|
some types) and decremented on exit. If the count exceeds the
|
|
|
|
nesting limit, enable code to detect circular data structures.
|
|
|
|
*/
|
2000-08-23 05:52:51 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
#define NESTING_LIMIT 60
|
|
|
|
#else
|
2000-04-15 03:13:24 +08:00
|
|
|
#define NESTING_LIMIT 500
|
2000-08-23 05:52:51 +08:00
|
|
|
#endif
|
2000-04-15 03:13:24 +08:00
|
|
|
int _PyCompareState_nesting = 0;
|
|
|
|
|
|
|
|
static PyObject*
|
2000-07-09 23:48:49 +08:00
|
|
|
get_inprogress_dict(void)
|
2000-04-15 03:13:24 +08:00
|
|
|
{
|
|
|
|
PyObject *tstate_dict, *inprogress;
|
|
|
|
|
|
|
|
tstate_dict = PyThreadState_GetDict();
|
|
|
|
if (tstate_dict == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
|
|
|
|
if (inprogress == NULL) {
|
|
|
|
inprogress = PyDict_New();
|
|
|
|
if (inprogress == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
|
|
|
|
inprogress) == -1) {
|
|
|
|
Py_DECREF(inprogress);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-10 00:20:39 +08:00
|
|
|
Py_DECREF(inprogress);
|
2000-04-15 03:13:24 +08:00
|
|
|
}
|
|
|
|
return inprogress;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
make_pair(PyObject *v, PyObject *w)
|
2000-04-15 03:13:24 +08:00
|
|
|
{
|
|
|
|
PyObject *pair;
|
2000-08-18 13:01:19 +08:00
|
|
|
Py_uintptr_t iv = (Py_uintptr_t)v;
|
|
|
|
Py_uintptr_t iw = (Py_uintptr_t)w;
|
2000-04-15 03:13:24 +08:00
|
|
|
|
|
|
|
pair = PyTuple_New(2);
|
|
|
|
if (pair == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-18 13:01:19 +08:00
|
|
|
if (iv <= iw) {
|
2000-04-15 03:13:24 +08:00
|
|
|
PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
|
|
|
|
PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
|
|
|
|
} else {
|
|
|
|
PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
|
|
|
|
PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
|
|
|
|
}
|
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Compare(PyObject *v, PyObject *w)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1998-06-10 02:58:44 +08:00
|
|
|
PyTypeObject *vtp, *wtp;
|
2000-04-15 03:13:24 +08:00
|
|
|
int result;
|
|
|
|
|
2000-08-23 05:52:51 +08:00
|
|
|
#if defined(USE_STACKCHECK)
|
|
|
|
if (PyOS_CheckStack() < 0) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
1997-05-23 08:06:51 +08:00
|
|
|
if (v == NULL || w == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
if (v == w)
|
|
|
|
return 0;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (PyInstance_Check(v) || PyInstance_Check(w)) {
|
|
|
|
PyObject *res;
|
1995-01-12 19:26:10 +08:00
|
|
|
int c;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyInstance_Check(v))
|
|
|
|
return -PyObject_Compare(w, v);
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting++;
|
|
|
|
if (_PyCompareState_nesting > NESTING_LIMIT) {
|
2000-04-15 03:13:24 +08:00
|
|
|
PyObject *inprogress, *pair;
|
|
|
|
|
|
|
|
inprogress = get_inprogress_dict();
|
|
|
|
if (inprogress == NULL) {
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pair = make_pair(v, w);
|
|
|
|
if (PyDict_GetItem(inprogress, pair)) {
|
|
|
|
/* already comparing these objects. assume
|
|
|
|
they're equal until shown otherwise */
|
|
|
|
Py_DECREF(pair);
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(inprogress, pair, pair) == -1) {
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = do_cmp(v, w);
|
|
|
|
/* XXX DelItem shouldn't fail */
|
|
|
|
PyDict_DelItem(inprogress, pair);
|
|
|
|
Py_DECREF(pair);
|
|
|
|
} else {
|
|
|
|
res = do_cmp(v, w);
|
|
|
|
}
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
1997-05-23 08:06:51 +08:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
1997-05-23 08:06:51 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"comparison did not return an int");
|
|
|
|
return -1;
|
1995-01-12 19:26:10 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
c = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
1995-01-12 19:26:10 +08:00
|
|
|
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
|
|
|
|
}
|
1998-06-10 02:58:44 +08:00
|
|
|
if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
|
|
|
|
char *vname = vtp->tp_name;
|
|
|
|
char *wname = wtp->tp_name;
|
1998-07-22 05:56:41 +08:00
|
|
|
if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
|
1997-11-20 00:03:17 +08:00
|
|
|
int err;
|
|
|
|
err = PyNumber_CoerceEx(&v, &w);
|
|
|
|
if (err < 0)
|
1997-05-23 08:06:51 +08:00
|
|
|
return -1;
|
1997-11-20 00:03:17 +08:00
|
|
|
else if (err == 0) {
|
1998-07-22 05:56:41 +08:00
|
|
|
int cmp;
|
|
|
|
vtp = v->ob_type;
|
|
|
|
if (vtp->tp_compare == NULL)
|
|
|
|
cmp = (v < w) ? -1 : 1;
|
|
|
|
else
|
|
|
|
cmp = (*vtp->tp_compare)(v, w);
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1991-07-02 03:48:04 +09:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
}
|
2000-04-10 21:42:33 +08:00
|
|
|
else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
|
|
|
|
int result = PyUnicode_Compare(v, w);
|
|
|
|
if (result == -1 && PyErr_Occurred() &&
|
|
|
|
PyErr_ExceptionMatches(PyExc_TypeError))
|
|
|
|
/* TypeErrors are ignored: if Unicode coercion
|
|
|
|
fails due to one of the arguments not
|
|
|
|
having the right type, we continue as
|
|
|
|
defined by the coercion protocol (see
|
|
|
|
above). Luckily, decoding errors are
|
|
|
|
reported as ValueErrors and are not masked
|
|
|
|
by this technique. */
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
1998-06-10 02:58:44 +08:00
|
|
|
else if (vtp->tp_as_number != NULL)
|
|
|
|
vname = "";
|
|
|
|
else if (wtp->tp_as_number != NULL)
|
|
|
|
wname = "";
|
|
|
|
/* Numerical types compare smaller than all other types */
|
|
|
|
return strcmp(vname, wname);
|
1991-07-02 03:48:04 +09:00
|
|
|
}
|
2000-04-15 03:13:24 +08:00
|
|
|
if (vtp->tp_compare == NULL) {
|
2000-08-18 13:01:19 +08:00
|
|
|
Py_uintptr_t iv = (Py_uintptr_t)v;
|
|
|
|
Py_uintptr_t iw = (Py_uintptr_t)w;
|
|
|
|
return (iv < iw) ? -1 : 1;
|
2000-04-15 03:13:24 +08:00
|
|
|
}
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting++;
|
|
|
|
if (_PyCompareState_nesting > NESTING_LIMIT
|
2000-04-15 03:13:24 +08:00
|
|
|
&& (vtp->tp_as_mapping
|
|
|
|
|| (vtp->tp_as_sequence && !PyString_Check(v)))) {
|
|
|
|
PyObject *inprogress, *pair;
|
|
|
|
|
|
|
|
inprogress = get_inprogress_dict();
|
|
|
|
if (inprogress == NULL) {
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pair = make_pair(v, w);
|
|
|
|
if (PyDict_GetItem(inprogress, pair)) {
|
|
|
|
/* already comparing these objects. assume
|
|
|
|
they're equal until shown otherwise */
|
|
|
|
Py_DECREF(pair);
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(inprogress, pair, pair) == -1) {
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result = (*vtp->tp_compare)(v, w);
|
|
|
|
PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
|
|
|
|
Py_DECREF(pair);
|
|
|
|
} else {
|
|
|
|
result = (*vtp->tp_compare)(v, w);
|
|
|
|
}
|
2000-08-11 08:14:26 +08:00
|
|
|
_PyCompareState_nesting--;
|
2000-04-15 03:13:24 +08:00
|
|
|
return result;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2000-06-30 03:17:04 +08:00
|
|
|
|
|
|
|
/* Set of hash utility functions to help maintaining the invariant that
|
|
|
|
iff a==b then hash(a)==hash(b)
|
|
|
|
|
|
|
|
All the utility functions (_Py_Hash*()) return "-1" to signify an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
long
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_HashDouble(double v)
|
2000-06-30 03:17:04 +08:00
|
|
|
{
|
2000-08-15 11:34:48 +08:00
|
|
|
double intpart, fractpart;
|
|
|
|
int expo;
|
|
|
|
long hipart;
|
|
|
|
long x; /* the final hash value */
|
|
|
|
/* This is designed so that Python numbers of different types
|
|
|
|
* that compare equal hash to the same value; otherwise comparisons
|
|
|
|
* of mapping keys will turn out weird.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
|
|
|
|
{
|
|
|
|
extended e;
|
|
|
|
fractpart = modf(v, &e);
|
|
|
|
intpart = e;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fractpart = modf(v, &intpart);
|
|
|
|
#endif
|
|
|
|
if (fractpart == 0.0) {
|
|
|
|
/* This must return the same hash as an equal int or long. */
|
|
|
|
if (intpart > LONG_MAX || -intpart > LONG_MAX) {
|
|
|
|
/* Convert to long and use its hash. */
|
|
|
|
PyObject *plong; /* converted to Python long */
|
|
|
|
if (Py_IS_INFINITY(intpart))
|
|
|
|
/* can't convert to long int -- arbitrary */
|
|
|
|
v = v < 0 ? -271828.0 : 314159.0;
|
|
|
|
plong = PyLong_FromDouble(v);
|
|
|
|
if (plong == NULL)
|
|
|
|
return -1;
|
|
|
|
x = PyObject_Hash(plong);
|
|
|
|
Py_DECREF(plong);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
/* Fits in a C long == a Python int, so is its own hash. */
|
|
|
|
x = (long)intpart;
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
/* The fractional part is non-zero, so we don't have to worry about
|
|
|
|
* making this match the hash of some other type.
|
|
|
|
* Use frexp to get at the bits in the double.
|
2000-06-30 03:17:04 +08:00
|
|
|
* Since the VAX D double format has 56 mantissa bits, which is the
|
|
|
|
* most of any double format in use, each of these parts may have as
|
|
|
|
* many as (but no more than) 56 significant bits.
|
2000-08-15 11:34:48 +08:00
|
|
|
* So, assuming sizeof(long) >= 4, each part can be broken into two
|
|
|
|
* longs; frexp and multiplication are used to do that.
|
|
|
|
* Also, since the Cray double format has 15 exponent bits, which is
|
|
|
|
* the most of any double format in use, shifting the exponent field
|
|
|
|
* left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
|
2000-06-30 03:17:04 +08:00
|
|
|
*/
|
2000-08-15 11:34:48 +08:00
|
|
|
v = frexp(v, &expo);
|
|
|
|
v *= 2147483648.0; /* 2**31 */
|
|
|
|
hipart = (long)v; /* take the top 32 bits */
|
|
|
|
v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
|
|
|
|
x = hipart + (long)v + (expo << 15);
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
2000-06-30 03:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_HashPointer(void *p)
|
2000-06-30 03:17:04 +08:00
|
|
|
{
|
|
|
|
#if SIZEOF_LONG >= SIZEOF_VOID_P
|
|
|
|
return (long)p;
|
|
|
|
#else
|
|
|
|
/* convert to a Python long and hash that */
|
|
|
|
PyObject* longobj;
|
|
|
|
long x;
|
|
|
|
|
|
|
|
if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
|
|
|
|
x = -1;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
x = PyObject_Hash(longobj);
|
|
|
|
|
|
|
|
finally:
|
|
|
|
Py_XDECREF(longobj);
|
|
|
|
return x;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-03-29 18:43:31 +08:00
|
|
|
long
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Hash(PyObject *v)
|
1993-03-29 18:43:31 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject *tp = v->ob_type;
|
1993-03-29 18:43:31 +08:00
|
|
|
if (tp->tp_hash != NULL)
|
|
|
|
return (*tp->tp_hash)(v);
|
2000-06-30 03:17:04 +08:00
|
|
|
if (tp->tp_compare == NULL) {
|
|
|
|
return _Py_HashPointer(v); /* Use address as hash value */
|
|
|
|
}
|
1993-03-29 18:43:31 +08:00
|
|
|
/* If there's a cmp but no hash defined, the object can't be hashed */
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "unhashable type");
|
1993-03-29 18:43:31 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_GetAttrString(PyObject *v, char *name)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1996-08-10 04:52:03 +08:00
|
|
|
if (v->ob_type->tp_getattro != NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *w, *res;
|
1997-01-18 15:57:16 +08:00
|
|
|
w = PyString_InternFromString(name);
|
1996-08-10 04:52:03 +08:00
|
|
|
if (w == NULL)
|
|
|
|
return NULL;
|
|
|
|
res = (*v->ob_type->tp_getattro)(v, w);
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_XDECREF(w);
|
1996-08-10 04:52:03 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
if (v->ob_type->tp_getattr == NULL) {
|
1998-01-20 06:16:36 +08:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
|
|
|
v->ob_type->tp_name,
|
|
|
|
name);
|
1990-12-20 23:06:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (*v->ob_type->tp_getattr)(v, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-07-12 03:55:34 +08:00
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_HasAttrString(PyObject *v, char *name)
|
1993-07-12 03:55:34 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *res = PyObject_GetAttrString(v, name);
|
1993-07-12 03:55:34 +08:00
|
|
|
if (res != NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(res);
|
1993-07-12 03:55:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_Clear();
|
1993-07-12 03:55:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1996-08-10 04:52:03 +08:00
|
|
|
if (v->ob_type->tp_setattro != NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *s;
|
1996-08-10 04:52:03 +08:00
|
|
|
int res;
|
1997-01-18 15:57:16 +08:00
|
|
|
s = PyString_InternFromString(name);
|
1996-08-10 04:52:03 +08:00
|
|
|
if (s == NULL)
|
1996-09-12 06:51:25 +08:00
|
|
|
return -1;
|
1996-08-10 04:52:03 +08:00
|
|
|
res = (*v->ob_type->tp_setattro)(v, s, w);
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_XDECREF(s);
|
1996-08-10 04:52:03 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
if (v->ob_type->tp_setattr == NULL) {
|
|
|
|
if (v->ob_type->tp_getattr == NULL)
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1991-12-24 21:28:03 +08:00
|
|
|
"attribute-less object (assign or del)");
|
1990-12-20 23:06:42 +08:00
|
|
|
else
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1991-12-24 21:28:03 +08:00
|
|
|
"object has read-only attributes");
|
1990-12-21 07:12:40 +08:00
|
|
|
return -1;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (*v->ob_type->tp_setattr)(v, name, w);
|
|
|
|
}
|
1993-05-12 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
1997-05-21 02:34:44 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_GetAttr(PyObject *v, PyObject *name)
|
1997-05-21 02:34:44 +08:00
|
|
|
{
|
|
|
|
if (v->ob_type->tp_getattro != NULL)
|
|
|
|
return (*v->ob_type->tp_getattro)(v, name);
|
2000-06-23 22:36:32 +08:00
|
|
|
|
|
|
|
if (!PyString_Check(name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_GetAttrString(v, PyString_AS_STRING(name));
|
1997-05-21 02:34:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_HasAttr(PyObject *v, PyObject *name)
|
1997-05-21 02:34:44 +08:00
|
|
|
{
|
|
|
|
PyObject *res = PyObject_GetAttr(v, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
|
1997-05-21 02:34:44 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
Py_INCREF(name);
|
2000-06-23 22:36:32 +08:00
|
|
|
if (PyString_Check(name))
|
|
|
|
PyString_InternInPlace(&name);
|
1997-05-21 02:34:44 +08:00
|
|
|
if (v->ob_type->tp_setattro != NULL)
|
|
|
|
err = (*v->ob_type->tp_setattro)(v, name, value);
|
2000-06-23 22:36:32 +08:00
|
|
|
else if (PyString_Check(name)) {
|
1997-05-21 02:34:44 +08:00
|
|
|
err = PyObject_SetAttrString(
|
2000-06-23 22:36:32 +08:00
|
|
|
v, PyString_AS_STRING(name), value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
err = -1;
|
|
|
|
}
|
1997-05-21 02:34:44 +08:00
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1993-05-12 16:24:20 +08:00
|
|
|
/* Test a value used as condition, e.g., in a for or if statement.
|
|
|
|
Return -1 if an error occurred */
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_IsTrue(PyObject *v)
|
1993-05-12 16:24:20 +08:00
|
|
|
{
|
|
|
|
int res;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (v == Py_None)
|
1993-05-12 16:24:20 +08:00
|
|
|
res = 0;
|
1998-05-22 08:53:24 +08:00
|
|
|
else if (v->ob_type->tp_as_number != NULL &&
|
|
|
|
v->ob_type->tp_as_number->nb_nonzero != NULL)
|
1993-05-12 16:24:20 +08:00
|
|
|
res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
|
1998-05-22 08:53:24 +08:00
|
|
|
else if (v->ob_type->tp_as_mapping != NULL &&
|
|
|
|
v->ob_type->tp_as_mapping->mp_length != NULL)
|
1993-05-12 16:24:20 +08:00
|
|
|
res = (*v->ob_type->tp_as_mapping->mp_length)(v);
|
1998-05-22 08:53:24 +08:00
|
|
|
else if (v->ob_type->tp_as_sequence != NULL &&
|
|
|
|
v->ob_type->tp_as_sequence->sq_length != NULL)
|
1993-05-12 16:24:20 +08:00
|
|
|
res = (*v->ob_type->tp_as_sequence->sq_length)(v);
|
|
|
|
else
|
|
|
|
res = 1;
|
|
|
|
if (res > 0)
|
|
|
|
res = 1;
|
|
|
|
return res;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1998-04-10 01:53:59 +08:00
|
|
|
/* equivalent of 'not v'
|
|
|
|
Return -1 if an error occurred */
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Not(PyObject *v)
|
1998-04-10 01:53:59 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
res = PyObject_IsTrue(v);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
return res == 0;
|
|
|
|
}
|
|
|
|
|
1995-01-10 23:26:20 +08:00
|
|
|
/* Coerce two numeric types to the "larger" one.
|
|
|
|
Increment the reference count on each argument.
|
|
|
|
Return -1 and raise an exception if no coercion is possible
|
|
|
|
(and then no reference count is incremented).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
|
1995-01-10 23:26:20 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
register PyObject *v = *pv;
|
|
|
|
register PyObject *w = *pw;
|
1995-01-10 23:26:20 +08:00
|
|
|
int res;
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
Py_INCREF(w);
|
1995-01-10 23:26:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
|
|
|
|
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
|
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
|
|
|
|
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
|
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
}
|
1997-11-20 00:03:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyNumber_Coerce(PyObject **pv, PyObject **pw)
|
1997-11-20 00:03:17 +08:00
|
|
|
{
|
|
|
|
int err = PyNumber_CoerceEx(pv, pw);
|
|
|
|
if (err <= 0)
|
|
|
|
return err;
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "number coercion failed");
|
1995-01-10 23:26:20 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1995-01-26 08:38:22 +08:00
|
|
|
/* Test whether an object can be called */
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
PyCallable_Check(PyObject *x)
|
1995-01-26 08:38:22 +08:00
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
return 0;
|
|
|
|
if (x->ob_type->tp_call != NULL ||
|
1997-05-02 11:12:38 +08:00
|
|
|
PyFunction_Check(x) ||
|
|
|
|
PyMethod_Check(x) ||
|
|
|
|
PyCFunction_Check(x) ||
|
|
|
|
PyClass_Check(x))
|
1995-01-26 08:38:22 +08:00
|
|
|
return 1;
|
1997-05-02 11:12:38 +08:00
|
|
|
if (PyInstance_Check(x)) {
|
|
|
|
PyObject *call = PyObject_GetAttrString(x, "__call__");
|
1995-01-26 08:38:22 +08:00
|
|
|
if (call == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_Clear();
|
1995-01-26 08:38:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Could test recursively but don't, for fear of endless
|
|
|
|
recursion if some joker sets self.__call__ = self */
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(call);
|
1995-01-26 08:38:22 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/*
|
|
|
|
NoObject is usable as a non-NULL undefined value, used by the macro None.
|
|
|
|
There is (and should be!) no way to create other objects of this type,
|
1990-12-20 23:06:42 +08:00
|
|
|
so there is exactly one (which is indestructible, by the way).
|
1990-10-14 20:07:46 +08:00
|
|
|
*/
|
|
|
|
|
1992-03-28 01:26:13 +08:00
|
|
|
/* ARGSUSED */
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
none_repr(PyObject *op)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyString_FromString("None");
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyTypeObject PyNothing_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
1990-12-20 23:06:42 +08:00
|
|
|
"None",
|
1990-10-14 20:07:46 +08:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, /*tp_dealloc*/ /*never called*/
|
1992-09-18 01:54:56 +08:00
|
|
|
0, /*tp_print*/
|
1990-12-20 23:06:42 +08:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
1994-08-30 16:27:36 +08:00
|
|
|
(reprfunc)none_repr, /*tp_repr*/
|
1990-12-20 23:06:42 +08:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1993-03-29 18:43:31 +08:00
|
|
|
0, /*tp_hash */
|
1990-10-14 20:07:46 +08:00
|
|
|
};
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject _Py_NoneStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyNothing_Type)
|
1990-10-14 20:07:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1996-05-23 00:34:47 +08:00
|
|
|
#ifdef Py_TRACE_REFS
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject refchain = {&refchain, &refchain};
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1997-08-05 10:04:34 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_ResetReferences(void)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
|
|
|
refchain._ob_prev = refchain._ob_next = &refchain;
|
|
|
|
_Py_RefTotal = 0;
|
|
|
|
}
|
|
|
|
|
1996-08-13 05:32:12 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_NewReference(PyObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
_Py_RefTotal++;
|
1990-10-14 20:07:46 +08:00
|
|
|
op->ob_refcnt = 1;
|
|
|
|
op->_ob_next = refchain._ob_next;
|
|
|
|
op->_ob_prev = &refchain;
|
|
|
|
refchain._ob_next->_ob_prev = op;
|
|
|
|
refchain._ob_next = op;
|
1993-10-11 20:54:31 +08:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inc_count(op->ob_type);
|
|
|
|
#endif
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1996-08-13 05:32:12 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_ForgetReference(register PyObject *op)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2000-01-21 06:32:56 +08:00
|
|
|
#ifdef SLOW_UNREF_CHECK
|
2000-03-11 06:55:18 +08:00
|
|
|
register PyObject *p;
|
2000-01-21 06:32:56 +08:00
|
|
|
#endif
|
1995-01-03 03:07:15 +08:00
|
|
|
if (op->ob_refcnt < 0)
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_FatalError("UNREF negative refcnt");
|
1992-09-04 04:32:55 +08:00
|
|
|
if (op == &refchain ||
|
1995-01-03 03:07:15 +08:00
|
|
|
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_FatalError("UNREF invalid object");
|
1992-09-04 04:32:55 +08:00
|
|
|
#ifdef SLOW_UNREF_CHECK
|
1990-12-20 23:06:42 +08:00
|
|
|
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
|
|
|
|
if (p == op)
|
|
|
|
break;
|
|
|
|
}
|
1995-01-03 03:07:15 +08:00
|
|
|
if (p == &refchain) /* Not found */
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_FatalError("UNREF unknown object");
|
1992-09-04 04:32:55 +08:00
|
|
|
#endif
|
1990-10-14 20:07:46 +08:00
|
|
|
op->_ob_next->_ob_prev = op->_ob_prev;
|
|
|
|
op->_ob_prev->_ob_next = op->_ob_next;
|
1992-09-04 04:32:55 +08:00
|
|
|
op->_ob_next = op->_ob_prev = NULL;
|
1995-04-06 22:46:26 +08:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
op->ob_type->tp_free++;
|
|
|
|
#endif
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1996-08-13 05:32:12 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_Dealloc(PyObject *op)
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1994-09-07 22:36:45 +08:00
|
|
|
destructor dealloc = op->ob_type->tp_dealloc;
|
1997-05-02 11:12:38 +08:00
|
|
|
_Py_ForgetReference(op);
|
2000-06-30 13:02:53 +08:00
|
|
|
#ifndef WITH_CYCLE_GC
|
2000-04-24 23:40:53 +08:00
|
|
|
if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
|
|
|
|
op->ob_type = NULL;
|
2000-06-30 13:02:53 +08:00
|
|
|
#endif
|
1994-09-07 22:36:45 +08:00
|
|
|
(*dealloc)(op);
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1996-08-13 05:32:12 +08:00
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_PrintReferences(FILE *fp)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *op;
|
1997-08-05 10:04:34 +08:00
|
|
|
fprintf(fp, "Remaining objects:\n");
|
1990-10-14 20:07:46 +08:00
|
|
|
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
|
|
|
|
fprintf(fp, "[%d] ", op->ob_refcnt);
|
1997-05-02 11:12:38 +08:00
|
|
|
if (PyObject_Print(op, fp, 0) != 0)
|
|
|
|
PyErr_Clear();
|
1990-10-14 20:07:46 +08:00
|
|
|
putc('\n', fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-08-29 17:18:14 +08:00
|
|
|
PyObject *
|
2000-07-09 23:48:49 +08:00
|
|
|
_Py_GetObjects(PyObject *self, PyObject *args)
|
1995-08-29 17:18:14 +08:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
PyObject *t = NULL;
|
|
|
|
PyObject *res, *op;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i|O", &n, &t))
|
|
|
|
return NULL;
|
|
|
|
op = refchain._ob_next;
|
|
|
|
res = PyList_New(0);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
|
|
|
|
while (op == self || op == args || op == res || op == t ||
|
|
|
|
t != NULL && op->ob_type != (PyTypeObject *) t) {
|
|
|
|
op = op->_ob_next;
|
|
|
|
if (op == &refchain)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (PyList_Append(res, op) < 0) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
op = op->_ob_next;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
#endif
|
1996-01-12 09:24:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Hack to force loading of cobject.o */
|
1996-12-06 05:58:58 +08:00
|
|
|
PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
|
1996-05-23 00:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Hack to force loading of abstract.o */
|
2000-07-12 20:56:19 +08:00
|
|
|
int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
|
1997-08-05 10:04:34 +08:00
|
|
|
|
|
|
|
|
2000-08-16 20:27:23 +08:00
|
|
|
/* Python's malloc wrappers (see pymem.h) */
|
1997-08-05 10:04:34 +08:00
|
|
|
|
2000-07-25 20:56:38 +08:00
|
|
|
void *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyMem_Malloc(size_t nbytes)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyMem_MALLOC(nbytes);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
|
|
|
|
2000-07-25 20:56:38 +08:00
|
|
|
void *
|
|
|
|
PyMem_Realloc(void *p, size_t nbytes)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyMem_REALLOC(p, nbytes);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-25 20:56:38 +08:00
|
|
|
PyMem_Free(void *p)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
PyMem_FREE(p);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
|
|
|
|
2000-05-04 07:44:39 +08:00
|
|
|
|
|
|
|
/* Python's object malloc wrappers (see objimpl.h) */
|
1997-08-05 10:04:34 +08:00
|
|
|
|
2000-07-25 20:56:38 +08:00
|
|
|
void *
|
2000-07-09 23:48:49 +08:00
|
|
|
PyObject_Malloc(size_t nbytes)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyObject_MALLOC(nbytes);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
|
|
|
|
2000-07-25 20:56:38 +08:00
|
|
|
void *
|
|
|
|
PyObject_Realloc(void *p, size_t nbytes)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
return PyObject_REALLOC(p, nbytes);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-25 20:56:38 +08:00
|
|
|
PyObject_Free(void *p)
|
1997-08-05 10:04:34 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
PyObject_FREE(p);
|
1997-08-05 10:04:34 +08:00
|
|
|
}
|
1998-04-11 06:32:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* These methods are used to control infinite recursion in repr, str, print,
|
|
|
|
etc. Container objects that may recursively contain themselves,
|
|
|
|
e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
|
|
|
|
Py_ReprLeave() to avoid infinite recursion.
|
|
|
|
|
|
|
|
Py_ReprEnter() returns 0 the first time it is called for a particular
|
|
|
|
object and 1 every time thereafter. It returns -1 if an exception
|
|
|
|
occurred. Py_ReprLeave() has no return value.
|
|
|
|
|
|
|
|
See dictobject.c and listobject.c for examples of use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define KEY "Py_Repr"
|
|
|
|
|
|
|
|
int
|
2000-07-09 23:48:49 +08:00
|
|
|
Py_ReprEnter(PyObject *obj)
|
1998-04-11 06:32:46 +08:00
|
|
|
{
|
|
|
|
PyObject *dict;
|
|
|
|
PyObject *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
|
|
|
if (dict == NULL)
|
|
|
|
return -1;
|
|
|
|
list = PyDict_GetItemString(dict, KEY);
|
|
|
|
if (list == NULL) {
|
|
|
|
list = PyList_New(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyDict_SetItemString(dict, KEY, list) < 0)
|
|
|
|
return -1;
|
|
|
|
Py_DECREF(list);
|
|
|
|
}
|
|
|
|
i = PyList_GET_SIZE(list);
|
|
|
|
while (--i >= 0) {
|
|
|
|
if (PyList_GET_ITEM(list, i) == obj)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyList_Append(list, obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
Py_ReprLeave(PyObject *obj)
|
1998-04-11 06:32:46 +08:00
|
|
|
{
|
|
|
|
PyObject *dict;
|
|
|
|
PyObject *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
1998-04-11 23:17:34 +08:00
|
|
|
if (dict == NULL)
|
|
|
|
return;
|
1998-04-11 06:32:46 +08:00
|
|
|
list = PyDict_GetItemString(dict, KEY);
|
1998-04-11 23:17:34 +08:00
|
|
|
if (list == NULL || !PyList_Check(list))
|
|
|
|
return;
|
1998-04-11 06:32:46 +08:00
|
|
|
i = PyList_GET_SIZE(list);
|
|
|
|
/* Count backwards because we always expect obj to be list[-1] */
|
|
|
|
while (--i >= 0) {
|
|
|
|
if (PyList_GET_ITEM(list, i) == obj) {
|
|
|
|
PyList_SetSlice(list, i, i + 1, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-14 00:01:29 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
trashcan
|
|
|
|
CT 2k0130
|
|
|
|
non-recursively destroy nested objects
|
|
|
|
|
|
|
|
CT 2k0223
|
|
|
|
everything is now done in a macro.
|
|
|
|
|
|
|
|
CT 2k0305
|
|
|
|
modified to use functions, after Tim Peter's suggestion.
|
|
|
|
|
|
|
|
CT 2k0309
|
|
|
|
modified to restore a possible error.
|
2000-03-26 02:39:19 +08:00
|
|
|
|
|
|
|
CT 2k0325
|
|
|
|
added better safe than sorry check for threadstate
|
2000-04-24 23:40:53 +08:00
|
|
|
|
|
|
|
CT 2k0422
|
|
|
|
complete rewrite. We now build a chain via ob_type
|
|
|
|
and save the limited number of types in ob_refcnt.
|
|
|
|
This is perfect since we don't need any memory.
|
|
|
|
A patch for free-threading would need just a lock.
|
2000-03-14 00:01:29 +08:00
|
|
|
*/
|
|
|
|
|
2000-04-24 23:40:53 +08:00
|
|
|
#define Py_TRASHCAN_TUPLE 1
|
|
|
|
#define Py_TRASHCAN_LIST 2
|
|
|
|
#define Py_TRASHCAN_DICT 3
|
|
|
|
#define Py_TRASHCAN_FRAME 4
|
|
|
|
#define Py_TRASHCAN_TRACEBACK 5
|
|
|
|
/* extend here if other objects want protection */
|
|
|
|
|
2000-03-14 00:01:29 +08:00
|
|
|
int _PyTrash_delete_nesting = 0;
|
2000-04-24 23:40:53 +08:00
|
|
|
|
2000-03-14 00:01:29 +08:00
|
|
|
PyObject * _PyTrash_delete_later = NULL;
|
|
|
|
|
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_PyTrash_deposit_object(PyObject *op)
|
2000-03-14 00:01:29 +08:00
|
|
|
{
|
2000-04-24 23:40:53 +08:00
|
|
|
int typecode;
|
|
|
|
|
|
|
|
if (PyTuple_Check(op))
|
|
|
|
typecode = Py_TRASHCAN_TUPLE;
|
|
|
|
else if (PyList_Check(op))
|
|
|
|
typecode = Py_TRASHCAN_LIST;
|
|
|
|
else if (PyDict_Check(op))
|
|
|
|
typecode = Py_TRASHCAN_DICT;
|
|
|
|
else if (PyFrame_Check(op))
|
|
|
|
typecode = Py_TRASHCAN_FRAME;
|
|
|
|
else if (PyTraceBack_Check(op))
|
|
|
|
typecode = Py_TRASHCAN_TRACEBACK;
|
2000-08-04 23:36:13 +08:00
|
|
|
else /* We have a bug here -- those are the only types in GC */ {
|
|
|
|
Py_FatalError("Type not supported in GC -- internal bug");
|
|
|
|
return; /* pacify compiler -- execution never here */
|
|
|
|
}
|
2000-04-24 23:40:53 +08:00
|
|
|
op->ob_refcnt = typecode;
|
|
|
|
|
|
|
|
op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
|
|
|
|
_PyTrash_delete_later = op;
|
2000-03-14 00:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-09 23:48:49 +08:00
|
|
|
_PyTrash_destroy_chain(void)
|
2000-03-14 00:01:29 +08:00
|
|
|
{
|
|
|
|
while (_PyTrash_delete_later) {
|
|
|
|
PyObject *shredder = _PyTrash_delete_later;
|
2000-04-24 23:40:53 +08:00
|
|
|
_PyTrash_delete_later = (PyObject*) shredder->ob_type;
|
|
|
|
|
|
|
|
switch (shredder->ob_refcnt) {
|
|
|
|
case Py_TRASHCAN_TUPLE:
|
|
|
|
shredder->ob_type = &PyTuple_Type;
|
|
|
|
break;
|
|
|
|
case Py_TRASHCAN_LIST:
|
|
|
|
shredder->ob_type = &PyList_Type;
|
|
|
|
break;
|
|
|
|
case Py_TRASHCAN_DICT:
|
|
|
|
shredder->ob_type = &PyDict_Type;
|
|
|
|
break;
|
|
|
|
case Py_TRASHCAN_FRAME:
|
|
|
|
shredder->ob_type = &PyFrame_Type;
|
|
|
|
break;
|
|
|
|
case Py_TRASHCAN_TRACEBACK:
|
|
|
|
shredder->ob_type = &PyTraceBack_Type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_Py_NewReference(shredder);
|
|
|
|
|
2000-03-14 00:01:29 +08:00
|
|
|
++_PyTrash_delete_nesting;
|
|
|
|
Py_DECREF(shredder);
|
|
|
|
--_PyTrash_delete_nesting;
|
|
|
|
}
|
|
|
|
}
|