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
|
|
|
|
2006-04-12 23:06:09 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
object.h special-build macro minefield: renamed all the new lexical
helper macros to something saner, and used them appropriately in other
files too, to reduce #ifdef blocks.
classobject.c, instance_dealloc(): One of my worst Python Memories is
trying to fix this routine a few years ago when COUNT_ALLOCS was defined
but Py_TRACE_REFS wasn't. The special-build code here is way too
complicated. Now it's much simpler. Difference: in a Py_TRACE_REFS
build, the instance is no longer in the doubly-linked list of live
objects while its __del__ method is executing, and that may be visible
via sys.getobjects() called from a __del__ method. Tough -- the object
is presumed dead while its __del__ is executing anyway, and not calling
_Py_NewReference() at the start allows enormous code simplification.
typeobject.c, call_finalizer(): The special-build instance_dealloc()
pain apparently spread to here too via cut-'n-paste, and this is much
simpler now too. In addition, I didn't understand why this routine
was calling _PyObject_GC_TRACK() after a resurrection, since there's no
plausible way _PyObject_GC_UNTRACK() could have been called on the
object by this point. I suspect it was left over from pasting the
instance_delloc() code. Instead asserted that the object is still
tracked. Caution: I suspect we don't have a test that actually
exercises the subtype_dealloc() __del__-resurrected-me code.
2002-07-11 03:23:50 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
2006-03-04 16:00:59 -04:00
|
|
|
Py_ssize_t _Py_RefTotal;
|
2006-04-12 14:06:05 -03:00
|
|
|
|
|
|
|
Py_ssize_t
|
|
|
|
_Py_GetRefTotal(void)
|
|
|
|
{
|
|
|
|
PyObject *o;
|
|
|
|
Py_ssize_t total = _Py_RefTotal;
|
|
|
|
/* ignore the references to the dummy object of the dicts and sets
|
|
|
|
because they are not reliable and not useful (now that the
|
|
|
|
hash table code is well-tested) */
|
|
|
|
o = _PyDict_Dummy();
|
|
|
|
if (o != NULL)
|
|
|
|
total -= o->ob_refcnt;
|
|
|
|
o = _PySet_Dummy();
|
|
|
|
if (o != NULL)
|
|
|
|
total -= o->ob_refcnt;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
#endif /* Py_REF_DEBUG */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-07-29 10:42:14 -03:00
|
|
|
int Py_DivisionWarningFlag;
|
2007-05-23 03:35:32 -03:00
|
|
|
int Py_Py3kWarningFlag;
|
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
|
|
|
|
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
|
|
|
|
2003-03-22 22:51:01 -04:00
|
|
|
#ifdef Py_TRACE_REFS
|
2003-03-23 13:52:28 -04:00
|
|
|
/* Head of circular doubly-linked list of all objects. These are linked
|
|
|
|
* together via the _ob_prev and _ob_next members of a PyObject, which
|
|
|
|
* exist only in a Py_TRACE_REFS build.
|
|
|
|
*/
|
2003-03-22 22:51:01 -04:00
|
|
|
static PyObject refchain = {&refchain, &refchain};
|
2003-03-22 23:33:13 -04:00
|
|
|
|
2003-03-23 13:52:28 -04:00
|
|
|
/* Insert op at the front of the list of all objects. If force is true,
|
|
|
|
* op is added even if _ob_prev and _ob_next are non-NULL already. If
|
|
|
|
* force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
|
|
|
|
* force should be true if and only if op points to freshly allocated,
|
|
|
|
* uninitialized memory, or you've unlinked op from the list and are
|
2003-03-23 14:06:08 -04:00
|
|
|
* relinking it into the front.
|
2003-03-23 13:52:28 -04:00
|
|
|
* Note that objects are normally added to the list via _Py_NewReference,
|
|
|
|
* which is called by PyObject_Init. Not all objects are initialized that
|
|
|
|
* way, though; exceptions include statically allocated type objects, and
|
|
|
|
* statically allocated singletons (like Py_True and Py_None).
|
|
|
|
*/
|
2003-03-22 23:33:13 -04:00
|
|
|
void
|
2003-03-23 13:52:28 -04:00
|
|
|
_Py_AddToAllObjects(PyObject *op, int force)
|
2003-03-22 23:33:13 -04:00
|
|
|
{
|
2003-03-23 13:52:28 -04:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
if (!force) {
|
|
|
|
/* If it's initialized memory, op must be in or out of
|
|
|
|
* the list unambiguously.
|
|
|
|
*/
|
|
|
|
assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
|
|
|
|
}
|
2003-03-22 22:51:01 -04:00
|
|
|
#endif
|
2003-03-23 13:52:28 -04:00
|
|
|
if (force || op->_ob_prev == NULL) {
|
|
|
|
op->_ob_next = refchain._ob_next;
|
|
|
|
op->_ob_prev = &refchain;
|
|
|
|
refchain._ob_next->_ob_prev = op;
|
|
|
|
refchain._ob_next = op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* Py_TRACE_REFS */
|
2003-03-22 22:51:01 -04:00
|
|
|
|
1993-10-11 09:54:31 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyTypeObject *type_list;
|
2006-04-18 08:49:53 -03:00
|
|
|
/* All types are added to type_list, at least when
|
2006-04-18 03:24:08 -03:00
|
|
|
they get one object created. That makes them
|
|
|
|
immortal, which unfortunately contributes to
|
|
|
|
garbage itself. If unlist_types_without_objects
|
|
|
|
is set, they will be removed from the type_list
|
|
|
|
once the last object is deallocated. */
|
|
|
|
int unlist_types_without_objects;
|
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
|
2006-04-18 03:24:08 -03:00
|
|
|
dump_counts(FILE* f)
|
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)
|
2006-04-18 03:24:08 -03:00
|
|
|
fprintf(f, "%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);
|
2006-04-18 03:24:08 -03:00
|
|
|
fprintf(f, "fast tuple allocs: %d, empty: %d\n",
|
1993-10-25 05:40:52 -03:00
|
|
|
fast_tuple_allocs, tuple_zero_allocs);
|
2006-04-18 03:24:08 -03:00
|
|
|
fprintf(f, "fast int allocs: pos: %d, neg: %d\n",
|
1993-10-25 05:40:52 -03:00
|
|
|
quick_int_allocs, quick_neg_int_allocs);
|
2006-04-18 03:24:08 -03:00
|
|
|
fprintf(f, "null strings: %d, 1-strings: %d\n",
|
1993-10-25 05:40:52 -03:00
|
|
|
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) {
|
2006-05-29 16:39:45 -03:00
|
|
|
v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
|
2001-08-02 01:15:00 -03:00
|
|
|
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
|
|
|
{
|
2006-04-18 03:24:08 -03:00
|
|
|
if (tp->tp_next == NULL && tp->tp_prev == NULL) {
|
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");
|
2006-04-18 03:24:08 -03:00
|
|
|
if (type_list)
|
|
|
|
type_list->tp_prev = tp;
|
1993-10-11 09:54:31 -03:00
|
|
|
tp->tp_next = type_list;
|
2002-07-08 19:11:52 -03:00
|
|
|
/* Note that as of Python 2.2, heap-allocated type objects
|
|
|
|
* can go away, but this code requires that they stay alive
|
|
|
|
* until program exit. That's why we're careful with
|
|
|
|
* refcounts here. type_list gets a new reference to tp,
|
|
|
|
* while ownership of the reference type_list used to hold
|
|
|
|
* (if any) was transferred to tp->tp_next in the line above.
|
|
|
|
* tp is thus effectively immortal after this.
|
|
|
|
*/
|
|
|
|
Py_INCREF(tp);
|
1993-10-11 09:54:31 -03:00
|
|
|
type_list = tp;
|
2003-03-22 23:04:32 -04:00
|
|
|
#ifdef Py_TRACE_REFS
|
2003-03-23 13:52:28 -04:00
|
|
|
/* Also insert in the doubly-linked list of all objects,
|
|
|
|
* if not already there.
|
|
|
|
*/
|
|
|
|
_Py_AddToAllObjects((PyObject *)tp, 0);
|
2003-03-22 22:51:01 -04:00
|
|
|
#endif
|
1993-10-11 09:54:31 -03:00
|
|
|
}
|
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
|
|
|
}
|
2006-04-18 03:24:08 -03:00
|
|
|
|
|
|
|
void dec_count(PyTypeObject *tp)
|
|
|
|
{
|
|
|
|
tp->tp_frees++;
|
|
|
|
if (unlist_types_without_objects &&
|
|
|
|
tp->tp_allocs == tp->tp_frees) {
|
|
|
|
/* unlink the type from type_list */
|
|
|
|
if (tp->tp_prev)
|
|
|
|
tp->tp_prev->tp_next = tp->tp_next;
|
|
|
|
else
|
|
|
|
type_list = tp->tp_next;
|
|
|
|
if (tp->tp_next)
|
|
|
|
tp->tp_next->tp_prev = tp->tp_prev;
|
|
|
|
tp->tp_next = tp->tp_prev = NULL;
|
|
|
|
Py_DECREF(tp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-11 09:54:31 -03:00
|
|
|
#endif
|
|
|
|
|
2002-07-08 23:57:01 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
|
|
|
/* Log a fatal error; doesn't return. */
|
|
|
|
void
|
|
|
|
_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
|
|
|
|
{
|
|
|
|
char buf[300];
|
|
|
|
|
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
2006-03-23 01:38:33 -04:00
|
|
|
"%s:%i object at %p has negative ref count "
|
|
|
|
"%" PY_FORMAT_SIZE_T "d",
|
2006-03-23 01:41:24 -04:00
|
|
|
fname, lineno, op, op->ob_refcnt);
|
2002-07-08 23:57:01 -03:00
|
|
|
Py_FatalError(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Py_REF_DEBUG */
|
|
|
|
|
2004-04-22 14:23:49 -03:00
|
|
|
void
|
|
|
|
Py_IncRef(PyObject *o)
|
|
|
|
{
|
|
|
|
Py_XINCREF(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_DecRef(PyObject *o)
|
|
|
|
{
|
|
|
|
Py_XDECREF(o);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-10-11 17:37:24 -03:00
|
|
|
if (op == NULL)
|
|
|
|
return PyErr_NoMemory();
|
2000-05-03 20:44:39 -03:00
|
|
|
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(op) = 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 *
|
2006-02-15 13:27:45 -04:00
|
|
|
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
|
2000-05-03 20:44:39 -03:00
|
|
|
{
|
2002-10-11 17:37:24 -03:00
|
|
|
if (op == NULL)
|
|
|
|
return (PyVarObject *) PyErr_NoMemory();
|
2000-05-03 20:44:39 -03:00
|
|
|
/* Any changes should be reflected in PyObject_INIT_VAR */
|
|
|
|
op->ob_size = size;
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(op) = tp;
|
2000-05-03 20:44:39 -03:00
|
|
|
_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 *
|
2006-02-15 13:27:45 -04:00
|
|
|
_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t 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
|
|
|
}
|
|
|
|
|
2003-01-13 16:13:12 -04:00
|
|
|
/* Implementation of PyObject_Print with recursion checking */
|
|
|
|
static int
|
|
|
|
internal_print(PyObject *op, FILE *fp, int flags, int nesting)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-07-27 18:40:24 -03:00
|
|
|
int ret = 0;
|
2003-01-13 16:13:12 -04:00
|
|
|
if (nesting > 10) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "print recursion");
|
|
|
|
return -1;
|
|
|
|
}
|
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) {
|
2007-09-17 00:28:34 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-06-07 13:10:43 -03:00
|
|
|
fprintf(fp, "<nil>");
|
2007-09-17 00:28:34 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
else {
|
|
|
|
if (op->ob_refcnt <= 0)
|
2006-03-01 01:41:20 -04:00
|
|
|
/* XXX(twouters) cast refcount to long until %zd is
|
|
|
|
universally available */
|
2007-09-17 00:28:34 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2006-03-01 01:41:20 -04:00
|
|
|
fprintf(fp, "<refcnt %ld at %p>",
|
|
|
|
(long)op->ob_refcnt, op);
|
2007-09-17 00:28:34 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2007-12-18 22:37:44 -04:00
|
|
|
else if (Py_TYPE(op)->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 {
|
2003-01-13 16:13:12 -04:00
|
|
|
ret = internal_print(s, fp, Py_PRINT_RAW,
|
|
|
|
nesting+1);
|
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
|
2007-12-18 22:37:44 -04:00
|
|
|
ret = (*Py_TYPE(op)->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
|
|
|
}
|
|
|
|
|
2003-01-13 16:13:12 -04:00
|
|
|
int
|
|
|
|
PyObject_Print(PyObject *op, FILE *fp, int flags)
|
|
|
|
{
|
|
|
|
return internal_print(op, fp, flags, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-23 12:24:35 -04:00
|
|
|
/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
|
2002-07-07 02:13:56 -03: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 {
|
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r67654 | georg.brandl | 2008-12-07 16:42:09 -0600 (Sun, 07 Dec 2008) | 2 lines
#4457: rewrite __import__() documentation.
................
r67676 | benjamin.peterson | 2008-12-08 20:03:03 -0600 (Mon, 08 Dec 2008) | 1 line
specify how things are copied
................
r67677 | benjamin.peterson | 2008-12-08 20:05:11 -0600 (Mon, 08 Dec 2008) | 1 line
revert unrelated change to installer script
................
r67681 | jeremy.hylton | 2008-12-09 15:03:10 -0600 (Tue, 09 Dec 2008) | 2 lines
Add simple unittests for Request
................
r67692 | amaury.forgeotdarc | 2008-12-10 18:03:42 -0600 (Wed, 10 Dec 2008) | 2 lines
#1030250: correctly pass the dry_run option to the mkpath() function.
................
r67725 | benjamin.peterson | 2008-12-12 22:02:20 -0600 (Fri, 12 Dec 2008) | 1 line
fix incorrect example
................
r67746 | antoine.pitrou | 2008-12-13 17:12:30 -0600 (Sat, 13 Dec 2008) | 3 lines
Issue #4163: Use unicode-friendly word splitting in the textwrap functions when given an unicode string.
................
r67748 | benjamin.peterson | 2008-12-13 19:46:11 -0600 (Sat, 13 Dec 2008) | 1 line
remove has_key usage
................
r67761 | benjamin.peterson | 2008-12-14 11:26:04 -0600 (Sun, 14 Dec 2008) | 1 line
fix missing bracket
................
r67784 | georg.brandl | 2008-12-15 02:33:58 -0600 (Mon, 15 Dec 2008) | 2 lines
#4446: document "platforms" argument for setup().
................
r67785 | georg.brandl | 2008-12-15 02:36:11 -0600 (Mon, 15 Dec 2008) | 2 lines
#4611: fix typo.
................
r67787 | georg.brandl | 2008-12-15 02:58:59 -0600 (Mon, 15 Dec 2008) | 2 lines
#4578: fix has_key() usage in compiler package.
................
r67788 | georg.brandl | 2008-12-15 03:07:39 -0600 (Mon, 15 Dec 2008) | 2 lines
#4568: remove limitation in varargs callback example.
................
r67802 | amaury.forgeotdarc | 2008-12-15 16:29:14 -0600 (Mon, 15 Dec 2008) | 4 lines
#3632: the "pyo" macro from gdbinit can now run when the GIL is released.
Patch by haypo.
................
r67832 | antoine.pitrou | 2008-12-17 16:46:54 -0600 (Wed, 17 Dec 2008) | 4 lines
Issue #2467: gc.DEBUG_STATS reports invalid elapsed times.
Patch by Neil Schemenauer, very slightly modified.
................
r67848 | benjamin.peterson | 2008-12-18 20:28:56 -0600 (Thu, 18 Dec 2008) | 1 line
fix typo
................
r67849 | benjamin.peterson | 2008-12-18 20:31:35 -0600 (Thu, 18 Dec 2008) | 1 line
_call_method -> _callmethod and _get_value to _getvalue
................
r67859 | amaury.forgeotdarc | 2008-12-19 16:56:48 -0600 (Fri, 19 Dec 2008) | 4 lines
#4700: crtlicense.txt is displayed by the license() command and should be kept ascii-only.
Will port to 3.0
................
r67862 | benjamin.peterson | 2008-12-19 20:48:02 -0600 (Fri, 19 Dec 2008) | 1 line
copy sentence from docstring
................
r67863 | benjamin.peterson | 2008-12-19 20:51:26 -0600 (Fri, 19 Dec 2008) | 1 line
add headings
................
r67864 | benjamin.peterson | 2008-12-19 20:57:19 -0600 (Fri, 19 Dec 2008) | 1 line
beef up docstring
................
r67880 | benjamin.peterson | 2008-12-20 16:49:24 -0600 (Sat, 20 Dec 2008) | 1 line
remove redundant sentence
................
r67882 | benjamin.peterson | 2008-12-20 16:59:49 -0600 (Sat, 20 Dec 2008) | 1 line
add some recent releases to the list
................
r67885 | benjamin.peterson | 2008-12-20 17:48:54 -0600 (Sat, 20 Dec 2008) | 1 line
silence annoying DeprecationWarning
................
r67889 | benjamin.peterson | 2008-12-20 19:04:32 -0600 (Sat, 20 Dec 2008) | 1 line
sphinx.web is long gone
................
r67890 | benjamin.peterson | 2008-12-20 19:12:26 -0600 (Sat, 20 Dec 2008) | 1 line
update readme
................
r67891 | benjamin.peterson | 2008-12-20 19:14:47 -0600 (Sat, 20 Dec 2008) | 1 line
there are way too many places which need to have the current version added
................
r67892 | benjamin.peterson | 2008-12-20 19:29:32 -0600 (Sat, 20 Dec 2008) | 9 lines
Merged revisions 67809 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r67809 | benjamin.peterson | 2008-12-15 21:54:45 -0600 (Mon, 15 Dec 2008) | 1 line
fix logic error
........
................
r67895 | neal.norwitz | 2008-12-21 08:28:32 -0600 (Sun, 21 Dec 2008) | 2 lines
Add Tarek for work on distutils.
................
2008-12-21 13:01:26 -04:00
|
|
|
PyGILState_STATE gil;
|
2001-09-14 12:50:08 -03:00
|
|
|
fprintf(stderr, "object : ");
|
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r67654 | georg.brandl | 2008-12-07 16:42:09 -0600 (Sun, 07 Dec 2008) | 2 lines
#4457: rewrite __import__() documentation.
................
r67676 | benjamin.peterson | 2008-12-08 20:03:03 -0600 (Mon, 08 Dec 2008) | 1 line
specify how things are copied
................
r67677 | benjamin.peterson | 2008-12-08 20:05:11 -0600 (Mon, 08 Dec 2008) | 1 line
revert unrelated change to installer script
................
r67681 | jeremy.hylton | 2008-12-09 15:03:10 -0600 (Tue, 09 Dec 2008) | 2 lines
Add simple unittests for Request
................
r67692 | amaury.forgeotdarc | 2008-12-10 18:03:42 -0600 (Wed, 10 Dec 2008) | 2 lines
#1030250: correctly pass the dry_run option to the mkpath() function.
................
r67725 | benjamin.peterson | 2008-12-12 22:02:20 -0600 (Fri, 12 Dec 2008) | 1 line
fix incorrect example
................
r67746 | antoine.pitrou | 2008-12-13 17:12:30 -0600 (Sat, 13 Dec 2008) | 3 lines
Issue #4163: Use unicode-friendly word splitting in the textwrap functions when given an unicode string.
................
r67748 | benjamin.peterson | 2008-12-13 19:46:11 -0600 (Sat, 13 Dec 2008) | 1 line
remove has_key usage
................
r67761 | benjamin.peterson | 2008-12-14 11:26:04 -0600 (Sun, 14 Dec 2008) | 1 line
fix missing bracket
................
r67784 | georg.brandl | 2008-12-15 02:33:58 -0600 (Mon, 15 Dec 2008) | 2 lines
#4446: document "platforms" argument for setup().
................
r67785 | georg.brandl | 2008-12-15 02:36:11 -0600 (Mon, 15 Dec 2008) | 2 lines
#4611: fix typo.
................
r67787 | georg.brandl | 2008-12-15 02:58:59 -0600 (Mon, 15 Dec 2008) | 2 lines
#4578: fix has_key() usage in compiler package.
................
r67788 | georg.brandl | 2008-12-15 03:07:39 -0600 (Mon, 15 Dec 2008) | 2 lines
#4568: remove limitation in varargs callback example.
................
r67802 | amaury.forgeotdarc | 2008-12-15 16:29:14 -0600 (Mon, 15 Dec 2008) | 4 lines
#3632: the "pyo" macro from gdbinit can now run when the GIL is released.
Patch by haypo.
................
r67832 | antoine.pitrou | 2008-12-17 16:46:54 -0600 (Wed, 17 Dec 2008) | 4 lines
Issue #2467: gc.DEBUG_STATS reports invalid elapsed times.
Patch by Neil Schemenauer, very slightly modified.
................
r67848 | benjamin.peterson | 2008-12-18 20:28:56 -0600 (Thu, 18 Dec 2008) | 1 line
fix typo
................
r67849 | benjamin.peterson | 2008-12-18 20:31:35 -0600 (Thu, 18 Dec 2008) | 1 line
_call_method -> _callmethod and _get_value to _getvalue
................
r67859 | amaury.forgeotdarc | 2008-12-19 16:56:48 -0600 (Fri, 19 Dec 2008) | 4 lines
#4700: crtlicense.txt is displayed by the license() command and should be kept ascii-only.
Will port to 3.0
................
r67862 | benjamin.peterson | 2008-12-19 20:48:02 -0600 (Fri, 19 Dec 2008) | 1 line
copy sentence from docstring
................
r67863 | benjamin.peterson | 2008-12-19 20:51:26 -0600 (Fri, 19 Dec 2008) | 1 line
add headings
................
r67864 | benjamin.peterson | 2008-12-19 20:57:19 -0600 (Fri, 19 Dec 2008) | 1 line
beef up docstring
................
r67880 | benjamin.peterson | 2008-12-20 16:49:24 -0600 (Sat, 20 Dec 2008) | 1 line
remove redundant sentence
................
r67882 | benjamin.peterson | 2008-12-20 16:59:49 -0600 (Sat, 20 Dec 2008) | 1 line
add some recent releases to the list
................
r67885 | benjamin.peterson | 2008-12-20 17:48:54 -0600 (Sat, 20 Dec 2008) | 1 line
silence annoying DeprecationWarning
................
r67889 | benjamin.peterson | 2008-12-20 19:04:32 -0600 (Sat, 20 Dec 2008) | 1 line
sphinx.web is long gone
................
r67890 | benjamin.peterson | 2008-12-20 19:12:26 -0600 (Sat, 20 Dec 2008) | 1 line
update readme
................
r67891 | benjamin.peterson | 2008-12-20 19:14:47 -0600 (Sat, 20 Dec 2008) | 1 line
there are way too many places which need to have the current version added
................
r67892 | benjamin.peterson | 2008-12-20 19:29:32 -0600 (Sat, 20 Dec 2008) | 9 lines
Merged revisions 67809 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r67809 | benjamin.peterson | 2008-12-15 21:54:45 -0600 (Mon, 15 Dec 2008) | 1 line
fix logic error
........
................
r67895 | neal.norwitz | 2008-12-21 08:28:32 -0600 (Sun, 21 Dec 2008) | 2 lines
Add Tarek for work on distutils.
................
2008-12-21 13:01:26 -04:00
|
|
|
gil = PyGILState_Ensure();
|
2001-02-22 18:39:18 -04:00
|
|
|
(void)PyObject_Print(op, stderr, 0);
|
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r67654 | georg.brandl | 2008-12-07 16:42:09 -0600 (Sun, 07 Dec 2008) | 2 lines
#4457: rewrite __import__() documentation.
................
r67676 | benjamin.peterson | 2008-12-08 20:03:03 -0600 (Mon, 08 Dec 2008) | 1 line
specify how things are copied
................
r67677 | benjamin.peterson | 2008-12-08 20:05:11 -0600 (Mon, 08 Dec 2008) | 1 line
revert unrelated change to installer script
................
r67681 | jeremy.hylton | 2008-12-09 15:03:10 -0600 (Tue, 09 Dec 2008) | 2 lines
Add simple unittests for Request
................
r67692 | amaury.forgeotdarc | 2008-12-10 18:03:42 -0600 (Wed, 10 Dec 2008) | 2 lines
#1030250: correctly pass the dry_run option to the mkpath() function.
................
r67725 | benjamin.peterson | 2008-12-12 22:02:20 -0600 (Fri, 12 Dec 2008) | 1 line
fix incorrect example
................
r67746 | antoine.pitrou | 2008-12-13 17:12:30 -0600 (Sat, 13 Dec 2008) | 3 lines
Issue #4163: Use unicode-friendly word splitting in the textwrap functions when given an unicode string.
................
r67748 | benjamin.peterson | 2008-12-13 19:46:11 -0600 (Sat, 13 Dec 2008) | 1 line
remove has_key usage
................
r67761 | benjamin.peterson | 2008-12-14 11:26:04 -0600 (Sun, 14 Dec 2008) | 1 line
fix missing bracket
................
r67784 | georg.brandl | 2008-12-15 02:33:58 -0600 (Mon, 15 Dec 2008) | 2 lines
#4446: document "platforms" argument for setup().
................
r67785 | georg.brandl | 2008-12-15 02:36:11 -0600 (Mon, 15 Dec 2008) | 2 lines
#4611: fix typo.
................
r67787 | georg.brandl | 2008-12-15 02:58:59 -0600 (Mon, 15 Dec 2008) | 2 lines
#4578: fix has_key() usage in compiler package.
................
r67788 | georg.brandl | 2008-12-15 03:07:39 -0600 (Mon, 15 Dec 2008) | 2 lines
#4568: remove limitation in varargs callback example.
................
r67802 | amaury.forgeotdarc | 2008-12-15 16:29:14 -0600 (Mon, 15 Dec 2008) | 4 lines
#3632: the "pyo" macro from gdbinit can now run when the GIL is released.
Patch by haypo.
................
r67832 | antoine.pitrou | 2008-12-17 16:46:54 -0600 (Wed, 17 Dec 2008) | 4 lines
Issue #2467: gc.DEBUG_STATS reports invalid elapsed times.
Patch by Neil Schemenauer, very slightly modified.
................
r67848 | benjamin.peterson | 2008-12-18 20:28:56 -0600 (Thu, 18 Dec 2008) | 1 line
fix typo
................
r67849 | benjamin.peterson | 2008-12-18 20:31:35 -0600 (Thu, 18 Dec 2008) | 1 line
_call_method -> _callmethod and _get_value to _getvalue
................
r67859 | amaury.forgeotdarc | 2008-12-19 16:56:48 -0600 (Fri, 19 Dec 2008) | 4 lines
#4700: crtlicense.txt is displayed by the license() command and should be kept ascii-only.
Will port to 3.0
................
r67862 | benjamin.peterson | 2008-12-19 20:48:02 -0600 (Fri, 19 Dec 2008) | 1 line
copy sentence from docstring
................
r67863 | benjamin.peterson | 2008-12-19 20:51:26 -0600 (Fri, 19 Dec 2008) | 1 line
add headings
................
r67864 | benjamin.peterson | 2008-12-19 20:57:19 -0600 (Fri, 19 Dec 2008) | 1 line
beef up docstring
................
r67880 | benjamin.peterson | 2008-12-20 16:49:24 -0600 (Sat, 20 Dec 2008) | 1 line
remove redundant sentence
................
r67882 | benjamin.peterson | 2008-12-20 16:59:49 -0600 (Sat, 20 Dec 2008) | 1 line
add some recent releases to the list
................
r67885 | benjamin.peterson | 2008-12-20 17:48:54 -0600 (Sat, 20 Dec 2008) | 1 line
silence annoying DeprecationWarning
................
r67889 | benjamin.peterson | 2008-12-20 19:04:32 -0600 (Sat, 20 Dec 2008) | 1 line
sphinx.web is long gone
................
r67890 | benjamin.peterson | 2008-12-20 19:12:26 -0600 (Sat, 20 Dec 2008) | 1 line
update readme
................
r67891 | benjamin.peterson | 2008-12-20 19:14:47 -0600 (Sat, 20 Dec 2008) | 1 line
there are way too many places which need to have the current version added
................
r67892 | benjamin.peterson | 2008-12-20 19:29:32 -0600 (Sat, 20 Dec 2008) | 9 lines
Merged revisions 67809 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r67809 | benjamin.peterson | 2008-12-15 21:54:45 -0600 (Mon, 15 Dec 2008) | 1 line
fix logic error
........
................
r67895 | neal.norwitz | 2008-12-21 08:28:32 -0600 (Sun, 21 Dec 2008) | 2 lines
Add Tarek for work on distutils.
................
2008-12-21 13:01:26 -04:00
|
|
|
PyGILState_Release(gil);
|
2006-03-01 01:41:20 -04:00
|
|
|
/* XXX(twouters) cast refcount to long until %zd is
|
|
|
|
universally available */
|
2001-09-14 12:50:08 -03:00
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"type : %s\n"
|
2006-03-01 01:41:20 -04:00
|
|
|
"refcount: %ld\n"
|
2001-09-14 12:50:08 -03:00
|
|
|
"address : %p\n",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
|
2006-03-01 01:41:20 -04:00
|
|
|
(long)op->ob_refcnt,
|
2001-09-14 12:50:08 -03:00
|
|
|
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)
|
2008-06-09 01:58:54 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
2007-12-18 22:37:44 -04:00
|
|
|
else if (Py_TYPE(v)->tp_repr == NULL)
|
2008-06-09 01:58:54 -03:00
|
|
|
return PyString_FromFormat("<%s object at %p>",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(v)->tp_name, v);
|
2000-03-10 18:55:18 -04:00
|
|
|
else {
|
|
|
|
PyObject *res;
|
2007-12-18 22:37:44 -04:00
|
|
|
res = (*Py_TYPE(v)->tp_repr)(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-01 11:31:09 -03:00
|
|
|
if (PyUnicode_Check(res)) {
|
|
|
|
PyObject* str;
|
2006-03-30 06:53:17 -04:00
|
|
|
str = PyUnicode_AsEncodedString(res, NULL, NULL);
|
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
|
2008-06-09 01:58:54 -03:00
|
|
|
if (!PyString_Check(res)) {
|
2000-03-10 18:55:18 -04:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-10 09:46:51 -03:00
|
|
|
"__repr__ returned non-string (type %.200s)",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(res)->tp_name);
|
2000-03-10 18:55:18 -04:00
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2005-08-12 14:34:58 -03:00
|
|
|
_PyObject_Str(PyObject *v)
|
1993-11-05 06:22:19 -04:00
|
|
|
{
|
2000-03-10 18:55:18 -04:00
|
|
|
PyObject *res;
|
2005-08-12 14:34:58 -03:00
|
|
|
int type_ok;
|
1993-11-05 06:22:19 -04:00
|
|
|
if (v == NULL)
|
2008-06-09 01:58:54 -03:00
|
|
|
return PyString_FromString("<NULL>");
|
|
|
|
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;
|
|
|
|
}
|
2005-08-12 14:34:58 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
|
|
|
if (PyUnicode_CheckExact(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
#endif
|
2007-12-18 22:37:44 -04:00
|
|
|
if (Py_TYPE(v)->tp_str == NULL)
|
2001-05-01 13:53:37 -03:00
|
|
|
return PyObject_Repr(v);
|
|
|
|
|
2007-09-30 16:45:10 -03:00
|
|
|
/* It is possible for a type to have a tp_str representation that loops
|
|
|
|
infinitely. */
|
|
|
|
if (Py_EnterRecursiveCall(" while getting the str of an object"))
|
|
|
|
return NULL;
|
2007-12-18 22:37:44 -04:00
|
|
|
res = (*Py_TYPE(v)->tp_str)(v);
|
2007-09-30 16:45:10 -03:00
|
|
|
Py_LeaveRecursiveCall();
|
2000-03-10 18:55:18 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2008-06-09 01:58:54 -03:00
|
|
|
type_ok = PyString_Check(res);
|
2005-08-12 14:34:58 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
|
|
|
type_ok = type_ok || PyUnicode_Check(res);
|
|
|
|
#endif
|
|
|
|
if (!type_ok) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__str__ returned non-string (type %.200s)",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(res)->tp_name);
|
2005-08-12 14:34:58 -03:00
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_Str(PyObject *v)
|
|
|
|
{
|
|
|
|
PyObject *res = _PyObject_Str(v);
|
|
|
|
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
|
2008-06-09 01:58:54 -03:00
|
|
|
assert(PyString_Check(res));
|
2000-03-10 18:55:18 -04:00
|
|
|
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;
|
2005-04-26 00:45:26 -03:00
|
|
|
PyObject *func;
|
2006-03-14 02:02:16 -04:00
|
|
|
PyObject *str;
|
2008-07-08 11:08:04 -03:00
|
|
|
int unicode_method_found = 0;
|
2005-04-26 00:45:26 -03:00
|
|
|
static PyObject *unicodestr;
|
2002-07-07 02:13:56 -03:00
|
|
|
|
2006-03-13 18:22:11 -04:00
|
|
|
if (v == NULL) {
|
2008-06-09 01:58:54 -03:00
|
|
|
res = PyString_FromString("<NULL>");
|
2006-03-14 02:02:16 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
str = PyUnicode_FromEncodedObject(res, NULL, "strict");
|
|
|
|
Py_DECREF(res);
|
|
|
|
return str;
|
2006-03-13 18:22:11 -04:00
|
|
|
} else 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;
|
|
|
|
}
|
2008-07-08 11:08:04 -03:00
|
|
|
|
|
|
|
/* Try the __unicode__ method */
|
2005-04-26 00:45:26 -03:00
|
|
|
if (unicodestr == NULL) {
|
2008-06-09 01:58:54 -03:00
|
|
|
unicodestr= PyString_InternFromString("__unicode__");
|
2005-04-26 00:45:26 -03:00
|
|
|
if (unicodestr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-08 11:08:04 -03:00
|
|
|
if (PyInstance_Check(v)) {
|
|
|
|
/* We're an instance of a classic class */
|
|
|
|
/* Try __unicode__ from the instance -- alas we have no type */
|
|
|
|
func = PyObject_GetAttr(v, unicodestr);
|
|
|
|
if (func != NULL) {
|
|
|
|
unicode_method_found = 1;
|
|
|
|
res = PyObject_CallFunctionObjArgs(func, NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2001-10-18 23:01:31 -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 {
|
2008-07-08 11:08:04 -03:00
|
|
|
/* Not a classic class instance, try __unicode__ from type */
|
|
|
|
/* _PyType_Lookup doesn't create a reference */
|
|
|
|
func = _PyType_Lookup(Py_TYPE(v), unicodestr);
|
|
|
|
if (func != NULL) {
|
|
|
|
unicode_method_found = 1;
|
|
|
|
res = PyObject_CallFunctionObjArgs(func, v, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Didn't find __unicode__ */
|
|
|
|
if (!unicode_method_found) {
|
2005-04-26 00:45:26 -03:00
|
|
|
if (PyUnicode_Check(v)) {
|
|
|
|
/* For a Unicode subtype that's didn't overwrite __unicode__,
|
|
|
|
return a true Unicode object with the same data. */
|
|
|
|
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
|
2008-07-08 11:08:04 -03:00
|
|
|
PyUnicode_GET_SIZE(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
|
|
|
}
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_CheckExact(v)) {
|
2005-04-26 00:45:26 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
res = 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
|
|
|
}
|
2001-10-18 23:01:31 -03:00
|
|
|
else {
|
2007-12-18 22:37:44 -04:00
|
|
|
if (Py_TYPE(v)->tp_str != NULL)
|
|
|
|
res = (*Py_TYPE(v)->tp_str)(v);
|
2001-10-18 23:01:31 -03:00
|
|
|
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
|
|
|
}
|
2008-07-08 11:08:04 -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 (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!PyUnicode_Check(res)) {
|
2001-10-18 23:01:31 -03:00
|
|
|
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);
|
2006-03-14 02:02:16 -04:00
|
|
|
res = str;
|
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 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
|
|
|
|
|
|
|
|
2002-05-31 17:03:54 -03:00
|
|
|
/* Helper to warn about deprecated tp_compare return values. Return:
|
|
|
|
-2 for an exception;
|
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w.
|
|
|
|
(This function cannot return 2.)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
adjust_tp_compare(int c)
|
|
|
|
{
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
if (c != -1 && c != -2) {
|
|
|
|
PyObject *t, *v, *tb;
|
|
|
|
PyErr_Fetch(&t, &v, &tb);
|
|
|
|
if (PyErr_Warn(PyExc_RuntimeWarning,
|
|
|
|
"tp_compare didn't return -1 or -2 "
|
|
|
|
"for exception") < 0) {
|
|
|
|
Py_XDECREF(t);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PyErr_Restore(t, v, tb);
|
|
|
|
}
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else if (c < -1 || c > 1) {
|
|
|
|
if (PyErr_Warn(PyExc_RuntimeWarning,
|
|
|
|
"tp_compare didn't return -1, 0 or 1") < 0)
|
|
|
|
return -2;
|
|
|
|
else
|
|
|
|
return c < -1 ? -1 : 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(c >= -1 && c <= 1);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
2004-09-24 22:37:24 -03:00
|
|
|
int _Py_SwappedOp[] = {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) {
|
2004-09-22 23:39:37 -03:00
|
|
|
res = (*f)(w, v, _Py_SwappedOp[op]);
|
2001-09-27 17:30:07 -03:00
|
|
|
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) {
|
2004-09-22 23:39:37 -03:00
|
|
|
return (*f)(w, v, _Py_SwappedOp[op]);
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
|
|
|
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);
|
2002-05-31 17:03:54 -03:00
|
|
|
return adjust_tp_compare(c);
|
2001-09-18 17:38:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2004-12-23 18:13:13 -04:00
|
|
|
/* If we're here, v and w,
|
|
|
|
a) are not instances;
|
|
|
|
b) have different types or a type without tp_compare; and
|
|
|
|
c) don't have a user-defined tp_compare.
|
|
|
|
tp_compare implementations in C assume that both arguments
|
|
|
|
have their type, so we give up if the coercion fails or if
|
|
|
|
it yields types which are still incompatible (which can
|
|
|
|
happen with a user-defined nb_coerce).
|
|
|
|
*/
|
2001-01-17 11:24:28 -04:00
|
|
|
c = PyNumber_CoerceEx(&v, &w);
|
|
|
|
if (c < 0)
|
|
|
|
return -2;
|
|
|
|
if (c > 0)
|
|
|
|
return 2;
|
2004-12-23 18:13:13 -04:00
|
|
|
f = v->ob_type->tp_compare;
|
|
|
|
if (f != NULL && f == w->ob_type->tp_compare) {
|
2001-01-17 11:24:28 -04:00
|
|
|
c = (*f)(v, w);
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
2002-05-31 17:03:54 -03:00
|
|
|
return adjust_tp_compare(c);
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
2001-01-17 11:24:28 -04:00
|
|
|
|
|
|
|
/* 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;
|
2005-12-10 14:50:16 -04:00
|
|
|
const 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-01-22 15:28:09 -04:00
|
|
|
/* None is smaller than anything */
|
|
|
|
if (v == Py_None)
|
|
|
|
return -1;
|
|
|
|
if (w == Py_None)
|
|
|
|
return 1;
|
|
|
|
|
2003-02-18 12:40:09 -04:00
|
|
|
/* different type: compare type names; numbers are smaller */
|
|
|
|
if (PyNumber_Check(v))
|
2001-01-22 11:59:32 -04:00
|
|
|
vname = "";
|
|
|
|
else
|
|
|
|
vname = v->ob_type->tp_name;
|
2003-02-18 12:40:09 -04:00
|
|
|
if (PyNumber_Check(w))
|
2001-01-22 11:59:32 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-05-07 17:53:51 -03:00
|
|
|
/* Do a 3-way comparison, by hook or by crook. Return:
|
2002-05-31 17:03:54 -03:00
|
|
|
-2 for an exception (but see below);
|
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;
|
2002-05-31 17:03:54 -03:00
|
|
|
BUT: if the object implements a tp_compare function, it returns
|
2001-06-09 04:34:05 -03:00
|
|
|
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);
|
2002-05-31 17:03:54 -03:00
|
|
|
if (PyInstance_Check(v)) {
|
|
|
|
/* Instance tp_compare has a different signature.
|
|
|
|
But if it returns undefined we fall through. */
|
|
|
|
if (c != 2)
|
|
|
|
return c;
|
2002-05-31 17:23:33 -03:00
|
|
|
/* Else fall through to try_rich_to_3way_compare() */
|
2002-05-31 17:03:54 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return adjust_tp_compare(c);
|
|
|
|
}
|
|
|
|
/* We only get here if one of the following is true:
|
|
|
|
a) v and w have different types
|
|
|
|
b) v and w have the same type, which doesn't have tp_compare
|
|
|
|
c) v and w are instances, and either __cmp__ is not defined or
|
|
|
|
__cmp__ returns NotImplemented
|
|
|
|
*/
|
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-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
|
|
|
{
|
2000-04-14 16:13:24 -03:00
|
|
|
int result;
|
|
|
|
|
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;
|
2003-10-28 08:05:48 -04:00
|
|
|
if (Py_EnterRecursiveCall(" in cmp"))
|
|
|
|
return -1;
|
|
|
|
result = do_cmp(v, w);
|
|
|
|
Py_LeaveRecursiveCall();
|
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
|
|
|
}
|
2002-07-07 02:13:56 -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);
|
2008-03-18 14:26:10 -03:00
|
|
|
if (c >= 2) {
|
|
|
|
|
|
|
|
/* Py3K warning if types are not equal and comparison isn't == or != */
|
|
|
|
if (Py_Py3kWarningFlag &&
|
2008-03-25 05:29:14 -03:00
|
|
|
v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE &&
|
2008-04-27 00:01:45 -03:00
|
|
|
PyErr_WarnEx(PyExc_DeprecationWarning,
|
2008-03-25 05:29:14 -03:00
|
|
|
"comparing unequal types not supported "
|
2008-04-27 00:01:45 -03:00
|
|
|
"in 3.x", 1) < 0) {
|
2008-03-18 14:26:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-09 04:34:05 -03:00
|
|
|
c = default_3way_compare(v, w);
|
2008-03-18 14:26:10 -03:00
|
|
|
}
|
2001-06-09 04:34:05 -03:00
|
|
|
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);
|
2003-10-28 08:05:48 -04:00
|
|
|
if (Py_EnterRecursiveCall(" in cmp"))
|
|
|
|
return NULL;
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2003-10-28 08:05:48 -04:00
|
|
|
/* If the types are equal, and not old-style instances, try to
|
2001-11-04 03:29:31 -04:00
|
|
|
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);
|
2002-05-31 17:03:54 -03:00
|
|
|
c = adjust_tp_compare(c);
|
|
|
|
if (c == -2) {
|
2001-11-04 03:29:31 -04:00
|
|
|
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:
|
2003-10-28 08:05:48 -04:00
|
|
|
Py_LeaveRecursiveCall();
|
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)
|
|
|
|
{
|
2004-03-21 13:01:44 -04:00
|
|
|
PyObject *res;
|
2001-01-17 11:24:28 -04:00
|
|
|
int ok;
|
|
|
|
|
2004-03-21 13:01:44 -04:00
|
|
|
/* Quick result when objects are the same.
|
|
|
|
Guarantees that identity implies equality. */
|
|
|
|
if (v == w) {
|
|
|
|
if (op == Py_EQ)
|
|
|
|
return 1;
|
|
|
|
else if (op == Py_NE)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = PyObject_RichCompare(v, w, op);
|
2001-01-17 11:24:28 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
2002-08-24 02:33:28 -03:00
|
|
|
if (PyBool_Check(res))
|
|
|
|
ok = (res == Py_True);
|
|
|
|
else
|
|
|
|
ok = PyObject_IsTrue(res);
|
2001-01-17 11:24:28 -04:00
|
|
|
Py_DECREF(res);
|
|
|
|
return ok;
|
|
|
|
}
|
2000-06-29 16:17:04 -03:00
|
|
|
|
|
|
|
/* Set of hash utility functions to help maintaining the invariant that
|
2004-03-21 13:35:06 -04:00
|
|
|
if a==b then hash(a)==hash(b)
|
2000-06-29 16:17:04 -03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fractpart = modf(v, &intpart);
|
|
|
|
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;
|
2002-07-07 02:13:56 -03:00
|
|
|
|
2000-06-29 16:17:04 -03:00
|
|
|
if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
|
|
|
|
x = -1;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
x = PyObject_Hash(longobj);
|
2002-07-07 02:13:56 -03:00
|
|
|
|
2000-06-29 16:17:04 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(longobj);
|
|
|
|
return x;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-15 11:27:37 -03:00
|
|
|
long
|
|
|
|
PyObject_HashNotImplemented(PyObject *self)
|
|
|
|
{
|
|
|
|
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
|
|
|
|
self->ob_type->tp_name);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-29 16:17:04 -03:00
|
|
|
|
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);
|
2008-12-29 21:36:00 -04:00
|
|
|
/* To keep to the general practice that inheriting
|
|
|
|
* solely from object in C code should work without
|
|
|
|
* an explicit call to PyType_Ready, we implicitly call
|
|
|
|
* PyType_Ready here and then check the tp_hash slot again
|
|
|
|
*/
|
|
|
|
if (tp->tp_dict == NULL) {
|
|
|
|
if (PyType_Ready(tp) < 0)
|
|
|
|
return -1;
|
|
|
|
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 */
|
2008-07-15 11:27:37 -03:00
|
|
|
return PyObject_HashNotImplemented(v);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2005-12-10 14:50:16 -04:00
|
|
|
PyObject_GetAttrString(PyObject *v, const 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
|
|
|
|
2007-12-18 22:37:44 -04:00
|
|
|
if (Py_TYPE(v)->tp_getattr != NULL)
|
|
|
|
return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
|
2008-06-09 01:58:54 -03:00
|
|
|
w = PyString_InternFromString(name);
|
2001-08-02 01:15:00 -03:00
|
|
|
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
|
2005-12-10 14:50:16 -04:00
|
|
|
PyObject_HasAttrString(PyObject *v, const 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
|
2005-12-10 14:50:16 -04:00
|
|
|
PyObject_SetAttrString(PyObject *v, const 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
|
|
|
|
2007-12-18 22:37:44 -04:00
|
|
|
if (Py_TYPE(v)->tp_setattr != NULL)
|
|
|
|
return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
|
2008-06-09 01:58:54 -03:00
|
|
|
s = PyString_InternFromString(name);
|
2001-08-02 01:15:00 -03:00
|
|
|
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
|
|
|
{
|
2007-12-18 22:37:44 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(v);
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2008-06-09 01:58:54 -03: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
|
|
|
{
|
2006-06-18 19:17:29 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(name)->tp_name);
|
2002-03-15 09:40:30 -04:00
|
|
|
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)
|
2008-06-09 01:58:54 -03:00
|
|
|
return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
2008-06-09 01:58:54 -03:00
|
|
|
tp->tp_name, PyString_AS_STRING(name));
|
2001-08-02 01:15:00 -03:00
|
|
|
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
|
|
|
{
|
2007-12-18 22:37:44 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(v);
|
1997-05-20 15:34:44 -03:00
|
|
|
int err;
|
2000-09-18 13:20:57 -03:00
|
|
|
|
2008-06-09 01:58:54 -03: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;
|
|
|
|
}
|
2002-07-07 02:13:56 -03:00
|
|
|
else
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
2006-06-18 19:17:29 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(name)->tp_name);
|
2002-03-15 09:40:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2000-06-23 11:36:32 -03:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
else
|
|
|
|
Py_INCREF(name);
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_InternInPlace(&name);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (tp->tp_setattro != NULL) {
|
|
|
|
err = (*tp->tp_setattro)(v, name, value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (tp->tp_setattr != NULL) {
|
2008-06-09 01:58:54 -03:00
|
|
|
err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
|
2001-08-02 01:15:00 -03:00
|
|
|
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",
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_AS_STRING(name));
|
2001-08-02 01:15:00 -03:00
|
|
|
else
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.100s' object has only read-only attributes "
|
|
|
|
"(%s .%.100s)",
|
|
|
|
tp->tp_name,
|
|
|
|
value==NULL ? "del" : "assign to",
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_AS_STRING(name));
|
2001-08-02 01:15:00 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to get a pointer to an object's __dict__ slot, if any */
|
|
|
|
|
|
|
|
PyObject **
|
|
|
|
_PyObject_GetDictPtr(PyObject *obj)
|
|
|
|
{
|
2006-03-07 08:08:51 -04:00
|
|
|
Py_ssize_t dictoffset;
|
2007-12-18 22:37:44 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
|
|
|
|
return NULL;
|
|
|
|
dictoffset = tp->tp_dictoffset;
|
|
|
|
if (dictoffset == 0)
|
|
|
|
return NULL;
|
|
|
|
if (dictoffset < 0) {
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t tsize;
|
2002-03-01 18:24:49 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-03-17 04:24:35 -04:00
|
|
|
PyObject *
|
2003-03-17 15:46:11 -04:00
|
|
|
PyObject_SelfIter(PyObject *obj)
|
2003-03-17 04:24:35 -04:00
|
|
|
{
|
|
|
|
Py_INCREF(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2004-09-14 14:09:47 -03:00
|
|
|
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|
|
|
{
|
2007-12-18 22:37:44 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
2002-08-19 16:22:50 -03:00
|
|
|
PyObject *descr = NULL;
|
2001-12-04 11:54:53 -04:00
|
|
|
PyObject *res = NULL;
|
2001-08-02 01:15:00 -03:00
|
|
|
descrgetfunc f;
|
2006-03-07 08:08:51 -04:00
|
|
|
Py_ssize_t dictoffset;
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject **dictptr;
|
|
|
|
|
2008-06-09 01:58:54 -03: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;
|
|
|
|
}
|
2002-07-07 02:13:56 -03:00
|
|
|
else
|
2001-12-04 11:54:53 -04:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
2006-06-18 19:17:29 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(name)->tp_name);
|
2002-03-15 09:40:30 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-01-13 20:29:41 -04:00
|
|
|
#if 0 /* XXX this is not quite _PyType_Lookup anymore */
|
2002-08-19 16:22:50 -03:00
|
|
|
/* Inline _PyType_Lookup */
|
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, n;
|
2002-08-19 16:22:50 -03:00
|
|
|
PyObject *mro, *base, *dict;
|
|
|
|
|
|
|
|
/* Look in tp_dict of types in MRO */
|
|
|
|
mro = tp->tp_mro;
|
|
|
|
assert(mro != NULL);
|
|
|
|
assert(PyTuple_Check(mro));
|
|
|
|
n = PyTuple_GET_SIZE(mro);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
base = PyTuple_GET_ITEM(mro, i);
|
|
|
|
if (PyClass_Check(base))
|
|
|
|
dict = ((PyClassObject *)base)->cl_dict;
|
|
|
|
else {
|
|
|
|
assert(PyType_Check(base));
|
|
|
|
dict = ((PyTypeObject *)base)->tp_dict;
|
|
|
|
}
|
|
|
|
assert(dict && PyDict_Check(dict));
|
|
|
|
descr = PyDict_GetItem(dict, name);
|
|
|
|
if (descr != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-13 20:29:41 -04:00
|
|
|
#else
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
#endif
|
2002-08-19 16:22:50 -03:00
|
|
|
|
2003-08-15 10:07:47 -03:00
|
|
|
Py_XINCREF(descr);
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
f = NULL;
|
2003-02-18 23:19:29 -04:00
|
|
|
if (descr != NULL &&
|
|
|
|
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
|
2001-08-02 01:15:00 -03:00
|
|
|
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);
|
2003-08-15 10:07:47 -03:00
|
|
|
Py_DECREF(descr);
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2002-08-19 13:50:48 -03:00
|
|
|
/* Inline _PyObject_GetDictPtr */
|
|
|
|
dictoffset = tp->tp_dictoffset;
|
|
|
|
if (dictoffset != 0) {
|
|
|
|
PyObject *dict;
|
|
|
|
if (dictoffset < 0) {
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t tsize;
|
2002-08-19 13:50:48 -03:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
tsize = ((PyVarObject *)obj)->ob_size;
|
|
|
|
if (tsize < 0)
|
|
|
|
tsize = -tsize;
|
|
|
|
size = _PyObject_VAR_SIZE(tp, tsize);
|
|
|
|
|
|
|
|
dictoffset += (long)size;
|
|
|
|
assert(dictoffset > 0);
|
|
|
|
assert(dictoffset % SIZEOF_VOID_P == 0);
|
|
|
|
}
|
|
|
|
dictptr = (PyObject **) ((char *)obj + dictoffset);
|
|
|
|
dict = *dictptr;
|
2001-08-02 01:15:00 -03:00
|
|
|
if (dict != NULL) {
|
2008-01-24 13:58:05 -04:00
|
|
|
Py_INCREF(dict);
|
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);
|
2003-08-15 10:07:47 -03:00
|
|
|
Py_XDECREF(descr);
|
2008-01-24 13:58:05 -04:00
|
|
|
Py_DECREF(dict);
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
2008-01-24 13:58:05 -04:00
|
|
|
Py_DECREF(dict);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-04 11:54:53 -04:00
|
|
|
if (f != NULL) {
|
2007-12-18 22:37:44 -04:00
|
|
|
res = f(descr, obj, (PyObject *)Py_TYPE(obj));
|
2003-08-15 10:07:47 -03:00
|
|
|
Py_DECREF(descr);
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (descr != NULL) {
|
2001-12-04 11:54:53 -04:00
|
|
|
res = descr;
|
2003-08-15 10:07:47 -03:00
|
|
|
/* descr was already increfed above */
|
2001-12-04 11:54:53 -04:00
|
|
|
goto done;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%.400s'",
|
2008-06-09 01:58:54 -03:00
|
|
|
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)
|
|
|
|
{
|
2007-12-18 22:37:44 -04:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *descr;
|
|
|
|
descrsetfunc f;
|
|
|
|
PyObject **dictptr;
|
2001-12-04 11:54:53 -04:00
|
|
|
int res = -1;
|
|
|
|
|
2008-06-09 01:58:54 -03: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;
|
|
|
|
}
|
2002-07-07 02:13:56 -03:00
|
|
|
else
|
2001-12-04 11:54:53 -04:00
|
|
|
#endif
|
2002-03-15 09:40:30 -04:00
|
|
|
{
|
2006-06-18 19:17:29 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(name)->tp_name);
|
2002-03-15 09:40:30 -04:00
|
|
|
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;
|
2003-02-18 23:19:29 -04:00
|
|
|
if (descr != NULL &&
|
|
|
|
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
|
2001-08-02 01:15:00 -03:00
|
|
|
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) {
|
2008-01-24 13:58:05 -04:00
|
|
|
Py_INCREF(dict);
|
2001-08-02 01:15:00 -03:00
|
|
|
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);
|
2008-01-24 13:58:05 -04:00
|
|
|
Py_DECREF(dict);
|
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,
|
2006-06-18 19:17:29 -03:00
|
|
|
"'%.100s' object has no attribute '%.200s'",
|
2008-06-09 01:58:54 -03:00
|
|
|
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",
|
2008-06-09 01:58:54 -03:00
|
|
|
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
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t res;
|
2002-08-24 03:31:34 -03:00
|
|
|
if (v == Py_True)
|
|
|
|
return 1;
|
|
|
|
if (v == Py_False)
|
|
|
|
return 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v == Py_None)
|
2002-06-13 18:32:44 -03:00
|
|
|
return 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
|
2002-06-13 18:32:44 -03:00
|
|
|
return 1;
|
2006-02-15 13:27:45 -04:00
|
|
|
/* if it is negative, it should be either -1 or -2 */
|
|
|
|
return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* equivalent of 'not v'
|
1998-04-09 14:53:59 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
/* ------------------------- PyObject_Dir() helpers ------------------------- */
|
|
|
|
|
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 */
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t 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++) {
|
2003-02-05 15:35:19 -04:00
|
|
|
int status;
|
2002-05-13 15:29:46 -03:00
|
|
|
PyObject *base = PySequence_GetItem(bases, i);
|
|
|
|
if (base == NULL) {
|
|
|
|
Py_DECREF(bases);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-05 15:35:19 -04:00
|
|
|
status = merge_class_dict(dict, base);
|
|
|
|
Py_DECREF(base);
|
|
|
|
if (status < 0) {
|
2002-05-13 15:29:46 -03:00
|
|
|
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
|
2005-12-10 14:50:16 -04:00
|
|
|
merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname)
|
2001-09-16 23:38:46 -03:00
|
|
|
{
|
|
|
|
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);
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(item)) {
|
2001-09-16 23:38:46 -03:00
|
|
|
result = PyDict_SetItem(dict, item, Py_None);
|
|
|
|
if (result < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-21 17:21:46 -03:00
|
|
|
if (Py_Py3kWarningFlag &&
|
|
|
|
(strcmp(attrname, "__members__") == 0 ||
|
|
|
|
strcmp(attrname, "__methods__") == 0)) {
|
2008-04-27 00:01:45 -03:00
|
|
|
if (PyErr_WarnEx(PyExc_DeprecationWarning,
|
2008-03-25 05:29:14 -03:00
|
|
|
"__members__ and __methods__ not "
|
2008-04-27 00:01:45 -03:00
|
|
|
"supported in 3.x", 1) < 0) {
|
2008-03-21 17:21:46 -03:00
|
|
|
Py_XDECREF(list);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-09-16 23:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_XDECREF(list);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
/* Helper for PyObject_Dir without arguments: returns the local scope. */
|
|
|
|
static PyObject *
|
2007-03-16 11:49:11 -03:00
|
|
|
_dir_locals(void)
|
2001-09-04 19:08:56 -03:00
|
|
|
{
|
2007-03-12 10:17:36 -03:00
|
|
|
PyObject *names;
|
|
|
|
PyObject *locals = PyEval_GetLocals();
|
2001-09-04 19:08:56 -03:00
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
if (locals == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "frame does not exist");
|
|
|
|
return NULL;
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
names = PyMapping_Keys(locals);
|
|
|
|
if (!names)
|
|
|
|
return NULL;
|
|
|
|
if (!PyList_Check(names)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"dir(): expected keys() of locals to be a list, "
|
2007-12-18 22:37:44 -04:00
|
|
|
"not '%.200s'", Py_TYPE(names)->tp_name);
|
2007-03-12 10:17:36 -03:00
|
|
|
Py_DECREF(names);
|
|
|
|
return NULL;
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
2007-03-12 10:17:36 -03:00
|
|
|
/* the locals don't need to be DECREF'd */
|
|
|
|
return names;
|
|
|
|
}
|
2001-09-04 19:08:56 -03:00
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
|
|
|
|
We deliberately don't suck up its __class__, as methods belonging to the
|
|
|
|
metaclass would probably be more confusing than helpful.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
_specialized_dir_type(PyObject *obj)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
PyObject *dict = PyDict_New();
|
2001-09-04 19:08:56 -03:00
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
if (dict != NULL && merge_class_dict(dict, obj) == 0)
|
|
|
|
result = PyDict_Keys(dict);
|
2001-09-16 23:38:46 -03:00
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
Py_XDECREF(dict);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for PyObject_Dir of module objects: returns the module's __dict__. */
|
|
|
|
static PyObject *
|
|
|
|
_specialized_dir_module(PyObject *obj)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
PyObject *dict = PyObject_GetAttrString(obj, "__dict__");
|
|
|
|
|
|
|
|
if (dict != NULL) {
|
|
|
|
if (PyDict_Check(dict))
|
|
|
|
result = PyDict_Keys(dict);
|
2001-09-04 19:08:56 -03:00
|
|
|
else {
|
2007-03-12 10:17:36 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%.200s.__dict__ is not a dictionary",
|
|
|
|
PyModule_GetName(obj));
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
Py_XDECREF(dict);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for PyObject_Dir of generic objects: returns __dict__, __class__,
|
|
|
|
and recursively up the __class__.__bases__ chain.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
_generic_dir(PyObject *obj)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
PyObject *dict = NULL;
|
|
|
|
PyObject *itsclass = NULL;
|
|
|
|
|
|
|
|
/* Get __dict__ (which may or may not be a real dict...) */
|
|
|
|
dict = PyObject_GetAttrString(obj, "__dict__");
|
|
|
|
if (dict == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
dict = PyDict_New();
|
|
|
|
}
|
|
|
|
else if (!PyDict_Check(dict)) {
|
|
|
|
Py_DECREF(dict);
|
|
|
|
dict = PyDict_New();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Copy __dict__ to avoid mutating it. */
|
|
|
|
PyObject *temp = PyDict_Copy(dict);
|
|
|
|
Py_DECREF(dict);
|
|
|
|
dict = temp;
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
if (dict == NULL)
|
2004-08-07 01:55:30 -03:00
|
|
|
goto error;
|
2007-03-12 10:17:36 -03:00
|
|
|
|
|
|
|
/* Merge in __members__ and __methods__ (if any).
|
|
|
|
* This is removed in Python 3000. */
|
|
|
|
if (merge_list_attr(dict, obj, "__members__") < 0)
|
2001-09-04 19:08:56 -03:00
|
|
|
goto error;
|
2007-03-12 10:17:36 -03:00
|
|
|
if (merge_list_attr(dict, obj, "__methods__") < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Merge in attrs reachable from its class. */
|
|
|
|
itsclass = PyObject_GetAttrString(obj, "__class__");
|
|
|
|
if (itsclass == NULL)
|
|
|
|
/* XXX(tomer): Perhaps fall back to obj->ob_type if no
|
|
|
|
__class__ exists? */
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
if (merge_class_dict(dict, itsclass) != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2001-09-04 19:08:56 -03:00
|
|
|
|
2007-03-12 10:17:36 -03:00
|
|
|
result = PyDict_Keys(dict);
|
2001-09-04 19:08:56 -03:00
|
|
|
/* fall through */
|
2007-03-12 10:17:36 -03:00
|
|
|
error:
|
|
|
|
Py_XDECREF(itsclass);
|
|
|
|
Py_XDECREF(dict);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for PyObject_Dir: object introspection.
|
|
|
|
This calls one of the above specialized versions if no __dir__ method
|
|
|
|
exists. */
|
|
|
|
static PyObject *
|
|
|
|
_dir_object(PyObject *obj)
|
|
|
|
{
|
2007-03-13 04:23:16 -03:00
|
|
|
PyObject *result = NULL;
|
|
|
|
PyObject *dirfunc = PyObject_GetAttrString((PyObject *)obj->ob_type,
|
|
|
|
"__dir__");
|
2007-03-12 10:17:36 -03:00
|
|
|
|
|
|
|
assert(obj);
|
|
|
|
if (dirfunc == NULL) {
|
|
|
|
/* use default implementation */
|
|
|
|
PyErr_Clear();
|
|
|
|
if (PyModule_Check(obj))
|
|
|
|
result = _specialized_dir_module(obj);
|
|
|
|
else if (PyType_Check(obj) || PyClass_Check(obj))
|
|
|
|
result = _specialized_dir_type(obj);
|
|
|
|
else
|
|
|
|
result = _generic_dir(obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* use __dir__ */
|
|
|
|
result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
|
|
|
|
Py_DECREF(dirfunc);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* result must be a list */
|
|
|
|
/* XXX(gbrandl): could also check if all items are strings */
|
|
|
|
if (!PyList_Check(result)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__dir__() must return a list, not %.200s",
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(result)->tp_name);
|
2007-03-12 10:17:36 -03:00
|
|
|
Py_DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Implementation of dir() -- if obj is NULL, returns the names in the current
|
|
|
|
(local) scope. Otherwise, performs introspection of the object: returns a
|
|
|
|
sorted list of attribute names (supposedly) accessible from the object
|
|
|
|
*/
|
|
|
|
PyObject *
|
|
|
|
PyObject_Dir(PyObject *obj)
|
|
|
|
{
|
|
|
|
PyObject * result;
|
|
|
|
|
|
|
|
if (obj == NULL)
|
|
|
|
/* no object -- introspect the locals */
|
|
|
|
result = _dir_locals();
|
|
|
|
else
|
|
|
|
/* object -- introspect the object */
|
|
|
|
result = _dir_object(obj);
|
|
|
|
|
|
|
|
assert(result == NULL || PyList_Check(result));
|
|
|
|
|
|
|
|
if (result != NULL && PyList_Sort(result) != 0) {
|
|
|
|
/* sorting the list failed */
|
|
|
|
Py_DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-04 19:08:56 -03:00
|
|
|
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
|
|
|
{
|
2008-06-09 01:58:54 -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
|
2002-07-07 02:13:56 -03:00
|
|
|
none_dealloc(PyObject* ignore)
|
2001-01-23 12:24:35 -04:00
|
|
|
{
|
|
|
|
/* This should never get called, but we also don't want to SEGV if
|
|
|
|
* we accidently decref None out of existance.
|
|
|
|
*/
|
2002-08-07 13:21:51 -03:00
|
|
|
Py_FatalError("deallocating None");
|
2001-01-23 12:24:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
static PyTypeObject PyNone_Type = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2001-08-16 05:17:26 -03:00
|
|
|
"NoneType",
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
0,
|
2006-03-30 07:57:00 -04:00
|
|
|
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*/
|
2006-03-30 07:57:00 -04:00
|
|
|
none_repr, /*tp_repr*/
|
1990-12-20 11:06:42 -04:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
2007-11-21 20:55:51 -04:00
|
|
|
(hashfunc)_Py_HashPointer, /*tp_hash */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject _Py_NoneStruct = {
|
2007-07-21 03:55:02 -03:00
|
|
|
_PyObject_EXTRA_INIT
|
|
|
|
1, &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)
|
|
|
|
{
|
2008-06-09 01:58:54 -03:00
|
|
|
return PyString_FromString("NotImplemented");
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyTypeObject PyNotImplemented_Type = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2001-08-16 05:17:26 -03:00
|
|
|
"NotImplementedType",
|
2001-01-03 21:48:10 -04:00
|
|
|
0,
|
|
|
|
0,
|
2006-03-30 07:57:00 -04:00
|
|
|
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*/
|
2006-03-30 07:57:00 -04:00
|
|
|
NotImplemented_repr, /*tp_repr*/
|
2001-01-03 21:48:10 -04:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject _Py_NotImplementedStruct = {
|
2007-07-21 03:55:02 -03:00
|
|
|
_PyObject_EXTRA_INIT
|
|
|
|
1, &PyNotImplemented_Type
|
2001-01-03 21:48:10 -04:00
|
|
|
};
|
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
void
|
|
|
|
_Py_ReadyTypes(void)
|
|
|
|
{
|
|
|
|
if (PyType_Ready(&PyType_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'type'");
|
|
|
|
|
2004-07-02 15:57:45 -03:00
|
|
|
if (PyType_Ready(&_PyWeakref_RefType) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'weakref'");
|
|
|
|
|
2002-04-03 18:41:51 -04:00
|
|
|
if (PyType_Ready(&PyBool_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'bool'");
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyType_Ready(&PyString_Type) < 0)
|
2002-05-24 16:01:59 -03:00
|
|
|
Py_FatalError("Can't initialize 'str'");
|
|
|
|
|
2008-05-26 09:29:14 -03:00
|
|
|
if (PyType_Ready(&PyByteArray_Type) < 0)
|
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/trunk-bytearray
........
r61750 | christian.heimes | 2008-03-22 20:47:44 +0100 (Sat, 22 Mar 2008) | 1 line
Copied files from py3k w/o modifications
........
r61752 | christian.heimes | 2008-03-22 20:53:20 +0100 (Sat, 22 Mar 2008) | 7 lines
Take One
* Added initialization code, warnings, flags etc. to the appropriate places
* Added new buffer interface to string type
* Modified tests
* Modified Makefile.pre.in to compile the new files
* Added bytesobject.c to Python.h
........
r61754 | christian.heimes | 2008-03-22 21:22:19 +0100 (Sat, 22 Mar 2008) | 2 lines
Disabled bytearray.extend for now since it causes an infinite recursion
Fixed serveral unit tests
........
r61756 | christian.heimes | 2008-03-22 21:43:38 +0100 (Sat, 22 Mar 2008) | 5 lines
Added PyBytes support to several places:
str + bytearray
ord(bytearray)
bytearray(str, encoding)
........
r61760 | christian.heimes | 2008-03-22 21:56:32 +0100 (Sat, 22 Mar 2008) | 1 line
Fixed more unit tests related to type('') is not unicode
........
r61763 | christian.heimes | 2008-03-22 22:20:28 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixed more unit tests
Fixed bytearray.extend
........
r61768 | christian.heimes | 2008-03-22 22:40:50 +0100 (Sat, 22 Mar 2008) | 1 line
Implemented old buffer interface for bytearray
........
r61772 | christian.heimes | 2008-03-22 23:24:52 +0100 (Sat, 22 Mar 2008) | 1 line
Added backport of the io module
........
r61775 | christian.heimes | 2008-03-23 03:50:49 +0100 (Sun, 23 Mar 2008) | 1 line
Fix str assignement to bytearray. Assignment of a str of size 1 is interpreted as a single byte
........
r61805 | christian.heimes | 2008-03-23 19:33:48 +0100 (Sun, 23 Mar 2008) | 3 lines
Fixed more tests
Fixed bytearray() comparsion with unicode()
Fixed iterator assignment of bytearray
........
r61809 | christian.heimes | 2008-03-23 21:02:21 +0100 (Sun, 23 Mar 2008) | 2 lines
str(bytesarray()) now returns the bytes and not the representation of the bytearray object
Enabled and fixed more unit tests
........
r61812 | christian.heimes | 2008-03-23 21:53:08 +0100 (Sun, 23 Mar 2008) | 3 lines
Clear error PyNumber_AsSsize_t() fails
Use CHARMASK for ob_svall access
disabled a test with memoryview again
........
r61819 | christian.heimes | 2008-03-23 23:05:57 +0100 (Sun, 23 Mar 2008) | 1 line
Untested updates to the PCBuild directory
........
r61917 | christian.heimes | 2008-03-26 00:57:06 +0100 (Wed, 26 Mar 2008) | 1 line
The type system of Python 2.6 has subtle differences to 3.0's. I've removed the Py_TPFLAGS_BASETYPE flags from bytearray for now. bytearray can't be subclasses until the issues with bytearray subclasses are fixed.
........
r61920 | christian.heimes | 2008-03-26 01:44:08 +0100 (Wed, 26 Mar 2008) | 2 lines
Disabled last failing test
I don't understand what the test is testing and how it suppose to work. Ka-Ping, please check it out.
........
r61930 | christian.heimes | 2008-03-26 12:46:18 +0100 (Wed, 26 Mar 2008) | 1 line
Re-enabled bytes warning code
........
r61933 | christian.heimes | 2008-03-26 13:20:46 +0100 (Wed, 26 Mar 2008) | 1 line
Fixed a bug in the new buffer protocol. The buffer slots weren't copied into a subclass.
........
r61934 | christian.heimes | 2008-03-26 13:25:09 +0100 (Wed, 26 Mar 2008) | 1 line
Re-enabled bytearray subclassing - all tests are passing.
........
2008-03-26 09:49:49 -03:00
|
|
|
Py_FatalError("Can't initialize 'bytes'");
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
object.h special-build macro minefield: renamed all the new lexical
helper macros to something saner, and used them appropriately in other
files too, to reduce #ifdef blocks.
classobject.c, instance_dealloc(): One of my worst Python Memories is
trying to fix this routine a few years ago when COUNT_ALLOCS was defined
but Py_TRACE_REFS wasn't. The special-build code here is way too
complicated. Now it's much simpler. Difference: in a Py_TRACE_REFS
build, the instance is no longer in the doubly-linked list of live
objects while its __del__ method is executing, and that may be visible
via sys.getobjects() called from a __del__ method. Tough -- the object
is presumed dead while its __del__ is executing anyway, and not calling
_Py_NewReference() at the start allows enormous code simplification.
typeobject.c, call_finalizer(): The special-build instance_dealloc()
pain apparently spread to here too via cut-'n-paste, and this is much
simpler now too. In addition, I didn't understand why this routine
was calling _PyObject_GC_TRACK() after a resurrection, since there's no
plausible way _PyObject_GC_UNTRACK() could have been called on the
object by this point. I suspect it was left over from pasting the
instance_delloc() code. Instead asserted that the object is still
tracked. Caution: I suspect we don't have a test that actually
exercises the subtype_dealloc() __del__-resurrected-me code.
2002-07-11 03:23:50 -03:00
|
|
|
_Py_INC_REFTOTAL;
|
1990-10-14 09:07:46 -03:00
|
|
|
op->ob_refcnt = 1;
|
2003-03-23 13:52:28 -04:00
|
|
|
_Py_AddToAllObjects(op, 1);
|
object.h special-build macro minefield: renamed all the new lexical
helper macros to something saner, and used them appropriately in other
files too, to reduce #ifdef blocks.
classobject.c, instance_dealloc(): One of my worst Python Memories is
trying to fix this routine a few years ago when COUNT_ALLOCS was defined
but Py_TRACE_REFS wasn't. The special-build code here is way too
complicated. Now it's much simpler. Difference: in a Py_TRACE_REFS
build, the instance is no longer in the doubly-linked list of live
objects while its __del__ method is executing, and that may be visible
via sys.getobjects() called from a __del__ method. Tough -- the object
is presumed dead while its __del__ is executing anyway, and not calling
_Py_NewReference() at the start allows enormous code simplification.
typeobject.c, call_finalizer(): The special-build instance_dealloc()
pain apparently spread to here too via cut-'n-paste, and this is much
simpler now too. In addition, I didn't understand why this routine
was calling _PyObject_GC_TRACK() after a resurrection, since there's no
plausible way _PyObject_GC_UNTRACK() could have been called on the
object by this point. I suspect it was left over from pasting the
instance_delloc() code. Instead asserted that the object is still
tracked. Caution: I suspect we don't have a test that actually
exercises the subtype_dealloc() __del__-resurrected-me code.
2002-07-11 03:23:50 -03:00
|
|
|
_Py_INC_TPALLOCS(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_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;
|
object.h special-build macro minefield: renamed all the new lexical
helper macros to something saner, and used them appropriately in other
files too, to reduce #ifdef blocks.
classobject.c, instance_dealloc(): One of my worst Python Memories is
trying to fix this routine a few years ago when COUNT_ALLOCS was defined
but Py_TRACE_REFS wasn't. The special-build code here is way too
complicated. Now it's much simpler. Difference: in a Py_TRACE_REFS
build, the instance is no longer in the doubly-linked list of live
objects while its __del__ method is executing, and that may be visible
via sys.getobjects() called from a __del__ method. Tough -- the object
is presumed dead while its __del__ is executing anyway, and not calling
_Py_NewReference() at the start allows enormous code simplification.
typeobject.c, call_finalizer(): The special-build instance_dealloc()
pain apparently spread to here too via cut-'n-paste, and this is much
simpler now too. In addition, I didn't understand why this routine
was calling _PyObject_GC_TRACK() after a resurrection, since there's no
plausible way _PyObject_GC_UNTRACK() could have been called on the
object by this point. I suspect it was left over from pasting the
instance_delloc() code. Instead asserted that the object is still
tracked. Caution: I suspect we don't have a test that actually
exercises the subtype_dealloc() __del__-resurrected-me code.
2002-07-11 03:23:50 -03:00
|
|
|
_Py_INC_TPFREES(op);
|
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
|
|
|
{
|
2007-12-18 22:37:44 -04:00
|
|
|
destructor dealloc = Py_TYPE(op)->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
|
|
|
}
|
|
|
|
|
2003-04-17 16:52:29 -03:00
|
|
|
/* Print all live objects. Because PyObject_Print is called, the
|
|
|
|
* interpreter must be in a healthy state.
|
|
|
|
*/
|
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) {
|
2006-04-11 16:12:33 -03:00
|
|
|
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-17 16:52:29 -03:00
|
|
|
/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
|
|
|
|
* doesn't make any calls to the Python C API, so is always safe to call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_Py_PrintReferenceAddresses(FILE *fp)
|
|
|
|
{
|
|
|
|
PyObject *op;
|
|
|
|
fprintf(fp, "Remaining object addresses:\n");
|
|
|
|
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
|
2006-04-11 16:12:33 -03:00
|
|
|
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
|
2007-12-18 22:37:44 -04:00
|
|
|
op->ob_refcnt, Py_TYPE(op)->tp_name);
|
2003-04-17 16:52:29 -03:00
|
|
|
}
|
|
|
|
|
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 ||
|
2007-12-18 22:37:44 -04:00
|
|
|
(t != NULL && Py_TYPE(op) != (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 */
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t (*_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;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i;
|
1998-04-10 19:32:46 -03:00
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
|
|
|
if (dict == NULL)
|
2003-04-15 12:12:39 -03:00
|
|
|
return 0;
|
1998-04-10 19:32:46 -03:00
|
|
|
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;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i;
|
1998-04-10 19:32:46 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* Trashcan support. */
|
2000-04-24 12:40:53 -03:00
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* Current call-stack depth of tp_dealloc calls. */
|
2000-03-13 12:01:29 -04:00
|
|
|
int _PyTrash_delete_nesting = 0;
|
2000-04-24 12:40:53 -03:00
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* List of objects that still need to be cleaned up, singly linked via their
|
|
|
|
* gc headers' gc_prev pointers.
|
|
|
|
*/
|
|
|
|
PyObject *_PyTrash_delete_later = NULL;
|
2000-03-13 12:01:29 -04:00
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* Add op to the _PyTrash_delete_later list. Called when the current
|
|
|
|
* call-stack depth gets large. op must be a currently untracked gc'ed
|
|
|
|
* object, with refcount 0. Py_DECREF must already have been called on it.
|
|
|
|
*/
|
2000-03-13 12:01:29 -04:00
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
_PyTrash_deposit_object(PyObject *op)
|
2000-03-13 12:01:29 -04:00
|
|
|
{
|
2002-07-07 02:13:56 -03:00
|
|
|
assert(PyObject_IS_GC(op));
|
|
|
|
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
|
|
|
|
assert(op->ob_refcnt == 0);
|
2002-03-28 23:05:54 -04:00
|
|
|
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
|
2000-04-24 12:40:53 -03:00
|
|
|
_PyTrash_delete_later = op;
|
2000-03-13 12:01:29 -04:00
|
|
|
}
|
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
|
|
|
|
* the call-stack unwinds again.
|
|
|
|
*/
|
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) {
|
2002-07-07 02:13:56 -03:00
|
|
|
PyObject *op = _PyTrash_delete_later;
|
2007-12-18 22:37:44 -04:00
|
|
|
destructor dealloc = Py_TYPE(op)->tp_dealloc;
|
2002-03-28 23:05:54 -04:00
|
|
|
|
|
|
|
_PyTrash_delete_later =
|
2002-07-07 02:13:56 -03:00
|
|
|
(PyObject*) _Py_AS_GC(op)->gc.gc_prev;
|
2000-04-24 12:40:53 -03:00
|
|
|
|
2002-07-07 02:13:56 -03:00
|
|
|
/* Call the deallocator directly. This used to try to
|
|
|
|
* fool Py_DECREF into calling it indirectly, but
|
|
|
|
* Py_DECREF was already called on this object, and in
|
|
|
|
* assorted non-release builds calling Py_DECREF again ends
|
|
|
|
* up distorting allocation statistics.
|
|
|
|
*/
|
|
|
|
assert(op->ob_refcnt == 0);
|
2000-03-13 12:01:29 -04:00
|
|
|
++_PyTrash_delete_nesting;
|
2002-07-07 02:13:56 -03:00
|
|
|
(*dealloc)(op);
|
2000-03-13 12:01:29 -04:00
|
|
|
--_PyTrash_delete_nesting;
|
|
|
|
}
|
|
|
|
}
|
2006-04-12 23:06:09 -03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|