1991-02-19 08:39:46 -04:00
|
|
|
|
2011-06-11 18:33:35 -03:00
|
|
|
/* Generic object operations; and implementation of None */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2009-04-19 23:09:13 -03:00
|
|
|
#include "frameobject.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2006-04-21 07:40:58 -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-21 07:40:58 -03:00
|
|
|
|
|
|
|
Py_ssize_t
|
|
|
|
_Py_GetRefTotal(void)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
#endif /* Py_REF_DEBUG */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
|
|
|
|
These are used by the individual routines for object creation.
|
|
|
|
Do not call them otherwise, they do not initialize the object! */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
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
|
2010-05-09 12:52:27 -03:00
|
|
|
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
|
2010-05-09 12:52:27 -03: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;
|
|
|
|
}
|
2003-03-23 13:52:28 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
#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-21 07:40:58 -03:00
|
|
|
/* All types are added to type_list, at least when
|
|
|
|
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. */
|
2009-01-11 13:13:55 -04:00
|
|
|
static int unlist_types_without_objects;
|
|
|
|
extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
|
|
|
|
extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
|
|
|
|
extern Py_ssize_t null_strings, one_strings;
|
1993-10-11 09:54:31 -03:00
|
|
|
void
|
2006-04-21 07:40:58 -03:00
|
|
|
dump_counts(FILE* f)
|
1993-10-11 09:54:31 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp;
|
|
|
|
|
|
|
|
for (tp = type_list; tp; tp = tp->tp_next)
|
|
|
|
fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
|
|
|
|
"freed: %" PY_FORMAT_SIZE_T "d, "
|
|
|
|
"max in use: %" PY_FORMAT_SIZE_T "d\n",
|
|
|
|
tp->tp_name, tp->tp_allocs, tp->tp_frees,
|
|
|
|
tp->tp_maxalloc);
|
|
|
|
fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
|
|
|
|
"empty: %" PY_FORMAT_SIZE_T "d\n",
|
|
|
|
fast_tuple_allocs, tuple_zero_allocs);
|
|
|
|
fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
|
|
|
|
"neg: %" PY_FORMAT_SIZE_T "d\n",
|
|
|
|
quick_int_allocs, quick_neg_int_allocs);
|
|
|
|
fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
|
|
|
|
"1-strings: %" PY_FORMAT_SIZE_T "d\n",
|
|
|
|
null_strings, one_strings);
|
1993-10-11 09:54:31 -03:00
|
|
|
}
|
|
|
|
|
1995-08-29 06:18:14 -03:00
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
get_counts(void)
|
1995-08-29 06:18:14 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -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) {
|
|
|
|
v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
|
|
|
|
tp->tp_frees, tp->tp_maxalloc);
|
|
|
|
if (v == NULL) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyList_Append(result, v) < 0) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
|
|
|
return result;
|
1995-08-29 06:18:14 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tp->tp_next == NULL && tp->tp_prev == NULL) {
|
|
|
|
/* first time; insert in linked list */
|
|
|
|
if (tp->tp_next != NULL) /* sanity check */
|
|
|
|
Py_FatalError("XXX inc_count sanity check");
|
|
|
|
if (type_list)
|
|
|
|
type_list->tp_prev = tp;
|
|
|
|
tp->tp_next = type_list;
|
|
|
|
/* 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);
|
|
|
|
type_list = tp;
|
2003-03-22 23:04:32 -04:00
|
|
|
#ifdef Py_TRACE_REFS
|
2010-05-09 12:52:27 -03: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
|
2010-05-09 12:52:27 -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-21 07:40:58 -03:00
|
|
|
|
|
|
|
void dec_count(PyTypeObject *tp)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
char buf[300];
|
2002-07-08 23:57:01 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
"%s:%i object at %p has negative ref count "
|
|
|
|
"%" PY_FORMAT_SIZE_T "d",
|
|
|
|
fname, lineno, op, op->ob_refcnt);
|
|
|
|
Py_FatalError(buf);
|
2002-07-08 23:57:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
|
|
|
|
Py_TYPE(op) = tp;
|
|
|
|
_Py_NewReference(op);
|
|
|
|
return op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op == NULL)
|
|
|
|
return (PyVarObject *) PyErr_NoMemory();
|
|
|
|
/* Any changes should be reflected in PyObject_INIT_VAR */
|
|
|
|
op->ob_size = size;
|
|
|
|
Py_TYPE(op) = tp;
|
|
|
|
_Py_NewReference((PyObject *)op);
|
|
|
|
return op;
|
2000-05-03 20:44:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
_PyObject_New(PyTypeObject *tp)
|
2000-05-03 20:44:39 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *op;
|
|
|
|
op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
|
|
|
|
if (op == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
return PyObject_INIT(op, tp);
|
2000-05-03 20:44:39 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject *op;
|
|
|
|
const size_t size = _PyObject_VAR_SIZE(tp, nitems);
|
|
|
|
op = (PyVarObject *) PyObject_MALLOC(size);
|
|
|
|
if (op == NULL)
|
|
|
|
return (PyVarObject *)PyErr_NoMemory();
|
|
|
|
return PyObject_INIT_VAR(op, tp, nitems);
|
2000-05-03 20:44:39 -03:00
|
|
|
}
|
|
|
|
|
2010-07-27 19:08:27 -03:00
|
|
|
int
|
|
|
|
PyObject_Print(PyObject *op, FILE *fp, int flags)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int ret = 0;
|
|
|
|
if (PyErr_CheckSignals())
|
|
|
|
return -1;
|
1998-04-28 13:06:54 -03:00
|
|
|
#ifdef USE_STACKCHECK
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyOS_CheckStack()) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "stack overflow");
|
|
|
|
return -1;
|
|
|
|
}
|
1998-04-28 13:06:54 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
clearerr(fp); /* Clear any previous error condition */
|
|
|
|
if (op == NULL) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fprintf(fp, "<nil>");
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (op->ob_refcnt <= 0)
|
|
|
|
/* XXX(twouters) cast refcount to long until %zd is
|
|
|
|
universally available */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fprintf(fp, "<refcnt %ld at %p>",
|
|
|
|
(long)op->ob_refcnt, op);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
else {
|
|
|
|
PyObject *s;
|
|
|
|
if (flags & Py_PRINT_RAW)
|
|
|
|
s = PyObject_Str(op);
|
|
|
|
else
|
|
|
|
s = PyObject_Repr(op);
|
|
|
|
if (s == NULL)
|
|
|
|
ret = -1;
|
|
|
|
else if (PyBytes_Check(s)) {
|
|
|
|
fwrite(PyBytes_AS_STRING(s), 1,
|
|
|
|
PyBytes_GET_SIZE(s), fp);
|
|
|
|
}
|
|
|
|
else if (PyUnicode_Check(s)) {
|
|
|
|
PyObject *t;
|
2011-09-28 02:41:54 -03:00
|
|
|
t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
|
2010-05-09 12:52:27 -03:00
|
|
|
if (t == NULL)
|
|
|
|
ret = 0;
|
|
|
|
else {
|
|
|
|
fwrite(PyBytes_AS_STRING(t), 1,
|
|
|
|
PyBytes_GET_SIZE(t), fp);
|
2010-05-17 06:33:42 -03:00
|
|
|
Py_DECREF(t);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"str() or repr() returned '%.100s'",
|
|
|
|
s->ob_type->tp_name);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
if (ferror(fp)) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(fp);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2006-08-21 20:36:26 -03:00
|
|
|
/* For debugging convenience. Set a breakpoint here and call it from your DLL */
|
|
|
|
void
|
Merged revisions 53451-53537 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r53454 | brett.cannon | 2007-01-15 20:12:08 +0100 (Mon, 15 Jan 2007) | 3 lines
Add a note for strptime that just because strftime supports some extra
directive that is not documented that strptime will as well.
........
r53458 | vinay.sajip | 2007-01-16 10:50:07 +0100 (Tue, 16 Jan 2007) | 1 line
Updated rotating file handlers to use _open().
........
r53459 | marc-andre.lemburg | 2007-01-16 14:03:06 +0100 (Tue, 16 Jan 2007) | 2 lines
Add news items for the recent pybench and platform changes.
........
r53460 | sjoerd.mullender | 2007-01-16 17:42:38 +0100 (Tue, 16 Jan 2007) | 4 lines
Fixed ntpath.expandvars to not replace references to non-existing
variables with nothing. Also added tests.
This fixes bug #494589.
........
r53464 | neal.norwitz | 2007-01-17 07:23:51 +0100 (Wed, 17 Jan 2007) | 1 line
Give Calvin Spealman access for python-dev summaries.
........
r53465 | neal.norwitz | 2007-01-17 09:37:26 +0100 (Wed, 17 Jan 2007) | 1 line
Remove Calvin since he only has access to the website currently.
........
r53466 | thomas.heller | 2007-01-17 10:40:34 +0100 (Wed, 17 Jan 2007) | 2 lines
Replace C++ comments with C comments.
........
r53472 | andrew.kuchling | 2007-01-17 20:55:06 +0100 (Wed, 17 Jan 2007) | 1 line
[Part of bug #1599254] Add suggestion to Mailbox docs to use Maildir, and warn user to lock/unlock mailboxes when modifying them
........
r53475 | georg.brandl | 2007-01-17 22:09:04 +0100 (Wed, 17 Jan 2007) | 2 lines
Bug #1637967: missing //= operator in list.
........
r53477 | georg.brandl | 2007-01-17 22:19:58 +0100 (Wed, 17 Jan 2007) | 2 lines
Bug #1629125: fix wrong data type (int -> Py_ssize_t) in PyDict_Next docs.
........
r53481 | neal.norwitz | 2007-01-18 06:40:58 +0100 (Thu, 18 Jan 2007) | 1 line
Try reverting part of r53145 that seems to cause the Windows buildbots to fail in test_uu.UUFileTest.test_encode
........
r53482 | fred.drake | 2007-01-18 06:42:30 +0100 (Thu, 18 Jan 2007) | 1 line
add missing version entry
........
r53483 | neal.norwitz | 2007-01-18 07:20:55 +0100 (Thu, 18 Jan 2007) | 7 lines
This test doesn't pass on Windows. The cause seems to be that chmod
doesn't support the same funcationality as on Unix. I'm not sure if
this fix is the best (or if it will even work)--it's a test to see
if the buildbots start passing again.
It might be better to not even run this test if it's windows (or non-posix).
........
r53488 | neal.norwitz | 2007-01-19 06:53:33 +0100 (Fri, 19 Jan 2007) | 1 line
SF #1635217, Fix unbalanced paren
........
r53489 | martin.v.loewis | 2007-01-19 07:42:22 +0100 (Fri, 19 Jan 2007) | 3 lines
Prefix AST symbols with _Py_. Fixes #1637022.
Will backport.
........
r53497 | martin.v.loewis | 2007-01-19 19:01:38 +0100 (Fri, 19 Jan 2007) | 2 lines
Add UUIDs for 2.5.1 and 2.5.2
........
r53499 | raymond.hettinger | 2007-01-19 19:07:18 +0100 (Fri, 19 Jan 2007) | 1 line
SF# 1635892: Fix docs for betavariate's input parameters .
........
r53503 | martin.v.loewis | 2007-01-20 15:05:39 +0100 (Sat, 20 Jan 2007) | 2 lines
Merge 53501 and 53502 from 25 branch:
Add /GS- for AMD64 and Itanium builds where missing.
........
r53504 | walter.doerwald | 2007-01-20 18:28:31 +0100 (Sat, 20 Jan 2007) | 2 lines
Port test_resource.py to unittest.
........
r53505 | walter.doerwald | 2007-01-20 19:19:33 +0100 (Sat, 20 Jan 2007) | 2 lines
Add argument tests an calls of resource.getrusage().
........
r53506 | walter.doerwald | 2007-01-20 20:03:17 +0100 (Sat, 20 Jan 2007) | 2 lines
resource.RUSAGE_BOTH might not exist.
........
r53507 | walter.doerwald | 2007-01-21 00:07:28 +0100 (Sun, 21 Jan 2007) | 2 lines
Port test_new.py to unittest.
........
r53508 | martin.v.loewis | 2007-01-21 10:33:07 +0100 (Sun, 21 Jan 2007) | 2 lines
Patch #1610575: Add support for _Bool to struct.
........
r53509 | georg.brandl | 2007-01-21 11:28:43 +0100 (Sun, 21 Jan 2007) | 3 lines
Bug #1486663: don't reject keyword arguments for subclasses of builtin
types.
........
r53511 | georg.brandl | 2007-01-21 11:35:10 +0100 (Sun, 21 Jan 2007) | 2 lines
Patch #1627441: close sockets properly in urllib2.
........
r53517 | georg.brandl | 2007-01-22 20:40:21 +0100 (Mon, 22 Jan 2007) | 3 lines
Use new email module names (#1637162, #1637159, #1637157).
........
r53518 | andrew.kuchling | 2007-01-22 21:26:40 +0100 (Mon, 22 Jan 2007) | 1 line
Improve pattern used for mbox 'From' lines; add a simple test
........
r53519 | andrew.kuchling | 2007-01-22 21:27:50 +0100 (Mon, 22 Jan 2007) | 1 line
Make comment match the code
........
r53522 | georg.brandl | 2007-01-22 22:10:33 +0100 (Mon, 22 Jan 2007) | 2 lines
Bug #1249573: fix rfc822.parsedate not accepting a certain date format
........
r53524 | georg.brandl | 2007-01-22 22:23:41 +0100 (Mon, 22 Jan 2007) | 2 lines
Bug #1627316: handle error in condition/ignore pdb commands more gracefully.
........
r53526 | lars.gustaebel | 2007-01-23 12:17:33 +0100 (Tue, 23 Jan 2007) | 4 lines
Patch #1507247: tarfile.py: use current umask for intermediate
directories.
........
r53527 | thomas.wouters | 2007-01-23 14:42:00 +0100 (Tue, 23 Jan 2007) | 13 lines
SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize
When running the interpreter in an environment that would cause it to set
stdout/stderr/stdin's encoding, having a sitecustomize that would replace
them with something other than PyFile objects would crash the interpreter.
Fix it by simply ignoring the encoding-setting for non-files.
This could do with a test, but I can think of no maintainable and portable
way to test this bug, short of adding a sitecustomize.py to the buildsystem
and have it always run with it (hmmm....)
........
r53528 | thomas.wouters | 2007-01-23 14:50:49 +0100 (Tue, 23 Jan 2007) | 4 lines
Add news entry about last checkin (oops.)
........
r53531 | martin.v.loewis | 2007-01-23 22:11:47 +0100 (Tue, 23 Jan 2007) | 4 lines
Make PyTraceBack_Here use the current thread, not the
frame's thread state. Fixes #1579370.
Will backport.
........
r53535 | brett.cannon | 2007-01-24 00:21:22 +0100 (Wed, 24 Jan 2007) | 5 lines
Fix crasher for when an object's __del__ creates a new weakref to itself.
Patch only fixes new-style classes; classic classes still buggy.
Closes bug #1377858. Already backported.
........
r53536 | walter.doerwald | 2007-01-24 01:42:19 +0100 (Wed, 24 Jan 2007) | 2 lines
Port test_popen.py to unittest.
........
2007-02-01 14:02:27 -04:00
|
|
|
_Py_BreakPoint(void)
|
2006-08-21 20:36:26 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-01-13 16:13:12 -04:00
|
|
|
|
2001-01-23 12:24:35 -04:00
|
|
|
/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
|
2006-08-21 20:36:26 -03:00
|
|
|
void
|
|
|
|
_PyObject_Dump(PyObject* op)
|
2001-01-23 12:24:35 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op == NULL)
|
|
|
|
fprintf(stderr, "NULL\n");
|
|
|
|
else {
|
2009-04-05 08:47:34 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 12:52:27 -03:00
|
|
|
PyGILState_STATE gil;
|
2009-04-05 08:47:34 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
fprintf(stderr, "object : ");
|
2009-04-05 08:47:34 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 12:52:27 -03:00
|
|
|
gil = PyGILState_Ensure();
|
2009-04-05 08:47:34 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
(void)PyObject_Print(op, stderr, 0);
|
2009-04-05 08:47:34 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 12:52:27 -03:00
|
|
|
PyGILState_Release(gil);
|
2009-04-05 08:47:34 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
/* XXX(twouters) cast refcount to long until %zd is
|
|
|
|
universally available */
|
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"type : %s\n"
|
|
|
|
"refcount: %ld\n"
|
|
|
|
"address : %p\n",
|
|
|
|
Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
|
|
|
|
(long)op->ob_refcnt,
|
|
|
|
op);
|
|
|
|
}
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res;
|
|
|
|
if (PyErr_CheckSignals())
|
|
|
|
return NULL;
|
1998-04-28 13:06:54 -03:00
|
|
|
#ifdef USE_STACKCHECK
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyOS_CheckStack()) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "stack overflow");
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-04-28 13:06:54 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return PyUnicode_FromString("<NULL>");
|
|
|
|
if (Py_TYPE(v)->tp_repr == NULL)
|
|
|
|
return PyUnicode_FromFormat("<%s object at %p>",
|
|
|
|
v->ob_type->tp_name, v);
|
|
|
|
res = (*v->ob_type->tp_repr)(v);
|
2011-11-30 22:22:44 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!PyUnicode_Check(res)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__repr__ returned non-string (type %.200s)",
|
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-30 21:15:00 -04:00
|
|
|
#ifndef Py_DEBUG
|
|
|
|
if (PyUnicode_READY(res) < 0)
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
return res;
|
2007-05-18 14:15:44 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2007-11-06 17:34:58 -04:00
|
|
|
PyObject_Str(PyObject *v)
|
1993-11-05 06:22:19 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res;
|
|
|
|
if (PyErr_CheckSignals())
|
|
|
|
return NULL;
|
2007-11-06 17:34:58 -04:00
|
|
|
#ifdef USE_STACKCHECK
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyOS_CheckStack()) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError, "stack overflow");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-06 17:34:58 -04:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return PyUnicode_FromString("<NULL>");
|
|
|
|
if (PyUnicode_CheckExact(v)) {
|
2011-11-30 21:15:00 -04:00
|
|
|
#ifndef Py_DEBUG
|
2011-11-20 14:48:36 -04:00
|
|
|
if (PyUnicode_READY(v) < 0)
|
|
|
|
return NULL;
|
2011-11-30 21:15:00 -04:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
if (Py_TYPE(v)->tp_str == NULL)
|
|
|
|
return PyObject_Repr(v);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
res = (*Py_TYPE(v)->tp_str)(v);
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!PyUnicode_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__str__ returned non-string (type %.200s)",
|
|
|
|
Py_TYPE(res)->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-30 21:15:00 -04:00
|
|
|
#ifndef Py_DEBUG
|
2011-11-20 14:48:36 -04:00
|
|
|
if (PyUnicode_READY(res) < 0)
|
|
|
|
return NULL;
|
2011-11-30 21:15:00 -04:00
|
|
|
#endif
|
2011-11-20 14:48:36 -04:00
|
|
|
assert(_PyUnicode_CheckConsistency(res, 1));
|
2010-05-09 12:52:27 -03:00
|
|
|
return res;
|
2005-08-12 14:34:58 -03:00
|
|
|
}
|
|
|
|
|
2008-06-11 15:37:52 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_ASCII(PyObject *v)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *repr, *ascii, *res;
|
|
|
|
|
|
|
|
repr = PyObject_Repr(v);
|
|
|
|
if (repr == NULL)
|
|
|
|
return NULL;
|
2008-06-11 15:37:52 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
|
2011-09-28 02:41:54 -03:00
|
|
|
ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(repr);
|
|
|
|
if (ascii == NULL)
|
|
|
|
return NULL;
|
2008-06-11 15:37:52 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
res = PyUnicode_DecodeASCII(
|
|
|
|
PyBytes_AS_STRING(ascii),
|
|
|
|
PyBytes_GET_SIZE(ascii),
|
|
|
|
NULL);
|
2008-06-11 15:37:52 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(ascii);
|
|
|
|
return res;
|
2008-06-11 15:37:52 -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
|
|
|
|
2008-08-26 13:46:47 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_Bytes(PyObject *v)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *result, *func;
|
2012-01-22 12:24:29 -04:00
|
|
|
_Py_IDENTIFIER(__bytes__);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (v == NULL)
|
|
|
|
return PyBytes_FromString("<NULL>");
|
|
|
|
|
|
|
|
if (PyBytes_CheckExact(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-01-22 12:24:29 -04:00
|
|
|
func = _PyObject_LookupSpecial(v, &PyId___bytes__);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (func != NULL) {
|
|
|
|
result = PyObject_CallFunctionObjArgs(func, NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
if (result == NULL)
|
2010-09-11 13:39:57 -03:00
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyBytes_Check(result)) {
|
2010-09-11 13:39:57 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__bytes__ returned non-bytes (type %.200s)",
|
|
|
|
Py_TYPE(result)->tp_name);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
return PyBytes_FromObject(v);
|
2008-08-26 13:46:47 -03:00
|
|
|
}
|
|
|
|
|
2009-02-01 09:59:22 -04:00
|
|
|
/* For Python 3.0.1 and later, the old three-way comparison has been
|
|
|
|
completely removed in favour of rich comparisons. PyObject_Compare() and
|
|
|
|
PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
|
2009-02-02 16:36:42 -04:00
|
|
|
The old tp_compare slot has been renamed to tp_reserved, and should no
|
2009-02-01 09:59:22 -04:00
|
|
|
longer be used. Use tp_richcompare instead.
|
2007-11-06 17:34:58 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
See (*) below for practical amendments.
|
2002-05-31 17:03:54 -03:00
|
|
|
|
2009-02-01 09:59:22 -04:00
|
|
|
tp_richcompare gets called with a first argument of the appropriate type
|
|
|
|
and a second object of an arbitrary type. We never do any kind of
|
|
|
|
coercion.
|
2002-05-31 17:03:54 -03:00
|
|
|
|
2009-02-01 09:59:22 -04:00
|
|
|
The tp_richcompare slot should return an object, as follows:
|
2001-01-17 17:27:02 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
NULL if an exception occurred
|
|
|
|
NotImplemented if the requested comparison is not implemented
|
|
|
|
any other false value if the requested comparison is false
|
|
|
|
any other true value if the requested comparison is true
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
|
|
|
|
NotImplemented.
|
2001-01-03 21:48:10 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
(*) Practical amendments:
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
- If rich comparison returns NotImplemented, == and != are decided by
|
|
|
|
comparing the object pointer (i.e. falling back to the base object
|
|
|
|
implementation).
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
*/
|
2001-01-22 15:28:09 -04:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
|
|
|
|
int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
|
2001-06-09 04:34:05 -03:00
|
|
|
|
2006-12-19 17:35:46 -04:00
|
|
|
static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
/* Perform a rich comparison, raising TypeError when the requested comparison
|
|
|
|
operator is not supported. */
|
2001-01-21 12:25:18 -04:00
|
|
|
static PyObject *
|
2006-08-23 21:41:19 -03:00
|
|
|
do_richcompare(PyObject *v, PyObject *w, int op)
|
2001-01-17 11:24:28 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
richcmpfunc f;
|
|
|
|
PyObject *res;
|
|
|
|
int checked_reverse_op = 0;
|
|
|
|
|
|
|
|
if (v->ob_type != w->ob_type &&
|
|
|
|
PyType_IsSubtype(w->ob_type, v->ob_type) &&
|
|
|
|
(f = w->ob_type->tp_richcompare) != NULL) {
|
|
|
|
checked_reverse_op = 1;
|
|
|
|
res = (*f)(w, v, _Py_SwappedOp[op]);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
if ((f = v->ob_type->tp_richcompare) != NULL) {
|
|
|
|
res = (*f)(v, w, op);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
|
|
|
|
res = (*f)(w, v, _Py_SwappedOp[op]);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
/* If neither object implements it, provide a sensible default
|
|
|
|
for == and !=, but raise an exception for ordering. */
|
|
|
|
switch (op) {
|
|
|
|
case Py_EQ:
|
|
|
|
res = (v == w) ? Py_True : Py_False;
|
|
|
|
break;
|
|
|
|
case Py_NE:
|
|
|
|
res = (v != w) ? Py_True : Py_False;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX Special-case None so it doesn't show as NoneType() */
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"unorderable types: %.100s() %s %.100s()",
|
|
|
|
v->ob_type->tp_name,
|
|
|
|
opstrings[op],
|
|
|
|
w->ob_type->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
/* Perform a rich comparison with object result. This wraps do_richcompare()
|
|
|
|
with a check for NULL arguments and a recursion check. */
|
|
|
|
|
2001-01-17 11:24:28 -04:00
|
|
|
PyObject *
|
|
|
|
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res;
|
2001-01-17 11:24:28 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(Py_LT <= op && op <= Py_GE);
|
|
|
|
if (v == NULL || w == NULL) {
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (Py_EnterRecursiveCall(" in comparison"))
|
|
|
|
return NULL;
|
|
|
|
res = do_richcompare(v, w, op);
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
return res;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
|
|
|
|
2006-08-23 21:41:19 -03:00
|
|
|
/* Perform a rich comparison with integer result. This wraps
|
|
|
|
PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
|
2001-01-17 11:24:28 -04:00
|
|
|
int
|
|
|
|
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyBool_Check(res))
|
|
|
|
ok = (res == Py_True);
|
|
|
|
else
|
|
|
|
ok = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return ok;
|
2001-01-17 11:24:28 -04:00
|
|
|
}
|
2000-06-29 16:17:04 -03:00
|
|
|
|
|
|
|
/* Set of hash utility functions to help maintaining the invariant that
|
2010-05-09 12:52:27 -03: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.
|
|
|
|
*/
|
|
|
|
|
2010-05-23 10:33:13 -03:00
|
|
|
/* For numeric types, the hash of a number x is based on the reduction
|
|
|
|
of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that
|
|
|
|
hash(x) == hash(y) whenever x and y are numerically equal, even if
|
|
|
|
x and y have different types.
|
|
|
|
|
|
|
|
A quick summary of the hashing strategy:
|
|
|
|
|
|
|
|
(1) First define the 'reduction of x modulo P' for any rational
|
|
|
|
number x; this is a standard extension of the usual notion of
|
|
|
|
reduction modulo P for integers. If x == p/q (written in lowest
|
|
|
|
terms), the reduction is interpreted as the reduction of p times
|
|
|
|
the inverse of the reduction of q, all modulo P; if q is exactly
|
|
|
|
divisible by P then define the reduction to be infinity. So we've
|
|
|
|
got a well-defined map
|
|
|
|
|
|
|
|
reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }.
|
|
|
|
|
|
|
|
(2) Now for a rational number x, define hash(x) by:
|
|
|
|
|
|
|
|
reduce(x) if x >= 0
|
|
|
|
-reduce(-x) if x < 0
|
|
|
|
|
|
|
|
If the result of the reduction is infinity (this is impossible for
|
|
|
|
integers, floats and Decimals) then use the predefined hash value
|
|
|
|
_PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead.
|
|
|
|
_PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the
|
|
|
|
hashes of float and Decimal infinities and nans.
|
|
|
|
|
|
|
|
A selling point for the above strategy is that it makes it possible
|
|
|
|
to compute hashes of decimal and binary floating-point numbers
|
|
|
|
efficiently, even if the exponent of the binary or decimal number
|
|
|
|
is large. The key point is that
|
|
|
|
|
|
|
|
reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS)
|
|
|
|
|
|
|
|
provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a
|
|
|
|
binary or decimal float is never infinity, since the denominator is a power
|
|
|
|
of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have,
|
|
|
|
for nonnegative x,
|
|
|
|
|
|
|
|
reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS
|
|
|
|
|
|
|
|
reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS
|
|
|
|
|
|
|
|
and reduce(10**e) can be computed efficiently by the usual modular
|
|
|
|
exponentiation algorithm. For reduce(2**e) it's even better: since
|
|
|
|
P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication
|
|
|
|
by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_HashDouble(double v)
|
2000-06-29 16:17:04 -03:00
|
|
|
{
|
2010-05-23 10:33:13 -03:00
|
|
|
int e, sign;
|
|
|
|
double m;
|
2010-10-23 13:20:50 -03:00
|
|
|
Py_uhash_t x, y;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (!Py_IS_FINITE(v)) {
|
|
|
|
if (Py_IS_INFINITY(v))
|
2010-05-23 10:33:13 -03:00
|
|
|
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2010-05-23 10:33:13 -03:00
|
|
|
return _PyHASH_NAN;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-05-23 10:33:13 -03:00
|
|
|
|
|
|
|
m = frexp(v, &e);
|
|
|
|
|
|
|
|
sign = 1;
|
|
|
|
if (m < 0) {
|
|
|
|
sign = -1;
|
|
|
|
m = -m;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-05-23 10:33:13 -03:00
|
|
|
|
|
|
|
/* process 28 bits at a time; this should work well both for binary
|
|
|
|
and hexadecimal floating point. */
|
|
|
|
x = 0;
|
|
|
|
while (m) {
|
|
|
|
x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
|
|
|
|
m *= 268435456.0; /* 2**28 */
|
|
|
|
e -= 28;
|
2010-10-23 13:20:50 -03:00
|
|
|
y = (Py_uhash_t)m; /* pull out integer part */
|
2010-05-23 10:33:13 -03:00
|
|
|
m -= y;
|
|
|
|
x += y;
|
|
|
|
if (x >= _PyHASH_MODULUS)
|
|
|
|
x -= _PyHASH_MODULUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
|
|
|
|
e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
|
|
|
|
x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
|
|
|
|
|
|
|
|
x = x * sign;
|
2010-10-23 13:20:50 -03:00
|
|
|
if (x == (Py_uhash_t)-1)
|
|
|
|
x = (Py_uhash_t)-2;
|
2010-10-17 17:54:53 -03:00
|
|
|
return (Py_hash_t)x;
|
2000-06-29 16:17:04 -03:00
|
|
|
}
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t
|
2000-07-09 12:48:49 -03:00
|
|
|
_Py_HashPointer(void *p)
|
2000-06-29 16:17:04 -03:00
|
|
|
{
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t x;
|
2010-05-09 12:52:27 -03:00
|
|
|
size_t y = (size_t)p;
|
|
|
|
/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
|
|
|
|
excessive hash collisions for dicts and sets */
|
|
|
|
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
|
2010-10-17 17:54:53 -03:00
|
|
|
x = (Py_hash_t)y;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
2000-06-29 16:17:04 -03:00
|
|
|
}
|
|
|
|
|
2011-11-21 15:46:33 -04:00
|
|
|
Py_hash_t
|
|
|
|
_Py_HashBytes(unsigned char *p, Py_ssize_t len)
|
|
|
|
{
|
|
|
|
Py_uhash_t x;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
2012-02-20 19:33:36 -04:00
|
|
|
/*
|
|
|
|
We make the hash of the empty string be 0, rather than using
|
|
|
|
(prefix ^ suffix), since this slightly obfuscates the hash secret
|
|
|
|
*/
|
2012-04-09 16:04:39 -03:00
|
|
|
#ifdef Py_DEBUG
|
2012-02-21 12:12:14 -04:00
|
|
|
assert(_Py_HashSecret_Initialized);
|
2012-04-09 16:04:39 -03:00
|
|
|
#endif
|
2012-02-20 19:33:36 -04:00
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x = (Py_uhash_t) _Py_HashSecret.prefix;
|
|
|
|
x ^= (Py_uhash_t) *p << 7;
|
2011-11-21 15:46:33 -04:00
|
|
|
for (i = 0; i < len; i++)
|
2012-01-14 19:45:13 -04:00
|
|
|
x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
|
2011-11-21 15:46:33 -04:00
|
|
|
x ^= (Py_uhash_t) len;
|
2012-02-20 19:33:36 -04:00
|
|
|
x ^= (Py_uhash_t) _Py_HashSecret.suffix;
|
2011-11-21 15:46:33 -04:00
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t
|
2008-07-15 12:46:38 -03:00
|
|
|
PyObject_HashNotImplemented(PyObject *v)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
|
|
|
|
Py_TYPE(v)->tp_name);
|
|
|
|
return -1;
|
2008-07-15 12:46:38 -03:00
|
|
|
}
|
2000-06-29 16:17:04 -03:00
|
|
|
|
2012-02-20 14:54:16 -04:00
|
|
|
_Py_HashSecret_t _Py_HashSecret;
|
|
|
|
|
2010-10-17 17:54:53 -03:00
|
|
|
Py_hash_t
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_Hash(PyObject *v)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(v);
|
|
|
|
if (tp->tp_hash != NULL)
|
|
|
|
return (*tp->tp_hash)(v);
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
/* Otherwise, the object can't be hashed */
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *w, *res;
|
1996-08-09 17:52:03 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (Py_TYPE(v)->tp_getattr != NULL)
|
|
|
|
return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
|
|
|
|
w = PyUnicode_InternFromString(name);
|
|
|
|
if (w == NULL)
|
|
|
|
return NULL;
|
|
|
|
res = PyObject_GetAttr(v, w);
|
2012-03-21 22:09:08 -03:00
|
|
|
Py_DECREF(w);
|
2010-05-09 12:52:27 -03:00
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res = PyObject_GetAttrString(v, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
1993-07-11 16:55:34 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *s;
|
|
|
|
int res;
|
1996-08-09 17:52:03 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (Py_TYPE(v)->tp_setattr != NULL)
|
|
|
|
return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
|
|
|
|
s = PyUnicode_InternFromString(name);
|
|
|
|
if (s == NULL)
|
|
|
|
return -1;
|
|
|
|
res = PyObject_SetAttr(v, s, w);
|
|
|
|
Py_XDECREF(s);
|
|
|
|
return res;
|
1993-05-12 05:24:20 -03:00
|
|
|
}
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
int
|
|
|
|
_PyObject_IsAbstract(PyObject *obj)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
PyObject* isabstract;
|
|
|
|
_Py_IDENTIFIER(__isabstractmethod__);
|
|
|
|
|
|
|
|
if (obj == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
|
|
|
|
if (isabstract == NULL) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = PyObject_IsTrue(isabstract);
|
|
|
|
Py_DECREF(isabstract);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-10-09 05:38:36 -03:00
|
|
|
PyObject *
|
|
|
|
_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
|
|
|
|
{
|
|
|
|
PyObject *result;
|
2011-11-07 08:00:05 -04:00
|
|
|
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
|
2011-10-09 05:38:36 -03:00
|
|
|
if (!oname)
|
|
|
|
return NULL;
|
|
|
|
result = PyObject_GetAttr(v, oname);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
|
|
|
|
{
|
|
|
|
int result;
|
2011-11-07 08:00:05 -04:00
|
|
|
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
|
2011-10-09 05:38:36 -03:00
|
|
|
if (!oname)
|
|
|
|
return -1;
|
|
|
|
result = PyObject_HasAttr(v, oname);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
|
|
|
|
{
|
|
|
|
int result;
|
2011-11-07 08:00:05 -04:00
|
|
|
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
|
2011-10-09 05:38:36 -03:00
|
|
|
if (!oname)
|
|
|
|
return -1;
|
|
|
|
result = PyObject_SetAttr(v, oname, w);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(v);
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(name)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
|
|
|
name->ob_type->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (tp->tp_getattro != NULL)
|
|
|
|
return (*tp->tp_getattro)(v, name);
|
|
|
|
if (tp->tp_getattr != NULL) {
|
|
|
|
char *name_str = _PyUnicode_AsString(name);
|
|
|
|
if (name_str == NULL)
|
|
|
|
return NULL;
|
|
|
|
return (*tp->tp_getattr)(v, name_str);
|
|
|
|
}
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%U'",
|
|
|
|
tp->tp_name, name);
|
|
|
|
return NULL;
|
1997-05-20 15:34:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 12:48:49 -03:00
|
|
|
PyObject_HasAttr(PyObject *v, PyObject *name)
|
1997-05-20 15:34:44 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *res = PyObject_GetAttr(v, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
1997-05-20 15:34:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(v);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(name)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
|
|
|
name->ob_type->tp_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(name);
|
|
|
|
|
|
|
|
PyUnicode_InternInPlace(&name);
|
|
|
|
if (tp->tp_setattro != NULL) {
|
|
|
|
err = (*tp->tp_setattro)(v, name, value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (tp->tp_setattr != NULL) {
|
|
|
|
char *name_str = _PyUnicode_AsString(name);
|
|
|
|
if (name_str == NULL)
|
|
|
|
return -1;
|
|
|
|
err = (*tp->tp_setattr)(v, name_str, value);
|
|
|
|
Py_DECREF(name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
Py_DECREF(name);
|
|
|
|
assert(name->ob_refcnt >= 1);
|
|
|
|
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.100s' object has no attributes "
|
|
|
|
"(%s .%U)",
|
|
|
|
tp->tp_name,
|
|
|
|
value==NULL ? "del" : "assign to",
|
|
|
|
name);
|
|
|
|
else
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.100s' object has only read-only attributes "
|
|
|
|
"(%s .%U)",
|
|
|
|
tp->tp_name,
|
|
|
|
value==NULL ? "del" : "assign to",
|
|
|
|
name);
|
|
|
|
return -1;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to get a pointer to an object's __dict__ slot, if any */
|
|
|
|
|
|
|
|
PyObject **
|
|
|
|
_PyObject_GetDictPtr(PyObject *obj)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t dictoffset;
|
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
dictoffset = tp->tp_dictoffset;
|
|
|
|
if (dictoffset == 0)
|
|
|
|
return NULL;
|
|
|
|
if (dictoffset < 0) {
|
|
|
|
Py_ssize_t tsize;
|
|
|
|
size_t size;
|
2002-03-01 18:24:49 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
tsize = ((PyVarObject *)obj)->ob_size;
|
|
|
|
if (tsize < 0)
|
|
|
|
tsize = -tsize;
|
|
|
|
size = _PyObject_VAR_SIZE(tp, tsize);
|
2002-03-01 18:24:49 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
dictoffset += (long)size;
|
|
|
|
assert(dictoffset > 0);
|
|
|
|
assert(dictoffset % SIZEOF_VOID_P == 0);
|
|
|
|
}
|
|
|
|
return (PyObject **) ((char *)obj + dictoffset);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(obj);
|
|
|
|
return obj;
|
2003-03-17 04:24:35 -04:00
|
|
|
}
|
|
|
|
|
2012-04-04 19:04:20 -03:00
|
|
|
/* Convenience function to get a builtin from its name */
|
|
|
|
PyObject *
|
|
|
|
_PyObject_GetBuiltin(const char *name)
|
|
|
|
{
|
|
|
|
PyObject *mod, *attr;
|
|
|
|
mod = PyImport_ImportModule("builtins");
|
|
|
|
if (mod == NULL)
|
|
|
|
return NULL;
|
|
|
|
attr = PyObject_GetAttrString(mod, name);
|
|
|
|
Py_DECREF(mod);
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
2009-01-12 19:58:21 -04:00
|
|
|
/* Helper used when the __next__ method is removed from a type:
|
|
|
|
tp_iternext is never NULL and can be safely called without checking
|
|
|
|
on every iteration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_NextNotImplemented(PyObject *self)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"'%.200s' object is not iterable",
|
|
|
|
Py_TYPE(self)->tp_name);
|
|
|
|
return NULL;
|
2009-01-12 19:58:21 -04:00
|
|
|
}
|
|
|
|
|
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 *
|
2010-08-28 15:17:03 -03:00
|
|
|
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
|
2001-08-02 01:15:00 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
|
|
|
PyObject *descr = NULL;
|
|
|
|
PyObject *res = NULL;
|
|
|
|
descrgetfunc f;
|
|
|
|
Py_ssize_t dictoffset;
|
|
|
|
PyObject **dictptr;
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(name)){
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
|
|
|
name->ob_type->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Py_INCREF(name);
|
|
|
|
|
|
|
|
if (tp->tp_dict == NULL) {
|
|
|
|
if (PyType_Ready(tp) < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
descr = _PyType_Lookup(tp, name);
|
|
|
|
Py_XINCREF(descr);
|
|
|
|
|
|
|
|
f = NULL;
|
|
|
|
if (descr != NULL) {
|
|
|
|
f = descr->ob_type->tp_descr_get;
|
|
|
|
if (f != NULL && PyDescr_IsData(descr)) {
|
|
|
|
res = f(descr, obj, (PyObject *)obj->ob_type);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 15:17:03 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
/* Inline _PyObject_GetDictPtr */
|
|
|
|
dictoffset = tp->tp_dictoffset;
|
|
|
|
if (dictoffset != 0) {
|
|
|
|
if (dictoffset < 0) {
|
|
|
|
Py_ssize_t tsize;
|
|
|
|
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);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-28 15:17:03 -03:00
|
|
|
dictptr = (PyObject **) ((char *)obj + dictoffset);
|
|
|
|
dict = *dictptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dict != NULL) {
|
|
|
|
Py_INCREF(dict);
|
|
|
|
res = PyDict_GetItem(dict, name);
|
|
|
|
if (res != NULL) {
|
|
|
|
Py_INCREF(res);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(dict);
|
2010-08-28 15:17:03 -03:00
|
|
|
goto done;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-28 15:17:03 -03:00
|
|
|
Py_DECREF(dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f != NULL) {
|
|
|
|
res = f(descr, obj, (PyObject *)Py_TYPE(obj));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (descr != NULL) {
|
|
|
|
res = descr;
|
2012-03-08 19:44:13 -04:00
|
|
|
descr = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object has no attribute '%U'",
|
|
|
|
tp->tp_name, name);
|
2001-12-04 11:54:53 -04:00
|
|
|
done:
|
2012-03-08 19:44:13 -04:00
|
|
|
Py_XDECREF(descr);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(name);
|
|
|
|
return res;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2010-08-28 15:17:03 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|
|
|
{
|
|
|
|
return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
|
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
int
|
2010-08-28 15:17:03 -03:00
|
|
|
_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
|
|
|
|
PyObject *value, PyObject *dict)
|
2001-08-02 01:15:00 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(obj);
|
|
|
|
PyObject *descr;
|
|
|
|
descrsetfunc f;
|
|
|
|
PyObject **dictptr;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(name)){
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute name must be string, not '%.200s'",
|
|
|
|
name->ob_type->tp_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 11:25:32 -04:00
|
|
|
if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Py_INCREF(name);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
2012-03-08 19:44:13 -04:00
|
|
|
Py_XINCREF(descr);
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
f = NULL;
|
|
|
|
if (descr != NULL) {
|
|
|
|
f = descr->ob_type->tp_descr_set;
|
|
|
|
if (f != NULL && PyDescr_IsData(descr)) {
|
|
|
|
res = f(descr, obj, value);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 15:17:03 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
dictptr = _PyObject_GetDictPtr(obj);
|
|
|
|
if (dictptr != NULL) {
|
2012-04-23 12:24:50 -03:00
|
|
|
res = _PyObjectDict_SetItem(Py_TYPE(obj), dictptr, name, value);
|
|
|
|
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
|
|
|
|
PyErr_SetObject(PyExc_AttributeError, name);
|
|
|
|
goto done;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
2010-08-28 15:17:03 -03:00
|
|
|
if (dict != NULL) {
|
|
|
|
Py_INCREF(dict);
|
|
|
|
if (value == NULL)
|
|
|
|
res = PyDict_DelItem(dict, name);
|
|
|
|
else
|
|
|
|
res = PyDict_SetItem(dict, name, value);
|
2012-03-09 11:25:32 -04:00
|
|
|
Py_DECREF(dict);
|
2010-08-28 15:17:03 -03:00
|
|
|
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
|
|
|
|
PyErr_SetObject(PyExc_AttributeError, name);
|
|
|
|
goto done;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (f != NULL) {
|
|
|
|
res = f(descr, obj, value);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (descr == NULL) {
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.100s' object has no attribute '%U'",
|
|
|
|
tp->tp_name, name);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.50s' object attribute '%U' is read-only",
|
|
|
|
tp->tp_name, name);
|
2001-12-04 11:54:53 -04:00
|
|
|
done:
|
2012-03-08 19:44:13 -04:00
|
|
|
Py_XDECREF(descr);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(name);
|
|
|
|
return res;
|
1997-05-20 15:34:44 -03:00
|
|
|
}
|
|
|
|
|
2010-08-28 15:17:03 -03:00
|
|
|
int
|
|
|
|
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
|
|
|
{
|
|
|
|
return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
|
|
|
|
}
|
|
|
|
|
2012-02-19 20:59:10 -04:00
|
|
|
int
|
|
|
|
PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
|
|
|
|
{
|
|
|
|
PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
|
|
|
|
if (dictptr == NULL) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
"This object has no __dict__");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__dict__ must be set to a dictionary, "
|
|
|
|
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dict = *dictptr;
|
|
|
|
Py_XINCREF(value);
|
|
|
|
*dictptr = value;
|
|
|
|
Py_XDECREF(dict);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-28 15:17:03 -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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t res;
|
|
|
|
if (v == Py_True)
|
|
|
|
return 1;
|
|
|
|
if (v == Py_False)
|
|
|
|
return 0;
|
|
|
|
if (v == Py_None)
|
|
|
|
return 0;
|
|
|
|
else if (v->ob_type->tp_as_number != NULL &&
|
|
|
|
v->ob_type->tp_as_number->nb_bool != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_number->nb_bool)(v);
|
|
|
|
else if (v->ob_type->tp_as_mapping != NULL &&
|
|
|
|
v->ob_type->tp_as_mapping->mp_length != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_mapping->mp_length)(v);
|
|
|
|
else if (v->ob_type->tp_as_sequence != NULL &&
|
|
|
|
v->ob_type->tp_as_sequence->sq_length != NULL)
|
|
|
|
res = (*v->ob_type->tp_as_sequence->sq_length)(v);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
/* 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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int res;
|
|
|
|
res = PyObject_IsTrue(v);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
return res == 0;
|
1998-04-09 14:53:59 -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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (x == NULL)
|
|
|
|
return 0;
|
|
|
|
return x->ob_type->tp_call != NULL;
|
1995-01-25 20:38:22 -04:00
|
|
|
}
|
|
|
|
|
2001-09-04 19:08:56 -03:00
|
|
|
|
2007-03-10 18:13:27 -04:00
|
|
|
/* Helper for PyObject_Dir without arguments: returns the local scope. */
|
|
|
|
static PyObject *
|
2007-04-12 22:39:34 -03:00
|
|
|
_dir_locals(void)
|
2007-03-10 18:13:27 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *names;
|
|
|
|
PyObject *locals = PyEval_GetLocals();
|
2001-09-16 23:38:46 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (locals == NULL) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "frame does not exist");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-10 18:13:27 -04:00
|
|
|
|
2010-05-09 12:52:27 -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, "
|
|
|
|
"not '%.200s'", Py_TYPE(names)->tp_name);
|
|
|
|
Py_DECREF(names);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-11 18:12:08 -03:00
|
|
|
if (PyList_Sort(names)) {
|
|
|
|
Py_DECREF(names);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
/* the locals don't need to be DECREF'd */
|
|
|
|
return names;
|
2007-03-10 18:13:27 -04:00
|
|
|
}
|
|
|
|
|
2011-05-24 13:09:06 -03:00
|
|
|
/* Helper for PyObject_Dir: object introspection. */
|
2007-03-10 18:13:27 -04:00
|
|
|
static PyObject *
|
|
|
|
_dir_object(PyObject *obj)
|
|
|
|
{
|
2011-06-11 18:12:08 -03:00
|
|
|
PyObject *result, *sorted;
|
2012-01-22 12:24:29 -04:00
|
|
|
_Py_IDENTIFIER(__dir__);
|
|
|
|
PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
assert(obj);
|
|
|
|
if (dirfunc == NULL) {
|
2011-05-24 13:09:06 -03:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-05-24 13:09:06 -03:00
|
|
|
/* use __dir__ */
|
|
|
|
result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
|
|
|
|
Py_DECREF(dirfunc);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
2011-06-11 18:12:08 -03:00
|
|
|
/* return sorted(result) */
|
|
|
|
sorted = PySequence_List(result);
|
|
|
|
Py_DECREF(result);
|
|
|
|
if (sorted == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (PyList_Sort(sorted)) {
|
|
|
|
Py_DECREF(sorted);
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-06-11 18:12:08 -03:00
|
|
|
return sorted;
|
2007-03-10 18:13:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2011-06-11 18:12:08 -03:00
|
|
|
return (obj == NULL) ? _dir_locals() : _dir_object(obj);
|
2001-09-04 19:08:56 -03:00
|
|
|
}
|
1995-01-25 20:38:22 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/*
|
2011-07-28 13:55:13 -03:00
|
|
|
None is a non-NULL undefined value.
|
1990-10-14 09:07:46 -03:00
|
|
|
There is (and should be!) no way to create other objects of this type,
|
1990-12-20 11:06:42 -04:00
|
|
|
so there is exactly one (which is indestructible, by the way).
|
1990-10-14 09:07:46 -03:00
|
|
|
*/
|
|
|
|
|
1992-03-27 13:26:13 -04:00
|
|
|
/* ARGSUSED */
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 12:48:49 -03:00
|
|
|
none_repr(PyObject *op)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyUnicode_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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* This should never get called, but we also don't want to SEGV if
|
|
|
|
* we accidentally decref None out of existence.
|
|
|
|
*/
|
|
|
|
Py_FatalError("deallocating None");
|
2001-01-23 12:24:35 -04:00
|
|
|
}
|
|
|
|
|
2011-07-29 20:19:43 -03:00
|
|
|
static PyObject *
|
|
|
|
none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2011-07-28 13:55:13 -03:00
|
|
|
static int
|
|
|
|
none_bool(PyObject *v)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyNumberMethods none_as_number = {
|
|
|
|
0, /* nb_add */
|
|
|
|
0, /* nb_subtract */
|
|
|
|
0, /* nb_multiply */
|
|
|
|
0, /* nb_remainder */
|
|
|
|
0, /* nb_divmod */
|
|
|
|
0, /* nb_power */
|
|
|
|
0, /* nb_negative */
|
|
|
|
0, /* nb_positive */
|
|
|
|
0, /* nb_absolute */
|
|
|
|
(inquiry)none_bool, /* nb_bool */
|
|
|
|
0, /* nb_invert */
|
|
|
|
0, /* nb_lshift */
|
|
|
|
0, /* nb_rshift */
|
|
|
|
0, /* nb_and */
|
|
|
|
0, /* nb_xor */
|
|
|
|
0, /* nb_or */
|
|
|
|
0, /* nb_int */
|
|
|
|
0, /* nb_reserved */
|
|
|
|
0, /* nb_float */
|
|
|
|
0, /* nb_inplace_add */
|
|
|
|
0, /* nb_inplace_subtract */
|
|
|
|
0, /* nb_inplace_multiply */
|
|
|
|
0, /* nb_inplace_remainder */
|
|
|
|
0, /* nb_inplace_power */
|
|
|
|
0, /* nb_inplace_lshift */
|
|
|
|
0, /* nb_inplace_rshift */
|
|
|
|
0, /* nb_inplace_and */
|
|
|
|
0, /* nb_inplace_xor */
|
|
|
|
0, /* nb_inplace_or */
|
|
|
|
0, /* nb_floor_divide */
|
|
|
|
0, /* nb_true_divide */
|
|
|
|
0, /* nb_inplace_floor_divide */
|
|
|
|
0, /* nb_inplace_true_divide */
|
|
|
|
0, /* nb_index */
|
|
|
|
};
|
2001-01-23 12:24:35 -04:00
|
|
|
|
2001-08-16 05:17:26 -03:00
|
|
|
static PyTypeObject PyNone_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"NoneType",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
none_dealloc, /*tp_dealloc*/ /*never called*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_reserved*/
|
|
|
|
none_repr, /*tp_repr*/
|
2011-07-28 13:55:13 -03:00
|
|
|
&none_as_number, /*tp_as_number*/
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash */
|
2011-07-29 20:19:43 -03:00
|
|
|
0, /*tp_call */
|
|
|
|
0, /*tp_str */
|
|
|
|
0, /*tp_getattro */
|
|
|
|
0, /*tp_setattro */
|
|
|
|
0, /*tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags */
|
|
|
|
0, /*tp_doc */
|
|
|
|
0, /*tp_traverse */
|
|
|
|
0, /*tp_clear */
|
|
|
|
0, /*tp_richcompare */
|
|
|
|
0, /*tp_weaklistoffset */
|
|
|
|
0, /*tp_iter */
|
|
|
|
0, /*tp_iternext */
|
|
|
|
0, /*tp_methods */
|
|
|
|
0, /*tp_members */
|
|
|
|
0, /*tp_getset */
|
|
|
|
0, /*tp_base */
|
|
|
|
0, /*tp_dict */
|
|
|
|
0, /*tp_descr_get */
|
|
|
|
0, /*tp_descr_set */
|
|
|
|
0, /*tp_dictoffset */
|
|
|
|
0, /*tp_init */
|
|
|
|
0, /*tp_alloc */
|
|
|
|
none_new, /*tp_new */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject _Py_NoneStruct = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -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)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyUnicode_FromString("NotImplemented");
|
2001-01-03 21:48:10 -04:00
|
|
|
}
|
|
|
|
|
2011-07-29 20:27:44 -03:00
|
|
|
static PyObject *
|
|
|
|
notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-10 22:28:54 -03:00
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2011-07-29 20:27:44 -03:00
|
|
|
}
|
|
|
|
|
2001-01-03 21:48:10 -04:00
|
|
|
static PyTypeObject PyNotImplemented_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"NotImplementedType",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
none_dealloc, /*tp_dealloc*/ /*never called*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_reserved*/
|
|
|
|
NotImplemented_repr, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash */
|
2011-07-29 20:27:44 -03:00
|
|
|
0, /*tp_call */
|
|
|
|
0, /*tp_str */
|
|
|
|
0, /*tp_getattro */
|
|
|
|
0, /*tp_setattro */
|
|
|
|
0, /*tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags */
|
|
|
|
0, /*tp_doc */
|
|
|
|
0, /*tp_traverse */
|
|
|
|
0, /*tp_clear */
|
|
|
|
0, /*tp_richcompare */
|
|
|
|
0, /*tp_weaklistoffset */
|
|
|
|
0, /*tp_iter */
|
|
|
|
0, /*tp_iternext */
|
|
|
|
0, /*tp_methods */
|
|
|
|
0, /*tp_members */
|
|
|
|
0, /*tp_getset */
|
|
|
|
0, /*tp_base */
|
|
|
|
0, /*tp_dict */
|
|
|
|
0, /*tp_descr_get */
|
|
|
|
0, /*tp_descr_set */
|
|
|
|
0, /*tp_dictoffset */
|
|
|
|
0, /*tp_init */
|
|
|
|
0, /*tp_alloc */
|
|
|
|
notimplemented_new, /*tp_new */
|
2001-01-03 21:48:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject _Py_NotImplementedStruct = {
|
2010-05-09 12:52:27 -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)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyType_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize type type");
|
2001-08-16 05:17:26 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&_PyWeakref_RefType) < 0)
|
|
|
|
Py_FatalError("Can't initialize weakref type");
|
2004-07-02 15:57:45 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
|
|
|
|
Py_FatalError("Can't initialize callable weakref proxy type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
|
|
|
|
Py_FatalError("Can't initialize weakref proxy type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyBool_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize bool type");
|
2002-04-03 18:41:51 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyByteArray_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize bytearray type");
|
2006-04-22 20:28:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyBytes_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize 'str'");
|
2002-05-24 16:01:59 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyList_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize list type");
|
2001-08-16 05:17:26 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyNone_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize None type");
|
2001-08-16 05:17:26 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyNotImplemented_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize NotImplemented type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyTraceBack_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize traceback type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PySuper_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize super type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyBaseObject_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize object type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyRange_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize range type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyDict_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize dict type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PySet_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize set type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyUnicode_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize str type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PySlice_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize slice type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyStaticMethod_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize static method type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyComplex_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize complex type");
|
2009-10-18 11:25:35 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyFloat_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize float type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyLong_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize int type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyFrozenSet_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize frozenset type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyProperty_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize property type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize managed buffer type");
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyMemoryView_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize memoryview type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyTuple_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize tuple type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyEnum_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize enumerate type");
|
2009-04-18 17:54:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyReversed_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize reversed type");
|
2006-08-23 21:41:19 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyStdPrinter_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize StdPrinter");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyCode_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize code type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyFrame_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize frame type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyCFunction_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize builtin function type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyMethod_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize method type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyFunction_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize function type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyDictProxy_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize dict proxy type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyGen_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize generator type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyGetSetDescr_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize get-set descriptor type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyWrapperDescr_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize wrapper type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2011-09-01 17:32:31 -03:00
|
|
|
if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize method wrapper type");
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyEllipsis_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize ellipsis type");
|
2009-04-19 23:09:13 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyMemberDescr_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize member descriptor type");
|
2009-05-09 15:10:51 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyFilter_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize filter type");
|
2009-05-09 15:10:51 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyMap_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize map type");
|
2009-05-09 15:10:51 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyType_Ready(&PyZip_Type) < 0)
|
|
|
|
Py_FatalError("Can't initialize zip type");
|
2001-08-16 05:17:26 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_Py_INC_REFTOTAL;
|
|
|
|
op->ob_refcnt = 1;
|
|
|
|
_Py_AddToAllObjects(op, 1);
|
|
|
|
_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
|
2010-05-09 12:52:27 -03:00
|
|
|
register PyObject *p;
|
2000-01-20 18:32:56 -04:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->ob_refcnt < 0)
|
|
|
|
Py_FatalError("UNREF negative refcnt");
|
|
|
|
if (op == &refchain ||
|
|
|
|
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
|
|
|
|
fprintf(stderr, "* ob\n");
|
|
|
|
_PyObject_Dump(op);
|
|
|
|
fprintf(stderr, "* op->_ob_prev->_ob_next\n");
|
|
|
|
_PyObject_Dump(op->_ob_prev->_ob_next);
|
|
|
|
fprintf(stderr, "* op->_ob_next->_ob_prev\n");
|
|
|
|
_PyObject_Dump(op->_ob_next->_ob_prev);
|
|
|
|
Py_FatalError("UNREF invalid object");
|
|
|
|
}
|
1992-09-03 17:32:55 -03:00
|
|
|
#ifdef SLOW_UNREF_CHECK
|
2010-05-09 12:52:27 -03:00
|
|
|
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
|
|
|
|
if (p == op)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p == &refchain) /* Not found */
|
|
|
|
Py_FatalError("UNREF unknown object");
|
1992-09-03 17:32:55 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
op->_ob_next->_ob_prev = op->_ob_prev;
|
|
|
|
op->_ob_prev->_ob_next = op->_ob_next;
|
|
|
|
op->_ob_next = op->_ob_prev = NULL;
|
|
|
|
_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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
destructor dealloc = Py_TYPE(op)->tp_dealloc;
|
|
|
|
_Py_ForgetReference(op);
|
|
|
|
(*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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *op;
|
|
|
|
fprintf(fp, "Remaining objects:\n");
|
|
|
|
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
|
|
|
|
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
|
|
|
|
if (PyObject_Print(op, fp, 0) != 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
putc('\n', fp);
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *op;
|
|
|
|
fprintf(fp, "Remaining object addresses:\n");
|
|
|
|
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
|
|
|
|
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
|
|
|
|
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
|
|
|
{
|
2010-05-09 12:52:27 -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 ||
|
|
|
|
(t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
|
|
|
|
op = op->_ob_next;
|
|
|
|
if (op == &refchain)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (PyList_Append(res, op) < 0) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
op = op->_ob_next;
|
|
|
|
}
|
|
|
|
return res;
|
1995-08-29 06:18:14 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
#endif
|
1996-01-11 21:24:09 -04:00
|
|
|
|
2009-05-05 19:31:58 -03:00
|
|
|
/* Hack to force loading of pycapsule.o */
|
|
|
|
PyTypeObject *_PyCapsule_hack = &PyCapsule_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
|
|
|
{
|
2010-05-09 12:52:27 -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
|
|
|
{
|
2010-05-09 12:52:27 -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
|
|
|
{
|
2010-05-09 12:52:27 -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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *dict;
|
|
|
|
PyObject *list;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
|
|
|
if (dict == NULL)
|
|
|
|
return 0;
|
|
|
|
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;
|
1998-04-10 19:32:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-09 12:48:49 -03:00
|
|
|
Py_ReprLeave(PyObject *obj)
|
1998-04-10 19:32:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *dict;
|
|
|
|
PyObject *list;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
dict = PyThreadState_GetDict();
|
|
|
|
if (dict == NULL)
|
|
|
|
return;
|
|
|
|
list = PyDict_GetItemString(dict, KEY);
|
|
|
|
if (list == NULL || !PyList_Check(list))
|
|
|
|
return;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
1998-04-10 19:32:46 -03:00
|
|
|
}
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(PyObject_IS_GC(op));
|
|
|
|
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
|
|
|
|
assert(op->ob_refcnt == 0);
|
|
|
|
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
|
|
|
|
_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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
while (_PyTrash_delete_later) {
|
|
|
|
PyObject *op = _PyTrash_delete_later;
|
|
|
|
destructor dealloc = Py_TYPE(op)->tp_dealloc;
|
|
|
|
|
|
|
|
_PyTrash_delete_later =
|
|
|
|
(PyObject*) _Py_AS_GC(op)->gc.gc_prev;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
++_PyTrash_delete_nesting;
|
|
|
|
(*dealloc)(op);
|
|
|
|
--_PyTrash_delete_nesting;
|
|
|
|
}
|
2000-03-13 12:01:29 -04:00
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
#ifndef Py_TRACE_REFS
|
|
|
|
/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
|
|
|
|
Define this here, so we can undefine the macro. */
|
|
|
|
#undef _Py_Dealloc
|
|
|
|
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
|
|
|
|
void
|
|
|
|
_Py_Dealloc(PyObject *op)
|
|
|
|
{
|
|
|
|
_Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
|
|
|
|
(*Py_TYPE(op)->tp_dealloc)(op);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|