1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:07:38 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Generic object operations; and implementation of None (NoObject) */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1995-03-29 12:57:48 -04:00
|
|
|
#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
|
1997-05-02 00:12:38 -03:00
|
|
|
long _Py_RefTotal;
|
1990-12-20 11:06:42 -04:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1990-12-20 11:06:42 -04: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 09:07:46 -03:00
|
|
|
|
1993-10-11 09:54:31 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyTypeObject *type_list;
|
1993-10-15 13:18:48 -03:00
|
|
|
extern int tuple_zero_allocs, fast_tuple_allocs;
|
|
|
|
extern int quick_int_allocs, quick_neg_int_allocs;
|
1993-10-22 09:04:32 -03:00
|
|
|
extern int null_strings, one_strings;
|
1993-10-11 09:54:31 -03:00
|
|
|
void
|
|
|
|
dump_counts()
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject *tp;
|
1993-10-11 09:54:31 -03:00
|
|
|
|
|
|
|
for (tp = type_list; tp; tp = tp->tp_next)
|
1993-10-25 05:40:52 -03: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 09:54:31 -03:00
|
|
|
}
|
|
|
|
|
1995-08-29 06:18:14 -03:00
|
|
|
PyObject *
|
|
|
|
get_counts()
|
|
|
|
{
|
|
|
|
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 09:54:31 -03:00
|
|
|
void
|
|
|
|
inc_count(tp)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject *tp;
|
1993-10-11 09:54:31 -03:00
|
|
|
{
|
|
|
|
if (tp->tp_alloc == 0) {
|
1995-04-06 11:46:26 -03:00
|
|
|
/* first time; insert in linked list */
|
1993-10-11 09:54:31 -03:00
|
|
|
if (tp->tp_next != NULL) /* sanity check */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_FatalError("XXX inc_count sanity check");
|
1993-10-11 09:54:31 -03: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
|
|
|
|
|
1996-07-20 23:30:39 -03:00
|
|
|
#ifndef MS_COREDLL
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
_PyObject_New(tp)
|
|
|
|
PyTypeObject *tp;
|
1996-07-20 23:30:39 -03:00
|
|
|
#else
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
_PyObject_New(tp,op)
|
|
|
|
PyTypeObject *tp;
|
1996-07-20 23:30:39 -03:00
|
|
|
PyObject *op;
|
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1996-07-20 23:30:39 -03:00
|
|
|
#ifndef MS_COREDLL
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
|
1996-07-20 23:30:39 -03:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyErr_NoMemory();
|
1990-10-14 09:07:46 -03:00
|
|
|
op->ob_type = tp;
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_NewReference(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
1996-07-20 23:30:39 -03:00
|
|
|
#ifndef MS_COREDLL
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *
|
1997-05-02 00:12:38 -03:00
|
|
|
_PyObject_NewVar(tp, size)
|
|
|
|
PyTypeObject *tp;
|
1995-02-10 13:00:27 -04:00
|
|
|
int size;
|
1996-07-20 23:30:39 -03:00
|
|
|
#else
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *
|
1997-05-02 00:12:38 -03:00
|
|
|
_PyObject_NewVar(tp, size, op)
|
|
|
|
PyTypeObject *tp;
|
1996-07-20 23:30:39 -03:00
|
|
|
int size;
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *op;
|
1996-07-20 23:30:39 -03:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1996-07-20 23:30:39 -03:00
|
|
|
#ifndef MS_COREDLL
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *op = (PyVarObject *)
|
1990-10-14 09:07:46 -03:00
|
|
|
malloc(tp->tp_basicsize + size * tp->tp_itemsize);
|
1996-07-20 23:30:39 -03:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op == NULL)
|
1997-05-15 18:31:03 -03:00
|
|
|
return (PyVarObject *)PyErr_NoMemory();
|
1990-10-14 09:07:46 -03:00
|
|
|
op->ob_type = tp;
|
|
|
|
op->ob_size = size;
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_NewReference(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
1991-06-07 13:10:43 -03:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_Print(op, fp, flags)
|
|
|
|
PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
FILE *fp;
|
|
|
|
int flags;
|
|
|
|
{
|
1991-07-27 18:40:24 -03:00
|
|
|
int ret = 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_CheckSignals())
|
1991-06-07 13:10:43 -03:00
|
|
|
return -1;
|
|
|
|
if (op == NULL) {
|
|
|
|
fprintf(fp, "<nil>");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
else {
|
|
|
|
if (op->ob_refcnt <= 0)
|
1992-09-03 17:32:55 -03:00
|
|
|
fprintf(fp, "<refcnt %u at %lx>",
|
|
|
|
op->ob_refcnt, (long)op);
|
|
|
|
else if (op->ob_type->tp_print == NULL) {
|
|
|
|
if (op->ob_type->tp_repr == NULL) {
|
|
|
|
fprintf(fp, "<%s object at %lx>",
|
|
|
|
op->ob_type->tp_name, (long)op);
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *s;
|
|
|
|
if (flags & Py_PRINT_RAW)
|
|
|
|
s = PyObject_Str(op);
|
1993-11-05 06:22:19 -04:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
s = PyObject_Repr(op);
|
1992-09-03 17:32:55 -03:00
|
|
|
if (s == NULL)
|
|
|
|
ret = -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (!PyString_Check(s)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-03 17:32:55 -03:00
|
|
|
"repr not string");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
fprintf(fp, "%s",
|
|
|
|
PyString_AsString(s));
|
1992-09-03 17:32:55 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(s);
|
1992-09-03 17:32:55 -03:00
|
|
|
}
|
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
else
|
1991-07-27 18:40:24 -03:00
|
|
|
ret = (*op->ob_type->tp_print)(op, fp, flags);
|
1991-06-07 13:10:43 -03:00
|
|
|
}
|
1991-07-27 18:40:24 -03:00
|
|
|
if (ret == 0) {
|
|
|
|
if (ferror(fp)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1991-07-27 18:40:24 -03:00
|
|
|
clearerr(fp);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_Repr(v)
|
|
|
|
PyObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_CheckSignals())
|
1991-06-07 13:10:43 -03:00
|
|
|
return NULL;
|
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
1991-06-07 13:10:43 -03:00
|
|
|
else if (v->ob_type->tp_repr == NULL) {
|
|
|
|
char buf[120];
|
|
|
|
sprintf(buf, "<%.80s object at %lx>",
|
|
|
|
v->ob_type->tp_name, (long)v);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
else
|
|
|
|
return (*v->ob_type->tp_repr)(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_Str(v)
|
|
|
|
PyObject *v;
|
1993-11-05 06:22:19 -04:00
|
|
|
{
|
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
|
|
|
else if (PyString_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
return v;
|
|
|
|
}
|
1995-01-17 12:35:13 -04:00
|
|
|
else if (v->ob_type->tp_str != NULL)
|
|
|
|
return (*v->ob_type->tp_str)(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
|
|
|
if (!PyInstance_Check(v) ||
|
|
|
|
(func = PyObject_GetAttrString(v, "__str__")) == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return PyObject_Repr(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1993-11-05 06:22:19 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1995-01-12 07:26:10 -04:00
|
|
|
do_cmp(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v, *w;
|
1995-01-12 07:26:10 -04:00
|
|
|
{
|
1997-05-22 21:06:51 -03:00
|
|
|
long c;
|
1995-01-12 07:26:10 -04:00
|
|
|
/* __rcmp__ actually won't be called unless __cmp__ isn't defined,
|
|
|
|
because the check in cmpobject() reverses the objects first.
|
1997-05-02 00:12:38 -03: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-22 21:06:51 -03:00
|
|
|
c = PyObject_Compare(v, w);
|
|
|
|
if (c && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(c);
|
1995-01-12 07:26:10 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_Compare(v, w)
|
|
|
|
PyObject *v, *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject *tp;
|
1997-05-22 21:06:51 -03:00
|
|
|
if (v == NULL || w == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
if (v == w)
|
|
|
|
return 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInstance_Check(v) || PyInstance_Check(w)) {
|
|
|
|
PyObject *res;
|
1995-01-12 07:26:10 -04:00
|
|
|
int c;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInstance_Check(v))
|
|
|
|
return -PyObject_Compare(w, v);
|
1995-01-12 07:26:10 -04:00
|
|
|
res = do_cmp(v, w);
|
1997-05-22 21:06:51 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
1997-05-22 21:06:51 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"comparison did not return an int");
|
|
|
|
return -1;
|
1995-01-12 07:26:10 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
c = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
1995-01-12 07:26:10 -04:00
|
|
|
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
|
|
|
|
}
|
1991-07-01 15:48:04 -03:00
|
|
|
if ((tp = v->ob_type) != w->ob_type) {
|
|
|
|
if (tp->tp_as_number != NULL &&
|
|
|
|
w->ob_type->tp_as_number != NULL) {
|
1997-11-19 12:03:17 -04:00
|
|
|
int err;
|
|
|
|
err = PyNumber_CoerceEx(&v, &w);
|
|
|
|
if (err < 0)
|
1997-05-22 21:06:51 -03:00
|
|
|
return -1;
|
1997-11-19 12:03:17 -04:00
|
|
|
else if (err == 0) {
|
1991-07-01 15:48:04 -03:00
|
|
|
int cmp = (*v->ob_type->tp_compare)(v, w);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1991-07-01 15:48:04 -03:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
return strcmp(tp->tp_name, w->ob_type->tp_name);
|
1991-07-01 15:48:04 -03:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
if (tp->tp_compare == NULL)
|
|
|
|
return (v < w) ? -1 : 1;
|
1991-07-01 15:48:04 -03:00
|
|
|
return (*tp->tp_compare)(v, w);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
long
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_Hash(v)
|
|
|
|
PyObject *v;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject *tp = v->ob_type;
|
1993-03-29 06:43:31 -04:00
|
|
|
if (tp->tp_hash != NULL)
|
|
|
|
return (*tp->tp_hash)(v);
|
|
|
|
if (tp->tp_compare == NULL)
|
|
|
|
return (long) v; /* Use address as hash value */
|
|
|
|
/* If there's a cmp but no hash defined, the object can't be hashed */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "unhashable type");
|
1993-03-29 06:43:31 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_GetAttrString(v, name)
|
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
char *name;
|
|
|
|
{
|
1996-08-09 17:52:03 -03:00
|
|
|
if (v->ob_type->tp_getattro != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *w, *res;
|
1997-01-18 03:57:16 -04:00
|
|
|
w = PyString_InternFromString(name);
|
1996-08-09 17:52:03 -03:00
|
|
|
if (w == NULL)
|
|
|
|
return NULL;
|
|
|
|
res = (*v->ob_type->tp_getattro)(v, w);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(w);
|
1996-08-09 17:52:03 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v->ob_type->tp_getattr == NULL) {
|
1998-01-19 18:16:36 -04:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
|
|
|
v->ob_type->tp_name,
|
|
|
|
name);
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (*v->ob_type->tp_getattr)(v, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-07-11 16:55:34 -03:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_HasAttrString(v, name)
|
|
|
|
PyObject *v;
|
1993-07-11 16:55:34 -03:00
|
|
|
char *name;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *res = PyObject_GetAttrString(v, name);
|
1993-07-11 16:55:34 -03:00
|
|
|
if (res != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1993-07-11 16:55:34 -03:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1993-07-11 16:55:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_SetAttrString(v, name, w)
|
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
char *name;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *w;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1996-08-09 17:52:03 -03:00
|
|
|
if (v->ob_type->tp_setattro != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *s;
|
1996-08-09 17:52:03 -03:00
|
|
|
int res;
|
1997-01-18 03:57:16 -04:00
|
|
|
s = PyString_InternFromString(name);
|
1996-08-09 17:52:03 -03:00
|
|
|
if (s == NULL)
|
1996-09-11 19:51:25 -03:00
|
|
|
return -1;
|
1996-08-09 17:52:03 -03:00
|
|
|
res = (*v->ob_type->tp_setattro)(v, s, w);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(s);
|
1996-08-09 17:52:03 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v->ob_type->tp_setattr == NULL) {
|
|
|
|
if (v->ob_type->tp_getattr == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1991-12-24 09:28:03 -04:00
|
|
|
"attribute-less object (assign or del)");
|
1990-12-20 11:06:42 -04:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1991-12-24 09:28:03 -04:00
|
|
|
"object has read-only attributes");
|
1990-12-20 19:12:40 -04:00
|
|
|
return -1;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (*v->ob_type->tp_setattr)(v, name, w);
|
|
|
|
}
|
1993-05-12 05:24:20 -03:00
|
|
|
}
|
|
|
|
|
1997-05-20 15:34:44 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_GetAttr(v, name)
|
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
|
|
|
{
|
|
|
|
if (v->ob_type->tp_getattro != NULL)
|
|
|
|
return (*v->ob_type->tp_getattro)(v, name);
|
|
|
|
else
|
|
|
|
return PyObject_GetAttrString(v, PyString_AsString(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyObject_HasAttr(v, name)
|
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
|
|
|
{
|
|
|
|
PyObject *res = PyObject_GetAttr(v, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyObject_SetAttr(v, name, value)
|
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *value;
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
Py_INCREF(name);
|
|
|
|
PyString_InternInPlace(&name);
|
|
|
|
if (v->ob_type->tp_setattro != NULL)
|
|
|
|
err = (*v->ob_type->tp_setattro)(v, name, value);
|
|
|
|
else
|
|
|
|
err = PyObject_SetAttrString(
|
|
|
|
v, PyString_AsString(name), value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1993-05-12 05:24:20 -03:00
|
|
|
/* Test a value used as condition, e.g., in a for or if statement.
|
|
|
|
Return -1 if an error occurred */
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_IsTrue(v)
|
|
|
|
PyObject *v;
|
1993-05-12 05:24:20 -03:00
|
|
|
{
|
|
|
|
int res;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v == Py_None)
|
1993-05-12 05:24:20 -03:00
|
|
|
res = 0;
|
|
|
|
else if (v->ob_type->tp_as_number != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
|
|
|
|
else if (v->ob_type->tp_as_mapping != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_mapping->mp_length)(v);
|
|
|
|
else if (v->ob_type->tp_as_sequence != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_sequence->sq_length)(v);
|
|
|
|
else
|
|
|
|
res = 1;
|
|
|
|
if (res > 0)
|
|
|
|
res = 1;
|
|
|
|
return res;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1995-01-10 11:26:20 -04: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
|
1997-11-19 12:03:17 -04:00
|
|
|
PyNumber_CoerceEx(pv, pw)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **pv, **pw;
|
1995-01-10 11:26:20 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *v = *pv;
|
|
|
|
register PyObject *w = *pw;
|
1995-01-10 11:26:20 -04:00
|
|
|
int res;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
Py_INCREF(w);
|
1995-01-10 11:26:20 -04: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-19 12:03:17 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyNumber_Coerce(pv, pw)
|
|
|
|
PyObject **pv, **pw;
|
|
|
|
{
|
|
|
|
int err = PyNumber_CoerceEx(pv, pw);
|
|
|
|
if (err <= 0)
|
|
|
|
return err;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "number coercion failed");
|
1995-01-10 11:26:20 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1995-01-25 20:38:22 -04:00
|
|
|
/* Test whether an object can be called */
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyCallable_Check(x)
|
|
|
|
PyObject *x;
|
1995-01-25 20:38:22 -04:00
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
return 0;
|
|
|
|
if (x->ob_type->tp_call != NULL ||
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFunction_Check(x) ||
|
|
|
|
PyMethod_Check(x) ||
|
|
|
|
PyCFunction_Check(x) ||
|
|
|
|
PyClass_Check(x))
|
1995-01-25 20:38:22 -04:00
|
|
|
return 1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInstance_Check(x)) {
|
|
|
|
PyObject *call = PyObject_GetAttrString(x, "__call__");
|
1995-01-25 20:38:22 -04:00
|
|
|
if (call == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1995-01-25 20:38:22 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Could test recursively but don't, for fear of endless
|
|
|
|
recursion if some joker sets self.__call__ = self */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(call);
|
1995-01-25 20:38:22 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1990-10-14 09:07:46 -03: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 11:06:42 -04:00
|
|
|
so there is exactly one (which is indestructible, by the way).
|
1990-10-14 09:07:46 -03:00
|
|
|
*/
|
|
|
|
|
1992-03-27 13:26:13 -04:00
|
|
|
/* ARGSUSED */
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
none_repr(op)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *op;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString("None");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyTypeObject PyNothing_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1990-12-20 11:06:42 -04:00
|
|
|
"None",
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0, /*tp_dealloc*/ /*never called*/
|
1992-09-17 14:54:56 -03:00
|
|
|
0, /*tp_print*/
|
1990-12-20 11:06:42 -04:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
1994-08-30 05:27:36 -03:00
|
|
|
(reprfunc)none_repr, /*tp_repr*/
|
1990-12-20 11:06:42 -04:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1993-03-29 06:43:31 -04:00
|
|
|
0, /*tp_hash */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject _Py_NoneStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyNothing_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1996-05-22 13:34:47 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject refchain = {&refchain, &refchain};
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-08-04 23:04:34 -03:00
|
|
|
void
|
|
|
|
_Py_ResetReferences()
|
|
|
|
{
|
|
|
|
refchain._ob_prev = refchain._ob_next = &refchain;
|
|
|
|
_Py_RefTotal = 0;
|
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_NewReference(op)
|
|
|
|
PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_RefTotal++;
|
1990-10-14 09:07:46 -03: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 09:54:31 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inc_count(op->ob_type);
|
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_ForgetReference(op)
|
|
|
|
register PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *p;
|
1995-01-02 15:07:15 -04:00
|
|
|
if (op->ob_refcnt < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_FatalError("UNREF negative refcnt");
|
1992-09-03 17:32:55 -03:00
|
|
|
if (op == &refchain ||
|
1995-01-02 15:07:15 -04:00
|
|
|
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_FatalError("UNREF invalid object");
|
1992-09-03 17:32:55 -03:00
|
|
|
#ifdef SLOW_UNREF_CHECK
|
1990-12-20 11:06:42 -04:00
|
|
|
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
|
|
|
|
if (p == op)
|
|
|
|
break;
|
|
|
|
}
|
1995-01-02 15:07:15 -04:00
|
|
|
if (p == &refchain) /* Not found */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_FatalError("UNREF unknown object");
|
1992-09-03 17:32:55 -03:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
op->_ob_next->_ob_prev = op->_ob_prev;
|
|
|
|
op->_ob_prev->_ob_next = op->_ob_next;
|
1992-09-03 17:32:55 -03:00
|
|
|
op->_ob_next = op->_ob_prev = NULL;
|
1995-04-06 11:46:26 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
op->ob_type->tp_free++;
|
|
|
|
#endif
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_Dealloc(op)
|
|
|
|
PyObject *op;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1994-09-07 11:36:45 -03:00
|
|
|
destructor dealloc = op->ob_type->tp_dealloc;
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_ForgetReference(op);
|
1992-09-03 17:32:55 -03:00
|
|
|
op->ob_type = NULL;
|
1994-09-07 11:36:45 -03:00
|
|
|
(*dealloc)(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
1996-05-24 17:48:31 -03:00
|
|
|
_Py_PrintReferences(fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
FILE *fp;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *op;
|
1997-08-04 23:04:34 -03:00
|
|
|
fprintf(fp, "Remaining objects:\n");
|
1990-10-14 09:07:46 -03:00
|
|
|
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
|
|
|
|
fprintf(fp, "[%d] ", op->ob_refcnt);
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyObject_Print(op, fp, 0) != 0)
|
|
|
|
PyErr_Clear();
|
1990-10-14 09:07:46 -03:00
|
|
|
putc('\n', fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-08-29 06:18:14 -03:00
|
|
|
PyObject *
|
1996-05-24 17:48:31 -03:00
|
|
|
_Py_GetObjects(self, args)
|
1995-08-29 06:18:14 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
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 09:07:46 -03:00
|
|
|
#endif
|
1996-01-11 21:24:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Hack to force loading of cobject.o */
|
1996-12-05 17:58:58 -04:00
|
|
|
PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
|
1996-05-22 13:34:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Hack to force loading of abstract.o */
|
1997-05-02 00:12:38 -03:00
|
|
|
int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
|
1997-08-04 23:04:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Malloc wrappers (see mymalloc.h) */
|
|
|
|
|
|
|
|
/* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
|
|
|
|
|
|
|
|
ANY *
|
|
|
|
Py_Malloc(nbytes)
|
|
|
|
size_t nbytes;
|
|
|
|
{
|
|
|
|
ANY *p;
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
|
|
|
p = malloc(nbytes);
|
|
|
|
if (p != NULL)
|
|
|
|
return p;
|
1997-08-12 11:54:54 -03:00
|
|
|
else {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-08-04 23:04:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
ANY *
|
|
|
|
Py_Realloc(p, nbytes)
|
|
|
|
ANY *p;
|
|
|
|
size_t nbytes;
|
|
|
|
{
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
|
|
|
p = realloc(p, nbytes);
|
|
|
|
if (p != NULL)
|
|
|
|
return p;
|
1997-08-12 11:54:54 -03:00
|
|
|
else {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-08-04 23:04:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_Free(p)
|
|
|
|
ANY *p;
|
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
|
|
|
|
|
|
|
|
ANY *
|
|
|
|
PyMem_Malloc(nbytes)
|
|
|
|
size_t nbytes;
|
|
|
|
{
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
|
|
|
return malloc(nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ANY *
|
|
|
|
PyMem_Realloc(p, nbytes)
|
|
|
|
ANY *p;
|
|
|
|
size_t nbytes;
|
|
|
|
{
|
|
|
|
#if _PyMem_EXTRA > 0
|
|
|
|
if (nbytes == 0)
|
|
|
|
nbytes = _PyMem_EXTRA;
|
|
|
|
#endif
|
|
|
|
return realloc(p, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PyMem_Free(p)
|
|
|
|
ANY *p;
|
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|