1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Class object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
1992-08-12 12:35:34 -03:00
|
|
|
|
2001-10-17 17:26:38 -03:00
|
|
|
#define TP_DESCR_GET(t) \
|
|
|
|
(PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL)
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
|
1994-09-05 04:32:29 -03:00
|
|
|
/* Forward */
|
2000-07-09 00:09:57 -03:00
|
|
|
static PyObject *class_lookup(PyClassObject *, PyObject *,
|
|
|
|
PyClassObject **);
|
|
|
|
static PyObject *instance_getattr1(PyInstanceObject *, PyObject *);
|
|
|
|
static PyObject *instance_getattr2(PyInstanceObject *, PyObject *);
|
1997-05-02 00:12:38 -03:00
|
|
|
|
1998-05-29 18:37:21 -03:00
|
|
|
static PyObject *getattrstr, *setattrstr, *delattrstr;
|
|
|
|
|
2000-07-09 01:06:11 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
|
|
|
|
/* bases is NULL or tuple of classobjects! */
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *op, *dummy;
|
1997-09-12 17:04:46 -03:00
|
|
|
static PyObject *docstr, *modstr, *namestr;
|
1996-08-21 11:54:28 -03:00
|
|
|
if (docstr == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
docstr= PyString_InternFromString("__doc__");
|
1996-08-21 11:54:28 -03:00
|
|
|
if (docstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-09-12 17:04:46 -03:00
|
|
|
if (modstr == NULL) {
|
|
|
|
modstr= PyString_InternFromString("__module__");
|
|
|
|
if (modstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (namestr == NULL) {
|
|
|
|
namestr= PyString_InternFromString("__name__");
|
|
|
|
if (namestr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-10-07 11:54:11 -03:00
|
|
|
if (name == NULL || !PyString_Check(name)) {
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-10-07 11:54:11 -03:00
|
|
|
"PyClass_New: name must be a string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (dict == NULL || !PyDict_Check(dict)) {
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-10-07 11:54:11 -03:00
|
|
|
"PyClass_New: dict must be a dictionary");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyDict_GetItem(dict, docstr) == NULL) {
|
|
|
|
if (PyDict_SetItem(dict, docstr, Py_None) < 0)
|
1995-01-07 08:35:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-09-12 17:04:46 -03:00
|
|
|
if (PyDict_GetItem(dict, modstr) == NULL) {
|
|
|
|
PyObject *globals = PyEval_GetGlobals();
|
|
|
|
if (globals != NULL) {
|
1997-10-07 11:54:11 -03:00
|
|
|
PyObject *modname = PyDict_GetItem(globals, namestr);
|
|
|
|
if (modname != NULL) {
|
|
|
|
if (PyDict_SetItem(dict, modstr, modname) < 0)
|
1997-09-12 17:04:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1991-12-10 09:53:23 -04:00
|
|
|
if (bases == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
bases = PyTuple_New(0);
|
1991-12-10 09:53:23 -04:00
|
|
|
if (bases == NULL)
|
1993-03-16 08:15:04 -04:00
|
|
|
return NULL;
|
1991-12-10 09:53:23 -04:00
|
|
|
}
|
1997-10-07 11:54:11 -03:00
|
|
|
else {
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, n;
|
2001-09-07 18:08:32 -03:00
|
|
|
PyObject *base;
|
1997-10-07 11:54:11 -03:00
|
|
|
if (!PyTuple_Check(bases)) {
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-10-07 11:54:11 -03:00
|
|
|
"PyClass_New: bases must be a tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-07 18:08:32 -03:00
|
|
|
n = PyTuple_Size(bases);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
base = PyTuple_GET_ITEM(bases, i);
|
|
|
|
if (!PyClass_Check(base)) {
|
|
|
|
if (PyCallable_Check(
|
|
|
|
(PyObject *) base->ob_type))
|
2006-05-25 16:15:31 -03:00
|
|
|
return PyObject_CallFunctionObjArgs(
|
2001-09-07 18:08:32 -03:00
|
|
|
(PyObject *) base->ob_type,
|
2006-05-25 16:15:31 -03:00
|
|
|
name, bases, dict, NULL);
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-10-07 11:54:11 -03:00
|
|
|
"PyClass_New: base must be a class");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(bases);
|
1997-10-07 11:54:11 -03:00
|
|
|
}
|
2001-08-29 20:54:21 -03:00
|
|
|
op = PyObject_GC_New(PyClassObject, &PyClass_Type);
|
1991-12-10 09:53:23 -04:00
|
|
|
if (op == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(bases);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1991-12-10 09:53:23 -04:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
op->cl_bases = bases;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(dict);
|
1993-05-20 11:24:46 -03:00
|
|
|
op->cl_dict = dict;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(name);
|
1991-10-20 17:11:48 -03:00
|
|
|
op->cl_name = name;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getattrstr == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
getattrstr = PyString_InternFromString("__getattr__");
|
|
|
|
setattrstr = PyString_InternFromString("__setattr__");
|
|
|
|
delattrstr = PyString_InternFromString("__delattr__");
|
1996-08-09 17:53:24 -03:00
|
|
|
}
|
|
|
|
op->cl_getattr = class_lookup(op, getattrstr, &dummy);
|
|
|
|
op->cl_setattr = class_lookup(op, setattrstr, &dummy);
|
|
|
|
op->cl_delattr = class_lookup(op, delattrstr, &dummy);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(op->cl_getattr);
|
|
|
|
Py_XINCREF(op->cl_setattr);
|
|
|
|
Py_XINCREF(op->cl_delattr);
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_TRACK(op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2001-09-05 19:52:50 -03:00
|
|
|
PyObject *
|
|
|
|
PyMethod_Function(PyObject *im)
|
|
|
|
{
|
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyMethodObject *)im)->im_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyMethod_Self(PyObject *im)
|
|
|
|
{
|
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyMethodObject *)im)->im_self;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyMethod_Class(PyObject *im)
|
|
|
|
{
|
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyMethodObject *)im)->im_class;
|
|
|
|
}
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
PyDoc_STRVAR(class_doc,
|
|
|
|
"classobj(name, bases, dict)\n\
|
|
|
|
\n\
|
|
|
|
Create a class object. The name must be a string; the second argument\n\
|
|
|
|
a tuple of classes, and the third a dictionary.");
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyObject *name, *bases, *dict;
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"name", "bases", "dict", 0};
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
|
|
|
|
&name, &bases, &dict))
|
|
|
|
return NULL;
|
|
|
|
return PyClass_New(bases, dict, name);
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Class methods */
|
|
|
|
|
|
|
|
static void
|
2000-07-09 01:06:11 -03:00
|
|
|
class_dealloc(PyClassObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_UNTRACK(op);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(op->cl_bases);
|
|
|
|
Py_DECREF(op->cl_dict);
|
|
|
|
Py_XDECREF(op->cl_name);
|
1998-08-04 11:59:16 -03:00
|
|
|
Py_XDECREF(op->cl_getattr);
|
|
|
|
Py_XDECREF(op->cl_setattr);
|
|
|
|
Py_XDECREF(op->cl_delattr);
|
2001-08-29 20:54:21 -03:00
|
|
|
PyObject_GC_Del(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass)
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, n;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *value = PyDict_GetItem(cp->cl_dict, name);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (value != NULL) {
|
|
|
|
*pclass = cp;
|
|
|
|
return value;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
n = PyTuple_Size(cp->cl_bases);
|
1993-05-20 11:24:46 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-09-12 17:04:46 -03:00
|
|
|
/* XXX What if one of the bases is not a class? */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = class_lookup(
|
|
|
|
(PyClassObject *)
|
|
|
|
PyTuple_GetItem(cp->cl_bases, i), name, pclass);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (v != NULL)
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
class_getattr(register PyClassObject *op, PyObject *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *v;
|
|
|
|
register char *sname = PyString_AsString(name);
|
2006-04-11 03:54:30 -03:00
|
|
|
PyClassObject *klass;
|
2001-08-02 01:15:00 -03:00
|
|
|
descrgetfunc f;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"class.__dict__ not accessible in restricted mode");
|
1995-01-10 06:39:49 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(op->cl_dict);
|
1995-01-10 06:39:49 -04:00
|
|
|
return op->cl_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__bases__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(op->cl_bases);
|
1995-01-10 06:39:49 -04:00
|
|
|
return op->cl_bases;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__name__") == 0) {
|
1995-01-10 06:39:49 -04:00
|
|
|
if (op->cl_name == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
v = Py_None;
|
1995-01-10 06:39:49 -04:00
|
|
|
else
|
|
|
|
v = op->cl_name;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1995-01-10 06:39:49 -04:00
|
|
|
return v;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
2006-04-11 03:54:30 -03:00
|
|
|
v = class_lookup(op, name, &klass);
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL) {
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"class %.50s has no attribute '%.400s'",
|
|
|
|
PyString_AS_STRING(op->cl_name), sname);
|
1993-05-21 16:56:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-17 17:26:38 -03:00
|
|
|
f = TP_DESCR_GET(v->ob_type);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (f == NULL)
|
|
|
|
Py_INCREF(v);
|
|
|
|
else
|
|
|
|
v = f(v, (PyObject *)NULL, (PyObject *)op);
|
1993-05-21 16:56:10 -03:00
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1998-05-29 18:37:21 -03:00
|
|
|
static void
|
2000-07-09 01:06:11 -03:00
|
|
|
set_slot(PyObject **slot, PyObject *v)
|
1998-05-29 18:37:21 -03:00
|
|
|
{
|
|
|
|
PyObject *temp = *slot;
|
|
|
|
Py_XINCREF(v);
|
|
|
|
*slot = v;
|
|
|
|
Py_XDECREF(temp);
|
|
|
|
}
|
|
|
|
|
1998-07-08 10:34:48 -03:00
|
|
|
static void
|
2000-07-09 01:06:11 -03:00
|
|
|
set_attr_slots(PyClassObject *c)
|
1998-05-29 18:37:21 -03:00
|
|
|
{
|
|
|
|
PyClassObject *dummy;
|
|
|
|
|
|
|
|
set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy));
|
|
|
|
set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy));
|
|
|
|
set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy));
|
1998-07-08 10:34:48 -03:00
|
|
|
}
|
1998-05-29 18:37:21 -03:00
|
|
|
|
1998-07-08 10:34:48 -03:00
|
|
|
static char *
|
2000-07-09 01:06:11 -03:00
|
|
|
set_dict(PyClassObject *c, PyObject *v)
|
1998-07-08 10:34:48 -03:00
|
|
|
{
|
|
|
|
if (v == NULL || !PyDict_Check(v))
|
|
|
|
return "__dict__ must be a dictionary object";
|
|
|
|
set_slot(&c->cl_dict, v);
|
|
|
|
set_attr_slots(c);
|
1998-05-29 18:37:21 -03:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2000-07-09 01:06:11 -03:00
|
|
|
set_bases(PyClassObject *c, PyObject *v)
|
1998-05-29 18:37:21 -03:00
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, n;
|
1998-05-29 18:37:21 -03:00
|
|
|
|
|
|
|
if (v == NULL || !PyTuple_Check(v))
|
|
|
|
return "__bases__ must be a tuple object";
|
|
|
|
n = PyTuple_Size(v);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
PyObject *x = PyTuple_GET_ITEM(v, i);
|
|
|
|
if (!PyClass_Check(x))
|
|
|
|
return "__bases__ items must be classes";
|
|
|
|
if (PyClass_IsSubclass(x, (PyObject *)c))
|
|
|
|
return "a __bases__ item causes an inheritance cycle";
|
|
|
|
}
|
|
|
|
set_slot(&c->cl_bases, v);
|
1998-07-08 10:34:48 -03:00
|
|
|
set_attr_slots(c);
|
1998-05-29 18:37:21 -03:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2000-07-09 01:06:11 -03:00
|
|
|
set_name(PyClassObject *c, PyObject *v)
|
1998-05-29 18:37:21 -03:00
|
|
|
{
|
|
|
|
if (v == NULL || !PyString_Check(v))
|
|
|
|
return "__name__ must be a string object";
|
2000-06-28 18:57:18 -03:00
|
|
|
if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v))
|
1998-05-29 18:37:21 -03:00
|
|
|
return "__name__ must not contain null bytes";
|
|
|
|
set_slot(&c->cl_name, v);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
1991-10-20 17:11:48 -03:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
class_setattr(PyClassObject *op, PyObject *name, PyObject *v)
|
1991-10-20 17:11:48 -03:00
|
|
|
{
|
1997-08-25 18:23:56 -03:00
|
|
|
char *sname;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1995-08-04 01:05:31 -03:00
|
|
|
"classes are read-only in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-08-25 18:23:56 -03:00
|
|
|
sname = PyString_AsString(name);
|
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
2006-04-22 08:40:03 -03:00
|
|
|
Py_ssize_t n = PyString_Size(name);
|
1997-08-25 18:23:56 -03:00
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1998-05-29 18:37:21 -03:00
|
|
|
char *err = NULL;
|
|
|
|
if (strcmp(sname, "__dict__") == 0)
|
|
|
|
err = set_dict(op, v);
|
|
|
|
else if (strcmp(sname, "__bases__") == 0)
|
|
|
|
err = set_bases(op, v);
|
|
|
|
else if (strcmp(sname, "__name__") == 0)
|
|
|
|
err = set_name(op, v);
|
|
|
|
else if (strcmp(sname, "__getattr__") == 0)
|
|
|
|
set_slot(&op->cl_getattr, v);
|
|
|
|
else if (strcmp(sname, "__setattr__") == 0)
|
|
|
|
set_slot(&op->cl_setattr, v);
|
|
|
|
else if (strcmp(sname, "__delattr__") == 0)
|
|
|
|
set_slot(&op->cl_delattr, v);
|
|
|
|
/* For the last three, we fall through to update the
|
|
|
|
dictionary as well. */
|
|
|
|
if (err != NULL) {
|
|
|
|
if (*err == '\0')
|
|
|
|
return 0;
|
|
|
|
PyErr_SetString(PyExc_TypeError, err);
|
1997-08-25 18:23:56 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int rv = PyDict_DelItem(op->cl_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"class %.50s has no attribute '%.400s'",
|
|
|
|
PyString_AS_STRING(op->cl_name), sname);
|
1992-09-04 06:45:18 -03:00
|
|
|
return rv;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyDict_SetItem(op->cl_dict, name, v);
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
class_repr(PyClassObject *op)
|
1993-05-19 11:50:45 -03:00
|
|
|
{
|
1997-10-20 20:26:11 -03:00
|
|
|
PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
|
1993-05-19 11:50:45 -03:00
|
|
|
char *name;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (op->cl_name == NULL || !PyString_Check(op->cl_name))
|
1993-05-19 11:50:45 -03:00
|
|
|
name = "?";
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
name = PyString_AsString(op->cl_name);
|
1997-10-20 20:26:11 -03:00
|
|
|
if (mod == NULL || !PyString_Check(mod))
|
2001-09-18 17:23:28 -03:00
|
|
|
return PyString_FromFormat("<class ?.%s at %p>", name, op);
|
1997-10-20 20:26:11 -03:00
|
|
|
else
|
2001-08-24 15:34:26 -03:00
|
|
|
return PyString_FromFormat("<class %s.%s at %p>",
|
|
|
|
PyString_AsString(mod),
|
|
|
|
name, op);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
|
1997-10-20 20:26:11 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
class_str(PyClassObject *op)
|
1997-10-20 20:26:11 -03:00
|
|
|
{
|
|
|
|
PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
|
|
|
|
PyObject *name = op->cl_name;
|
|
|
|
PyObject *res;
|
2006-04-22 08:40:03 -03:00
|
|
|
Py_ssize_t m, n;
|
1997-10-20 20:26:11 -03:00
|
|
|
|
|
|
|
if (name == NULL || !PyString_Check(name))
|
|
|
|
return class_repr(op);
|
|
|
|
if (mod == NULL || !PyString_Check(mod)) {
|
|
|
|
Py_INCREF(name);
|
|
|
|
return name;
|
|
|
|
}
|
2006-03-19 21:53:23 -04:00
|
|
|
m = PyString_GET_SIZE(mod);
|
|
|
|
n = PyString_GET_SIZE(name);
|
1997-10-20 20:26:11 -03:00
|
|
|
res = PyString_FromStringAndSize((char *)NULL, m+1+n);
|
|
|
|
if (res != NULL) {
|
2006-03-19 21:53:23 -04:00
|
|
|
char *s = PyString_AS_STRING(res);
|
|
|
|
memcpy(s, PyString_AS_STRING(mod), m);
|
1997-10-20 20:26:11 -03:00
|
|
|
s += m;
|
|
|
|
*s++ = '.';
|
2006-03-19 21:53:23 -04:00
|
|
|
memcpy(s, PyString_AS_STRING(name), n);
|
1997-10-20 20:26:11 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
class_traverse(PyClassObject *o, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(o->cl_bases);
|
|
|
|
Py_VISIT(o->cl_dict);
|
|
|
|
Py_VISIT(o->cl_name);
|
|
|
|
Py_VISIT(o->cl_getattr);
|
|
|
|
Py_VISIT(o->cl_setattr);
|
|
|
|
Py_VISIT(o->cl_delattr);
|
2000-06-23 11:18:11 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyClass_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2002-06-14 17:41:17 -03:00
|
|
|
"classobj",
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyClassObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-01-17 11:28:20 -04:00
|
|
|
(destructor)class_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
(reprfunc)class_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
2001-08-02 01:15:00 -03:00
|
|
|
PyInstance_New, /* tp_call */
|
2001-01-17 11:28:20 -04:00
|
|
|
(reprfunc)class_str, /* tp_str */
|
|
|
|
(getattrofunc)class_getattr, /* tp_getattro */
|
|
|
|
(setattrofunc)class_setattr, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 20:54:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
2002-06-14 17:41:17 -03:00
|
|
|
class_doc, /* tp_doc */
|
2001-01-17 11:28:20 -04:00
|
|
|
(traverseproc)class_traverse, /* tp_traverse */
|
2001-08-02 01:15:00 -03:00
|
|
|
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 */
|
|
|
|
class_new, /* tp_new */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
int
|
2006-04-11 03:54:30 -03:00
|
|
|
PyClass_IsSubclass(PyObject *klass, PyObject *base)
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, n;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *cp;
|
2006-04-11 03:54:30 -03:00
|
|
|
if (klass == base)
|
1993-05-20 11:24:46 -03:00
|
|
|
return 1;
|
2002-12-12 12:41:44 -04:00
|
|
|
if (PyTuple_Check(base)) {
|
|
|
|
n = PyTuple_GET_SIZE(base);
|
|
|
|
for (i = 0; i < n; i++) {
|
2006-04-11 03:54:30 -03:00
|
|
|
if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i)))
|
2002-12-12 12:41:44 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2002-12-12 15:14:08 -04:00
|
|
|
return 0;
|
2002-12-12 12:41:44 -04:00
|
|
|
}
|
2006-04-11 03:54:30 -03:00
|
|
|
if (klass == NULL || !PyClass_Check(klass))
|
1993-05-25 06:38:27 -03:00
|
|
|
return 0;
|
2006-04-11 03:54:30 -03:00
|
|
|
cp = (PyClassObject *)klass;
|
1997-05-02 00:12:38 -03:00
|
|
|
n = PyTuple_Size(cp->cl_bases);
|
1993-05-20 11:24:46 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
|
1993-05-20 11:24:46 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
/* Instance objects */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2001-01-27 23:53:08 -04:00
|
|
|
PyInstance_NewRaw(PyObject *klass, PyObject *dict)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-01-27 23:53:08 -04:00
|
|
|
PyInstanceObject *inst;
|
|
|
|
|
|
|
|
if (!PyClass_Check(klass)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-27 23:53:08 -04:00
|
|
|
if (dict == NULL) {
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyDict_Check(dict)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(dict);
|
|
|
|
}
|
2001-08-29 20:54:21 -03:00
|
|
|
inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);
|
2001-01-27 23:53:08 -04:00
|
|
|
if (inst == NULL) {
|
|
|
|
Py_DECREF(dict);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-03-22 14:26:47 -04:00
|
|
|
inst->in_weakreflist = NULL;
|
2001-01-27 23:53:08 -04:00
|
|
|
Py_INCREF(klass);
|
|
|
|
inst->in_class = (PyClassObject *)klass;
|
|
|
|
inst->in_dict = dict;
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_TRACK(inst);
|
2001-01-27 23:53:08 -04:00
|
|
|
return (PyObject *)inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
|
|
|
|
{
|
|
|
|
register PyInstanceObject *inst;
|
|
|
|
PyObject *init;
|
|
|
|
static PyObject *initstr;
|
|
|
|
|
|
|
|
inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
|
|
|
|
if (inst == NULL)
|
|
|
|
return NULL;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (initstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
initstr = PyString_InternFromString("__init__");
|
2000-04-26 17:39:20 -03:00
|
|
|
init = instance_getattr2(inst, initstr);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (init == NULL) {
|
2002-10-29 14:36:40 -04:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(inst);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((arg != NULL && (!PyTuple_Check(arg) ||
|
|
|
|
PyTuple_Size(arg) != 0))
|
|
|
|
|| (kw != NULL && (!PyDict_Check(kw) ||
|
|
|
|
PyDict_Size(kw) != 0))) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-07-26 15:07:32 -03:00
|
|
|
"this constructor takes no arguments");
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
|
|
|
|
Py_DECREF(init);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (res == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (res != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1993-05-19 11:50:45 -03:00
|
|
|
"__init__() should return None");
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)inst;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
/* Instance methods */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
PyDoc_STRVAR(instance_doc,
|
|
|
|
"instance(class[, dict])\n\
|
|
|
|
\n\
|
|
|
|
Create an instance without calling its __init__() method.\n\
|
|
|
|
The class must be a classic class.\n\
|
|
|
|
If present, dict must be a dictionary or None.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instance_new(PyTypeObject* type, PyObject* args, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *klass;
|
|
|
|
PyObject *dict = Py_None;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O!|O:instance",
|
|
|
|
&PyClass_Type, &klass, &dict))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dict == Py_None)
|
|
|
|
dict = NULL;
|
|
|
|
else if (!PyDict_Check(dict)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"instance() second arg must be dictionary or None");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyInstance_NewRaw(klass, dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_dealloc(register PyInstanceObject *inst)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
PyObject *del;
|
|
|
|
static PyObject *delstr;
|
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
|
|
|
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_UNTRACK(inst);
|
2001-10-26 14:56:51 -03:00
|
|
|
if (inst->in_weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs((PyObject *) inst);
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2000-09-17 11:40:17 -03:00
|
|
|
/* Temporarily resurrect the object. */
|
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
|
|
|
assert(inst->ob_type == &PyInstance_Type);
|
|
|
|
assert(inst->ob_refcnt == 0);
|
|
|
|
inst->ob_refcnt = 1;
|
2000-09-17 11:40:17 -03:00
|
|
|
|
|
|
|
/* Save the current exception, if any. */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
2000-09-17 11:40:17 -03:00
|
|
|
/* Execute __del__ method, if any. */
|
1996-08-09 17:53:24 -03:00
|
|
|
if (delstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delstr = PyString_InternFromString("__del__");
|
2000-04-26 17:39:20 -03:00
|
|
|
if ((del = instance_getattr2(inst, delstr)) != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
|
2000-09-17 11:40:17 -03:00
|
|
|
if (res == NULL)
|
2000-08-31 23:47:25 -03:00
|
|
|
PyErr_WriteUnraisable(del);
|
1996-08-12 19:00:53 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
|
|
|
Py_DECREF(del);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
2000-09-17 11:40:17 -03:00
|
|
|
/* Restore the saved exception. */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
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
|
|
|
|
2000-09-17 11:40:17 -03:00
|
|
|
/* Undo the temporary resurrection; can't use DECREF here, it would
|
|
|
|
* cause a recursive call.
|
|
|
|
*/
|
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
|
|
|
assert(inst->ob_refcnt > 0);
|
|
|
|
if (--inst->ob_refcnt == 0) {
|
|
|
|
Py_DECREF(inst->in_class);
|
|
|
|
Py_XDECREF(inst->in_dict);
|
|
|
|
PyObject_GC_Del(inst);
|
1995-08-28 06:00:43 -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
|
|
|
else {
|
2006-04-22 08:40:03 -03:00
|
|
|
Py_ssize_t refcnt = inst->ob_refcnt;
|
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
|
|
|
/* __del__ resurrected it! Make it look like the original
|
|
|
|
* Py_DECREF never happened.
|
|
|
|
*/
|
|
|
|
_Py_NewReference((PyObject *)inst);
|
|
|
|
inst->ob_refcnt = refcnt;
|
|
|
|
_PyObject_GC_TRACK(inst);
|
2004-08-03 07:21:03 -03:00
|
|
|
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
|
|
|
* we need to undo that. */
|
|
|
|
_Py_DEC_REFTOTAL;
|
|
|
|
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the
|
|
|
|
* object chain, so no more to do there.
|
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
|
|
|
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
2004-08-03 07:21:03 -03:00
|
|
|
* _Py_NewReference bumped tp_allocs: both of those need to be
|
|
|
|
* undone.
|
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
|
|
|
*/
|
1995-08-28 06:00:43 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
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
|
|
|
--inst->ob_type->tp_frees;
|
|
|
|
--inst->ob_type->tp_allocs;
|
2000-09-17 11:40:17 -03:00
|
|
|
#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
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_getattr1(register PyInstanceObject *inst, PyObject *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *v;
|
|
|
|
register char *sname = PyString_AsString(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"instance.__dict__ not accessible in restricted mode");
|
1995-01-10 06:39:49 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(inst->in_dict);
|
1993-10-22 09:04:32 -03:00
|
|
|
return inst->in_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__class__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(inst->in_class);
|
|
|
|
return (PyObject *)inst->in_class;
|
1993-10-22 09:04:32 -03:00
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
2000-04-26 17:39:20 -03:00
|
|
|
v = instance_getattr2(inst, name);
|
2002-10-29 14:36:40 -04:00
|
|
|
if (v == NULL && !PyErr_Occurred()) {
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"%.50s instance has no attribute '%.400s'",
|
2000-04-26 17:39:20 -03:00
|
|
|
PyString_AS_STRING(inst->in_class->cl_name), sname);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_getattr2(register PyInstanceObject *inst, PyObject *name)
|
2000-04-26 17:39:20 -03:00
|
|
|
{
|
|
|
|
register PyObject *v;
|
2006-04-11 03:54:30 -03:00
|
|
|
PyClassObject *klass;
|
2001-08-02 01:15:00 -03:00
|
|
|
descrgetfunc f;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
v = PyDict_GetItem(inst->in_dict, name);
|
2001-08-17 10:43:27 -03:00
|
|
|
if (v != NULL) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2006-04-11 03:54:30 -03:00
|
|
|
v = class_lookup(inst->in_class, name, &klass);
|
2001-08-17 10:43:27 -03:00
|
|
|
if (v != NULL) {
|
|
|
|
Py_INCREF(v);
|
2001-10-17 17:26:38 -03:00
|
|
|
f = TP_DESCR_GET(v->ob_type);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (f != NULL) {
|
|
|
|
PyObject *w = f(v, (PyObject *)inst,
|
|
|
|
(PyObject *)(inst->in_class));
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
v = w;
|
|
|
|
}
|
1993-05-21 16:56:10 -03:00
|
|
|
}
|
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_getattr(register PyInstanceObject *inst, PyObject *name)
|
1994-09-05 04:31:41 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *func, *res;
|
1994-09-05 04:32:29 -03:00
|
|
|
res = instance_getattr1(inst, name);
|
|
|
|
if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *args;
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(2, inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
1994-09-05 04:31:41 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(args);
|
1994-09-05 04:31:41 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-04-07 14:51:59 -03:00
|
|
|
/* See classobject.h comments: this only does dict lookups, and is always
|
|
|
|
* safe to call.
|
|
|
|
*/
|
|
|
|
PyObject *
|
|
|
|
_PyInstance_Lookup(PyObject *pinst, PyObject *name)
|
|
|
|
{
|
|
|
|
PyObject *v;
|
2006-04-11 03:54:30 -03:00
|
|
|
PyClassObject *klass;
|
2003-04-07 14:51:59 -03:00
|
|
|
PyInstanceObject *inst; /* pinst cast to the right type */
|
|
|
|
|
|
|
|
assert(PyInstance_Check(pinst));
|
|
|
|
inst = (PyInstanceObject *)pinst;
|
|
|
|
|
|
|
|
assert(PyString_Check(name));
|
|
|
|
|
|
|
|
v = PyDict_GetItem(inst->in_dict, name);
|
|
|
|
if (v == NULL)
|
2006-04-11 03:54:30 -03:00
|
|
|
v = class_lookup(inst->in_class, name, &klass);
|
2003-04-07 14:51:59 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int rv = PyDict_DelItem(inst->in_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
2000-10-24 16:57:45 -03:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"%.50s instance has no attribute '%.400s'",
|
|
|
|
PyString_AS_STRING(inst->in_class->cl_name),
|
|
|
|
PyString_AS_STRING(name));
|
1992-09-04 06:45:18 -03:00
|
|
|
return rv;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyDict_SetItem(inst->in_dict, name, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-09-05 04:31:41 -03:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
|
1994-09-05 04:31:41 -03:00
|
|
|
{
|
1997-08-25 18:23:56 -03:00
|
|
|
PyObject *func, *args, *res, *tmp;
|
1997-05-02 00:12:38 -03:00
|
|
|
char *sname = PyString_AsString(name);
|
1997-08-25 18:23:56 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
2006-04-22 08:40:03 -03:00
|
|
|
Py_ssize_t n = PyString_Size(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1997-08-25 18:23:56 -03:00
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"__dict__ not accessible in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == NULL || !PyDict_Check(v)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__dict__ must be set to a dictionary");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp = inst->in_dict;
|
|
|
|
Py_INCREF(v);
|
|
|
|
inst->in_dict = v;
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (strcmp(sname, "__class__") == 0) {
|
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"__class__ not accessible in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == NULL || !PyClass_Check(v)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__class__ must be set to a class");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp = (PyObject *)(inst->in_class);
|
|
|
|
Py_INCREF(v);
|
|
|
|
inst->in_class = (PyClassObject *)v;
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
1994-09-05 04:31:41 -03:00
|
|
|
}
|
|
|
|
}
|
1994-09-05 04:32:29 -03:00
|
|
|
if (v == NULL)
|
|
|
|
func = inst->in_class->cl_delattr;
|
|
|
|
else
|
|
|
|
func = inst->in_class->cl_setattr;
|
|
|
|
if (func == NULL)
|
|
|
|
return instance_setattr1(inst, name, v);
|
|
|
|
if (v == NULL)
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(2, inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
else
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(3, inst, name, v);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(args);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1994-09-05 04:31:41 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_repr(PyInstanceObject *inst)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
|
|
|
static PyObject *reprstr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (reprstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
reprstr = PyString_InternFromString("__repr__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, reprstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
PyObject *classname, *mod;
|
1993-05-19 11:50:45 -03:00
|
|
|
char *cname;
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
|
|
|
PyErr_Clear();
|
|
|
|
classname = inst->in_class->cl_name;
|
|
|
|
mod = PyDict_GetItemString(inst->in_class->cl_dict,
|
|
|
|
"__module__");
|
1997-05-02 00:12:38 -03:00
|
|
|
if (classname != NULL && PyString_Check(classname))
|
|
|
|
cname = PyString_AsString(classname);
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
|
|
|
cname = "?";
|
1997-12-02 20:06:02 -04:00
|
|
|
if (mod == NULL || !PyString_Check(mod))
|
2001-08-24 15:34:26 -03:00
|
|
|
return PyString_FromFormat("<?.%s instance at %p>",
|
|
|
|
cname, inst);
|
1997-12-02 20:06:02 -04:00
|
|
|
else
|
2001-08-24 15:34:26 -03:00
|
|
|
return PyString_FromFormat("<%s.%s instance at %p>",
|
|
|
|
PyString_AsString(mod),
|
|
|
|
cname, inst);
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-04-30 11:39:18 -03:00
|
|
|
static PyObject *
|
|
|
|
instance_str(PyInstanceObject *inst)
|
|
|
|
{
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
|
|
|
static PyObject *strstr;
|
|
|
|
|
|
|
|
if (strstr == NULL)
|
|
|
|
strstr = PyString_InternFromString("__str__");
|
|
|
|
func = instance_getattr(inst, strstr);
|
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2001-04-30 11:39:18 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
return instance_repr(inst);
|
|
|
|
}
|
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_hash(PyInstanceObject *inst)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
1993-04-08 09:56:19 -03:00
|
|
|
long outcome;
|
2001-01-18 19:46:31 -04:00
|
|
|
static PyObject *hashstr, *eqstr, *cmpstr;
|
1993-03-29 06:43:31 -04:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (hashstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
hashstr = PyString_InternFromString("__hash__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, hashstr);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
|
|
|
PyErr_Clear();
|
2001-01-18 19:46:31 -04:00
|
|
|
/* If there is no __eq__ and no __cmp__ method, we hash on the
|
|
|
|
address. If an __eq__ or __cmp__ method exists, there must
|
|
|
|
be a __hash__. */
|
|
|
|
if (eqstr == NULL)
|
|
|
|
eqstr = PyString_InternFromString("__eq__");
|
|
|
|
func = instance_getattr(inst, eqstr);
|
1993-04-08 09:56:19 -03:00
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
2001-01-18 19:46:31 -04:00
|
|
|
if (cmpstr == NULL)
|
|
|
|
cmpstr = PyString_InternFromString("__cmp__");
|
|
|
|
func = instance_getattr(inst, cmpstr);
|
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(
|
|
|
|
PyExc_AttributeError))
|
|
|
|
return -1;
|
2001-01-18 19:46:31 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
return _Py_HashPointer(inst);
|
|
|
|
}
|
1993-04-08 09:56:19 -03:00
|
|
|
}
|
2003-09-16 04:11:46 -03:00
|
|
|
Py_XDECREF(func);
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "unhashable instance");
|
1993-03-29 06:43:31 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInt_Check(res)) {
|
|
|
|
outcome = PyInt_AsLong(res);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (outcome == -1)
|
|
|
|
outcome = -2;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__hash__() should return an int");
|
1993-03-29 06:43:31 -04:00
|
|
|
outcome = -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1993-03-29 06:43:31 -04:00
|
|
|
return outcome;
|
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
instance_traverse(PyInstanceObject *o, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(o->in_class);
|
|
|
|
Py_VISIT(o->in_dict);
|
2000-06-23 14:14:56 -03:00
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
2001-04-23 11:08:49 -03:00
|
|
|
static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
|
|
|
|
static PyObject *iterstr, *nextstr;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_length(PyInstanceObject *inst)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t outcome;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (lenstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
lenstr = PyString_InternFromString("__len__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, lenstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-11-26 06:30:26 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInt_Check(res)) {
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t temp = PyInt_AsSsize_t(res);
|
|
|
|
if (temp == -1 && PyErr_Occurred()) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
outcome = (Py_ssize_t)temp;
|
|
|
|
#if SIZEOF_SIZE_T < SIZEOF_LONG
|
2005-09-19 19:42:41 -03:00
|
|
|
/* Overflow check -- range of PyInt is more than C int */
|
|
|
|
if (outcome != temp) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
2005-09-20 15:49:54 -03:00
|
|
|
"__len__() should return 0 <= outcome < 2**31");
|
2005-09-19 19:42:41 -03:00
|
|
|
outcome = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
1992-08-12 12:35:34 -03:00
|
|
|
if (outcome < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"__len__() should return >= 0");
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__len__() should return an int");
|
1992-08-12 12:35:34 -03:00
|
|
|
outcome = -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return outcome;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_subscript(PyInstanceObject *inst, PyObject *key)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getitemstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
2003-10-12 16:09:37 -03:00
|
|
|
arg = PyTuple_Pack(1, key);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
if (delitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delitemstr = PyString_InternFromString("__delitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delitemstr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
setitemstr = PyString_InternFromString("__setitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setitemstr);
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return -1;
|
|
|
|
if (value == NULL)
|
2003-10-12 16:09:37 -03:00
|
|
|
arg = PyTuple_Pack(1, key);
|
1992-08-12 12:35:34 -03:00
|
|
|
else
|
2003-10-12 16:09:37 -03:00
|
|
|
arg = PyTuple_Pack(2, key, value);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMappingMethods instance_as_mapping = {
|
2006-02-15 13:27:45 -04:00
|
|
|
(lenfunc)instance_length, /* mp_length */
|
2001-01-17 11:28:20 -04:00
|
|
|
(binaryfunc)instance_subscript, /* mp_subscript */
|
|
|
|
(objobjargproc)instance_ass_subscript, /* mp_ass_subscript */
|
1992-08-12 12:35:34 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
instance_item(PyInstanceObject *inst, Py_ssize_t i)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getitemstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
2006-02-15 13:27:45 -04:00
|
|
|
arg = Py_BuildValue("(n)", i);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
|
|
|
static PyObject *getslicestr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getslicestr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getslicestr = PyString_InternFromString("__getslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getslicestr);
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2000-08-17 19:37:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
if (getitemstr == NULL)
|
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
|
|
|
func = instance_getattr(inst, getitemstr);
|
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
2006-03-23 02:03:08 -04:00
|
|
|
arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j));
|
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
|
|
|
} else
|
2006-02-17 11:57:41 -04:00
|
|
|
arg = Py_BuildValue("(nn)", i, j);
|
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
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (item == NULL) {
|
|
|
|
if (delitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delitemstr = PyString_InternFromString("__delitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delitemstr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
setitemstr = PyString_InternFromString("__setitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setitemstr);
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
if (item == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("i", i);
|
1992-08-12 12:35:34 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(iO)", i, item);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
|
|
|
static PyObject *setslicestr, *delslicestr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
if (delslicestr == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
delslicestr =
|
|
|
|
PyString_InternFromString("__delslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delslicestr);
|
2000-08-17 19:37:32 -03:00
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
2000-08-17 19:37:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if (delitemstr == NULL)
|
|
|
|
delitemstr =
|
|
|
|
PyString_InternFromString("__delitem__");
|
|
|
|
func = instance_getattr(inst, delitemstr);
|
|
|
|
if (func == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
arg = Py_BuildValue("(N)",
|
2006-03-23 02:03:08 -04:00
|
|
|
_PySlice_FromIndices(i, j));
|
2000-08-17 19:37:32 -03:00
|
|
|
} else
|
2006-02-17 11:57:41 -04:00
|
|
|
arg = Py_BuildValue("(nn)", i, j);
|
1996-08-09 17:53:24 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setslicestr == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
setslicestr =
|
|
|
|
PyString_InternFromString("__setslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setslicestr);
|
2000-08-17 19:37:32 -03:00
|
|
|
if (func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
2000-08-17 19:37:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if (setitemstr == NULL)
|
|
|
|
setitemstr =
|
|
|
|
PyString_InternFromString("__setitem__");
|
|
|
|
func = instance_getattr(inst, setitemstr);
|
|
|
|
if (func == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
arg = Py_BuildValue("(NO)",
|
2006-03-23 02:03:08 -04:00
|
|
|
_PySlice_FromIndices(i, j), value);
|
2000-08-17 19:37:32 -03:00
|
|
|
} else
|
2006-02-17 11:57:41 -04:00
|
|
|
arg = Py_BuildValue("(nnO)", i, j, value);
|
1996-08-09 17:53:24 -03:00
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-05 18:05:01 -03:00
|
|
|
static int
|
|
|
|
instance_contains(PyInstanceObject *inst, PyObject *member)
|
2000-02-28 11:03:15 -04:00
|
|
|
{
|
|
|
|
static PyObject *__contains__;
|
2001-05-05 18:05:01 -03:00
|
|
|
PyObject *func;
|
|
|
|
|
|
|
|
/* Try __contains__ first.
|
|
|
|
* If that can't be done, try iterator-based searching.
|
|
|
|
*/
|
2000-02-28 11:03:15 -04:00
|
|
|
|
|
|
|
if(__contains__ == NULL) {
|
|
|
|
__contains__ = PyString_InternFromString("__contains__");
|
|
|
|
if(__contains__ == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
func = instance_getattr(inst, __contains__);
|
2001-05-05 18:05:01 -03:00
|
|
|
if (func) {
|
|
|
|
PyObject *res;
|
|
|
|
int ret;
|
2003-10-12 16:09:37 -03:00
|
|
|
PyObject *arg = PyTuple_Pack(1, member);
|
2001-05-05 18:05:01 -03:00
|
|
|
if(arg == NULL) {
|
|
|
|
Py_DECREF(func);
|
2000-02-28 11:03:15 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2001-05-05 18:05:01 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
2000-02-28 11:03:15 -04:00
|
|
|
Py_DECREF(func);
|
2001-05-05 18:05:01 -03:00
|
|
|
Py_DECREF(arg);
|
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
|
|
|
if(res == NULL)
|
2001-05-05 18:05:01 -03:00
|
|
|
return -1;
|
|
|
|
ret = PyObject_IsTrue(res);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return ret;
|
2000-02-28 11:03:15 -04:00
|
|
|
}
|
2001-05-05 18:05:01 -03:00
|
|
|
|
|
|
|
/* Couldn't find __contains__. */
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
/* Assume the failure was simply due to that there is no
|
|
|
|
* __contains__ attribute, and try iterating instead.
|
|
|
|
*/
|
|
|
|
PyErr_Clear();
|
2001-09-08 01:00:12 -03:00
|
|
|
return _PySequence_IterSearch((PyObject *)inst, member,
|
2006-04-22 08:40:03 -03:00
|
|
|
PY_ITERSEARCH_CONTAINS) > 0;
|
2001-05-05 18:05:01 -03:00
|
|
|
}
|
|
|
|
else
|
2000-02-28 11:03:15 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-09 01:06:11 -03:00
|
|
|
static PySequenceMethods
|
|
|
|
instance_as_sequence = {
|
2006-02-15 13:27:45 -04:00
|
|
|
(lenfunc)instance_length, /* sq_length */
|
2001-01-17 11:28:20 -04:00
|
|
|
0, /* sq_concat */
|
|
|
|
0, /* sq_repeat */
|
2006-02-15 13:27:45 -04:00
|
|
|
(ssizeargfunc)instance_item, /* sq_item */
|
|
|
|
(ssizessizeargfunc)instance_slice, /* sq_slice */
|
|
|
|
(ssizeobjargproc)instance_ass_item, /* sq_ass_item */
|
|
|
|
(ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */
|
2001-01-17 11:28:20 -04:00
|
|
|
(objobjproc)instance_contains, /* sq_contains */
|
1992-08-12 12:35:34 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
generic_unary_op(PyInstanceObject *self, PyObject *methodname)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
|
|
|
if ((func = instance_getattr(self, methodname)) == NULL)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
static PyObject *
|
|
|
|
generic_binary_op(PyObject *v, PyObject *w, char *opname)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
2001-01-03 21:43:46 -04:00
|
|
|
PyObject *result;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *func = PyObject_GetAttrString(v, opname);
|
|
|
|
if (func == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_INCREF(Py_NotImplemented);
|
|
|
|
return Py_NotImplemented;
|
|
|
|
}
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, w);
|
2001-01-03 21:43:46 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
Py_DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(func);
|
|
|
|
return result;
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *coerce_obj;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
/* Try one half of a binary operator involving a class instance. */
|
|
|
|
static PyObject *
|
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
|
|
|
half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
|
2001-01-03 21:43:46 -04:00
|
|
|
int swapped)
|
1994-09-28 12:51:32 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *args;
|
1997-11-18 15:23:07 -04:00
|
|
|
PyObject *coercefunc;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *coerced = NULL;
|
|
|
|
PyObject *v1;
|
2001-01-03 21:43:46 -04:00
|
|
|
PyObject *result;
|
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
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
if (!PyInstance_Check(v)) {
|
|
|
|
Py_INCREF(Py_NotImplemented);
|
|
|
|
return Py_NotImplemented;
|
|
|
|
}
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
coerce_obj = PyString_InternFromString("__coerce__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL)
|
2001-01-03 21:43:46 -04:00
|
|
|
return NULL;
|
1996-08-09 17:53:24 -03:00
|
|
|
}
|
1997-11-18 15:23:07 -04:00
|
|
|
coercefunc = PyObject_GetAttr(v, coerce_obj);
|
|
|
|
if (coercefunc == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
2001-01-03 21:43:46 -04:00
|
|
|
return generic_binary_op(v, w, opname);
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, w);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (args == NULL) {
|
2002-10-18 11:15:33 -03:00
|
|
|
Py_DECREF(coercefunc);
|
2001-01-03 21:43:46 -04:00
|
|
|
return NULL;
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
coerced = PyEval_CallObject(coercefunc, args);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(args);
|
2001-01-03 21:43:46 -04:00
|
|
|
Py_DECREF(coercefunc);
|
|
|
|
if (coerced == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (coerced == Py_None || coerced == Py_NotImplemented) {
|
|
|
|
Py_DECREF(coerced);
|
|
|
|
return generic_binary_op(v, w, opname);
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
|
|
|
|
Py_DECREF(coerced);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"coercion should return None or 2-tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v1 = PyTuple_GetItem(coerced, 0);
|
|
|
|
w = PyTuple_GetItem(coerced, 1);
|
2001-01-17 11:28:20 -04:00
|
|
|
if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
|
2001-01-03 21:43:46 -04:00
|
|
|
/* prevent recursion if __coerce__ returns self as the first
|
|
|
|
* argument */
|
2001-01-17 11:28:20 -04:00
|
|
|
result = generic_binary_op(v1, w, opname);
|
2001-01-03 21:43:46 -04:00
|
|
|
} else {
|
|
|
|
if (swapped)
|
|
|
|
result = (thisfunc)(w, v1);
|
|
|
|
else
|
|
|
|
result = (thisfunc)(v1, w);
|
|
|
|
}
|
|
|
|
Py_DECREF(coerced);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Implement a binary operator involving at least one class instance. */
|
|
|
|
static PyObject *
|
|
|
|
do_binop(PyObject *v, PyObject *w, char *opname, char *ropname,
|
|
|
|
binaryfunc thisfunc)
|
|
|
|
{
|
|
|
|
PyObject *result = half_binop(v, w, opname, thisfunc, 0);
|
|
|
|
if (result == Py_NotImplemented) {
|
2001-01-17 11:28:20 -04:00
|
|
|
Py_DECREF(result);
|
2001-01-03 21:43:46 -04:00
|
|
|
result = half_binop(w, v, ropname, thisfunc, 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname,
|
|
|
|
char *ropname, binaryfunc thisfunc)
|
|
|
|
{
|
|
|
|
PyObject *result = half_binop(v, w, iopname, thisfunc, 0);
|
|
|
|
if (result == Py_NotImplemented) {
|
2001-01-17 11:28:20 -04:00
|
|
|
Py_DECREF(result);
|
2001-01-03 21:43:46 -04:00
|
|
|
result = do_binop(v, w, opname, ropname, thisfunc);
|
|
|
|
}
|
|
|
|
return result;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
|
1995-01-10 11:24:06 -04:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_coerce(PyObject **pv, PyObject **pw)
|
1995-01-10 11:24:06 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = *pv;
|
|
|
|
PyObject *w = *pw;
|
1997-11-18 15:23:07 -04:00
|
|
|
PyObject *coercefunc;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *args;
|
|
|
|
PyObject *coerced;
|
1995-01-10 11:24:06 -04:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
coerce_obj = PyString_InternFromString("__coerce__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
1997-11-18 15:23:07 -04:00
|
|
|
coercefunc = PyObject_GetAttr(v, coerce_obj);
|
|
|
|
if (coercefunc == NULL) {
|
2001-01-03 21:43:46 -04:00
|
|
|
/* No __coerce__ method */
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
2001-01-03 21:43:46 -04:00
|
|
|
return 1;
|
1995-01-10 11:24:06 -04:00
|
|
|
}
|
|
|
|
/* Has __coerce__ method: call it */
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, w);
|
1995-01-10 11:24:06 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-11-18 15:23:07 -04:00
|
|
|
coerced = PyEval_CallObject(coercefunc, args);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(args);
|
1997-11-18 15:23:07 -04:00
|
|
|
Py_DECREF(coercefunc);
|
1995-01-10 11:24:06 -04:00
|
|
|
if (coerced == NULL) {
|
|
|
|
/* __coerce__ call raised an exception */
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
if (coerced == Py_None || coerced == Py_NotImplemented) {
|
1995-01-10 11:24:06 -04:00
|
|
|
/* __coerce__ says "I can't do it" */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(coerced);
|
1995-01-10 11:24:06 -04:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
|
1995-01-10 11:24:06 -04:00
|
|
|
/* __coerce__ return value is malformed */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(coerced);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-01-10 11:24:06 -04:00
|
|
|
"coercion should return None or 2-tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* __coerce__ returned two new values */
|
1997-05-02 00:12:38 -03:00
|
|
|
*pv = PyTuple_GetItem(coerced, 0);
|
|
|
|
*pw = PyTuple_GetItem(coerced, 1);
|
|
|
|
Py_INCREF(*pv);
|
|
|
|
Py_INCREF(*pw);
|
|
|
|
Py_DECREF(coerced);
|
1995-01-10 11:24:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
#define UNARY(funcname, methodname) \
|
2000-07-23 19:09:59 -03:00
|
|
|
static PyObject *funcname(PyInstanceObject *self) { \
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *o; \
|
1997-01-18 03:59:12 -04:00
|
|
|
if (o == NULL) o = PyString_InternFromString(methodname); \
|
1996-08-09 17:53:24 -03:00
|
|
|
return generic_unary_op(self, o); \
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
#define BINARY(f, m, n) \
|
|
|
|
static PyObject *f(PyObject *v, PyObject *w) { \
|
|
|
|
return do_binop(v, w, "__" m "__", "__r" m "__", n); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BINARY_INPLACE(f, m, n) \
|
|
|
|
static PyObject *f(PyObject *v, PyObject *w) { \
|
|
|
|
return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \
|
|
|
|
"__r" m "__", n); \
|
|
|
|
}
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
UNARY(instance_neg, "__neg__")
|
|
|
|
UNARY(instance_pos, "__pos__")
|
|
|
|
UNARY(instance_abs, "__abs__")
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
BINARY(instance_or, "or", PyNumber_Or)
|
|
|
|
BINARY(instance_and, "and", PyNumber_And)
|
|
|
|
BINARY(instance_xor, "xor", PyNumber_Xor)
|
|
|
|
BINARY(instance_lshift, "lshift", PyNumber_Lshift)
|
|
|
|
BINARY(instance_rshift, "rshift", PyNumber_Rshift)
|
|
|
|
BINARY(instance_add, "add", PyNumber_Add)
|
|
|
|
BINARY(instance_sub, "sub", PyNumber_Subtract)
|
|
|
|
BINARY(instance_mul, "mul", PyNumber_Multiply)
|
|
|
|
BINARY(instance_div, "div", PyNumber_Divide)
|
|
|
|
BINARY(instance_mod, "mod", PyNumber_Remainder)
|
|
|
|
BINARY(instance_divmod, "divmod", PyNumber_Divmod)
|
2001-08-08 02:00:18 -03:00
|
|
|
BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
|
|
|
|
BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
|
2001-01-03 21:43:46 -04:00
|
|
|
|
|
|
|
BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
|
|
|
|
BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
|
|
|
|
BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd)
|
|
|
|
BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift)
|
|
|
|
BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift)
|
|
|
|
BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd)
|
|
|
|
BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
|
|
|
|
BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
|
|
|
|
BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
|
|
|
|
BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
|
2001-08-08 02:00:18 -03:00
|
|
|
BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
|
|
|
|
BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
|
2001-01-03 21:43:46 -04:00
|
|
|
|
2001-01-17 11:28:20 -04:00
|
|
|
/* Try a 3-way comparison, returning an int; v is an instance. Return:
|
|
|
|
-2 for an exception;
|
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w;
|
|
|
|
2 if this particular 3-way comparison is not implemented or undefined.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
half_cmp(PyObject *v, PyObject *w)
|
2001-01-03 21:43:46 -04:00
|
|
|
{
|
2001-01-17 11:28:20 -04:00
|
|
|
static PyObject *cmp_obj;
|
|
|
|
PyObject *args;
|
2001-01-29 19:50:25 -04:00
|
|
|
PyObject *cmp_func;
|
2001-01-17 11:28:20 -04:00
|
|
|
PyObject *result;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
assert(PyInstance_Check(v));
|
|
|
|
|
|
|
|
if (cmp_obj == NULL) {
|
|
|
|
cmp_obj = PyString_InternFromString("__cmp__");
|
|
|
|
if (cmp_obj == NULL)
|
|
|
|
return -2;
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
|
|
|
|
2001-01-29 19:50:25 -04:00
|
|
|
cmp_func = PyObject_GetAttr(v, cmp_obj);
|
|
|
|
if (cmp_func == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -2;
|
2001-01-17 11:28:20 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, w);
|
2002-10-18 11:15:33 -03:00
|
|
|
if (args == NULL) {
|
|
|
|
Py_DECREF(cmp_func);
|
2001-01-17 11:28:20 -04:00
|
|
|
return -2;
|
2002-10-18 11:15:33 -03:00
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
|
2001-01-29 19:50:25 -04:00
|
|
|
result = PyEval_CallObject(cmp_func, args);
|
2001-01-17 11:28:20 -04:00
|
|
|
Py_DECREF(args);
|
2001-01-29 19:50:25 -04:00
|
|
|
Py_DECREF(cmp_func);
|
2001-01-17 11:28:20 -04:00
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
return -2;
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
if (result == Py_NotImplemented) {
|
2001-01-17 11:28:20 -04:00
|
|
|
Py_DECREF(result);
|
|
|
|
return 2;
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
|
|
|
|
l = PyInt_AsLong(result);
|
|
|
|
Py_DECREF(result);
|
|
|
|
if (l == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"comparison did not return an int");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return l < 0 ? -1 : l > 0 ? 1 : 0;
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
|
|
|
|
2001-01-17 11:28:20 -04:00
|
|
|
/* Try a 3-way comparison, returning an int; either v or w is an instance.
|
|
|
|
We first try a coercion. Return:
|
|
|
|
-2 for an exception;
|
|
|
|
-1 if v < w;
|
|
|
|
0 if v == w;
|
|
|
|
1 if v > w;
|
|
|
|
2 if this particular 3-way comparison is not implemented or undefined.
|
|
|
|
THIS IS ONLY CALLED FROM object.c!
|
|
|
|
*/
|
2001-01-03 21:43:46 -04:00
|
|
|
static int
|
2001-01-17 11:28:20 -04:00
|
|
|
instance_compare(PyObject *v, PyObject *w)
|
2001-01-03 21:43:46 -04:00
|
|
|
{
|
2001-01-17 11:28:20 -04:00
|
|
|
int c;
|
|
|
|
|
|
|
|
c = PyNumber_CoerceEx(&v, &w);
|
|
|
|
if (c < 0)
|
|
|
|
return -2;
|
|
|
|
if (c == 0) {
|
|
|
|
/* If neither is now an instance, use regular comparison */
|
|
|
|
if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
|
|
|
|
c = PyObject_Compare(v, w);
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return -2;
|
|
|
|
return c < 0 ? -1 : c > 0 ? 1 : 0;
|
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
else {
|
|
|
|
/* The coercion didn't do anything.
|
|
|
|
Treat this the same as returning v and w unchanged. */
|
|
|
|
Py_INCREF(v);
|
|
|
|
Py_INCREF(w);
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
|
|
|
|
if (PyInstance_Check(v)) {
|
|
|
|
c = half_cmp(v, w);
|
|
|
|
if (c <= 1) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
|
|
|
return c;
|
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
if (PyInstance_Check(w)) {
|
|
|
|
c = half_cmp(w, v);
|
|
|
|
if (c <= 1) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
|
|
|
if (c >= -1)
|
|
|
|
c = -c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
|
|
|
return 2;
|
2001-01-03 21:43:46 -04:00
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_nonzero(PyInstanceObject *self)
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
long outcome;
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *nonzerostr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (nonzerostr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
nonzerostr = PyString_InternFromString("__nonzero__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if ((func = instance_getattr(self, nonzerostr)) == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1996-08-09 17:53:24 -03:00
|
|
|
if (lenstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
lenstr = PyString_InternFromString("__len__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if ((func = instance_getattr(self, lenstr)) == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1992-08-12 12:35:34 -03:00
|
|
|
/* Fall back to the default behavior:
|
|
|
|
all instances are nonzero */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__nonzero__ should return an int");
|
1992-08-12 12:35:34 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
outcome = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (outcome < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"__nonzero__ should return >= 0");
|
1992-08-12 12:35:34 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return outcome > 0;
|
|
|
|
}
|
|
|
|
|
2006-03-07 14:50:55 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
instance_index(PyInstanceObject *self)
|
|
|
|
{
|
|
|
|
PyObject *func, *res;
|
|
|
|
Py_ssize_t outcome;
|
|
|
|
static PyObject *indexstr = NULL;
|
|
|
|
|
|
|
|
if (indexstr == NULL) {
|
|
|
|
indexstr = PyString_InternFromString("__index__");
|
|
|
|
if (indexstr == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((func = instance_getattr(self, indexstr)) == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"object cannot be interpreted as an index");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyInt_Check(res) || PyLong_Check(res)) {
|
|
|
|
outcome = res->ob_type->tp_as_number->nb_index(res);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__index__ must return an int or a long");
|
|
|
|
outcome = -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(res);
|
|
|
|
return outcome;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
UNARY(instance_invert, "__invert__")
|
1992-09-12 08:09:23 -03:00
|
|
|
UNARY(instance_int, "__int__")
|
|
|
|
UNARY(instance_long, "__long__")
|
|
|
|
UNARY(instance_float, "__float__")
|
|
|
|
UNARY(instance_oct, "__oct__")
|
|
|
|
UNARY(instance_hex, "__hex__")
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
static PyObject *
|
|
|
|
bin_power(PyObject *v, PyObject *w)
|
|
|
|
{
|
|
|
|
return PyNumber_Power(v, w, Py_None);
|
|
|
|
}
|
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
/* This version is for ternary calls only (z != None) */
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instance_pow(PyObject *v, PyObject *w, PyObject *z)
|
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
|
|
|
{
|
2001-01-03 21:43:46 -04:00
|
|
|
if (z == Py_None) {
|
|
|
|
return do_binop(v, w, "__pow__", "__rpow__", bin_power);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *result;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
/* XXX Doesn't do coercions... */
|
|
|
|
func = PyObject_GetAttrString(v, "__pow__");
|
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(2, w, z);
|
2001-01-03 21:43:46 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
Py_DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = PyEval_CallObject(func, args);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
2001-01-03 21:43:46 -04:00
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-24 17:08:19 -03:00
|
|
|
static PyObject *
|
2001-01-03 21:43:46 -04:00
|
|
|
bin_inplace_power(PyObject *v, PyObject *w)
|
2000-08-24 17:08:19 -03:00
|
|
|
{
|
2001-01-03 21:43:46 -04:00
|
|
|
return PyNumber_InPlacePower(v, w, Py_None);
|
|
|
|
}
|
2000-08-24 17:08:19 -03:00
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instance_ipow(PyObject *v, PyObject *w, PyObject *z)
|
|
|
|
{
|
|
|
|
if (z == Py_None) {
|
|
|
|
return do_binop_inplace(v, w, "__ipow__", "__pow__",
|
|
|
|
"__rpow__", bin_inplace_power);
|
2000-08-24 17:08:19 -03:00
|
|
|
}
|
2001-01-03 21:43:46 -04:00
|
|
|
else {
|
|
|
|
/* XXX Doesn't do coercions... */
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
func = PyObject_GetAttrString(v, "__ipow__");
|
|
|
|
if (func == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
|
|
|
PyErr_Clear();
|
|
|
|
return instance_pow(v, w, z);
|
|
|
|
}
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(2, w, z);
|
2001-01-03 21:43:46 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
Py_DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = PyEval_CallObject(func, args);
|
2000-08-24 17:08:19 -03:00
|
|
|
Py_DECREF(func);
|
2001-01-03 21:43:46 -04:00
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
2000-08-24 17:08:19 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-17 11:28:20 -04:00
|
|
|
/* Map rich comparison operators to their __xx__ namesakes */
|
2001-05-11 11:48:41 -03:00
|
|
|
#define NAME_OPS 6
|
|
|
|
static PyObject **name_op = 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
|
|
|
static int
|
2001-05-21 23:33:08 -03:00
|
|
|
init_name_op(void)
|
2001-05-11 11:48:41 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *_name_op[] = {
|
|
|
|
"__lt__",
|
|
|
|
"__le__",
|
|
|
|
"__eq__",
|
|
|
|
"__ne__",
|
|
|
|
"__gt__",
|
|
|
|
"__ge__",
|
|
|
|
};
|
|
|
|
|
|
|
|
name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS);
|
|
|
|
if (name_op == NULL)
|
|
|
|
return -1;
|
|
|
|
for (i = 0; i < NAME_OPS; ++i) {
|
|
|
|
name_op[i] = PyString_InternFromString(_name_op[i]);
|
|
|
|
if (name_op[i] == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-01-17 11:28:20 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
half_richcompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *method;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
assert(PyInstance_Check(v));
|
|
|
|
|
2001-05-11 11:48:41 -03:00
|
|
|
if (name_op == NULL) {
|
|
|
|
if (init_name_op() < 0)
|
2001-01-17 11:28:20 -04:00
|
|
|
return NULL;
|
2001-05-11 11:48:41 -03:00
|
|
|
}
|
|
|
|
/* If the instance doesn't define an __getattr__ method, use
|
|
|
|
instance_getattr2 directly because it will not set an
|
|
|
|
exception on failure. */
|
2002-10-29 14:36:40 -04:00
|
|
|
if (((PyInstanceObject *)v)->in_class->cl_getattr == 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
|
|
|
method = instance_getattr2((PyInstanceObject *)v,
|
2001-05-11 11:48:41 -03:00
|
|
|
name_op[op]);
|
2002-10-29 14:36:40 -04:00
|
|
|
else
|
2001-05-11 11:48:41 -03:00
|
|
|
method = PyObject_GetAttr(v, name_op[op]);
|
2002-10-29 14:36:40 -04:00
|
|
|
if (method == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
2001-05-11 11:48:41 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2002-10-29 14:36:40 -04:00
|
|
|
res = Py_NotImplemented;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
2001-01-17 11:28:20 -04:00
|
|
|
}
|
|
|
|
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, w);
|
2001-01-17 11:28:20 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
Py_DECREF(method);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = PyEval_CallObject(method, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(method);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instance_richcompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
|
|
|
|
if (PyInstance_Check(v)) {
|
|
|
|
res = half_richcompare(v, w, op);
|
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyInstance_Check(w)) {
|
2004-09-22 23:39:37 -03:00
|
|
|
res = half_richcompare(w, v, _Py_SwappedOp[op]);
|
2001-01-17 11:28:20 -04:00
|
|
|
if (res != Py_NotImplemented)
|
|
|
|
return res;
|
|
|
|
Py_DECREF(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_NotImplemented);
|
|
|
|
return Py_NotImplemented;
|
|
|
|
}
|
|
|
|
|
2001-01-03 21:43:46 -04:00
|
|
|
|
2001-04-20 16:13:02 -03:00
|
|
|
/* Get the iterator */
|
|
|
|
static PyObject *
|
|
|
|
instance_getiter(PyInstanceObject *self)
|
|
|
|
{
|
|
|
|
PyObject *func;
|
|
|
|
|
2002-06-13 18:42:04 -03:00
|
|
|
if (iterstr == NULL) {
|
2001-04-20 16:13:02 -03:00
|
|
|
iterstr = PyString_InternFromString("__iter__");
|
2002-06-13 18:42:04 -03:00
|
|
|
if (iterstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (getitemstr == NULL) {
|
2001-04-20 16:13:02 -03:00
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
2002-06-13 18:42:04 -03:00
|
|
|
if (getitemstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-04-20 16:13:02 -03:00
|
|
|
|
|
|
|
if ((func = instance_getattr(self, iterstr)) != NULL) {
|
|
|
|
PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
2001-04-23 11:08:49 -03:00
|
|
|
if (res != NULL && !PyIter_Check(res)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__iter__ returned non-iterator "
|
|
|
|
"of type '%.100s'",
|
|
|
|
res->ob_type->tp_name);
|
|
|
|
Py_DECREF(res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
2001-04-20 16:13:02 -03:00
|
|
|
return res;
|
|
|
|
}
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2001-04-20 16:13:02 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if ((func = instance_getattr(self, getitemstr)) == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"iteration over non-sequence");
|
2001-04-20 16:13:02 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(func);
|
2001-04-23 11:08:49 -03:00
|
|
|
return PySeqIter_New((PyObject *)self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Call the iterator's next */
|
|
|
|
static PyObject *
|
|
|
|
instance_iternext(PyInstanceObject *self)
|
|
|
|
{
|
|
|
|
PyObject *func;
|
|
|
|
|
|
|
|
if (nextstr == NULL)
|
|
|
|
nextstr = PyString_InternFromString("next");
|
|
|
|
|
|
|
|
if ((func = instance_getattr(self, nextstr)) != NULL) {
|
|
|
|
PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
|
|
|
if (res != NULL) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_TypeError, "instance has no next() method");
|
|
|
|
return NULL;
|
2001-04-20 16:13:02 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
instance_call(PyObject *func, PyObject *arg, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
|
|
|
|
if (call == NULL) {
|
|
|
|
PyInstanceObject *inst = (PyInstanceObject*) func;
|
2002-06-13 18:42:04 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"%.200s instance has no __call__ method",
|
|
|
|
PyString_AsString(inst->in_class->cl_name));
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-13 18:32:51 -03:00
|
|
|
/* We must check and increment the recursion depth here. Scenario:
|
|
|
|
class A:
|
|
|
|
pass
|
|
|
|
A.__call__ = A() # that's right
|
|
|
|
a = A() # ok
|
|
|
|
a() # infinite recursion
|
|
|
|
This bounces between instance_call() and PyObject_Call() without
|
|
|
|
ever hitting eval_frame() (which has the main recursion check). */
|
2003-10-28 08:05:48 -04:00
|
|
|
if (Py_EnterRecursiveCall(" in __call__")) {
|
2002-06-13 18:32:51 -03:00
|
|
|
res = NULL;
|
|
|
|
}
|
2003-10-28 08:05:48 -04:00
|
|
|
else {
|
2002-06-13 18:32:51 -03:00
|
|
|
res = PyObject_Call(call, arg, kw);
|
2003-10-28 08:05:48 -04:00
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
Py_DECREF(call);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-04-20 16:13:02 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyNumberMethods instance_as_number = {
|
2006-03-30 07:57:00 -04:00
|
|
|
instance_add, /* nb_add */
|
|
|
|
instance_sub, /* nb_subtract */
|
|
|
|
instance_mul, /* nb_multiply */
|
|
|
|
instance_div, /* nb_divide */
|
|
|
|
instance_mod, /* nb_remainder */
|
|
|
|
instance_divmod, /* nb_divmod */
|
|
|
|
instance_pow, /* nb_power */
|
|
|
|
(unaryfunc)instance_neg, /* nb_negative */
|
|
|
|
(unaryfunc)instance_pos, /* nb_positive */
|
|
|
|
(unaryfunc)instance_abs, /* nb_absolute */
|
|
|
|
(inquiry)instance_nonzero, /* nb_nonzero */
|
|
|
|
(unaryfunc)instance_invert, /* nb_invert */
|
|
|
|
instance_lshift, /* nb_lshift */
|
|
|
|
instance_rshift, /* nb_rshift */
|
|
|
|
instance_and, /* nb_and */
|
|
|
|
instance_xor, /* nb_xor */
|
|
|
|
instance_or, /* nb_or */
|
|
|
|
instance_coerce, /* nb_coerce */
|
|
|
|
(unaryfunc)instance_int, /* nb_int */
|
|
|
|
(unaryfunc)instance_long, /* nb_long */
|
|
|
|
(unaryfunc)instance_float, /* nb_float */
|
|
|
|
(unaryfunc)instance_oct, /* nb_oct */
|
|
|
|
(unaryfunc)instance_hex, /* nb_hex */
|
|
|
|
instance_iadd, /* nb_inplace_add */
|
|
|
|
instance_isub, /* nb_inplace_subtract */
|
|
|
|
instance_imul, /* nb_inplace_multiply */
|
|
|
|
instance_idiv, /* nb_inplace_divide */
|
|
|
|
instance_imod, /* nb_inplace_remainder */
|
|
|
|
instance_ipow, /* nb_inplace_power */
|
|
|
|
instance_ilshift, /* nb_inplace_lshift */
|
|
|
|
instance_irshift, /* nb_inplace_rshift */
|
|
|
|
instance_iand, /* nb_inplace_and */
|
|
|
|
instance_ixor, /* nb_inplace_xor */
|
|
|
|
instance_ior, /* nb_inplace_or */
|
|
|
|
instance_floordiv, /* nb_floor_divide */
|
|
|
|
instance_truediv, /* nb_true_divide */
|
|
|
|
instance_ifloordiv, /* nb_inplace_floor_divide */
|
|
|
|
instance_itruediv, /* nb_inplace_true_divide */
|
|
|
|
(lenfunc)instance_index, /* nb_index */
|
1992-08-12 12:35:34 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyInstance_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
"instance",
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyInstanceObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-01-17 11:28:20 -04:00
|
|
|
(destructor)instance_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
instance_compare, /* tp_compare */
|
|
|
|
(reprfunc)instance_repr, /* tp_repr */
|
|
|
|
&instance_as_number, /* tp_as_number */
|
|
|
|
&instance_as_sequence, /* tp_as_sequence */
|
|
|
|
&instance_as_mapping, /* tp_as_mapping */
|
|
|
|
(hashfunc)instance_hash, /* tp_hash */
|
2001-08-02 01:15:00 -03:00
|
|
|
instance_call, /* tp_call */
|
2001-04-30 11:39:18 -03:00
|
|
|
(reprfunc)instance_str, /* tp_str */
|
2001-01-17 11:28:20 -04:00
|
|
|
(getattrofunc)instance_getattr, /* tp_getattro */
|
|
|
|
(setattrofunc)instance_setattr, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 20:54:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
|
2002-06-14 17:41:17 -03:00
|
|
|
instance_doc, /* tp_doc */
|
2000-06-23 11:18:11 -03:00
|
|
|
(traverseproc)instance_traverse, /* tp_traverse */
|
2001-01-17 11:28:20 -04:00
|
|
|
0, /* tp_clear */
|
|
|
|
instance_richcompare, /* tp_richcompare */
|
2001-04-20 16:13:02 -03:00
|
|
|
offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
|
|
|
|
(getiterfunc)instance_getiter, /* tp_iter */
|
2001-04-23 11:08:49 -03:00
|
|
|
(iternextfunc)instance_iternext, /* tp_iternext */
|
2002-06-14 17:41:17 -03:00
|
|
|
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 */
|
|
|
|
instance_new, /* tp_new */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
/* Instance method objects are used for two purposes:
|
|
|
|
(a) as bound instance methods (returned by instancename.methodname)
|
|
|
|
(b) as unbound methods (returned by ClassName.methodname)
|
|
|
|
In case (b), im_self is NULL
|
|
|
|
*/
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-08-04 23:06:53 -03:00
|
|
|
static PyMethodObject *free_list;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2006-04-11 03:54:30 -03:00
|
|
|
PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyMethodObject *im;
|
1998-07-08 11:58:16 -03:00
|
|
|
if (!PyCallable_Check(func)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-08-04 23:06:53 -03:00
|
|
|
im = free_list;
|
|
|
|
if (im != NULL) {
|
|
|
|
free_list = (PyMethodObject *)(im->im_self);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_INIT(im, &PyMethod_Type);
|
1997-08-04 23:06:53 -03:00
|
|
|
}
|
|
|
|
else {
|
2001-08-29 20:54:21 -03:00
|
|
|
im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
|
1997-08-04 23:06:53 -03:00
|
|
|
if (im == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-03-23 00:19:27 -04:00
|
|
|
im->im_weakreflist = NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(func);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_func = func;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(self);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_self = self;
|
2006-04-11 03:54:30 -03:00
|
|
|
Py_XINCREF(klass);
|
|
|
|
im->im_class = klass;
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_TRACK(im);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)im;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:53:24 -03:00
|
|
|
/* Descriptors for PyMethod attributes */
|
|
|
|
|
|
|
|
/* im_class, im_func and im_self are stored in the PyMethod object */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define OFF(x) offsetof(PyMethodObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
static PyMemberDef instancemethod_memberlist[] = {
|
|
|
|
{"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
|
|
|
|
"the class associated with a method"},
|
|
|
|
{"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
|
|
|
|
"the function (or other callable) implementing a method"},
|
|
|
|
{"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
|
|
|
|
"the instance to which a method is bound; None for unbound methods"},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2003-11-22 19:55:50 -04:00
|
|
|
/* Christian Tismer argued convincingly that method attributes should
|
|
|
|
(nearly) always override function attributes.
|
|
|
|
The one exception is __doc__; there's a default __doc__ which
|
|
|
|
should only be used for the class, not for instances */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_get_doc(PyMethodObject *im, void *context)
|
|
|
|
{
|
|
|
|
static PyObject *docstr;
|
|
|
|
if (docstr == NULL) {
|
|
|
|
docstr= PyString_InternFromString("__doc__");
|
|
|
|
if (docstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_GetAttr(im->im_func, docstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef instancemethod_getset[] = {
|
|
|
|
{"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
|
|
|
|
{0}
|
|
|
|
};
|
2001-01-15 16:40:19 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-09-18 00:53:24 -03:00
|
|
|
instancemethod_getattro(PyObject *obj, PyObject *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2001-09-18 00:53:24 -03:00
|
|
|
PyMethodObject *im = (PyMethodObject *)obj;
|
|
|
|
PyTypeObject *tp = obj->ob_type;
|
2003-11-22 19:55:50 -04:00
|
|
|
PyObject *descr = NULL;
|
2001-09-18 00:53:24 -03:00
|
|
|
|
2001-10-17 17:26:38 -03:00
|
|
|
if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) {
|
|
|
|
if (tp->tp_dict == NULL) {
|
|
|
|
if (PyType_Ready(tp) < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
descr = _PyType_Lookup(tp, name);
|
1996-05-14 18:54:20 -03:00
|
|
|
}
|
2001-09-18 00:53:24 -03:00
|
|
|
|
|
|
|
if (descr != NULL) {
|
2003-11-22 19:55:50 -04:00
|
|
|
descrgetfunc f = TP_DESCR_GET(descr->ob_type);
|
|
|
|
if (f != NULL)
|
2001-09-18 00:53:24 -03:00
|
|
|
return f(descr, obj, (PyObject *)obj->ob_type);
|
2003-11-22 19:55:50 -04:00
|
|
|
else {
|
|
|
|
Py_INCREF(descr);
|
|
|
|
return descr;
|
|
|
|
}
|
2001-09-18 00:53:24 -03:00
|
|
|
}
|
|
|
|
|
2003-11-22 19:55:50 -04:00
|
|
|
return PyObject_GetAttr(im->im_func, name);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
PyDoc_STRVAR(instancemethod_doc,
|
|
|
|
"instancemethod(function, instance, class)\n\
|
|
|
|
\n\
|
|
|
|
Create an instance method object.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *self;
|
2003-04-09 16:35:08 -03:00
|
|
|
PyObject *classObj = NULL;
|
2002-06-14 17:41:17 -03:00
|
|
|
|
2003-04-09 16:35:08 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3,
|
2002-06-14 17:41:17 -03:00
|
|
|
&func, &self, &classObj))
|
|
|
|
return NULL;
|
|
|
|
if (!PyCallable_Check(func)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be callable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (self == Py_None)
|
|
|
|
self = NULL;
|
2005-03-30 12:32:10 -04:00
|
|
|
if (self == NULL && classObj == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"unbound methods must have non-NULL im_class");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
return PyMethod_New(func, self, classObj);
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 01:06:11 -03:00
|
|
|
instancemethod_dealloc(register PyMethodObject *im)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_UNTRACK(im);
|
2001-10-26 14:56:51 -03:00
|
|
|
if (im->im_weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs((PyObject *)im);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(im->im_func);
|
|
|
|
Py_XDECREF(im->im_self);
|
2001-08-17 09:07:34 -03:00
|
|
|
Py_XDECREF(im->im_class);
|
1997-08-04 23:06:53 -03:00
|
|
|
im->im_self = (PyObject *)free_list;
|
|
|
|
free_list = im;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1992-09-03 17:39:51 -03:00
|
|
|
static int
|
2000-07-09 01:06:11 -03:00
|
|
|
instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
|
1992-09-03 17:39:51 -03:00
|
|
|
{
|
1995-04-06 11:46:51 -03:00
|
|
|
if (a->im_self != b->im_self)
|
|
|
|
return (a->im_self < b->im_self) ? -1 : 1;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyObject_Compare(a->im_func, b->im_func);
|
1992-09-03 17:39:51 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 01:06:11 -03:00
|
|
|
instancemethod_repr(PyMethodObject *a)
|
1993-05-19 11:50:45 -03:00
|
|
|
{
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *self = a->im_self;
|
1998-07-08 11:58:16 -03:00
|
|
|
PyObject *func = a->im_func;
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *klass = a->im_class;
|
|
|
|
PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
|
|
|
|
char *sfuncname = "?", *sklassname = "?";
|
|
|
|
|
|
|
|
funcname = PyObject_GetAttrString(func, "__name__");
|
2002-06-13 18:42:04 -03:00
|
|
|
if (funcname == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_Clear();
|
2002-06-13 18:42:04 -03:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
else if (!PyString_Check(funcname)) {
|
|
|
|
Py_DECREF(funcname);
|
|
|
|
funcname = NULL;
|
1998-07-08 11:58:16 -03:00
|
|
|
}
|
|
|
|
else
|
2001-08-02 01:15:00 -03:00
|
|
|
sfuncname = PyString_AS_STRING(funcname);
|
2001-08-17 10:59:27 -03:00
|
|
|
if (klass == NULL)
|
2001-08-02 01:15:00 -03:00
|
|
|
klassname = NULL;
|
2001-08-17 10:59:27 -03:00
|
|
|
else {
|
|
|
|
klassname = PyObject_GetAttrString(klass, "__name__");
|
2002-06-13 18:42:04 -03:00
|
|
|
if (klassname == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return NULL;
|
2001-08-17 10:59:27 -03:00
|
|
|
PyErr_Clear();
|
2002-06-13 18:42:04 -03:00
|
|
|
}
|
2001-08-17 10:59:27 -03:00
|
|
|
else if (!PyString_Check(klassname)) {
|
|
|
|
Py_DECREF(klassname);
|
|
|
|
klassname = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sklassname = PyString_AS_STRING(klassname);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
1993-05-20 11:24:46 -03:00
|
|
|
if (self == NULL)
|
2001-08-24 15:34:26 -03:00
|
|
|
result = PyString_FromFormat("<unbound method %s.%s>",
|
|
|
|
sklassname, sfuncname);
|
1993-05-20 11:24:46 -03:00
|
|
|
else {
|
2001-08-02 01:15:00 -03:00
|
|
|
/* XXX Shouldn't use repr() here! */
|
|
|
|
PyObject *selfrepr = PyObject_Repr(self);
|
|
|
|
if (selfrepr == NULL)
|
|
|
|
goto fail;
|
|
|
|
if (!PyString_Check(selfrepr)) {
|
|
|
|
Py_DECREF(selfrepr);
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-08-24 15:34:26 -03:00
|
|
|
result = PyString_FromFormat("<bound method %s.%s of %s>",
|
|
|
|
sklassname, sfuncname,
|
|
|
|
PyString_AS_STRING(selfrepr));
|
2001-08-02 01:15:00 -03:00
|
|
|
Py_DECREF(selfrepr);
|
1993-05-20 11:24:46 -03:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
fail:
|
1999-10-11 11:03:12 -03:00
|
|
|
Py_XDECREF(funcname);
|
2001-08-02 01:15:00 -03:00
|
|
|
Py_XDECREF(klassname);
|
|
|
|
return result;
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
2000-07-09 01:06:11 -03:00
|
|
|
instancemethod_hash(PyMethodObject *a)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
|
|
|
long x, y;
|
1993-05-20 11:24:46 -03:00
|
|
|
if (a->im_self == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
x = PyObject_Hash(Py_None);
|
1993-05-20 11:24:46 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
x = PyObject_Hash(a->im_self);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (x == -1)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
y = PyObject_Hash(a->im_func);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (y == -1)
|
|
|
|
return -1;
|
|
|
|
return x ^ y;
|
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(im->im_func);
|
|
|
|
Py_VISIT(im->im_self);
|
|
|
|
Py_VISIT(im->im_class);
|
2000-06-23 14:14:56 -03:00
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
2002-08-19 18:43:18 -03:00
|
|
|
static void
|
2006-04-11 03:54:30 -03:00
|
|
|
getclassname(PyObject *klass, char *buf, int bufsize)
|
2001-08-24 15:48:27 -03:00
|
|
|
{
|
|
|
|
PyObject *name;
|
|
|
|
|
2002-08-19 18:43:18 -03:00
|
|
|
assert(bufsize > 1);
|
|
|
|
strcpy(buf, "?"); /* Default outcome */
|
2006-04-11 03:54:30 -03:00
|
|
|
if (klass == NULL)
|
2002-08-19 18:43:18 -03:00
|
|
|
return;
|
2006-04-11 03:54:30 -03:00
|
|
|
name = PyObject_GetAttrString(klass, "__name__");
|
2001-08-24 15:48:27 -03:00
|
|
|
if (name == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
/* This function cannot return an exception */
|
2001-08-24 15:48:27 -03:00
|
|
|
PyErr_Clear();
|
2002-08-19 18:43:18 -03:00
|
|
|
return;
|
2001-08-24 15:48:27 -03:00
|
|
|
}
|
2002-08-19 18:43:18 -03:00
|
|
|
if (PyString_Check(name)) {
|
|
|
|
strncpy(buf, PyString_AS_STRING(name), bufsize);
|
|
|
|
buf[bufsize-1] = '\0';
|
2001-08-24 15:48:27 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(name);
|
|
|
|
}
|
|
|
|
|
2002-08-19 18:43:18 -03:00
|
|
|
static void
|
|
|
|
getinstclassname(PyObject *inst, char *buf, int bufsize)
|
2001-08-24 15:48:27 -03:00
|
|
|
{
|
2006-04-11 03:54:30 -03:00
|
|
|
PyObject *klass;
|
2001-08-24 15:48:27 -03:00
|
|
|
|
2002-08-19 18:43:18 -03:00
|
|
|
if (inst == NULL) {
|
2002-08-20 11:31:35 -03:00
|
|
|
assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
|
2002-08-19 18:43:18 -03:00
|
|
|
strcpy(buf, "nothing");
|
|
|
|
return;
|
|
|
|
}
|
2001-08-24 15:48:27 -03:00
|
|
|
|
2006-04-11 03:54:30 -03:00
|
|
|
klass = PyObject_GetAttrString(inst, "__class__");
|
|
|
|
if (klass == NULL) {
|
2002-06-13 18:42:04 -03:00
|
|
|
/* This function cannot return an exception */
|
2001-08-24 15:48:27 -03:00
|
|
|
PyErr_Clear();
|
2006-04-11 03:54:30 -03:00
|
|
|
klass = (PyObject *)(inst->ob_type);
|
|
|
|
Py_INCREF(klass);
|
2001-08-24 15:48:27 -03:00
|
|
|
}
|
2006-04-11 03:54:30 -03:00
|
|
|
getclassname(klass, buf, bufsize);
|
|
|
|
Py_XDECREF(klass);
|
2001-08-24 15:48:27 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *self = PyMethod_GET_SELF(func);
|
2006-04-11 03:54:30 -03:00
|
|
|
PyObject *klass = PyMethod_GET_CLASS(func);
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
func = PyMethod_GET_FUNCTION(func);
|
|
|
|
if (self == NULL) {
|
|
|
|
/* Unbound methods must be called with an instance of
|
|
|
|
the class (or a derived class) as first argument */
|
|
|
|
int ok;
|
|
|
|
if (PyTuple_Size(arg) >= 1)
|
|
|
|
self = PyTuple_GET_ITEM(arg, 0);
|
|
|
|
if (self == NULL)
|
|
|
|
ok = 0;
|
|
|
|
else {
|
2006-04-11 03:54:30 -03:00
|
|
|
ok = PyObject_IsInstance(self, klass);
|
2001-08-02 01:15:00 -03:00
|
|
|
if (ok < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!ok) {
|
2002-08-19 18:43:18 -03:00
|
|
|
char clsbuf[256];
|
|
|
|
char instbuf[256];
|
2006-04-11 03:54:30 -03:00
|
|
|
getclassname(klass, clsbuf, sizeof(clsbuf));
|
2002-08-19 18:43:18 -03:00
|
|
|
getinstclassname(self, instbuf, sizeof(instbuf));
|
2001-08-02 01:15:00 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2001-08-24 15:48:27 -03:00
|
|
|
"unbound method %s%s must be called with "
|
|
|
|
"%s instance as first argument "
|
|
|
|
"(got %s%s instead)",
|
2001-08-02 01:15:00 -03:00
|
|
|
PyEval_GetFuncName(func),
|
2001-08-24 15:48:27 -03:00
|
|
|
PyEval_GetFuncDesc(func),
|
2002-08-19 18:43:18 -03:00
|
|
|
clsbuf,
|
|
|
|
instbuf,
|
2001-08-24 15:48:27 -03:00
|
|
|
self == NULL ? "" : " instance");
|
2001-08-02 01:15:00 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(arg);
|
|
|
|
}
|
|
|
|
else {
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t argcount = PyTuple_Size(arg);
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject *newarg = PyTuple_New(argcount + 1);
|
|
|
|
int i;
|
|
|
|
if (newarg == NULL)
|
|
|
|
return NULL;
|
|
|
|
Py_INCREF(self);
|
|
|
|
PyTuple_SET_ITEM(newarg, 0, self);
|
|
|
|
for (i = 0; i < argcount; i++) {
|
|
|
|
PyObject *v = PyTuple_GET_ITEM(arg, i);
|
|
|
|
Py_XINCREF(v);
|
|
|
|
PyTuple_SET_ITEM(newarg, i+1, v);
|
|
|
|
}
|
|
|
|
arg = newarg;
|
|
|
|
}
|
|
|
|
result = PyObject_Call((PyObject *)func, arg, kw);
|
|
|
|
Py_DECREF(arg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-08-15 14:52:31 -03:00
|
|
|
static PyObject *
|
2003-02-11 14:43:00 -04:00
|
|
|
instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
|
2001-08-15 14:52:31 -03:00
|
|
|
{
|
2001-08-17 10:43:27 -03:00
|
|
|
/* Don't rebind an already bound method, or an unbound method
|
2003-02-11 14:43:00 -04:00
|
|
|
of a class that's not a base class of cls. */
|
|
|
|
|
|
|
|
if (PyMethod_GET_SELF(meth) != NULL) {
|
|
|
|
/* Already bound */
|
2001-08-16 17:41:56 -03:00
|
|
|
Py_INCREF(meth);
|
|
|
|
return meth;
|
|
|
|
}
|
2003-02-11 14:43:00 -04:00
|
|
|
/* No, it is an unbound method */
|
|
|
|
if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) {
|
|
|
|
/* Do subclass test. If it fails, return meth unchanged. */
|
|
|
|
int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth));
|
|
|
|
if (ok < 0)
|
|
|
|
return NULL;
|
|
|
|
if (!ok) {
|
|
|
|
Py_INCREF(meth);
|
|
|
|
return meth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Bind it to obj */
|
|
|
|
return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls);
|
2001-08-15 14:52:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyMethod_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2002-06-14 17:41:17 -03:00
|
|
|
"instancemethod",
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyMethodObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-01-17 11:28:20 -04:00
|
|
|
(destructor)instancemethod_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
(cmpfunc)instancemethod_compare, /* tp_compare */
|
|
|
|
(reprfunc)instancemethod_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
(hashfunc)instancemethod_hash, /* tp_hash */
|
2001-08-02 01:15:00 -03:00
|
|
|
instancemethod_call, /* tp_call */
|
2001-01-17 11:28:20 -04:00
|
|
|
0, /* tp_str */
|
2006-03-30 07:57:00 -04:00
|
|
|
instancemethod_getattro, /* tp_getattro */
|
2001-09-18 00:53:24 -03:00
|
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
2001-01-17 11:28:20 -04:00
|
|
|
0, /* tp_as_buffer */
|
2005-06-19 05:42:20 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
|
2002-06-14 17:41:17 -03:00
|
|
|
instancemethod_doc, /* tp_doc */
|
2000-06-23 11:18:11 -03:00
|
|
|
(traverseproc)instancemethod_traverse, /* tp_traverse */
|
2001-03-23 00:19:27 -04:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
2001-08-15 14:52:31 -03:00
|
|
|
offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
2001-09-18 00:53:24 -03:00
|
|
|
instancemethod_memberlist, /* tp_members */
|
2003-11-22 19:55:50 -04:00
|
|
|
instancemethod_getset, /* tp_getset */
|
2001-08-15 14:52:31 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
instancemethod_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
2002-06-14 17:41:17 -03:00
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
instancemethod_new, /* tp_new */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1997-08-04 23:06:53 -03:00
|
|
|
|
|
|
|
/* Clear out the free list */
|
|
|
|
|
|
|
|
void
|
2000-07-09 01:06:11 -03:00
|
|
|
PyMethod_Fini(void)
|
1997-08-04 23:06:53 -03:00
|
|
|
{
|
|
|
|
while (free_list) {
|
2000-05-03 20:44:39 -03:00
|
|
|
PyMethodObject *im = free_list;
|
|
|
|
free_list = (PyMethodObject *)(im->im_self);
|
2001-08-29 20:54:21 -03:00
|
|
|
PyObject_GC_Del(im);
|
1997-08-04 23:06:53 -03:00
|
|
|
}
|
|
|
|
}
|