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
|
|
|
|
2000-07-11 18:47:20 -03:00
|
|
|
#ifdef macintosh
|
|
|
|
#include "macglue.h"
|
|
|
|
#endif
|
|
|
|
|
2000-04-24 12:40:53 -03:00
|
|
|
/* just for trashcan: */
|
|
|
|
#include "compile.h"
|
|
|
|
#include "frameobject.h"
|
|
|
|
#include "traceback.h"
|
|
|
|
|
1995-03-29 12:57:48 -04:00
|
|
|
#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
|
2000-01-20 18:32:56 -04:00
|
|
|
DL_IMPORT(long) _Py_RefTotal;
|
1990-12-20 11:06:42 -04:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
|
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
2001-08-31 14:40:15 -03:00
|
|
|
DL_IMPORT(int) Py_DivisionWarningFlag;
|
|
|
|
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
dump_counts(void)
|
1993-10-11 09:54:31 -03:00
|
|
|
{
|
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",
|
2001-08-02 01:15:00 -03:00
|
|
|
tp->tp_name, tp->tp_allocs, tp->tp_frees,
|
1993-10-25 05:40:52 -03:00
|
|
|
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 *
|
2000-07-09 12:48:49 -03:00
|
|
|
get_counts(void)
|
1995-08-29 06:18:14 -03: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) {
|
2001-08-02 01:15:00 -03:00
|
|
|
v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
|
|
|
|
tp->tp_frees, tp->tp_maxalloc);
|
1995-08-29 06:18:14 -03:00
|
|
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
inc_count(PyTypeObject *tp)
|
1993-10-11 09:54:31 -03:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
if (tp->tp_allocs == 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;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
tp->tp_allocs++;
|
|
|
|
if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
|
|
|
|
tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
|
1993-10-11 09:54:31 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Init(PyObject *op, PyTypeObject *tp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2000-05-03 20:44:39 -03:00
|
|
|
if (op == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"NULL object passed to PyObject_Init");
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
|
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;
|
|
|
|
}
|
|
|
|
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
|
2000-05-03 20:44:39 -03:00
|
|
|
{
|
|
|
|
if (op == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"NULL object passed to PyObject_InitVar");
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
/* 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 12:48:49 -03:00
|
|
|
_PyObject_New(PyTypeObject *tp)
|
2000-05-03 20:44:39 -03:00
|
|
|
{
|
|
|
|
PyObject *op;
|
|
|
|
op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
|
|
|
|
if (op == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
return PyObject_INIT(op, tp);
|
|
|
|
}
|
|
|
|
|
1997-05-15 18:31:03 -03:00
|
|
|
PyVarObject *
|
2001-10-06 18:27:34 -03:00
|
|
|
_PyObject_NewVar(PyTypeObject *tp, int nitems)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2000-05-03 20:44:39 -03:00
|
|
|
PyVarObject *op;
|
2001-10-07 00:54:51 -03:00
|
|
|
const size_t size = _PyObject_VAR_SIZE(tp, nitems);
|
2001-10-06 18:27:34 -03:00
|
|
|
op = (PyVarObject *) PyObject_MALLOC(size);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op == NULL)
|
1997-05-15 18:31:03 -03:00
|
|
|
return (PyVarObject *)PyErr_NoMemory();
|
2001-10-06 18:27:34 -03:00
|
|
|
return PyObject_INIT_VAR(op, tp, nitems);
|
2000-05-03 20:44:39 -03:00
|
|
|
}
|
|
|
|
|
2002-04-12 00:08:42 -03:00
|
|
|
/* for binary compatibility with 2.2 */
|
|
|
|
#undef _PyObject_Del
|
2000-05-03 20:44:39 -03:00
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_PyObject_Del(PyObject *op)
|
2000-05-03 20:44:39 -03:00
|
|
|
{
|
2000-06-30 22:00:38 -03:00
|
|
|
PyObject_FREE(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-06-07 13:10:43 -03:00
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Print(PyObject *op, FILE *fp, int flags)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
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;
|
1998-04-28 13:06:54 -03:00
|
|
|
#ifdef USE_STACKCHECK
|
|
|
|
if (PyOS_CheckStack()) {
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_SetString(PyExc_MemoryError, "stack overflow");
|
1998-04-28 13:06:54 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2000-01-12 12:28:58 -04:00
|
|
|
clearerr(fp); /* Clear any previous error condition */
|
1991-06-07 13:10:43 -03:00
|
|
|
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)
|
2000-06-30 12:01:00 -03:00
|
|
|
fprintf(fp, "<refcnt %u at %p>",
|
|
|
|
op->ob_refcnt, op);
|
1992-09-03 17:32:55 -03:00
|
|
|
else if (op->ob_type->tp_print == NULL) {
|
2001-05-01 13:53:37 -03:00
|
|
|
PyObject *s;
|
|
|
|
if (flags & Py_PRINT_RAW)
|
|
|
|
s = PyObject_Str(op);
|
|
|
|
else
|
|
|
|
s = PyObject_Repr(op);
|
|
|
|
if (s == NULL)
|
|
|
|
ret = -1;
|
1992-09-03 17:32:55 -03:00
|
|
|
else {
|
2001-05-01 13:53:37 -03:00
|
|
|
ret = PyObject_Print(s, fp, Py_PRINT_RAW);
|
1992-09-03 17:32:55 -03:00
|
|
|
}
|
2001-05-01 13:53:37 -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
|
|
|
}
|
|
|
|
|
2001-01-23 12:24:35 -04:00
|
|
|
/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
|
2001-01-24 00:18:13 -04:00
|
|
|
void _PyObject_Dump(PyObject* op)
|
2001-01-23 12:24:35 -04:00
|
|
|
{
|
2001-02-22 18:39:18 -04:00
|
|
|
if (op == NULL)
|
|
|
|
fprintf(stderr, "NULL\n");
|
|
|
|
else {
|
2001-09-14 12:50:08 -03:00
|
|
|
fprintf(stderr, "object : ");
|
2001-02-22 18:39:18 -04:00
|
|
|
(void)PyObject_Print(op, stderr, 0);
|
2001-09-14 12:50:08 -03:00
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"type : %s\n"
|
|
|
|
"refcount: %d\n"
|
|
|
|
"address : %p\n",
|
|
|
|
op->ob_type==NULL ? "NULL" : op->ob_type->tp_name,
|
|
|
|
op->ob_refcnt,
|
|
|
|
op);
|
2001-02-22 18:39:18 -04:00
|
|
|
}
|
2001-01-23 12:24:35 -04:00
|
|
|
}
|
2001-01-23 12:33:18 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Repr(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;
|
1998-04-28 13:06:54 -03:00
|
|
|
#ifdef USE_STACKCHECK
|
|
|
|
if (PyOS_CheckStack()) {
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_SetString(PyExc_MemoryError, "stack overflow");
|
1998-04-28 13:06:54 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1991-06-07 13:10:43 -03:00
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
2001-08-24 15:34:26 -03:00
|
|
|
else if (v->ob_type->tp_repr == NULL)
|
2001-08-30 17:26:05 -03:00
|
|
|
return PyString_FromFormat("<%s object at %p>",
|
2001-08-24 15:34:26 -03:00
|
|
|
v->ob_type->tp_name, v);
|
2000-03-10 18:55:18 -04:00
|
|
|
else {
|
|
|
|
PyObject *res;
|
|
|
|
res = (*v->ob_type->tp_repr)(v);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-07-01 11:31:09 -03:00
|
|
|
if (PyUnicode_Check(res)) {
|
|
|
|
PyObject* str;
|
2000-07-08 14:43:32 -03:00
|
|
|
str = PyUnicode_AsUnicodeEscapeString(res);
|
2000-07-03 06:57:53 -03:00
|
|
|
Py_DECREF(res);
|
|
|
|
if (str)
|
2000-07-01 11:31:09 -03:00
|
|
|
res = str;
|
2000-07-03 06:57:53 -03:00
|
|
|
else
|
|
|
|
return NULL;
|
2000-07-01 11:31:09 -03:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 18:55:18 -04:00
|
|
|
if (!PyString_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-10 09:46:51 -03:00
|
|
|
"__repr__ returned non-string (type %.200s)",
|
2000-03-10 18:55:18 -04:00
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Str(PyObject *v)
|
1993-11-05 06:22:19 -04:00
|
|
|
{
|
2000-03-10 18:55:18 -04:00
|
|
|
PyObject *res;
|
|
|
|
|
1993-11-05 06:22:19 -04:00
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
2001-09-10 22:41:59 -03:00
|
|
|
if (PyString_CheckExact(v)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
return v;
|
|
|
|
}
|
2001-05-01 13:53:37 -03:00
|
|
|
if (v->ob_type->tp_str == NULL)
|
|
|
|
return PyObject_Repr(v);
|
|
|
|
|
|
|
|
res = (*v->ob_type->tp_str)(v);
|
2000-03-10 18:55:18 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-07-03 06:57:53 -03:00
|
|
|
if (PyUnicode_Check(res)) {
|
|
|
|
PyObject* str;
|
|
|
|
str = PyUnicode_AsEncodedString(res, NULL, NULL);
|
|
|
|
Py_DECREF(res);
|
|
|
|
if (str)
|
|
|
|
res = str;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 18:55:18 -04:00
|
|
|
if (!PyString_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-10 09:46:51 -03:00
|
|
|
"__str__ returned non-string (type %.200s)",
|
2000-03-10 18:55:18 -04:00
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
1993-11-05 06:22:19 -04:00
|
|
|
}
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyObject *
|
|
|
|
PyObject_Unicode(PyObject *v)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
if (v == NULL)
|
|
|
|
res = PyString_FromString("<NULL>");
|
2001-10-18 23:01:31 -03:00
|
|
|
if (PyUnicode_CheckExact(v)) {
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
2001-10-18 23:01:31 -03:00
|
|
|
if (PyUnicode_Check(v)) {
|
|
|
|
/* For a Unicode subtype that's not a Unicode object,
|
|
|
|
return a true Unicode object with the same data. */
|
|
|
|
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
|
|
|
|
PyUnicode_GET_SIZE(v));
|
|
|
|
}
|
|
|
|
if (PyString_Check(v)) {
|
2001-03-25 15:16:13 -04:00
|
|
|
Py_INCREF(v);
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
res = v;
|
2001-03-25 15:16:13 -04:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else {
|
|
|
|
PyObject *func;
|
2001-10-18 23:01:31 -03:00
|
|
|
static PyObject *unicodestr;
|
|
|
|
/* XXX As soon as we have a tp_unicode slot, we should
|
|
|
|
check this before trying the __unicode__
|
|
|
|
method. */
|
|
|
|
if (unicodestr == NULL) {
|
|
|
|
unicodestr= PyString_InternFromString(
|
|
|
|
"__unicode__");
|
|
|
|
if (unicodestr == NULL)
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-18 23:01:31 -03:00
|
|
|
func = PyObject_GetAttr(v, unicodestr);
|
|
|
|
if (func != NULL) {
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
}
|
2001-10-18 23:01:31 -03:00
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (v->ob_type->tp_str != NULL)
|
|
|
|
res = (*v->ob_type->tp_str)(v);
|
|
|
|
else
|
|
|
|
res = PyObject_Repr(v);
|
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
}
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!PyUnicode_Check(res)) {
|
2001-10-18 23:01:31 -03:00
|
|
|
PyObject *str;
|
|
|
|
str = PyUnicode_FromEncodedObject(res, NULL, "strict");
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
Py_DECREF(res);
|
|
|
|
if (str)
|
|
|
|
res = str;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|
|
|
|
|
2001-01-24 18:14:43 -04:00
|
|
|
/* Macro to get the tp_richcompare field of a type if defined */
|
|
|
|
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
|
|
|
|
? (t)->tp_richcompare : NULL)
|
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
|
|
|
|
static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
|
2001-01-03 21:48:10 -04:00
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Try a genuine rich comparison, returning an object. Return:
|
|
|
|
NULL for exception;
|
|
|
|
NotImplemented if this particular rich comparison is not implemented or
|
|
|
|
undefined;
|
|
|
|
some object not equal to NotImplemented if it is implemented
|
|
|
|
(this latter object may not be a Boolean).
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
try_rich_compare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
richcmpfunc f;
|
|
|
|
PyObject *res;
|
|
|
|
|
2001-09-27 17:30:07 -03:00
|
|
|
if (v->ob_type != w->ob_type &&
|
|
|
|
PyType_IsSubtype(w->ob_type, v->ob_type) &&
|
|
|
|
(f = RICHCOMPARE(w->ob_type)) != NULL) {
|
|
|
|
res = (*f)(w, v, swapped_op[op]);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
2001-01-24 18:14:43 -04:00
|
|
|
if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
|
2001-01-17 11:24:28 -04:00
|
|
|
res = (*f)(v, w, op);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
2001-01-24 18:14:43 -04:00
|
|
|
if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
|
2001-01-17 11:24:28 -04:00
|
|
|
return (*f)(w, v, swapped_op[op]);
|
|
|
|
}
|
|
|
|
res = Py_NotImplemented;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try a genuine rich comparison, returning an int. Return:
|
|
|
|
-1 for exception (including the case where try_rich_compare() returns an
|
|
|
|
object that's not a Boolean);
|
2001-11-04 01:57:16 -04:00
|
|
|
0 if the outcome is false;
|
|
|
|
1 if the outcome is true;
|
|
|
|
2 if this particular rich comparison is not implemented or undefined.
|
2001-01-17 11:24:28 -04:00
|
|
|
*/
|
2001-01-03 21:48:10 -04:00
|
|
|
static int
|
2001-01-17 11:24:28 -04:00
|
|
|
try_rich_compare_bool(PyObject *v, PyObject *w, int op)
|
2001-01-03 21:48:10 -04:00
|
|
|
{
|
2001-01-17 11:24:28 -04:00
|
|
|
PyObject *res;
|
|
|
|
int ok;
|
|
|
|
|
2001-01-24 18:14:43 -04:00
|
|
|
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
|
2001-01-17 11:24:28 -04:00
|
|
|
return 2; /* Shortcut, avoid INCREF+DECREF */
|
|
|
|
res = try_rich_compare(v, w, op);
|
|
|
|
if (res == NULL)
|
2001-01-03 21:48:10 -04:00
|
|
|
return -1;
|
2001-01-17 11:24:28 -04:00
|
|
|
if (res == Py_NotImplemented) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 2;
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
ok = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return ok;
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Try rich comparisons to determine a 3-way comparison. Return:
|
|
|
|
-2 for an exception;
|
2001-11-04 01:57:16 -04:00
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w;
|
|
|
|
2 if this particular rich comparison is not implemented or undefined.
|
2001-01-17 11:24:28 -04:00
|
|
|
*/
|
2001-01-03 21:48:10 -04:00
|
|
|
static int
|
2001-01-17 11:24:28 -04:00
|
|
|
try_rich_to_3way_compare(PyObject *v, PyObject *w)
|
|
|
|
{
|
2001-01-17 17:27:02 -04:00
|
|
|
static struct { int op; int outcome; } tries[3] = {
|
|
|
|
/* Try this operator, and if it is true, use this outcome: */
|
|
|
|
{Py_EQ, 0},
|
|
|
|
{Py_LT, -1},
|
|
|
|
{Py_GT, 1},
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
2001-01-24 18:14:43 -04:00
|
|
|
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
|
2001-01-17 11:24:28 -04:00
|
|
|
return 2; /* Shortcut */
|
2001-01-17 17:27:02 -04:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
switch (try_rich_compare_bool(v, w, tries[i].op)) {
|
|
|
|
case -1:
|
2001-05-07 17:53:51 -03:00
|
|
|
return -2;
|
2001-01-17 17:27:02 -04:00
|
|
|
case 1:
|
|
|
|
return tries[i].outcome;
|
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
2001-01-17 17:27:02 -04:00
|
|
|
|
|
|
|
return 2;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try a 3-way comparison, returning an int. Return:
|
|
|
|
-2 for an exception;
|
2001-11-04 01:57:16 -04:00
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w;
|
|
|
|
2 if this particular 3-way comparison is not implemented or undefined.
|
2001-01-17 11:24:28 -04:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
try_3way_compare(PyObject *v, PyObject *w)
|
1995-01-12 07:26:10 -04:00
|
|
|
{
|
2001-01-03 21:48:10 -04:00
|
|
|
int c;
|
2001-01-17 11:24:28 -04:00
|
|
|
cmpfunc f;
|
2001-01-03 21:48:10 -04:00
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Comparisons involving instances are given to instance_compare,
|
|
|
|
which has the same return conventions as this function. */
|
|
|
|
|
2001-09-18 17:38:53 -03:00
|
|
|
f = v->ob_type->tp_compare;
|
2001-01-17 11:24:28 -04:00
|
|
|
if (PyInstance_Check(v))
|
2001-09-18 17:38:53 -03:00
|
|
|
return (*f)(v, w);
|
2001-01-17 11:24:28 -04:00
|
|
|
if (PyInstance_Check(w))
|
|
|
|
return (*w->ob_type->tp_compare)(v, w);
|
|
|
|
|
2001-09-18 17:38:53 -03:00
|
|
|
/* If both have the same (non-NULL) tp_compare, use it. */
|
|
|
|
if (f != NULL && f == w->ob_type->tp_compare) {
|
|
|
|
c = (*f)(v, w);
|
|
|
|
if (c < 0 && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
return c < 0 ? -1 : c > 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If either tp_compare is _PyObject_SlotCompare, that's safe. */
|
|
|
|
if (f == _PyObject_SlotCompare ||
|
|
|
|
w->ob_type->tp_compare == _PyObject_SlotCompare)
|
|
|
|
return _PyObject_SlotCompare(v, w);
|
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Try coercion; if it fails, give up */
|
|
|
|
c = PyNumber_CoerceEx(&v, &w);
|
|
|
|
if (c < 0)
|
|
|
|
return -2;
|
|
|
|
if (c > 0)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
/* Try v's comparison, if defined */
|
|
|
|
if ((f = v->ob_type->tp_compare) != NULL) {
|
|
|
|
c = (*f)(v, w);
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
2001-06-09 04:34:05 -03:00
|
|
|
if (c < 0 && PyErr_Occurred())
|
2001-01-17 11:24:28 -04:00
|
|
|
return -2;
|
|
|
|
return c < 0 ? -1 : c > 0 ? 1 : 0;
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
|
|
|
|
/* Try w's comparison, if defined */
|
|
|
|
if ((f = w->ob_type->tp_compare) != NULL) {
|
|
|
|
c = (*f)(w, v); /* swapped! */
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
2001-06-09 04:34:05 -03:00
|
|
|
if (c < 0 && PyErr_Occurred())
|
2001-01-17 11:24:28 -04:00
|
|
|
return -2;
|
|
|
|
return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No comparison defined */
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final fallback 3-way comparison, returning an int. Return:
|
|
|
|
-2 if an error occurred;
|
2001-11-04 01:57:16 -04:00
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w.
|
2001-01-17 11:24:28 -04:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
default_3way_compare(PyObject *v, PyObject *w)
|
|
|
|
{
|
|
|
|
int c;
|
2001-01-22 11:59:32 -04:00
|
|
|
char *vname, *wname;
|
2001-01-17 11:24:28 -04:00
|
|
|
|
|
|
|
if (v->ob_type == w->ob_type) {
|
2001-01-20 02:24:55 -04:00
|
|
|
/* When comparing these pointers, they must be cast to
|
|
|
|
* integer types (i.e. Py_uintptr_t, our spelling of C9X's
|
|
|
|
* uintptr_t). ANSI specifies that pointer compares other
|
|
|
|
* than == and != to non-related structures are undefined.
|
|
|
|
*/
|
2001-01-20 02:08:10 -04:00
|
|
|
Py_uintptr_t vv = (Py_uintptr_t)v;
|
|
|
|
Py_uintptr_t ww = (Py_uintptr_t)w;
|
2001-01-17 11:24:28 -04:00
|
|
|
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2001-01-17 11:24:28 -04:00
|
|
|
/* Special case for Unicode */
|
|
|
|
if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
|
|
|
|
c = PyUnicode_Compare(v, w);
|
|
|
|
if (!PyErr_Occurred())
|
2001-01-03 21:48:10 -04:00
|
|
|
return c;
|
2001-01-17 11:24:28 -04:00
|
|
|
/* 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. */
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_TypeError))
|
|
|
|
return -2;
|
|
|
|
PyErr_Clear();
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2001-01-22 15:28:09 -04:00
|
|
|
/* None is smaller than anything */
|
|
|
|
if (v == Py_None)
|
|
|
|
return -1;
|
|
|
|
if (w == Py_None)
|
|
|
|
return 1;
|
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
/* different type: compare type names */
|
2001-01-22 11:59:32 -04:00
|
|
|
if (v->ob_type->tp_as_number)
|
|
|
|
vname = "";
|
|
|
|
else
|
|
|
|
vname = v->ob_type->tp_name;
|
|
|
|
if (w->ob_type->tp_as_number)
|
|
|
|
wname = "";
|
|
|
|
else
|
|
|
|
wname = w->ob_type->tp_name;
|
|
|
|
c = strcmp(vname, wname);
|
|
|
|
if (c < 0)
|
|
|
|
return -1;
|
|
|
|
if (c > 0)
|
|
|
|
return 1;
|
|
|
|
/* Same type name, or (more likely) incomparable numeric types */
|
|
|
|
return ((Py_uintptr_t)(v->ob_type) < (
|
|
|
|
Py_uintptr_t)(w->ob_type)) ? -1 : 1;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
|
|
|
|
|
2001-05-07 17:53:51 -03:00
|
|
|
/* Do a 3-way comparison, by hook or by crook. Return:
|
|
|
|
-2 for an exception;
|
2001-11-04 01:57:16 -04:00
|
|
|
-1 if v < w;
|
2001-05-07 17:53:51 -03:00
|
|
|
0 if v == w;
|
2001-11-04 01:57:16 -04:00
|
|
|
1 if v > w;
|
2001-06-09 04:34:05 -03:00
|
|
|
If the object implements a tp_compare function, it returns
|
|
|
|
whatever this function returns (whether with an exception or not).
|
2001-05-07 17:53:51 -03:00
|
|
|
*/
|
2001-01-17 11:24:28 -04:00
|
|
|
static int
|
|
|
|
do_cmp(PyObject *v, PyObject *w)
|
|
|
|
{
|
|
|
|
int c;
|
2001-06-09 04:34:05 -03:00
|
|
|
cmpfunc f;
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2001-06-09 04:34:05 -03:00
|
|
|
if (v->ob_type == w->ob_type
|
2001-08-16 05:02:45 -03:00
|
|
|
&& (f = v->ob_type->tp_compare) != NULL) {
|
|
|
|
c = (*f)(v, w);
|
|
|
|
if (c != 2 || !PyInstance_Check(v))
|
|
|
|
return c;
|
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
c = try_rich_to_3way_compare(v, w);
|
|
|
|
if (c < 2)
|
|
|
|
return c;
|
|
|
|
c = try_3way_compare(v, w);
|
|
|
|
if (c < 2)
|
|
|
|
return c;
|
|
|
|
return default_3way_compare(v, w);
|
1995-01-12 07:26:10 -04:00
|
|
|
}
|
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
/* compare_nesting is incremented before calling compare (for
|
2000-04-14 16:13:24 -03:00
|
|
|
some types) and decremented on exit. If the count exceeds the
|
|
|
|
nesting limit, enable code to detect circular data structures.
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|
|
|
|
This is a tunable parameter that should only affect the performance
|
|
|
|
of comparisons, nothing else. Setting it high makes comparing deeply
|
|
|
|
nested non-cyclical data structures faster, but makes comparing cyclical
|
|
|
|
data structures slower.
|
2000-04-14 16:13:24 -03:00
|
|
|
*/
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
#define NESTING_LIMIT 20
|
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
static int compare_nesting = 0;
|
2000-04-14 16:13:24 -03:00
|
|
|
|
|
|
|
static PyObject*
|
2000-07-09 12:48:49 -03:00
|
|
|
get_inprogress_dict(void)
|
2000-04-14 16:13:24 -03:00
|
|
|
{
|
2001-01-17 17:27:02 -04:00
|
|
|
static PyObject *key;
|
2000-04-14 16:13:24 -03:00
|
|
|
PyObject *tstate_dict, *inprogress;
|
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
if (key == NULL) {
|
|
|
|
key = PyString_InternFromString("cmp_state");
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-04-14 16:13:24 -03:00
|
|
|
tstate_dict = PyThreadState_GetDict();
|
|
|
|
if (tstate_dict == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
inprogress = PyDict_GetItem(tstate_dict, key);
|
2000-04-14 16:13:24 -03:00
|
|
|
if (inprogress == NULL) {
|
|
|
|
inprogress = PyDict_New();
|
|
|
|
if (inprogress == NULL)
|
|
|
|
return NULL;
|
2001-01-17 17:27:02 -04:00
|
|
|
if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
|
2000-04-14 16:13:24 -03:00
|
|
|
Py_DECREF(inprogress);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-09 13:20:39 -03:00
|
|
|
Py_DECREF(inprogress);
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
|
|
|
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
return inprogress;
|
2001-01-17 13:09:53 -04:00
|
|
|
}
|
|
|
|
|
2000-04-14 16:13:24 -03:00
|
|
|
static PyObject *
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
check_recursion(PyObject *v, PyObject *w, int op)
|
2000-04-14 16:13:24 -03:00
|
|
|
{
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyObject *inprogress;
|
|
|
|
PyObject *token;
|
2000-08-18 02:01:19 -03:00
|
|
|
Py_uintptr_t iv = (Py_uintptr_t)v;
|
|
|
|
Py_uintptr_t iw = (Py_uintptr_t)w;
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyObject *x, *y, *z;
|
2000-04-14 16:13:24 -03:00
|
|
|
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
inprogress = get_inprogress_dict();
|
|
|
|
if (inprogress == NULL)
|
2000-04-14 16:13:24 -03:00
|
|
|
return NULL;
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|
|
|
|
token = PyTuple_New(3);
|
|
|
|
if (token == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2000-08-18 02:01:19 -03:00
|
|
|
if (iv <= iw) {
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
|
|
|
|
PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
|
|
|
|
if (op >= 0)
|
|
|
|
op = swapped_op[op];
|
2000-04-14 16:13:24 -03:00
|
|
|
} else {
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
|
|
|
|
PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
|
|
|
|
if (x == NULL || y == NULL || z == NULL) {
|
|
|
|
Py_DECREF(token);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyDict_GetItem(inprogress, token) != NULL) {
|
|
|
|
Py_DECREF(token);
|
|
|
|
return Py_None; /* Without INCREF! */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyDict_SetItem(inprogress, token, token) < 0) {
|
|
|
|
Py_DECREF(token);
|
|
|
|
return NULL;
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_token(PyObject *token)
|
|
|
|
{
|
|
|
|
PyObject *inprogress;
|
|
|
|
|
|
|
|
if (token == NULL || token == Py_None)
|
|
|
|
return;
|
|
|
|
inprogress = get_inprogress_dict();
|
|
|
|
if (inprogress == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
PyDict_DelItem(inprogress, token);
|
|
|
|
Py_DECREF(token);
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
|
|
|
|
2001-11-04 01:57:16 -04:00
|
|
|
/* Compare v to w. Return
|
|
|
|
-1 if v < w or exception (PyErr_Occurred() true in latter case).
|
|
|
|
0 if v == w.
|
|
|
|
1 if v > w.
|
|
|
|
XXX The docs (C API manual) say the return value is undefined in case
|
|
|
|
XXX of error.
|
|
|
|
*/
|
1990-10-14 09:07:46 -03:00
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Compare(PyObject *v, PyObject *w)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyTypeObject *vtp;
|
2000-04-14 16:13:24 -03:00
|
|
|
int result;
|
|
|
|
|
2000-08-22 18:52:51 -03:00
|
|
|
#if defined(USE_STACKCHECK)
|
2000-08-30 12:53:50 -03:00
|
|
|
if (PyOS_CheckStack()) {
|
2000-08-22 18:52:51 -03:00
|
|
|
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
|
2001-10-22 13:30:36 -03:00
|
|
|
return -1;
|
2000-08-22 18:52:51 -03:00
|
|
|
}
|
|
|
|
#endif
|
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;
|
2001-01-03 21:48:10 -04:00
|
|
|
vtp = v->ob_type;
|
2001-01-17 17:27:02 -04:00
|
|
|
compare_nesting++;
|
|
|
|
if (compare_nesting > NESTING_LIMIT &&
|
2001-01-03 21:48:10 -04:00
|
|
|
(vtp->tp_as_mapping
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|| (vtp->tp_as_sequence
|
|
|
|
&& !PyString_Check(v)
|
|
|
|
&& !PyTuple_Check(v)))) {
|
2001-01-03 21:48:10 -04:00
|
|
|
/* try to detect circular data structures */
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyObject *token = check_recursion(v, w, -1);
|
2000-04-14 16:13:24 -03:00
|
|
|
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
if (token == NULL) {
|
|
|
|
result = -1;
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else if (token == Py_None) {
|
2000-04-14 16:13:24 -03:00
|
|
|
/* already comparing these objects. assume
|
|
|
|
they're equal until shown otherwise */
|
2001-01-03 21:48:10 -04:00
|
|
|
result = 0;
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else {
|
|
|
|
result = do_cmp(v, w);
|
|
|
|
delete_token(token);
|
2000-04-14 16:13:24 -03:00
|
|
|
}
|
|
|
|
}
|
2001-01-03 21:48:10 -04:00
|
|
|
else {
|
|
|
|
result = do_cmp(v, w);
|
|
|
|
}
|
2001-01-17 17:27:02 -04:00
|
|
|
compare_nesting--;
|
2001-01-17 11:24:28 -04:00
|
|
|
return result < 0 ? -1 : result;
|
|
|
|
}
|
|
|
|
|
2001-11-04 01:57:16 -04:00
|
|
|
/* Return (new reference to) Py_True or Py_False. */
|
2001-01-17 11:24:28 -04:00
|
|
|
static PyObject *
|
2001-06-09 04:34:05 -03:00
|
|
|
convert_3way_to_object(int op, int c)
|
2001-01-17 11:24:28 -04:00
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
switch (op) {
|
|
|
|
case Py_LT: c = c < 0; break;
|
|
|
|
case Py_LE: c = c <= 0; break;
|
|
|
|
case Py_EQ: c = c == 0; break;
|
|
|
|
case Py_NE: c = c != 0; break;
|
|
|
|
case Py_GT: c = c > 0; break;
|
|
|
|
case Py_GE: c = c >= 0; break;
|
|
|
|
}
|
|
|
|
result = c ? Py_True : Py_False;
|
|
|
|
Py_INCREF(result);
|
2000-04-14 16:13:24 -03:00
|
|
|
return result;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2001-06-09 04:34:05 -03:00
|
|
|
|
2001-11-04 01:57:16 -04:00
|
|
|
/* We want a rich comparison but don't have one. Try a 3-way cmp instead.
|
|
|
|
Return
|
|
|
|
NULL if error
|
|
|
|
Py_True if v op w
|
|
|
|
Py_False if not (v op w)
|
|
|
|
*/
|
2001-06-09 04:34:05 -03:00
|
|
|
static PyObject *
|
|
|
|
try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = try_3way_compare(v, w);
|
|
|
|
if (c >= 2)
|
|
|
|
c = default_3way_compare(v, w);
|
|
|
|
if (c <= -2)
|
|
|
|
return NULL;
|
|
|
|
return convert_3way_to_object(op, c);
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2001-11-04 01:57:16 -04:00
|
|
|
/* Do rich comparison on v and w. Return
|
|
|
|
NULL if error
|
|
|
|
Else a new reference to an object other than Py_NotImplemented, usually(?):
|
|
|
|
Py_True if v op w
|
|
|
|
Py_False if not (v op w)
|
|
|
|
*/
|
2001-01-21 12:25:18 -04:00
|
|
|
static PyObject *
|
2001-01-17 11:24:28 -04:00
|
|
|
do_richcmp(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
res = try_rich_compare(v, w, op);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
|
|
|
|
return try_3way_to_rich_compare(v, w, op);
|
|
|
|
}
|
|
|
|
|
2001-11-04 01:57:16 -04:00
|
|
|
/* Return:
|
|
|
|
NULL for exception;
|
|
|
|
some object not equal to NotImplemented if it is implemented
|
|
|
|
(this latter object may not be a Boolean).
|
|
|
|
*/
|
2001-01-17 11:24:28 -04:00
|
|
|
PyObject *
|
|
|
|
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
assert(Py_LT <= op && op <= Py_GE);
|
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
compare_nesting++;
|
|
|
|
if (compare_nesting > NESTING_LIMIT &&
|
|
|
|
(v->ob_type->tp_as_mapping
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
|| (v->ob_type->tp_as_sequence
|
|
|
|
&& !PyString_Check(v)
|
|
|
|
&& !PyTuple_Check(v)))) {
|
2001-11-04 03:29:31 -04:00
|
|
|
|
2001-01-17 17:27:02 -04:00
|
|
|
/* try to detect circular data structures */
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
PyObject *token = check_recursion(v, w, op);
|
|
|
|
if (token == NULL) {
|
|
|
|
res = NULL;
|
2001-11-04 03:29:31 -04:00
|
|
|
goto Done;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else if (token == Py_None) {
|
|
|
|
/* already comparing these objects with this operator.
|
|
|
|
assume they're equal until shown otherwise */
|
|
|
|
if (op == Py_EQ)
|
2001-01-17 17:27:02 -04:00
|
|
|
res = Py_True;
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else if (op == Py_NE)
|
2001-01-17 17:27:02 -04:00
|
|
|
res = Py_False;
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"can't order recursive values");
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
Py_XINCREF(res);
|
2001-01-17 17:27:02 -04:00
|
|
|
}
|
Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results. In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects. (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
done in the support routines so less needs to be done in the calling
routines (even at the expense of slowing it down a bit -- this
should normally never be invoked, it's mostly just there to avoid
blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
stored. (The dictionary now stores triples (v, w, op) instead of
pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
slows down comparisons of very deeply nested objects (unlikely to
occur in practice), while speeding up comparisons of recursive
objects (previously, this would first waste time and space on 500
nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
exception when recursion is detected for ordering oprators (<, <=,
>, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
belongs. MAL's patch must've inserted in a random spot between two
functions in the file -- between two helpers for rich comparison...
2001-01-18 18:07:06 -04:00
|
|
|
else {
|
|
|
|
res = do_richcmp(v, w, op);
|
|
|
|
delete_token(token);
|
2001-01-17 17:27:02 -04:00
|
|
|
}
|
2001-11-04 03:29:31 -04:00
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No nesting extremism.
|
|
|
|
If the types are equal, and not old-style instances, try to
|
|
|
|
get out cheap (don't bother with coercions etc.). */
|
|
|
|
if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
|
|
|
|
cmpfunc fcmp;
|
|
|
|
richcmpfunc frich = RICHCOMPARE(v->ob_type);
|
|
|
|
/* If the type has richcmp, try it first. try_rich_compare
|
|
|
|
tries it two-sided, which is not needed since we've a
|
|
|
|
single type only. */
|
|
|
|
if (frich != NULL) {
|
|
|
|
res = (*frich)(v, w, op);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
goto Done;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
/* No richcmp, or this particular richmp not implemented.
|
|
|
|
Try 3-way cmp. */
|
|
|
|
fcmp = v->ob_type->tp_compare;
|
|
|
|
if (fcmp != NULL) {
|
|
|
|
int c = (*fcmp)(v, w);
|
|
|
|
if (c < 0 && PyErr_Occurred()) {
|
|
|
|
res = NULL;
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
res = convert_3way_to_object(op, c);
|
|
|
|
goto Done;
|
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
2001-11-04 03:29:31 -04:00
|
|
|
|
|
|
|
/* Fast path not taken, or couldn't deliver a useful result. */
|
|
|
|
res = do_richcmp(v, w, op);
|
|
|
|
Done:
|
2001-01-17 17:27:02 -04:00
|
|
|
compare_nesting--;
|
2001-01-17 11:24:28 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-05-05 07:06:17 -03:00
|
|
|
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
|
2001-01-17 11:24:28 -04:00
|
|
|
int
|
|
|
|
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *res = PyObject_RichCompare(v, w, op);
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
ok = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return ok;
|
|
|
|
}
|
2000-06-29 16:17:04 -03: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 12:48:49 -03:00
|
|
|
_Py_HashDouble(double v)
|
2000-06-29 16:17:04 -03:00
|
|
|
{
|
2000-08-15 00:34:48 -03: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-29 16:17:04 -03: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 00:34:48 -03: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-29 16:17:04 -03:00
|
|
|
*/
|
2000-08-15 00:34:48 -03: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-29 16:17:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_HashPointer(void *p)
|
2000-06-29 16:17:04 -03: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 06:43:31 -04:00
|
|
|
long
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Hash(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);
|
2001-01-24 18:14:43 -04:00
|
|
|
if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
|
2000-06-29 16:17:04 -03:00
|
|
|
return _Py_HashPointer(v); /* Use address as hash value */
|
|
|
|
}
|
1993-03-29 06:43:31 -04:00
|
|
|
/* 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 *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_GetAttrString(PyObject *v, char *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *w, *res;
|
1996-08-09 17:52:03 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
if (v->ob_type->tp_getattr != NULL)
|
1990-12-20 11:06:42 -04:00
|
|
|
return (*v->ob_type->tp_getattr)(v, name);
|
2001-08-02 01:15:00 -03:00
|
|
|
w = PyString_InternFromString(name);
|
|
|
|
if (w == NULL)
|
|
|
|
return NULL;
|
|
|
|
res = PyObject_GetAttr(v, w);
|
|
|
|
Py_XDECREF(w);
|
|
|
|
return res;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1993-07-11 16:55:34 -03:00
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_HasAttrString(PyObject *v, char *name)
|
1993-07-11 16:55:34 -03:00
|
|
|
{
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *s;
|
|
|
|
int res;
|
1996-08-09 17:52:03 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
if (v->ob_type->tp_setattr != NULL)
|
1990-12-20 11:06:42 -04:00
|
|
|
return (*v->ob_type->tp_setattr)(v, name, w);
|
2001-08-02 01:15:00 -03:00
|
|
|
s = PyString_InternFromString(name);
|
|
|
|
if (s == NULL)
|
|
|
|
return -1;
|
|
|
|
res = PyObject_SetAttr(v, s, w);
|
|
|
|
Py_XDECREF(s);
|
|
|
|
return res;
|
1993-05-12 05:24:20 -03:00
|
|
|
}
|
|
|
|
|
1997-05-20 15:34:44 -03:00
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_GetAttr(PyObject *v, PyObject *name)
|
1997-05-20 15:34:44 -03:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject *tp = v->ob_type;
|
|
|
|
|
2002-03-15 09:40:30 -04:00
|
|
|
if (!PyString_Check(name)) {
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-03-15 09:40:30 -04:00
|
|
|
/* The Unicode to string conversion is done here because the
|
|
|
|
existing tp_getattro slots expect a string object as name
|
|
|
|
and we wouldn't want to break those. */
|
|
|
|
if (PyUnicode_Check(name)) {
|
|
|
|
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-23 11:36:32 -03:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
if (tp->tp_getattro != NULL)
|
|
|
|
return (*tp->tp_getattro)(v, name);
|
|
|
|
if (tp->tp_getattr != NULL)
|
|
|
|
return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
|
|
|
tp->tp_name, PyString_AS_STRING(name));
|
|
|
|
return NULL;
|
1997-05-20 15:34:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_HasAttr(PyObject *v, PyObject *name)
|
1997-05-20 15:34:44 -03:00
|
|
|
{
|
|
|
|
PyObject *res = PyObject_GetAttr(v, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
|
1997-05-20 15:34:44 -03:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject *tp = v->ob_type;
|
1997-05-20 15:34:44 -03:00
|
|
|
int err;
|
2000-09-18 13:20:57 -03:00
|
|
|
|
2002-03-15 09:40:30 -04:00
|
|
|
if (!PyString_Check(name)){
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-03-15 09:40:30 -04:00
|
|
|
/* The Unicode to string conversion is done here because the
|
|
|
|
existing tp_setattro slots expect a string object as name
|
|
|
|
and we wouldn't want to break those. */
|
|
|
|
if (PyUnicode_Check(name)) {
|
|
|
|
name = PyUnicode_AsEncodedString(name, NULL, NULL);
|
|
|
|
if (name == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-23 11:36:32 -03:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
else
|
|
|
|
Py_INCREF(name);
|
|
|
|
|
|
|
|
PyString_InternInPlace(&name);
|
|
|
|
if (tp->tp_setattro != NULL) {
|
|
|
|
err = (*tp->tp_setattro)(v, name, value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (tp->tp_setattr != NULL) {
|
|
|
|
err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
2000-09-18 13:20:57 -03:00
|
|
|
}
|
1997-05-20 15:34:44 -03:00
|
|
|
Py_DECREF(name);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.100s' object has no attributes "
|
|
|
|
"(%s .%.100s)",
|
|
|
|
tp->tp_name,
|
|
|
|
value==NULL ? "del" : "assign to",
|
|
|
|
PyString_AS_STRING(name));
|
|
|
|
else
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.100s' object has only read-only attributes "
|
|
|
|
"(%s .%.100s)",
|
|
|
|
tp->tp_name,
|
|
|
|
value==NULL ? "del" : "assign to",
|
|
|
|
PyString_AS_STRING(name));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to get a pointer to an object's __dict__ slot, if any */
|
|
|
|
|
|
|
|
PyObject **
|
|
|
|
_PyObject_GetDictPtr(PyObject *obj)
|
|
|
|
{
|
|
|
|
long dictoffset;
|
|
|
|
PyTypeObject *tp = obj->ob_type;
|
|
|
|
|
|
|
|
if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
|
|
|
|
return NULL;
|
|
|
|
dictoffset = tp->tp_dictoffset;
|
|
|
|
if (dictoffset == 0)
|
|
|
|
return NULL;
|
|
|
|
if (dictoffset < 0) {
|
2002-03-01 18:24:49 -04:00
|
|
|
int tsize;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
tsize = ((PyVarObject *)obj)->ob_size;
|
|
|
|
if (tsize < 0)
|
|
|
|
tsize = -tsize;
|
|
|
|
size = _PyObject_VAR_SIZE(tp, tsize);
|
|
|
|
|
2001-10-06 18:27:34 -03:00
|
|
|
dictoffset += (long)size;
|
|
|
|
assert(dictoffset > 0);
|
|
|
|
assert(dictoffset % SIZEOF_VOID_P == 0);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
return (PyObject **) ((char *)obj + dictoffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|
|
|
{
|
|
|
|
PyTypeObject *tp = obj->ob_type;
|
|
|
|
PyObject *descr;
|
2001-12-04 11:54:53 -04:00
|
|
|
PyObject *res = NULL;
|
2001-08-02 01:15:00 -03:00
|
|
|
descrgetfunc f;
|
|
|
|
PyObject **dictptr;
|
|
|
|
|
2002-03-15 09:40:30 -04:00
|
|
|
if (!PyString_Check(name)){
|
2001-12-04 11:54:53 -04:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-03-15 09:40:30 -04:00
|
|
|
/* The Unicode to string conversion is done here because the
|
|
|
|
existing tp_setattro slots expect a string object as name
|
|
|
|
and we wouldn't want to break those. */
|
|
|
|
if (PyUnicode_Check(name)) {
|
|
|
|
name = PyUnicode_AsEncodedString(name, NULL, NULL);
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
2001-12-04 11:54:53 -04:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-12-04 11:54:53 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Py_INCREF(name);
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
if (tp->tp_dict == NULL) {
|
2001-08-07 14:24:28 -03:00
|
|
|
if (PyType_Ready(tp) < 0)
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
f = NULL;
|
|
|
|
if (descr != NULL) {
|
|
|
|
f = descr->ob_type->tp_descr_get;
|
2001-12-04 11:54:53 -04:00
|
|
|
if (f != NULL && PyDescr_IsData(descr)) {
|
|
|
|
res = f(descr, obj, (PyObject *)obj->ob_type);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
dictptr = _PyObject_GetDictPtr(obj);
|
|
|
|
if (dictptr != NULL) {
|
|
|
|
PyObject *dict = *dictptr;
|
|
|
|
if (dict != NULL) {
|
2001-12-04 11:54:53 -04:00
|
|
|
res = PyDict_GetItem(dict, name);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (res != NULL) {
|
|
|
|
Py_INCREF(res);
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 11:54:53 -04:00
|
|
|
if (f != NULL) {
|
|
|
|
res = f(descr, obj, (PyObject *)obj->ob_type);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (descr != NULL) {
|
|
|
|
Py_INCREF(descr);
|
2001-12-04 11:54:53 -04:00
|
|
|
res = descr;
|
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
|
|
|
tp->tp_name, PyString_AS_STRING(name));
|
2001-12-04 11:54:53 -04:00
|
|
|
done:
|
|
|
|
Py_DECREF(name);
|
|
|
|
return res;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
|
|
|
{
|
|
|
|
PyTypeObject *tp = obj->ob_type;
|
|
|
|
PyObject *descr;
|
|
|
|
descrsetfunc f;
|
|
|
|
PyObject **dictptr;
|
2001-12-04 11:54:53 -04:00
|
|
|
int res = -1;
|
|
|
|
|
2002-03-15 09:40:30 -04:00
|
|
|
if (!PyString_Check(name)){
|
2001-12-04 11:54:53 -04:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-03-15 09:40:30 -04:00
|
|
|
/* The Unicode to string conversion is done here because the
|
|
|
|
existing tp_setattro slots expect a string object as name
|
|
|
|
and we wouldn't want to break those. */
|
|
|
|
if (PyUnicode_Check(name)) {
|
|
|
|
name = PyUnicode_AsEncodedString(name, NULL, NULL);
|
|
|
|
if (name == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2001-12-04 11:54:53 -04:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute name must be string");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-12-04 11:54:53 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Py_INCREF(name);
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (tp->tp_dict == NULL) {
|
2001-08-07 14:24:28 -03:00
|
|
|
if (PyType_Ready(tp) < 0)
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
f = NULL;
|
|
|
|
if (descr != NULL) {
|
|
|
|
f = descr->ob_type->tp_descr_set;
|
2001-12-04 11:54:53 -04:00
|
|
|
if (f != NULL && PyDescr_IsData(descr)) {
|
|
|
|
res = f(descr, obj, value);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
dictptr = _PyObject_GetDictPtr(obj);
|
|
|
|
if (dictptr != NULL) {
|
|
|
|
PyObject *dict = *dictptr;
|
|
|
|
if (dict == NULL && value != NULL) {
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL)
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
*dictptr = dict;
|
|
|
|
}
|
|
|
|
if (dict != NULL) {
|
|
|
|
if (value == NULL)
|
|
|
|
res = PyDict_DelItem(dict, name);
|
|
|
|
else
|
|
|
|
res = PyDict_SetItem(dict, name, value);
|
|
|
|
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
|
|
|
|
PyErr_SetObject(PyExc_AttributeError, name);
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 11:54:53 -04:00
|
|
|
if (f != NULL) {
|
|
|
|
res = f(descr, obj, value);
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (descr == NULL) {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
|
|
|
tp->tp_name, PyString_AS_STRING(name));
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object attribute '%.400s' is read-only",
|
|
|
|
tp->tp_name, PyString_AS_STRING(name));
|
2001-12-04 11:54:53 -04:00
|
|
|
done:
|
|
|
|
Py_DECREF(name);
|
|
|
|
return res;
|
1997-05-20 15:34:44 -03:00
|
|
|
}
|
|
|
|
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_IsTrue(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;
|
1998-05-21 21:53:24 -03:00
|
|
|
else if (v->ob_type->tp_as_number != NULL &&
|
|
|
|
v->ob_type->tp_as_number->nb_nonzero != NULL)
|
1993-05-12 05:24:20 -03:00
|
|
|
res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
|
1998-05-21 21:53:24 -03:00
|
|
|
else if (v->ob_type->tp_as_mapping != NULL &&
|
|
|
|
v->ob_type->tp_as_mapping->mp_length != NULL)
|
1993-05-12 05:24:20 -03:00
|
|
|
res = (*v->ob_type->tp_as_mapping->mp_length)(v);
|
1998-05-21 21:53:24 -03:00
|
|
|
else if (v->ob_type->tp_as_sequence != NULL &&
|
|
|
|
v->ob_type->tp_as_sequence->sq_length != NULL)
|
1993-05-12 05:24:20 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-04-09 14:53:59 -03:00
|
|
|
/* equivalent of 'not v'
|
|
|
|
Return -1 if an error occurred */
|
|
|
|
|
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Not(PyObject *v)
|
1998-04-09 14:53:59 -03:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
res = PyObject_IsTrue(v);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
return res == 0;
|
|
|
|
}
|
|
|
|
|
1995-01-10 11:26:20 -04:00
|
|
|
/* Coerce two numeric types to the "larger" one.
|
|
|
|
Increment the reference count on each argument.
|
2001-01-17 11:24:28 -04:00
|
|
|
Return value:
|
|
|
|
-1 if an error occurred;
|
|
|
|
0 if the coercion succeeded (and then the reference counts are increased);
|
|
|
|
1 if no coercion is possible (and no error is raised).
|
1995-01-10 11:26:20 -04:00
|
|
|
*/
|
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyNumber_CoerceEx(PyObject **pv, PyObject **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;
|
|
|
|
|
2002-04-25 23:49:14 -03:00
|
|
|
/* Shortcut only for old-style types */
|
|
|
|
if (v->ob_type == w->ob_type &&
|
|
|
|
!PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES))
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-01-17 11:24:28 -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).
|
|
|
|
*/
|
1997-11-19 12:03:17 -04:00
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyNumber_Coerce(PyObject **pv, PyObject **pw)
|
1997-11-19 12:03:17 -04:00
|
|
|
{
|
|
|
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
PyCallable_Check(PyObject *x)
|
1995-01-25 20:38:22 -04:00
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
return 0;
|
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;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
else {
|
|
|
|
return x->ob_type->tp_call != NULL;
|
|
|
|
}
|
1995-01-25 20:38:22 -04:00
|
|
|
}
|
|
|
|
|
2001-09-04 19:08:56 -03:00
|
|
|
/* Helper for PyObject_Dir.
|
|
|
|
Merge the __dict__ of aclass into dict, and recursively also all
|
|
|
|
the __dict__s of aclass's base classes. The order of merging isn't
|
|
|
|
defined, as it's expected that only the final set of dict keys is
|
|
|
|
interesting.
|
|
|
|
Return 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
merge_class_dict(PyObject* dict, PyObject* aclass)
|
|
|
|
{
|
|
|
|
PyObject *classdict;
|
|
|
|
PyObject *bases;
|
|
|
|
|
|
|
|
assert(PyDict_Check(dict));
|
|
|
|
assert(aclass);
|
|
|
|
|
|
|
|
/* Merge in the type's dict (if any). */
|
|
|
|
classdict = PyObject_GetAttrString(aclass, "__dict__");
|
|
|
|
if (classdict == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
int status = PyDict_Update(dict, classdict);
|
|
|
|
Py_DECREF(classdict);
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursively merge in the base types' (if any) dicts. */
|
|
|
|
bases = PyObject_GetAttrString(aclass, "__bases__");
|
2001-09-16 17:33:22 -03:00
|
|
|
if (bases == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
2002-05-13 15:29:46 -03:00
|
|
|
/* We have no guarantee that bases is a real tuple */
|
2001-09-04 19:08:56 -03:00
|
|
|
int i, n;
|
2002-05-13 15:29:46 -03:00
|
|
|
n = PySequence_Size(bases); /* This better be right */
|
|
|
|
if (n < 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
PyObject *base = PySequence_GetItem(bases, i);
|
|
|
|
if (base == NULL) {
|
|
|
|
Py_DECREF(bases);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (merge_class_dict(dict, base) < 0) {
|
|
|
|
Py_DECREF(bases);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(bases);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-16 23:38:46 -03:00
|
|
|
/* Helper for PyObject_Dir.
|
|
|
|
If obj has an attr named attrname that's a list, merge its string
|
|
|
|
elements into keys of dict.
|
|
|
|
Return 0 on success, -1 on error. Errors due to not finding the attr,
|
|
|
|
or the attr not being a list, are suppressed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
merge_list_attr(PyObject* dict, PyObject* obj, char *attrname)
|
|
|
|
{
|
|
|
|
PyObject *list;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
assert(PyDict_Check(dict));
|
|
|
|
assert(obj);
|
|
|
|
assert(attrname);
|
|
|
|
|
|
|
|
list = PyObject_GetAttrString(obj, attrname);
|
|
|
|
if (list == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
else if (PyList_Check(list)) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < PyList_GET_SIZE(list); ++i) {
|
|
|
|
PyObject *item = PyList_GET_ITEM(list, i);
|
|
|
|
if (PyString_Check(item)) {
|
|
|
|
result = PyDict_SetItem(dict, item, Py_None);
|
|
|
|
if (result < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_XDECREF(list);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-09-04 19:08:56 -03:00
|
|
|
/* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
|
|
|
|
docstring, which should be kept in synch with this implementation. */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_Dir(PyObject *arg)
|
|
|
|
{
|
|
|
|
/* Set exactly one of these non-NULL before the end. */
|
|
|
|
PyObject *result = NULL; /* result list */
|
|
|
|
PyObject *masterdict = NULL; /* result is masterdict.keys() */
|
|
|
|
|
|
|
|
/* If NULL arg, return the locals. */
|
|
|
|
if (arg == NULL) {
|
|
|
|
PyObject *locals = PyEval_GetLocals();
|
|
|
|
if (locals == NULL)
|
|
|
|
goto error;
|
|
|
|
result = PyDict_Keys(locals);
|
|
|
|
if (result == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Elif this is some form of module, we only want its dict. */
|
2001-09-10 15:27:43 -03:00
|
|
|
else if (PyModule_Check(arg)) {
|
2001-09-04 19:08:56 -03:00
|
|
|
masterdict = PyObject_GetAttrString(arg, "__dict__");
|
|
|
|
if (masterdict == NULL)
|
|
|
|
goto error;
|
2001-09-10 15:27:43 -03:00
|
|
|
if (!PyDict_Check(masterdict)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"module.__dict__ is not a dictionary");
|
|
|
|
goto error;
|
|
|
|
}
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Elif some form of type or class, grab its dict and its bases.
|
|
|
|
We deliberately don't suck up its __class__, as methods belonging
|
|
|
|
to the metaclass would probably be more confusing than helpful. */
|
|
|
|
else if (PyType_Check(arg) || PyClass_Check(arg)) {
|
|
|
|
masterdict = PyDict_New();
|
|
|
|
if (masterdict == NULL)
|
|
|
|
goto error;
|
|
|
|
if (merge_class_dict(masterdict, arg) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Else look at its dict, and the attrs reachable from its class. */
|
|
|
|
else {
|
|
|
|
PyObject *itsclass;
|
|
|
|
/* Create a dict to start with. CAUTION: Not everything
|
|
|
|
responding to __dict__ returns a dict! */
|
|
|
|
masterdict = PyObject_GetAttrString(arg, "__dict__");
|
|
|
|
if (masterdict == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
masterdict = PyDict_New();
|
|
|
|
}
|
|
|
|
else if (!PyDict_Check(masterdict)) {
|
|
|
|
Py_DECREF(masterdict);
|
|
|
|
masterdict = PyDict_New();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The object may have returned a reference to its
|
|
|
|
dict, so copy it to avoid mutating it. */
|
|
|
|
PyObject *temp = PyDict_Copy(masterdict);
|
|
|
|
Py_DECREF(masterdict);
|
|
|
|
masterdict = temp;
|
|
|
|
}
|
|
|
|
if (masterdict == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2001-09-16 23:38:46 -03:00
|
|
|
/* Merge in __members__ and __methods__ (if any).
|
|
|
|
XXX Would like this to go away someday; for now, it's
|
|
|
|
XXX needed to get at im_self etc of method objects. */
|
|
|
|
if (merge_list_attr(masterdict, arg, "__members__") < 0)
|
|
|
|
goto error;
|
|
|
|
if (merge_list_attr(masterdict, arg, "__methods__") < 0)
|
|
|
|
goto error;
|
|
|
|
|
2001-09-04 19:08:56 -03:00
|
|
|
/* Merge in attrs reachable from its class.
|
|
|
|
CAUTION: Not all objects have a __class__ attr. */
|
|
|
|
itsclass = PyObject_GetAttrString(arg, "__class__");
|
|
|
|
if (itsclass == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
int status = merge_class_dict(masterdict, itsclass);
|
|
|
|
Py_DECREF(itsclass);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((result == NULL) ^ (masterdict == NULL));
|
|
|
|
if (masterdict != NULL) {
|
|
|
|
/* The result comes from its keys. */
|
|
|
|
assert(result == NULL);
|
|
|
|
result = PyDict_Keys(masterdict);
|
|
|
|
if (result == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
if (PyList_Sort(result) != 0)
|
|
|
|
goto error;
|
|
|
|
else
|
|
|
|
goto normal_return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
/* fall through */
|
|
|
|
normal_return:
|
|
|
|
Py_XDECREF(masterdict);
|
|
|
|
return result;
|
|
|
|
}
|
1995-01-25 20:38:22 -04:00
|
|
|
|
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).
|
2001-08-16 05:17:26 -03:00
|
|
|
(XXX This type and the type of NotImplemented below should be unified.)
|
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 *
|
2000-07-09 12:48:49 -03:00
|
|
|
none_repr(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
|
|
|
}
|
|
|
|
|
2001-01-23 12:24:35 -04:00
|
|
|
/* ARGUSED */
|
|
|
|
static void
|
|
|
|
none_dealloc(PyObject* ignore)
|
|
|
|
{
|
|
|
|
/* This should never get called, but we also don't want to SEGV if
|
|
|
|
* we accidently decref None out of existance.
|
|
|
|
*/
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
static PyTypeObject PyNone_Type = {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-08-16 05:17:26 -03:00
|
|
|
"NoneType",
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
0,
|
2001-01-23 12:24:35 -04:00
|
|
|
(destructor)none_dealloc, /*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 = {
|
2001-08-16 05:17:26 -03:00
|
|
|
PyObject_HEAD_INIT(&PyNone_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
2001-01-03 21:48:10 -04:00
|
|
|
/* NotImplemented is an object that can be used to signal that an
|
|
|
|
operation is not implemented for the given type combination. */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
NotImplemented_repr(PyObject *op)
|
|
|
|
{
|
|
|
|
return PyString_FromString("NotImplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyTypeObject PyNotImplemented_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0,
|
2001-08-16 05:17:26 -03:00
|
|
|
"NotImplementedType",
|
2001-01-03 21:48:10 -04:00
|
|
|
0,
|
|
|
|
0,
|
2001-01-23 12:24:35 -04:00
|
|
|
(destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
|
2001-01-03 21:48:10 -04:00
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
(reprfunc)NotImplemented_repr, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject _Py_NotImplementedStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyNotImplemented_Type)
|
|
|
|
};
|
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
void
|
|
|
|
_Py_ReadyTypes(void)
|
|
|
|
{
|
|
|
|
if (PyType_Ready(&PyType_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'type'");
|
|
|
|
|
2002-04-03 18:41:51 -04:00
|
|
|
if (PyType_Ready(&PyBool_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'bool'");
|
|
|
|
|
2002-05-24 16:01:59 -03:00
|
|
|
if (PyType_Ready(&PyString_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'str'");
|
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
if (PyType_Ready(&PyList_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'list'");
|
|
|
|
|
|
|
|
if (PyType_Ready(&PyNone_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize type(None)");
|
|
|
|
|
|
|
|
if (PyType_Ready(&PyNotImplemented_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize type(NotImplemented)");
|
|
|
|
}
|
|
|
|
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_ResetReferences(void)
|
1997-08-04 23:04:34 -03:00
|
|
|
{
|
|
|
|
refchain._ob_prev = refchain._ob_next = &refchain;
|
|
|
|
_Py_RefTotal = 0;
|
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_NewReference(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
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_ForgetReference(register PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2000-01-20 18:32:56 -04:00
|
|
|
#ifdef SLOW_UNREF_CHECK
|
2000-03-10 18:55:18 -04:00
|
|
|
register PyObject *p;
|
2000-01-20 18:32:56 -04:00
|
|
|
#endif
|
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
|
2001-08-02 01:15:00 -03:00
|
|
|
op->ob_type->tp_frees++;
|
1995-04-06 11:46:26 -03:00
|
|
|
#endif
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1996-08-12 18:32:12 -03:00
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_Dealloc(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);
|
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
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_PrintReferences(FILE *fp)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
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 *
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_GetObjects(PyObject *self, PyObject *args)
|
1995-08-29 06:18:14 -03: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 ||
|
2001-07-14 14:58:00 -03:00
|
|
|
(t != NULL && op->ob_type != (PyTypeObject *) t)) {
|
1995-08-29 06:18:14 -03:00
|
|
|
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 */
|
2000-07-12 09:56:19 -03:00
|
|
|
int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
|
1997-08-04 23:04:34 -03:00
|
|
|
|
|
|
|
|
2000-08-16 09:27:23 -03:00
|
|
|
/* Python's malloc wrappers (see pymem.h) */
|
1997-08-04 23:04:34 -03:00
|
|
|
|
2000-07-25 09:56:38 -03:00
|
|
|
void *
|
2000-07-09 12:48:49 -03:00
|
|
|
PyMem_Malloc(size_t nbytes)
|
1997-08-04 23:04:34 -03:00
|
|
|
{
|
2000-05-03 20:44:39 -03:00
|
|
|
return PyMem_MALLOC(nbytes);
|
1997-08-04 23:04:34 -03:00
|
|
|
}
|
|
|
|
|
2000-07-25 09:56:38 -03:00
|
|
|
void *
|
|
|
|
PyMem_Realloc(void *p, size_t nbytes)
|
1997-08-04 23:04:34 -03:00
|
|
|
{
|
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
PyMem_{Del, DEL} doesn't work yet (compilation problems).
pyport.h: _PyMem_EXTRA is gone.
pmem.h: Repaired comments. PyMem_{Malloc, MALLOC} and
PyMem_{Realloc, REALLOC} now make the same x-platform guarantees when
asking for 0 bytes, and when passing a NULL pointer to the latter.
object.c: PyMem_{Malloc, Realloc} just call their macro versions
now, since the latter take care of the x-platform 0 and NULL stuff
by themselves now.
pypcre.c, grow_stack(): So sue me. On two lines, this called
PyMem_RESIZE to grow a "const" area. It's not legit to realloc a
const area, so the compiler warned given the new expansion of
PyMem_RESIZE. It would have gotten the same warning before if it
had used PyMem_Resize() instead; the older macro version, but not the
function version, silently cast away the constness. IMO that was a wrong
thing to do, and the docs say the macro versions of PyMem_xyz are
deprecated anyway. If somebody else is resizing const areas with the
macro spelling, they'll get a warning when they recompile now too.
2002-04-12 04:22:56 -03:00
|
|
|
return PyMem_REALLOC(p, nbytes);
|
1997-08-04 23:04:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-25 09:56:38 -03:00
|
|
|
PyMem_Free(void *p)
|
1997-08-04 23:04:34 -03:00
|
|
|
{
|
2000-05-03 20:44:39 -03:00
|
|
|
PyMem_FREE(p);
|
1997-08-04 23:04:34 -03:00
|
|
|
}
|
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
|
1998-04-10 19:32:46 -03: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 12:48:49 -03:00
|
|
|
Py_ReprEnter(PyObject *obj)
|
1998-04-10 19:32:46 -03: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 12:48:49 -03:00
|
|
|
Py_ReprLeave(PyObject *obj)
|
1998-04-10 19:32:46 -03:00
|
|
|
{
|
|
|
|
PyObject *dict;
|
|
|
|
PyObject *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
1998-04-11 12:17:34 -03:00
|
|
|
if (dict == NULL)
|
|
|
|
return;
|
1998-04-10 19:32:46 -03:00
|
|
|
list = PyDict_GetItemString(dict, KEY);
|
1998-04-11 12:17:34 -03:00
|
|
|
if (list == NULL || !PyList_Check(list))
|
|
|
|
return;
|
1998-04-10 19:32:46 -03: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-13 12:01:29 -04: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-25 14:39:19 -04:00
|
|
|
|
|
|
|
CT 2k0325
|
|
|
|
added better safe than sorry check for threadstate
|
2000-04-24 12:40:53 -03: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-13 12:01:29 -04:00
|
|
|
*/
|
|
|
|
|
2000-04-24 12:40:53 -03: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-13 12:01:29 -04:00
|
|
|
int _PyTrash_delete_nesting = 0;
|
2000-04-24 12:40:53 -03:00
|
|
|
|
2000-03-13 12:01:29 -04:00
|
|
|
PyObject * _PyTrash_delete_later = NULL;
|
|
|
|
|
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_PyTrash_deposit_object(PyObject *op)
|
2000-03-13 12:01:29 -04:00
|
|
|
{
|
2002-03-28 23:05:54 -04:00
|
|
|
#ifndef WITH_CYCLE_GC
|
2000-04-24 12:40:53 -03: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 12:36:13 -03: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 12:40:53 -03:00
|
|
|
op->ob_refcnt = typecode;
|
|
|
|
op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
|
2002-03-28 23:05:54 -04:00
|
|
|
#else
|
|
|
|
assert (_Py_AS_GC(op)->gc.gc_next == NULL);
|
|
|
|
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
|
|
|
|
#endif
|
2000-04-24 12:40:53 -03:00
|
|
|
_PyTrash_delete_later = op;
|
2000-03-13 12:01:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_PyTrash_destroy_chain(void)
|
2000-03-13 12:01:29 -04:00
|
|
|
{
|
|
|
|
while (_PyTrash_delete_later) {
|
|
|
|
PyObject *shredder = _PyTrash_delete_later;
|
2002-03-28 23:05:54 -04:00
|
|
|
|
|
|
|
#ifndef WITH_CYCLE_GC
|
2000-04-24 12:40:53 -03: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;
|
|
|
|
}
|
2002-03-28 23:05:54 -04:00
|
|
|
#else
|
|
|
|
_PyTrash_delete_later =
|
|
|
|
(PyObject*) _Py_AS_GC(shredder)->gc.gc_prev;
|
|
|
|
#endif
|
|
|
|
|
2000-04-24 12:40:53 -03:00
|
|
|
_Py_NewReference(shredder);
|
|
|
|
|
2000-03-13 12:01:29 -04:00
|
|
|
++_PyTrash_delete_nesting;
|
|
|
|
Py_DECREF(shredder);
|
|
|
|
--_PyTrash_delete_nesting;
|
|
|
|
}
|
|
|
|
}
|