summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-08-17 05:42:55 (GMT)
committerGuido van Rossum <guido@python.org>2006-08-17 05:42:55 (GMT)
commit50e9fb9e2d6b4b12524116ab775ac6543e4a5332 (patch)
tree96f41e9a6d07a2754fcaceb7b54a7c11f112687f
parentd033ddf4dc501e0920adec9e193750e515bbd128 (diff)
downloadcpython-50e9fb9e2d6b4b12524116ab775ac6543e4a5332.zip
cpython-50e9fb9e2d6b4b12524116ab775ac6543e4a5332.tar.gz
cpython-50e9fb9e2d6b4b12524116ab775ac6543e4a5332.tar.bz2
Completely get rid of PyClass and PyInstance.
(classobject.[ch] aren't empty yet because they also define PyMethod.) This breaks lots of stuff, notably cPickle. But it's a step in the right direction. I'll clean it up later. (Also a few unrelated changes, e.g. T_NONE to define a "struct member" that is always None, and simplification of __hash__ -- these are unfinished.)
-rw-r--r--Include/Python.h8
-rw-r--r--Include/classobject.h44
-rw-r--r--Include/methodobject.h2
-rw-r--r--Include/object.h9
-rw-r--r--Include/structmember.h4
-rw-r--r--Modules/_sre.c14
-rw-r--r--Modules/arraymodule.c3
-rw-r--r--Modules/cPickle.c219
-rw-r--r--Modules/gcmodule.c31
-rw-r--r--Objects/abstract.c5
-rw-r--r--Objects/bytesobject.c9
-rw-r--r--Objects/classobject.c2034
-rw-r--r--Objects/dictobject.c9
-rw-r--r--Objects/listobject.c9
-rw-r--r--Objects/object.c50
-rw-r--r--Objects/setobject.c9
-rw-r--r--Objects/stringobject.c121
-rw-r--r--Objects/typeobject.c137
-rw-r--r--Objects/unicodeobject.c122
-rw-r--r--Python/ceval.c15
-rw-r--r--Python/structmember.c4
-rw-r--r--README10
22 files changed, 325 insertions, 2543 deletions
diff --git a/Include/Python.h b/Include/Python.h
index bbb9a08..d75854c 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -7,14 +7,6 @@
#include "patchlevel.h"
#include "pyconfig.h"
-/* Cyclic gc is always enabled, starting with release 2.3a1. Supply the
- * old symbol for the benefit of extension modules written before then
- * that may be conditionalizing on it. The core doesn't use it anymore.
- */
-#ifndef WITH_CYCLE_GC
-#define WITH_CYCLE_GC 1
-#endif
-
#include <limits.h>
#ifndef UCHAR_MAX
diff --git a/Include/classobject.h b/Include/classobject.h
index 8f8db7d..885c43e 100644
--- a/Include/classobject.h
+++ b/Include/classobject.h
@@ -1,5 +1,4 @@
-
-/* Class object interface */
+/* Former class object interface -- now only (un)bound methods are here */
/* Revealing some structures (not for general use) */
@@ -11,58 +10,22 @@ extern "C" {
typedef struct {
PyObject_HEAD
- PyObject *cl_bases; /* A tuple of class objects */
- PyObject *cl_dict; /* A dictionary */
- PyObject *cl_name; /* A string */
- /* The following three are functions or NULL */
- PyObject *cl_getattr;
- PyObject *cl_setattr;
- PyObject *cl_delattr;
-} PyClassObject;
-
-typedef struct {
- PyObject_HEAD
- PyClassObject *in_class; /* The class object */
- PyObject *in_dict; /* A dictionary */
- PyObject *in_weakreflist; /* List of weak references */
-} PyInstanceObject;
-
-typedef struct {
- PyObject_HEAD
PyObject *im_func; /* The callable object implementing the method */
PyObject *im_self; /* The instance it is bound to, or NULL */
PyObject *im_class; /* The class that asked for the method */
PyObject *im_weakreflist; /* List of weak references */
} PyMethodObject;
-PyAPI_DATA(PyTypeObject) PyClass_Type, PyInstance_Type, PyMethod_Type;
+PyAPI_DATA(PyTypeObject) PyMethod_Type;
-#define PyClass_Check(op) ((op)->ob_type == &PyClass_Type)
-#define PyInstance_Check(op) ((op)->ob_type == &PyInstance_Type)
#define PyMethod_Check(op) ((op)->ob_type == &PyMethod_Type)
-PyAPI_FUNC(PyObject *) PyClass_New(PyObject *, PyObject *, PyObject *);
-PyAPI_FUNC(PyObject *) PyInstance_New(PyObject *, PyObject *,
- PyObject *);
-PyAPI_FUNC(PyObject *) PyInstance_NewRaw(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyMethod_Function(PyObject *);
PyAPI_FUNC(PyObject *) PyMethod_Self(PyObject *);
PyAPI_FUNC(PyObject *) PyMethod_Class(PyObject *);
-/* Look up attribute with name (a string) on instance object pinst, using
- * only the instance and base class dicts. If a descriptor is found in
- * a class dict, the descriptor is returned without calling it.
- * Returns NULL if nothing found, else a borrowed reference to the
- * value associated with name in the dict in which name was found.
- * The point of this routine is that it never calls arbitrary Python
- * code, so is always "safe": all it does is dict lookups. The function
- * can't fail, never sets an exception, and NULL is not an error (it just
- * means "not found").
- */
-PyAPI_FUNC(PyObject *) _PyInstance_Lookup(PyObject *pinst, PyObject *name);
-
/* Macros for direct access to these values. Type checks are *not*
done, so use with care. */
#define PyMethod_GET_FUNCTION(meth) \
@@ -72,9 +35,6 @@ PyAPI_FUNC(PyObject *) _PyInstance_Lookup(PyObject *pinst, PyObject *name);
#define PyMethod_GET_CLASS(meth) \
(((PyMethodObject *)meth) -> im_class)
-PyAPI_FUNC(int) PyClass_IsSubclass(PyObject *, PyObject *);
-
-
#ifdef __cplusplus
}
#endif
diff --git a/Include/methodobject.h b/Include/methodobject.h
index c887d94..67a2473 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -63,7 +63,7 @@ PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
#define METH_CLASS 0x0010
#define METH_STATIC 0x0020
-/* METH_COEXIST allows a method to be entered eventhough a slot has
+/* METH_COEXIST allows a method to be entered even though a slot has
already filled the entry. When defined, the flag allows a separate
method, "__contains__" for example, to coexist with a defined
slot like sq_contains. */
diff --git a/Include/object.h b/Include/object.h
index f8484ba..a9a880a 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -147,7 +147,7 @@ typedef int (*visitproc)(PyObject *, void *);
typedef int (*traverseproc)(PyObject *, visitproc, void *);
typedef struct {
- /* Number implementations should check *both*
+ /* Number implementations must check *both*
arguments for proper type and implement the necessary conversions
in the slot functions themselves. */
@@ -173,7 +173,7 @@ typedef struct {
unaryfunc nb_float;
unaryfunc nb_oct;
unaryfunc nb_hex;
- /* Added in release 2.0 */
+
binaryfunc nb_inplace_add;
binaryfunc nb_inplace_subtract;
binaryfunc nb_inplace_multiply;
@@ -185,13 +185,11 @@ typedef struct {
binaryfunc nb_inplace_xor;
binaryfunc nb_inplace_or;
- /* Added in release 2.2 */
binaryfunc nb_floor_divide;
binaryfunc nb_true_divide;
binaryfunc nb_inplace_floor_divide;
binaryfunc nb_inplace_true_divide;
- /* Added in release 2.5 */
lenfunc nb_index;
} PyNumberMethods;
@@ -204,7 +202,7 @@ typedef struct {
ssizeobjargproc sq_ass_item;
ssizessizeobjargproc sq_ass_slice;
objobjproc sq_contains;
- /* Added in release 2.0 */
+
binaryfunc sq_inplace_concat;
ssizeargfunc sq_inplace_repeat;
} PySequenceMethods;
@@ -292,7 +290,6 @@ typedef struct _typeobject {
/* weak reference enabler */
Py_ssize_t tp_weaklistoffset;
- /* Added in release 2.2 */
/* Iterators */
getiterfunc tp_iter;
iternextfunc tp_iternext;
diff --git a/Include/structmember.h b/Include/structmember.h
index e761f6d..a35b7a7 100644
--- a/Include/structmember.h
+++ b/Include/structmember.h
@@ -67,9 +67,11 @@ typedef struct PyMemberDef {
converting to None. */
#ifdef HAVE_LONG_LONG
#define T_LONGLONG 17
-#define T_ULONGLONG 18
+#define T_ULONGLONG 18
#endif /* HAVE_LONG_LONG */
+#define T_NONE 19 /* Value is always None */
+
/* Flags */
#define READONLY 1
#define RO READONLY /* Shorthand */
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 68d511e..6f1e2b6 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -3363,19 +3363,19 @@ static PyMethodDef _functions[] = {
{NULL, NULL}
};
-#if PY_VERSION_HEX < 0x02030000
-DL_EXPORT(void) init_sre(void)
-#else
PyMODINIT_FUNC init_sre(void)
-#endif
{
PyObject* m;
PyObject* d;
PyObject* x;
- /* Patch object types */
- Pattern_Type.ob_type = Match_Type.ob_type =
- Scanner_Type.ob_type = &PyType_Type;
+ /* Initialize object types */
+ if (PyType_Ready(&Pattern_Type) < 0)
+ return;
+ if (PyType_Ready(&Match_Type) < 0)
+ return;
+ if (PyType_Ready(&Scanner_Type) < 0)
+ return;
m = Py_InitModule("_" SRE_MODULE, _functions);
if (m == NULL)
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index b739cc8..e44b8e1 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2112,7 +2112,8 @@ initarray(void)
{
PyObject *m;
- Arraytype.ob_type = &PyType_Type;
+ if (PyType_Ready(&Arraytype) < 0)
+ return;
PyArrayIter_Type.ob_type = &PyType_Type;
m = Py_InitModule3("array", a_methods, module_doc);
if (m == NULL)
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index 24c98cc..d6b4581 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -1786,148 +1786,6 @@ save_dict(Picklerobject *self, PyObject *args)
}
-static int
-save_inst(Picklerobject *self, PyObject *args)
-{
- PyObject *class = 0, *module = 0, *name = 0, *state = 0,
- *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
- char *module_str, *name_str;
- int module_size, name_size, res = -1;
-
- static char inst = INST, obj = OBJ, build = BUILD;
-
- if (self->fast && !fast_save_enter(self, args))
- goto finally;
-
- if (self->write_func(self, &MARKv, 1) < 0)
- goto finally;
-
- if (!( class = PyObject_GetAttr(args, __class___str)))
- goto finally;
-
- if (self->bin) {
- if (save(self, class, 0) < 0)
- goto finally;
- }
-
- if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
- PyObject *element = 0;
- int i, len;
-
- if (!( class_args =
- PyObject_Call(getinitargs_func, empty_tuple, NULL)))
- goto finally;
-
- if ((len = PyObject_Size(class_args)) < 0)
- goto finally;
-
- for (i = 0; i < len; i++) {
- if (!( element = PySequence_GetItem(class_args, i)))
- goto finally;
-
- if (save(self, element, 0) < 0) {
- Py_DECREF(element);
- goto finally;
- }
-
- Py_DECREF(element);
- }
- }
- else {
- if (PyErr_ExceptionMatches(PyExc_AttributeError))
- PyErr_Clear();
- else
- goto finally;
- }
-
- if (!self->bin) {
- if (!( name = ((PyClassObject *)class)->cl_name )) {
- PyErr_SetString(PicklingError, "class has no name");
- goto finally;
- }
-
- if (!( module = whichmodule(class, name)))
- goto finally;
-
-
- if ((module_size = PyString_Size(module)) < 0 ||
- (name_size = PyString_Size(name)) < 0)
- goto finally;
-
- module_str = PyString_AS_STRING((PyStringObject *)module);
- name_str = PyString_AS_STRING((PyStringObject *)name);
-
- if (self->write_func(self, &inst, 1) < 0)
- goto finally;
-
- if (self->write_func(self, module_str, module_size) < 0)
- goto finally;
-
- if (self->write_func(self, "\n", 1) < 0)
- goto finally;
-
- if (self->write_func(self, name_str, name_size) < 0)
- goto finally;
-
- if (self->write_func(self, "\n", 1) < 0)
- goto finally;
- }
- else if (self->write_func(self, &obj, 1) < 0) {
- goto finally;
- }
-
- if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
- state = PyObject_Call(getstate_func, empty_tuple, NULL);
- if (!state)
- goto finally;
- }
- else {
- if (PyErr_ExceptionMatches(PyExc_AttributeError))
- PyErr_Clear();
- else
- goto finally;
-
- if (!( state = PyObject_GetAttr(args, __dict___str))) {
- if (PyErr_ExceptionMatches(PyExc_AttributeError))
- PyErr_Clear();
- else
- goto finally;
- res = 0;
- goto finally;
- }
- }
-
- if (!PyDict_Check(state)) {
- if (put2(self, args) < 0)
- goto finally;
- }
- else {
- if (put(self, args) < 0)
- goto finally;
- }
-
- if (save(self, state, 0) < 0)
- goto finally;
-
- if (self->write_func(self, &build, 1) < 0)
- goto finally;
-
- res = 0;
-
- finally:
- if (self->fast && !fast_save_leave(self, args))
- res = -1;
-
- Py_XDECREF(module);
- Py_XDECREF(class);
- Py_XDECREF(state);
- Py_XDECREF(getinitargs_func);
- Py_XDECREF(getstate_func);
- Py_XDECREF(class_args);
-
- return res;
-}
-
static int
save_global(Picklerobject *self, PyObject *args, PyObject *name)
@@ -2420,20 +2278,6 @@ save(Picklerobject *self, PyObject *args, int pers_save)
}
break;
- case 'i':
- if (type == &PyInstance_Type) {
- res = save_inst(self, args);
- goto finally;
- }
- break;
-
- case 'c':
- if (type == &PyClass_Type) {
- res = save_global(self, args, NULL);
- goto finally;
- }
- break;
-
case 'f':
if (type == &PyFunction_Type) {
res = save_global(self, args, NULL);
@@ -3594,57 +3438,6 @@ load_dict(Unpicklerobject *self)
return 0;
}
-static PyObject *
-Instance_New(PyObject *cls, PyObject *args)
-{
- PyObject *r = 0;
-
- if (PyClass_Check(cls)) {
- int l;
-
- if ((l=PyObject_Size(args)) < 0) goto err;
- if (!( l )) {
- PyObject *__getinitargs__;
-
- __getinitargs__ = PyObject_GetAttr(cls,
- __getinitargs___str);
- if (!__getinitargs__) {
- /* We have a class with no __getinitargs__,
- so bypass usual construction */
- PyObject *inst;
-
- PyErr_Clear();
- if (!( inst=PyInstance_NewRaw(cls, NULL)))
- goto err;
- return inst;
- }
- Py_DECREF(__getinitargs__);
- }
-
- if ((r=PyInstance_New(cls, args, NULL))) return r;
- else goto err;
- }
-
- if ((r=PyObject_CallObject(cls, args))) return r;
-
- err:
- {
- PyObject *tp, *v, *tb, *tmp_value;
-
- PyErr_Fetch(&tp, &v, &tb);
- tmp_value = v;
- /* NULL occurs when there was a KeyboardInterrupt */
- if (tmp_value == NULL)
- tmp_value = Py_None;
- if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
- Py_XDECREF(v);
- v=r;
- }
- PyErr_Restore(tp,v,tb);
- }
- return NULL;
-}
-
static int
load_obj(Unpicklerobject *self)
@@ -3655,10 +3448,6 @@ load_obj(Unpicklerobject *self)
if ((i = marker(self)) < 0) return -1;
if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
PDATA_POP(self->stack, class);
- if (class) {
- obj = Instance_New(class, tup);
- Py_DECREF(class);
- }
Py_DECREF(tup);
if (! obj) return -1;
@@ -3694,8 +3483,8 @@ load_inst(Unpicklerobject *self)
if (! class) return -1;
if ((tup=Pdata_popTuple(self->stack, i))) {
- obj = Instance_New(class, tup);
- Py_DECREF(tup);
+ PyErr_SetString(UnpicklingError, "it's dead, Jim");
+ return -1;
}
Py_DECREF(class);
@@ -4388,10 +4177,6 @@ load_reduce(Unpicklerobject *self)
PDATA_POP(self->stack, arg_tup);
if (! arg_tup) return -1;
PDATA_POP(self->stack, callable);
- if (callable) {
- ob = Instance_New(callable, arg_tup);
- Py_DECREF(callable);
- }
Py_DECREF(arg_tup);
if (! ob) return -1;
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 872727d..5c2f381 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -66,12 +66,10 @@ static PyObject *delstr = NULL;
#define DEBUG_STATS (1<<0) /* print collection statistics */
#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
-#define DEBUG_INSTANCES (1<<3) /* print instances */
#define DEBUG_OBJECTS (1<<4) /* print other objects */
#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
#define DEBUG_LEAK DEBUG_COLLECTABLE | \
DEBUG_UNCOLLECTABLE | \
- DEBUG_INSTANCES | \
DEBUG_OBJECTS | \
DEBUG_SAVEALL
static int debug;
@@ -410,13 +408,7 @@ move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
static int
has_finalizer(PyObject *op)
{
- if (PyInstance_Check(op)) {
- assert(delstr != NULL);
- return _PyInstance_Lookup(op, delstr) != NULL;
- }
- else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
- return op->ob_type->tp_del != NULL;
- else if (PyGen_CheckExact(op))
+ if (PyGen_CheckExact(op))
return PyGen_NeedsFinalizing((PyGenObject *)op);
else
return 0;
@@ -633,26 +625,9 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
}
static void
-debug_instance(char *msg, PyInstanceObject *inst)
-{
- char *cname;
- /* simple version of instance_repr */
- PyObject *classname = inst->in_class->cl_name;
- if (classname != NULL && PyString_Check(classname))
- cname = PyString_AsString(classname);
- else
- cname = "?";
- PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
- msg, cname, inst);
-}
-
-static void
debug_cycle(char *msg, PyObject *op)
{
- if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
- debug_instance(msg, (PyInstanceObject *)op);
- }
- else if (debug & DEBUG_OBJECTS) {
+ if (debug & DEBUG_OBJECTS) {
PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
msg, op->ob_type->tp_name, op);
}
@@ -983,7 +958,6 @@ PyDoc_STRVAR(gc_set_debug__doc__,
" DEBUG_STATS - Print statistics during collection.\n"
" DEBUG_COLLECTABLE - Print collectable objects found.\n"
" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
-" DEBUG_INSTANCES - Print instance objects.\n"
" DEBUG_OBJECTS - Print objects other than instances.\n"
" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
@@ -1244,7 +1218,6 @@ initgc(void)
ADD_INT(DEBUG_STATS);
ADD_INT(DEBUG_COLLECTABLE);
ADD_INT(DEBUG_UNCOLLECTABLE);
- ADD_INT(DEBUG_INSTANCES);
ADD_INT(DEBUG_OBJECTS);
ADD_INT(DEBUG_SAVEALL);
ADD_INT(DEBUG_LEAK);
diff --git a/Objects/abstract.c b/Objects/abstract.c
index de88457..ee137fe 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1000,8 +1000,6 @@ PyNumber_Float(PyObject *o)
int
PySequence_Check(PyObject *s)
{
- if (s && PyInstance_Check(s))
- return PyObject_HasAttrString(s, "__getitem__");
return s != NULL && s->ob_type->tp_as_sequence &&
s->ob_type->tp_as_sequence->sq_item != NULL;
}
@@ -1586,9 +1584,6 @@ PySequence_Index(PyObject *s, PyObject *o)
int
PyMapping_Check(PyObject *o)
{
- if (o && PyInstance_Check(o))
- return PyObject_HasAttrString(o, "__getitem__");
-
return o && o->ob_type->tp_as_mapping &&
o->ob_type->tp_as_mapping->mp_subscript &&
!(o->ob_type->tp_as_sequence &&
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 7bcca8a..2ee71bb 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -379,13 +379,6 @@ bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value)
return 0;
}
-static long
-bytes_nohash(PyObject *self)
-{
- PyErr_SetString(PyExc_TypeError, "bytes objects are unhashable");
- return -1;
-}
-
static int
bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
{
@@ -833,7 +826,7 @@ PyTypeObject PyBytes_Type = {
0, /* tp_as_number */
&bytes_as_sequence, /* tp_as_sequence */
&bytes_as_mapping, /* tp_as_mapping */
- bytes_nohash, /* tp_hash */
+ 0, /* tp_hash */
0, /* tp_call */
(reprfunc)bytes_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 2d80073..cc09960 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1,5 +1,4 @@
-
-/* Class object implementation */
+/* Class object implementation (dead now except for methods) */
#include "Python.h"
#include "structmember.h"
@@ -7,114 +6,6 @@
#define TP_DESCR_GET(t) ((t)->tp_descr_get)
-/* Forward */
-static PyObject *class_lookup(PyClassObject *, PyObject *,
- PyClassObject **);
-static PyObject *instance_getattr1(PyInstanceObject *, PyObject *);
-static PyObject *instance_getattr2(PyInstanceObject *, PyObject *);
-
-static PyObject *getattrstr, *setattrstr, *delattrstr;
-
-
-PyObject *
-PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
- /* bases is NULL or tuple of classobjects! */
-{
- PyClassObject *op, *dummy;
- static PyObject *docstr, *modstr, *namestr;
- if (docstr == NULL) {
- docstr= PyString_InternFromString("__doc__");
- if (docstr == NULL)
- return NULL;
- }
- if (modstr == NULL) {
- modstr= PyString_InternFromString("__module__");
- if (modstr == NULL)
- return NULL;
- }
- if (namestr == NULL) {
- namestr= PyString_InternFromString("__name__");
- if (namestr == NULL)
- return NULL;
- }
- if (name == NULL || !PyString_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "PyClass_New: name must be a string");
- return NULL;
- }
- if (dict == NULL || !PyDict_Check(dict)) {
- PyErr_SetString(PyExc_TypeError,
- "PyClass_New: dict must be a dictionary");
- return NULL;
- }
- if (PyDict_GetItem(dict, docstr) == NULL) {
- if (PyDict_SetItem(dict, docstr, Py_None) < 0)
- return NULL;
- }
- if (PyDict_GetItem(dict, modstr) == NULL) {
- PyObject *globals = PyEval_GetGlobals();
- if (globals != NULL) {
- PyObject *modname = PyDict_GetItem(globals, namestr);
- if (modname != NULL) {
- if (PyDict_SetItem(dict, modstr, modname) < 0)
- return NULL;
- }
- }
- }
- if (bases == NULL) {
- bases = PyTuple_New(0);
- if (bases == NULL)
- return NULL;
- }
- else {
- Py_ssize_t i, n;
- PyObject *base;
- if (!PyTuple_Check(bases)) {
- PyErr_SetString(PyExc_TypeError,
- "PyClass_New: bases must be a tuple");
- return NULL;
- }
- 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))
- return PyObject_CallFunctionObjArgs(
- (PyObject *) base->ob_type,
- name, bases, dict, NULL);
- PyErr_SetString(PyExc_TypeError,
- "PyClass_New: base must be a class");
- return NULL;
- }
- }
- Py_INCREF(bases);
- }
- op = PyObject_GC_New(PyClassObject, &PyClass_Type);
- if (op == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
- op->cl_bases = bases;
- Py_INCREF(dict);
- op->cl_dict = dict;
- Py_XINCREF(name);
- op->cl_name = name;
- if (getattrstr == NULL) {
- getattrstr = PyString_InternFromString("__getattr__");
- setattrstr = PyString_InternFromString("__setattr__");
- delattrstr = PyString_InternFromString("__delattr__");
- }
- op->cl_getattr = class_lookup(op, getattrstr, &dummy);
- op->cl_setattr = class_lookup(op, setattrstr, &dummy);
- op->cl_delattr = class_lookup(op, delattrstr, &dummy);
- Py_XINCREF(op->cl_getattr);
- Py_XINCREF(op->cl_setattr);
- Py_XINCREF(op->cl_delattr);
- _PyObject_GC_TRACK(op);
- return (PyObject *) op;
-}
-
PyObject *
PyMethod_Function(PyObject *im)
{
@@ -145,1929 +36,8 @@ PyMethod_Class(PyObject *im)
return ((PyMethodObject *)im)->im_class;
}
-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.");
-
-static PyObject *
-class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
- PyObject *name, *bases, *dict;
- static char *kwlist[] = {"name", "bases", "dict", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
- &name, &bases, &dict))
- return NULL;
- return PyClass_New(bases, dict, name);
-}
-
-/* Class methods */
-
-static void
-class_dealloc(PyClassObject *op)
-{
- _PyObject_GC_UNTRACK(op);
- Py_DECREF(op->cl_bases);
- Py_DECREF(op->cl_dict);
- Py_XDECREF(op->cl_name);
- Py_XDECREF(op->cl_getattr);
- Py_XDECREF(op->cl_setattr);
- Py_XDECREF(op->cl_delattr);
- PyObject_GC_Del(op);
-}
-
-static PyObject *
-class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass)
-{
- Py_ssize_t i, n;
- PyObject *value = PyDict_GetItem(cp->cl_dict, name);
- if (value != NULL) {
- *pclass = cp;
- return value;
- }
- n = PyTuple_Size(cp->cl_bases);
- for (i = 0; i < n; i++) {
- /* XXX What if one of the bases is not a class? */
- PyObject *v = class_lookup(
- (PyClassObject *)
- PyTuple_GetItem(cp->cl_bases, i), name, pclass);
- if (v != NULL)
- return v;
- }
- return NULL;
-}
-
-static PyObject *
-class_getattr(register PyClassObject *op, PyObject *name)
-{
- register PyObject *v;
- register char *sname = PyString_AsString(name);
- PyClassObject *klass;
- descrgetfunc f;
-
- if (sname[0] == '_' && sname[1] == '_') {
- if (strcmp(sname, "__dict__") == 0) {
- if (PyEval_GetRestricted()) {
- PyErr_SetString(PyExc_RuntimeError,
- "class.__dict__ not accessible in restricted mode");
- return NULL;
- }
- Py_INCREF(op->cl_dict);
- return op->cl_dict;
- }
- if (strcmp(sname, "__bases__") == 0) {
- Py_INCREF(op->cl_bases);
- return op->cl_bases;
- }
- if (strcmp(sname, "__name__") == 0) {
- if (op->cl_name == NULL)
- v = Py_None;
- else
- v = op->cl_name;
- Py_INCREF(v);
- return v;
- }
- }
- v = class_lookup(op, name, &klass);
- if (v == NULL) {
- PyErr_Format(PyExc_AttributeError,
- "class %.50s has no attribute '%.400s'",
- PyString_AS_STRING(op->cl_name), sname);
- return NULL;
- }
- f = TP_DESCR_GET(v->ob_type);
- if (f == NULL)
- Py_INCREF(v);
- else
- v = f(v, (PyObject *)NULL, (PyObject *)op);
- return v;
-}
-
-static void
-set_slot(PyObject **slot, PyObject *v)
-{
- PyObject *temp = *slot;
- Py_XINCREF(v);
- *slot = v;
- Py_XDECREF(temp);
-}
-
-static void
-set_attr_slots(PyClassObject *c)
-{
- 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));
-}
-
-static char *
-set_dict(PyClassObject *c, PyObject *v)
-{
- if (v == NULL || !PyDict_Check(v))
- return "__dict__ must be a dictionary object";
- set_slot(&c->cl_dict, v);
- set_attr_slots(c);
- return "";
-}
-
-static char *
-set_bases(PyClassObject *c, PyObject *v)
-{
- Py_ssize_t i, n;
-
- 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);
- set_attr_slots(c);
- return "";
-}
-
-static char *
-set_name(PyClassObject *c, PyObject *v)
-{
- if (v == NULL || !PyString_Check(v))
- return "__name__ must be a string object";
- if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v))
- return "__name__ must not contain null bytes";
- set_slot(&c->cl_name, v);
- return "";
-}
-
-static int
-class_setattr(PyClassObject *op, PyObject *name, PyObject *v)
-{
- char *sname;
- if (PyEval_GetRestricted()) {
- PyErr_SetString(PyExc_RuntimeError,
- "classes are read-only in restricted mode");
- return -1;
- }
- sname = PyString_AsString(name);
- if (sname[0] == '_' && sname[1] == '_') {
- Py_ssize_t n = PyString_Size(name);
- if (sname[n-1] == '_' && sname[n-2] == '_') {
- 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);
- return -1;
- }
- }
- }
- if (v == NULL) {
- int rv = PyDict_DelItem(op->cl_dict, name);
- if (rv < 0)
- PyErr_Format(PyExc_AttributeError,
- "class %.50s has no attribute '%.400s'",
- PyString_AS_STRING(op->cl_name), sname);
- return rv;
- }
- else
- return PyDict_SetItem(op->cl_dict, name, v);
-}
-
-static PyObject *
-class_repr(PyClassObject *op)
-{
- PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
- char *name;
- if (op->cl_name == NULL || !PyString_Check(op->cl_name))
- name = "?";
- else
- name = PyString_AsString(op->cl_name);
- if (mod == NULL || !PyString_Check(mod))
- return PyString_FromFormat("<class ?.%s at %p>", name, op);
- else
- return PyString_FromFormat("<class %s.%s at %p>",
- PyString_AsString(mod),
- name, op);
-}
-
-static PyObject *
-class_str(PyClassObject *op)
-{
- PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
- PyObject *name = op->cl_name;
- PyObject *res;
- Py_ssize_t m, n;
-
- if (name == NULL || !PyString_Check(name))
- return class_repr(op);
- if (mod == NULL || !PyString_Check(mod)) {
- Py_INCREF(name);
- return name;
- }
- m = PyString_GET_SIZE(mod);
- n = PyString_GET_SIZE(name);
- res = PyString_FromStringAndSize((char *)NULL, m+1+n);
- if (res != NULL) {
- char *s = PyString_AS_STRING(res);
- memcpy(s, PyString_AS_STRING(mod), m);
- s += m;
- *s++ = '.';
- memcpy(s, PyString_AS_STRING(name), n);
- }
- return res;
-}
-
-static int
-class_traverse(PyClassObject *o, visitproc visit, void *arg)
-{
- 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);
- return 0;
-}
-
-PyTypeObject PyClass_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0,
- "classobj",
- sizeof(PyClassObject),
- 0,
- (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 */
- PyInstance_New, /* tp_call */
- (reprfunc)class_str, /* tp_str */
- (getattrofunc)class_getattr, /* tp_getattro */
- (setattrofunc)class_setattr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- class_doc, /* tp_doc */
- (traverseproc)class_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- class_new, /* tp_new */
-};
-
-int
-PyClass_IsSubclass(PyObject *klass, PyObject *base)
-{
- Py_ssize_t i, n;
- PyClassObject *cp;
- if (klass == base)
- return 1;
- if (PyTuple_Check(base)) {
- n = PyTuple_GET_SIZE(base);
- for (i = 0; i < n; i++) {
- if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i)))
- return 1;
- }
- return 0;
- }
- if (klass == NULL || !PyClass_Check(klass))
- return 0;
- cp = (PyClassObject *)klass;
- n = PyTuple_Size(cp->cl_bases);
- for (i = 0; i < n; i++) {
- if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
- return 1;
- }
- return 0;
-}
-
-
-/* Instance objects */
-
-PyObject *
-PyInstance_NewRaw(PyObject *klass, PyObject *dict)
-{
- PyInstanceObject *inst;
-
- if (!PyClass_Check(klass)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (dict == NULL) {
- dict = PyDict_New();
- if (dict == NULL)
- return NULL;
- }
- else {
- if (!PyDict_Check(dict)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- Py_INCREF(dict);
- }
- inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);
- if (inst == NULL) {
- Py_DECREF(dict);
- return NULL;
- }
- inst->in_weakreflist = NULL;
- Py_INCREF(klass);
- inst->in_class = (PyClassObject *)klass;
- inst->in_dict = dict;
- _PyObject_GC_TRACK(inst);
- 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;
- if (initstr == NULL)
- initstr = PyString_InternFromString("__init__");
- init = instance_getattr2(inst, initstr);
- if (init == NULL) {
- if (PyErr_Occurred()) {
- Py_DECREF(inst);
- return NULL;
- }
- if ((arg != NULL && (!PyTuple_Check(arg) ||
- PyTuple_Size(arg) != 0))
- || (kw != NULL && (!PyDict_Check(kw) ||
- PyDict_Size(kw) != 0))) {
- PyErr_SetString(PyExc_TypeError,
- "this constructor takes no arguments");
- Py_DECREF(inst);
- inst = NULL;
- }
- }
- else {
- PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
- Py_DECREF(init);
- if (res == NULL) {
- Py_DECREF(inst);
- inst = NULL;
- }
- else {
- if (res != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "__init__() should return None");
- Py_DECREF(inst);
- inst = NULL;
- }
- Py_DECREF(res);
- }
- }
- return (PyObject *)inst;
-}
-
-/* Instance methods */
-
-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);
-}
-
-
-static void
-instance_dealloc(register PyInstanceObject *inst)
-{
- PyObject *error_type, *error_value, *error_traceback;
- PyObject *del;
- static PyObject *delstr;
-
- _PyObject_GC_UNTRACK(inst);
- if (inst->in_weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) inst);
-
- /* Temporarily resurrect the object. */
- assert(inst->ob_type == &PyInstance_Type);
- assert(inst->ob_refcnt == 0);
- inst->ob_refcnt = 1;
-
- /* Save the current exception, if any. */
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
- /* Execute __del__ method, if any. */
- if (delstr == NULL)
- delstr = PyString_InternFromString("__del__");
- if ((del = instance_getattr2(inst, delstr)) != NULL) {
- PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
- if (res == NULL)
- PyErr_WriteUnraisable(del);
- else
- Py_DECREF(res);
- Py_DECREF(del);
- }
- /* Restore the saved exception. */
- PyErr_Restore(error_type, error_value, error_traceback);
-
- /* Undo the temporary resurrection; can't use DECREF here, it would
- * cause a recursive call.
- */
- assert(inst->ob_refcnt > 0);
- if (--inst->ob_refcnt == 0) {
- Py_DECREF(inst->in_class);
- Py_XDECREF(inst->in_dict);
- PyObject_GC_Del(inst);
- }
- else {
- Py_ssize_t refcnt = inst->ob_refcnt;
- /* __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);
- /* 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.
- * If COUNT_ALLOCS, the original decref bumped tp_frees, and
- * _Py_NewReference bumped tp_allocs: both of those need to be
- * undone.
- */
-#ifdef COUNT_ALLOCS
- --inst->ob_type->tp_frees;
- --inst->ob_type->tp_allocs;
-#endif
- }
-}
-
-static PyObject *
-instance_getattr1(register PyInstanceObject *inst, PyObject *name)
-{
- register PyObject *v;
- register char *sname = PyString_AsString(name);
- if (sname[0] == '_' && sname[1] == '_') {
- if (strcmp(sname, "__dict__") == 0) {
- if (PyEval_GetRestricted()) {
- PyErr_SetString(PyExc_RuntimeError,
- "instance.__dict__ not accessible in restricted mode");
- return NULL;
- }
- Py_INCREF(inst->in_dict);
- return inst->in_dict;
- }
- if (strcmp(sname, "__class__") == 0) {
- Py_INCREF(inst->in_class);
- return (PyObject *)inst->in_class;
- }
- }
- v = instance_getattr2(inst, name);
- if (v == NULL && !PyErr_Occurred()) {
- PyErr_Format(PyExc_AttributeError,
- "%.50s instance has no attribute '%.400s'",
- PyString_AS_STRING(inst->in_class->cl_name), sname);
- }
- return v;
-}
-
-static PyObject *
-instance_getattr2(register PyInstanceObject *inst, PyObject *name)
-{
- register PyObject *v;
- PyClassObject *klass;
- descrgetfunc f;
-
- v = PyDict_GetItem(inst->in_dict, name);
- if (v != NULL) {
- Py_INCREF(v);
- return v;
- }
- v = class_lookup(inst->in_class, name, &klass);
- if (v != NULL) {
- Py_INCREF(v);
- f = TP_DESCR_GET(v->ob_type);
- if (f != NULL) {
- PyObject *w = f(v, (PyObject *)inst,
- (PyObject *)(inst->in_class));
- Py_DECREF(v);
- v = w;
- }
- }
- return v;
-}
-
-static PyObject *
-instance_getattr(register PyInstanceObject *inst, PyObject *name)
-{
- register PyObject *func, *res;
- res = instance_getattr1(inst, name);
- if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
- PyObject *args;
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- args = PyTuple_Pack(2, inst, name);
- if (args == NULL)
- return NULL;
- res = PyEval_CallObject(func, args);
- Py_DECREF(args);
- }
- return res;
-}
-
-/* See classobject.h comments: this only does dict lookups, and is always
- * safe to call.
- */
-PyObject *
-_PyInstance_Lookup(PyObject *pinst, PyObject *name)
-{
- PyObject *v;
- PyClassObject *klass;
- 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)
- v = class_lookup(inst->in_class, name, &klass);
- return v;
-}
-
-static int
-instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
-{
- if (v == NULL) {
- int rv = PyDict_DelItem(inst->in_dict, name);
- if (rv < 0)
- PyErr_Format(PyExc_AttributeError,
- "%.50s instance has no attribute '%.400s'",
- PyString_AS_STRING(inst->in_class->cl_name),
- PyString_AS_STRING(name));
- return rv;
- }
- else
- return PyDict_SetItem(inst->in_dict, name, v);
-}
-
-static int
-instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
-{
- PyObject *func, *args, *res, *tmp;
- char *sname = PyString_AsString(name);
- if (sname[0] == '_' && sname[1] == '_') {
- Py_ssize_t n = PyString_Size(name);
- if (sname[n-1] == '_' && sname[n-2] == '_') {
- 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;
- }
- }
- }
- 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)
- args = PyTuple_Pack(2, inst, name);
- else
- args = PyTuple_Pack(3, inst, name, v);
- if (args == NULL)
- return -1;
- res = PyEval_CallObject(func, args);
- Py_DECREF(args);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
-}
-
-static PyObject *
-instance_repr(PyInstanceObject *inst)
-{
- PyObject *func;
- PyObject *res;
- static PyObject *reprstr;
-
- if (reprstr == NULL)
- reprstr = PyString_InternFromString("__repr__");
- func = instance_getattr(inst, reprstr);
- if (func == NULL) {
- PyObject *classname, *mod;
- char *cname;
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- classname = inst->in_class->cl_name;
- mod = PyDict_GetItemString(inst->in_class->cl_dict,
- "__module__");
- if (classname != NULL && PyString_Check(classname))
- cname = PyString_AsString(classname);
- else
- cname = "?";
- if (mod == NULL || !PyString_Check(mod))
- return PyString_FromFormat("<?.%s instance at %p>",
- cname, inst);
- else
- return PyString_FromFormat("<%s.%s instance at %p>",
- PyString_AsString(mod),
- cname, inst);
- }
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- return res;
-}
-
-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) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- return instance_repr(inst);
- }
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- return res;
-}
-
-static long
-instance_hash(PyInstanceObject *inst)
-{
- PyObject *func;
- PyObject *res;
- long outcome;
- static PyObject *hashstr, *eqstr, *cmpstr;
-
- if (hashstr == NULL)
- hashstr = PyString_InternFromString("__hash__");
- func = instance_getattr(inst, hashstr);
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- /* 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);
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- if (cmpstr == NULL)
- cmpstr = PyString_InternFromString("__cmp__");
- func = instance_getattr(inst, cmpstr);
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(
- PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- return _Py_HashPointer(inst);
- }
- }
- Py_XDECREF(func);
- PyErr_SetString(PyExc_TypeError, "unhashable instance");
- return -1;
- }
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- if (res == NULL)
- return -1;
- if (PyInt_Check(res) || PyLong_Check(res))
- /* This already converts a -1 result to -2. */
- outcome = res->ob_type->tp_hash(res);
- else {
- PyErr_SetString(PyExc_TypeError,
- "__hash__() should return an int");
- outcome = -1;
- }
- Py_DECREF(res);
- return outcome;
-}
-
-static int
-instance_traverse(PyInstanceObject *o, visitproc visit, void *arg)
-{
- Py_VISIT(o->in_class);
- Py_VISIT(o->in_dict);
- return 0;
-}
-
-static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
-static PyObject *iterstr, *nextstr;
-
-static Py_ssize_t
-instance_length(PyInstanceObject *inst)
-{
- PyObject *func;
- PyObject *res;
- Py_ssize_t outcome;
-
- if (lenstr == NULL)
- lenstr = PyString_InternFromString("__len__");
- func = instance_getattr(inst, lenstr);
- if (func == NULL)
- return -1;
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- if (res == NULL)
- return -1;
- if (PyInt_Check(res)) {
- 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
- /* Overflow check -- range of PyInt is more than C int */
- if (outcome != temp) {
- PyErr_SetString(PyExc_OverflowError,
- "__len__() should return 0 <= outcome < 2**31");
- outcome = -1;
- }
- else
-#endif
- if (outcome < 0)
- PyErr_SetString(PyExc_ValueError,
- "__len__() should return >= 0");
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "__len__() should return an int");
- outcome = -1;
- }
- Py_DECREF(res);
- return outcome;
-}
-
-static PyObject *
-instance_subscript(PyInstanceObject *inst, PyObject *key)
-{
- PyObject *func;
- PyObject *arg;
- PyObject *res;
-
- if (getitemstr == NULL)
- getitemstr = PyString_InternFromString("__getitem__");
- func = instance_getattr(inst, getitemstr);
- if (func == NULL)
- return NULL;
- arg = PyTuple_Pack(1, key);
- if (arg == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- return res;
-}
-
-static int
-instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
-{
- PyObject *func;
- PyObject *arg;
- PyObject *res;
-
- if (value == NULL) {
- if (delitemstr == NULL)
- delitemstr = PyString_InternFromString("__delitem__");
- func = instance_getattr(inst, delitemstr);
- }
- else {
- if (setitemstr == NULL)
- setitemstr = PyString_InternFromString("__setitem__");
- func = instance_getattr(inst, setitemstr);
- }
- if (func == NULL)
- return -1;
- if (value == NULL)
- arg = PyTuple_Pack(1, key);
- else
- arg = PyTuple_Pack(2, key, value);
- if (arg == NULL) {
- Py_DECREF(func);
- return -1;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
-}
-
-static PyMappingMethods instance_as_mapping = {
- (lenfunc)instance_length, /* mp_length */
- (binaryfunc)instance_subscript, /* mp_subscript */
- (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */
-};
-
-static PyObject *
-instance_item(PyInstanceObject *inst, Py_ssize_t i)
-{
- PyObject *func, *res;
-
- if (getitemstr == NULL)
- getitemstr = PyString_InternFromString("__getitem__");
- func = instance_getattr(inst, getitemstr);
- if (func == NULL)
- return NULL;
- res = PyObject_CallFunction(func, "n", i);
- Py_DECREF(func);
- return res;
-}
-
-static PyObject *
-instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
-{
- PyObject *func, *arg, *res;
- static PyObject *getslicestr;
-
- if (getslicestr == NULL)
- getslicestr = PyString_InternFromString("__getslice__");
- func = instance_getattr(inst, getslicestr);
-
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
-
- if (getitemstr == NULL)
- getitemstr = PyString_InternFromString("__getitem__");
- func = instance_getattr(inst, getitemstr);
- if (func == NULL)
- return NULL;
- arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j));
- } else
- arg = Py_BuildValue("(nn)", i, j);
-
- if (arg == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- return res;
-}
-
-static int
-instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
-{
- PyObject *func, *arg, *res;
-
- if (item == NULL) {
- if (delitemstr == NULL)
- delitemstr = PyString_InternFromString("__delitem__");
- func = instance_getattr(inst, delitemstr);
- }
- else {
- if (setitemstr == NULL)
- setitemstr = PyString_InternFromString("__setitem__");
- func = instance_getattr(inst, setitemstr);
- }
- if (func == NULL)
- return -1;
- if (item == NULL)
- arg = PyInt_FromSsize_t(i);
- else
- arg = Py_BuildValue("(nO)", i, item);
- if (arg == NULL) {
- Py_DECREF(func);
- return -1;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
-}
-
-static int
-instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value)
-{
- PyObject *func, *arg, *res;
- static PyObject *setslicestr, *delslicestr;
-
- if (value == NULL) {
- if (delslicestr == NULL)
- delslicestr =
- PyString_InternFromString("__delslice__");
- func = instance_getattr(inst, delslicestr);
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- if (delitemstr == NULL)
- delitemstr =
- PyString_InternFromString("__delitem__");
- func = instance_getattr(inst, delitemstr);
- if (func == NULL)
- return -1;
-
- arg = Py_BuildValue("(N)",
- _PySlice_FromIndices(i, j));
- } else
- arg = Py_BuildValue("(nn)", i, j);
- }
- else {
- if (setslicestr == NULL)
- setslicestr =
- PyString_InternFromString("__setslice__");
- func = instance_getattr(inst, setslicestr);
- if (func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- if (setitemstr == NULL)
- setitemstr =
- PyString_InternFromString("__setitem__");
- func = instance_getattr(inst, setitemstr);
- if (func == NULL)
- return -1;
-
- arg = Py_BuildValue("(NO)",
- _PySlice_FromIndices(i, j), value);
- } else
- arg = Py_BuildValue("(nnO)", i, j, value);
- }
- if (arg == NULL) {
- Py_DECREF(func);
- return -1;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
-}
-
-static int
-instance_contains(PyInstanceObject *inst, PyObject *member)
-{
- static PyObject *__contains__;
- PyObject *func;
-
- /* Try __contains__ first.
- * If that can't be done, try iterator-based searching.
- */
-
- if(__contains__ == NULL) {
- __contains__ = PyString_InternFromString("__contains__");
- if(__contains__ == NULL)
- return -1;
- }
- func = instance_getattr(inst, __contains__);
- if (func) {
- PyObject *res;
- int ret;
- PyObject *arg = PyTuple_Pack(1, member);
- if(arg == NULL) {
- Py_DECREF(func);
- return -1;
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(func);
- Py_DECREF(arg);
- if(res == NULL)
- return -1;
- ret = PyObject_IsTrue(res);
- Py_DECREF(res);
- return ret;
- }
-
- /* 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();
- return _PySequence_IterSearch((PyObject *)inst, member,
- PY_ITERSEARCH_CONTAINS) > 0;
- }
- else
- return -1;
-}
-
-static PySequenceMethods
-instance_as_sequence = {
- (lenfunc)instance_length, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- (ssizeargfunc)instance_item, /* sq_item */
- (ssizessizeargfunc)instance_slice, /* sq_slice */
- (ssizeobjargproc)instance_ass_item, /* sq_ass_item */
- (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */
- (objobjproc)instance_contains, /* sq_contains */
-};
-
-static PyObject *
-generic_unary_op(PyInstanceObject *self, PyObject *methodname)
-{
- PyObject *func, *res;
-
- if ((func = instance_getattr(self, methodname)) == NULL)
- return NULL;
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- return res;
-}
-
-static PyObject *
-generic_binary_op(PyObject *v, PyObject *w, char *opname)
-{
- 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;
- }
- args = PyTuple_Pack(1, w);
- if (args == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- result = PyEval_CallObject(func, args);
- Py_DECREF(args);
- Py_DECREF(func);
- return result;
-}
-
-
-static PyObject *coerce_obj;
-
-/* Try one half of a binary operator involving a class instance. */
-static PyObject *
-half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
- int swapped)
-{
- PyObject *args;
- PyObject *coercefunc;
- PyObject *coerced = NULL;
- PyObject *v1;
- PyObject *result;
-
- if (!PyInstance_Check(v)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- if (coerce_obj == NULL) {
- coerce_obj = PyString_InternFromString("__coerce__");
- if (coerce_obj == NULL)
- return NULL;
- }
- coercefunc = PyObject_GetAttr(v, coerce_obj);
- if (coercefunc == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- return generic_binary_op(v, w, opname);
- }
-
- args = PyTuple_Pack(1, w);
- if (args == NULL) {
- Py_DECREF(coercefunc);
- return NULL;
- }
- coerced = PyEval_CallObject(coercefunc, args);
- Py_DECREF(args);
- 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);
- if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
- /* prevent recursion if __coerce__ returns self as the first
- * argument */
- result = generic_binary_op(v1, w, opname);
- } else {
- if (Py_EnterRecursiveCall(" after coercion"))
- return NULL;
- if (swapped)
- result = (thisfunc)(w, v1);
- else
- result = (thisfunc)(v1, w);
- Py_LeaveRecursiveCall();
- }
- 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) {
- Py_DECREF(result);
- 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) {
- Py_DECREF(result);
- result = do_binop(v, w, opname, ropname, thisfunc);
- }
- return result;
-}
-
-static int
-instance_coerce(PyObject **pv, PyObject **pw)
-{
- PyObject *v = *pv;
- PyObject *w = *pw;
- PyObject *coercefunc;
- PyObject *args;
- PyObject *coerced;
-
- if (coerce_obj == NULL) {
- coerce_obj = PyString_InternFromString("__coerce__");
- if (coerce_obj == NULL)
- return -1;
- }
- coercefunc = PyObject_GetAttr(v, coerce_obj);
- if (coercefunc == NULL) {
- /* No __coerce__ method */
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- return 1;
- }
- /* Has __coerce__ method: call it */
- args = PyTuple_Pack(1, w);
- if (args == NULL) {
- return -1;
- }
- coerced = PyEval_CallObject(coercefunc, args);
- Py_DECREF(args);
- Py_DECREF(coercefunc);
- if (coerced == NULL) {
- /* __coerce__ call raised an exception */
- return -1;
- }
- if (coerced == Py_None || coerced == Py_NotImplemented) {
- /* __coerce__ says "I can't do it" */
- Py_DECREF(coerced);
- return 1;
- }
- if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
- /* __coerce__ return value is malformed */
- Py_DECREF(coerced);
- PyErr_SetString(PyExc_TypeError,
- "coercion should return None or 2-tuple");
- return -1;
- }
- /* __coerce__ returned two new values */
- *pv = PyTuple_GetItem(coerced, 0);
- *pw = PyTuple_GetItem(coerced, 1);
- Py_INCREF(*pv);
- Py_INCREF(*pw);
- Py_DECREF(coerced);
- return 0;
-}
-
-#define UNARY(funcname, methodname) \
-static PyObject *funcname(PyInstanceObject *self) { \
- static PyObject *o; \
- if (o == NULL) o = PyString_InternFromString(methodname); \
- return generic_unary_op(self, o); \
-}
-
-#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); \
-}
-
-UNARY(instance_neg, "__neg__")
-UNARY(instance_pos, "__pos__")
-UNARY(instance_abs, "__abs__")
-
-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_mod, "mod", PyNumber_Remainder)
-BINARY(instance_divmod, "divmod", PyNumber_Divmod)
-BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
-BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
-
-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_imod, "mod", PyNumber_InPlaceRemainder)
-BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
-BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
-
-/* 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)
-{
- static PyObject *cmp_obj;
- PyObject *args;
- PyObject *cmp_func;
- PyObject *result;
- long l;
-
- assert(PyInstance_Check(v));
-
- if (cmp_obj == NULL) {
- cmp_obj = PyString_InternFromString("__cmp__");
- if (cmp_obj == NULL)
- return -2;
- }
-
- cmp_func = PyObject_GetAttr(v, cmp_obj);
- if (cmp_func == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -2;
- PyErr_Clear();
- return 2;
- }
-
- args = PyTuple_Pack(1, w);
- if (args == NULL) {
- Py_DECREF(cmp_func);
- return -2;
- }
-
- result = PyEval_CallObject(cmp_func, args);
- Py_DECREF(args);
- Py_DECREF(cmp_func);
-
- if (result == NULL)
- return -2;
-
- if (result == Py_NotImplemented) {
- Py_DECREF(result);
- return 2;
- }
-
- 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;
-}
-
-/* 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!
-*/
-static int
-instance_compare(PyObject *v, PyObject *w)
-{
- 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;
- }
- }
- else {
- /* The coercion didn't do anything.
- Treat this the same as returning v and w unchanged. */
- Py_INCREF(v);
- Py_INCREF(w);
- }
-
- if (PyInstance_Check(v)) {
- c = half_cmp(v, w);
- if (c <= 1) {
- Py_DECREF(v);
- Py_DECREF(w);
- return c;
- }
- }
- 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;
-}
-
-static int
-instance_nonzero(PyInstanceObject *self)
-{
- PyObject *func, *res;
- long outcome;
- static PyObject *nonzerostr;
-
- if (nonzerostr == NULL)
- nonzerostr = PyString_InternFromString("__nonzero__");
- if ((func = instance_getattr(self, nonzerostr)) == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- if (lenstr == NULL)
- lenstr = PyString_InternFromString("__len__");
- if ((func = instance_getattr(self, lenstr)) == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
- /* Fall back to the default behavior:
- all instances are nonzero */
- return 1;
- }
- }
- res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- if (res == NULL)
- return -1;
- if (!PyInt_Check(res)) {
- Py_DECREF(res);
- PyErr_SetString(PyExc_TypeError,
- "__nonzero__ should return an int");
- return -1;
- }
- outcome = PyInt_AsLong(res);
- Py_DECREF(res);
- if (outcome < 0) {
- PyErr_SetString(PyExc_ValueError,
- "__nonzero__ should return >= 0");
- return -1;
- }
- return outcome > 0;
-}
-
-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;
-}
-
-
-UNARY(instance_invert, "__invert__")
-UNARY(instance_int, "__int__")
-UNARY(instance_long, "__long__")
-UNARY(instance_float, "__float__")
-UNARY(instance_oct, "__oct__")
-UNARY(instance_hex, "__hex__")
-
-static PyObject *
-bin_power(PyObject *v, PyObject *w)
-{
- return PyNumber_Power(v, w, Py_None);
-}
-
-/* This version is for ternary calls only (z != None) */
-static PyObject *
-instance_pow(PyObject *v, PyObject *w, PyObject *z)
-{
- if (z == Py_None) {
- return do_binop(v, w, "__pow__", "__rpow__", bin_power);
- }
- else {
- PyObject *func;
- PyObject *args;
- PyObject *result;
-
- /* XXX Doesn't do coercions... */
- func = PyObject_GetAttrString(v, "__pow__");
- if (func == NULL)
- return NULL;
- args = PyTuple_Pack(2, w, z);
- if (args == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- result = PyEval_CallObject(func, args);
- Py_DECREF(func);
- Py_DECREF(args);
- return result;
- }
-}
-
-static PyObject *
-bin_inplace_power(PyObject *v, PyObject *w)
-{
- return PyNumber_InPlacePower(v, w, Py_None);
-}
-
-
-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);
- }
- 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);
- }
- args = PyTuple_Pack(2, w, z);
- if (args == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- result = PyEval_CallObject(func, args);
- Py_DECREF(func);
- Py_DECREF(args);
- return result;
- }
-}
-
-
-/* Map rich comparison operators to their __xx__ namesakes */
-#define NAME_OPS 6
-static PyObject **name_op = NULL;
-
-static int
-init_name_op(void)
-{
- 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;
-}
-
-static PyObject *
-half_richcompare(PyObject *v, PyObject *w, int op)
-{
- PyObject *method;
- PyObject *args;
- PyObject *res;
-
- assert(PyInstance_Check(v));
-
- if (name_op == NULL) {
- if (init_name_op() < 0)
- return NULL;
- }
- /* If the instance doesn't define an __getattr__ method, use
- instance_getattr2 directly because it will not set an
- exception on failure. */
- if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL)
- method = instance_getattr2((PyInstanceObject *)v,
- name_op[op]);
- else
- method = PyObject_GetAttr(v, name_op[op]);
- if (method == NULL) {
- if (PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- }
- res = Py_NotImplemented;
- Py_INCREF(res);
- return res;
- }
-
- args = PyTuple_Pack(1, w);
- 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)) {
- res = half_richcompare(w, v, _Py_SwappedOp[op]);
- if (res != Py_NotImplemented)
- return res;
- Py_DECREF(res);
- }
-
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
-}
-
-
-/* Get the iterator */
-static PyObject *
-instance_getiter(PyInstanceObject *self)
-{
- PyObject *func;
-
- if (iterstr == NULL) {
- iterstr = PyString_InternFromString("__iter__");
- if (iterstr == NULL)
- return NULL;
- }
- if (getitemstr == NULL) {
- getitemstr = PyString_InternFromString("__getitem__");
- if (getitemstr == NULL)
- return NULL;
- }
-
- if ((func = instance_getattr(self, iterstr)) != NULL) {
- PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
- Py_DECREF(func);
- 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;
- }
- return res;
- }
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- if ((func = instance_getattr(self, getitemstr)) == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "iteration over non-sequence");
- return NULL;
- }
- Py_DECREF(func);
- 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;
-}
-
-static PyObject *
-instance_call(PyObject *func, PyObject *arg, PyObject *kw)
-{
- PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
- if (call == NULL) {
- PyInstanceObject *inst = (PyInstanceObject*) func;
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- PyErr_Format(PyExc_AttributeError,
- "%.200s instance has no __call__ method",
- PyString_AsString(inst->in_class->cl_name));
- return NULL;
- }
- /* 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). */
- if (Py_EnterRecursiveCall(" in __call__")) {
- res = NULL;
- }
- else {
- res = PyObject_Call(call, arg, kw);
- Py_LeaveRecursiveCall();
- }
- Py_DECREF(call);
- return res;
-}
-
-
-static PyNumberMethods instance_as_number = {
- instance_add, /* nb_add */
- instance_sub, /* nb_subtract */
- instance_mul, /* nb_multiply */
- 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_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 */
-};
-
-PyTypeObject PyInstance_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0,
- "instance",
- sizeof(PyInstanceObject),
- 0,
- (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 */
- instance_call, /* tp_call */
- (reprfunc)instance_str, /* tp_str */
- (getattrofunc)instance_getattr, /* tp_getattro */
- (setattrofunc)instance_setattr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
- instance_doc, /* tp_doc */
- (traverseproc)instance_traverse, /* tp_traverse */
- 0, /* tp_clear */
- instance_richcompare, /* tp_richcompare */
- offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
- (getiterfunc)instance_getiter, /* tp_iter */
- (iternextfunc)instance_iternext, /* 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 */
- instance_new, /* tp_new */
-};
-
-/* Instance method objects are used for two purposes:
+/* 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
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index f3b6b7f..f4c185d 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2006,13 +2006,6 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
return dict_update_common(self, args, kwds, "dict");
}
-static long
-dict_nohash(PyObject *self)
-{
- PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
- return -1;
-}
-
static PyObject *
dict_iter(dictobject *dict)
{
@@ -2045,7 +2038,7 @@ PyTypeObject PyDict_Type = {
0, /* tp_as_number */
&dict_as_sequence, /* tp_as_sequence */
&dict_as_mapping, /* tp_as_mapping */
- dict_nohash, /* tp_hash */
+ 0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
diff --git a/Objects/listobject.c b/Objects/listobject.c
index ea03a2a..b84998f 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2381,13 +2381,6 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
return 0;
}
-static long
-list_nohash(PyObject *self)
-{
- PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
- return -1;
-}
-
static PyObject *list_iter(PyObject *seq);
static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
@@ -2655,7 +2648,7 @@ PyTypeObject PyList_Type = {
0, /* tp_as_number */
&list_as_sequence, /* tp_as_sequence */
&list_as_mapping, /* tp_as_mapping */
- list_nohash, /* tp_hash */
+ 0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
diff --git a/Objects/object.c b/Objects/object.c
index ff13574..516098d 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -2,6 +2,7 @@
/* Generic object operations; and implementation of None (NoObject) */
#include "Python.h"
+#include "sliceobject.h" /* For PyEllipsis_Type */
#ifdef __cplusplus
extern "C" {
@@ -663,10 +664,6 @@ try_3way_compare(PyObject *v, PyObject *w)
which has the same return conventions as this function. */
f = v->ob_type->tp_compare;
- if (PyInstance_Check(v))
- return (*f)(v, w);
- if (PyInstance_Check(w))
- return (*w->ob_type->tp_compare)(v, w);
/* If both have the same (non-NULL) tp_compare, use it. */
if (f != NULL && f == w->ob_type->tp_compare) {
@@ -789,15 +786,7 @@ do_cmp(PyObject *v, PyObject *w)
if (v->ob_type == w->ob_type
&& (f = v->ob_type->tp_compare) != NULL) {
c = (*f)(v, w);
- if (PyInstance_Check(v)) {
- /* Instance tp_compare has a different signature.
- But if it returns undefined we fall through. */
- if (c != 2)
- return c;
- /* Else fall through to try_rich_to_3way_compare() */
- }
- else
- return adjust_tp_compare(c);
+ return adjust_tp_compare(c);
}
/* We only get here if one of the following is true:
a) v and w have different types
@@ -911,7 +900,7 @@ PyObject_RichCompare(PyObject *v, PyObject *w, int op)
/* If the types are equal, and not old-style instances, try to
get out cheap (don't bother with coercions etc.). */
- if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
+ if (v->ob_type == w->ob_type) {
cmpfunc fcmp;
richcmpfunc frich = RICHCOMPARE(v->ob_type);
/* If the type has richcmp, try it first. try_rich_compare
@@ -1063,10 +1052,7 @@ PyObject_Hash(PyObject *v)
PyTypeObject *tp = v->ob_type;
if (tp->tp_hash != NULL)
return (*tp->tp_hash)(v);
- if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
- return _Py_HashPointer(v); /* Use address as hash value */
- }
- /* If there's a cmp but no hash defined, the object can't be hashed */
+ /* Otherwise, the object can't be hashed */
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
v->ob_type->tp_name);
return -1;
@@ -1303,12 +1289,8 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
n = PyTuple_GET_SIZE(mro);
for (i = 0; i < n; i++) {
base = PyTuple_GET_ITEM(mro, i);
- if (PyClass_Check(base))
- dict = ((PyClassObject *)base)->cl_dict;
- else {
- assert(PyType_Check(base));
- dict = ((PyTypeObject *)base)->tp_dict;
- }
+ assert(PyType_Check(base));
+ dict = ((PyTypeObject *)base)->tp_dict;
assert(dict && PyDict_Check(dict));
descr = PyDict_GetItem(dict, name);
if (descr != NULL)
@@ -1554,20 +1536,7 @@ PyCallable_Check(PyObject *x)
{
if (x == NULL)
return 0;
- if (PyInstance_Check(x)) {
- PyObject *call = PyObject_GetAttrString(x, "__call__");
- if (call == NULL) {
- PyErr_Clear();
- return 0;
- }
- /* Could test recursively but don't, for fear of endless
- recursion if some joker sets self.__call__ = self */
- Py_DECREF(call);
- return 1;
- }
- else {
- return x->ob_type->tp_call != NULL;
- }
+ return x->ob_type->tp_call != NULL;
}
/* Helper for PyObject_Dir.
@@ -1701,7 +1670,7 @@ PyObject_Dir(PyObject *arg)
/* Elif some form of type or class, grab its dict and its bases.
We deliberately don't suck up its __class__, as methods belonging
to the metaclass would probably be more confusing than helpful. */
- else if (PyType_Check(arg) || PyClass_Check(arg)) {
+ else if (PyType_Check(arg)) {
masterdict = PyDict_New();
if (masterdict == NULL)
goto error;
@@ -1886,6 +1855,9 @@ _Py_ReadyTypes(void)
if (PyType_Ready(&PyNone_Type) < 0)
Py_FatalError("Can't initialize type(None)");
+ if (PyType_Ready(Py_Ellipsis->ob_type) < 0)
+ Py_FatalError("Can't initialize type(Ellipsis)");
+
if (PyType_Ready(&PyNotImplemented_Type) < 0)
Py_FatalError("Can't initialize type(NotImplemented)");
}
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 55a2b85..d651457 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -718,13 +718,6 @@ frozenset_hash(PyObject *self)
return hash;
}
-static long
-set_nohash(PyObject *self)
-{
- PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
- return -1;
-}
-
/***** Set iterator type ***********************************************/
typedef struct {
@@ -1813,7 +1806,7 @@ PyTypeObject PySet_Type = {
&set_as_number, /* tp_as_number */
&set_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
- set_nohash, /* tp_hash */
+ 0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 0819c98..3ba85e6 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -3982,6 +3982,8 @@ PyDoc_STRVAR(string_doc,
Return a nice string representation of the object.\n\
If the argument is a string, the return value is the same object.");
+static PyObject *str_iter(PyObject *seq);
+
PyTypeObject PyString_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
@@ -4009,7 +4011,7 @@ PyTypeObject PyString_Type = {
0, /* tp_clear */
(richcmpfunc)string_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
- 0, /* tp_iter */
+ str_iter, /* tp_iter */
0, /* tp_iternext */
string_methods, /* tp_methods */
0, /* tp_members */
@@ -4984,3 +4986,120 @@ void _Py_ReleaseInternedStrings(void)
Py_DECREF(interned);
interned = NULL;
}
+
+
+/*********************** Str Iterator ****************************/
+
+typedef struct {
+ PyObject_HEAD
+ long it_index;
+ PyStringObject *it_seq; /* Set to NULL when iterator is exhausted */
+} striterobject;
+
+static void
+striter_dealloc(striterobject *it)
+{
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
+}
+
+static int
+striter_traverse(striterobject *it, visitproc visit, void *arg)
+{
+ Py_VISIT(it->it_seq);
+ return 0;
+}
+
+static PyObject *
+striter_next(striterobject *it)
+{
+ PyStringObject *seq;
+ PyObject *item;
+
+ assert(it != NULL);
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+ assert(PyString_Check(seq));
+
+ if (it->it_index < PyString_GET_SIZE(seq)) {
+ item = PyString_FromStringAndSize(PyString_AS_STRING(seq)+it->it_index, 1);
+ if (item != NULL)
+ ++it->it_index;
+ return item;
+ }
+
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ return NULL;
+}
+
+static PyObject *
+striter_len(striterobject *it)
+{
+ Py_ssize_t len = 0;
+ if (it->it_seq)
+ len = PyString_GET_SIZE(it->it_seq) - it->it_index;
+ return PyInt_FromSsize_t(len);
+}
+
+PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
+
+static PyMethodDef striter_methods[] = {
+ {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
+};
+
+PyTypeObject PyStringIter_Type = {
+ PyObject_HEAD_INIT(&PyType_Type)
+ 0, /* ob_size */
+ "striterator", /* tp_name */
+ sizeof(striterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)striter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)striter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)striter_next, /* tp_iternext */
+ striter_methods, /* tp_methods */
+ 0,
+};
+
+static PyObject *
+str_iter(PyObject *seq)
+{
+ striterobject *it;
+
+ if (!PyString_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(striterobject, &PyStringIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = (PyStringObject *)seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
+}
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 1ec345f..93a730a 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -214,7 +214,7 @@ type_set_bases(PyTypeObject *type, PyObject *value, void *context)
}
for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
ob = PyTuple_GET_ITEM(value, i);
- if (!PyClass_Check(ob) && !PyType_Check(ob)) {
+ if (!PyType_Check(ob)) {
PyErr_Format(
PyExc_TypeError,
"%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
@@ -957,47 +957,6 @@ call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
return retval;
}
-static int
-fill_classic_mro(PyObject *mro, PyObject *cls)
-{
- PyObject *bases, *base;
- Py_ssize_t i, n;
-
- assert(PyList_Check(mro));
- assert(PyClass_Check(cls));
- i = PySequence_Contains(mro, cls);
- if (i < 0)
- return -1;
- if (!i) {
- if (PyList_Append(mro, cls) < 0)
- return -1;
- }
- bases = ((PyClassObject *)cls)->cl_bases;
- assert(bases && PyTuple_Check(bases));
- n = PyTuple_GET_SIZE(bases);
- for (i = 0; i < n; i++) {
- base = PyTuple_GET_ITEM(bases, i);
- if (fill_classic_mro(mro, base) < 0)
- return -1;
- }
- return 0;
-}
-
-static PyObject *
-classic_mro(PyObject *cls)
-{
- PyObject *mro;
-
- assert(PyClass_Check(cls));
- mro = PyList_New(0);
- if (mro != NULL) {
- if (fill_classic_mro(mro, cls) == 0)
- return mro;
- Py_DECREF(mro);
- }
- return NULL;
-}
-
/*
Method resolution order algorithm C3 described in
"A Monotonic Superclass Linearization for Dylan",
@@ -1229,11 +1188,7 @@ mro_implementation(PyTypeObject *type)
for (i = 0; i < n; i++) {
PyObject *base = PyTuple_GET_ITEM(bases, i);
PyObject *parentMRO;
- if (PyType_Check(base))
- parentMRO = PySequence_List(
- ((PyTypeObject*)base)->tp_mro);
- else
- parentMRO = classic_mro(base);
+ parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
if (parentMRO == NULL) {
Py_DECREF(to_merge);
return NULL;
@@ -1315,9 +1270,7 @@ mro_internal(PyTypeObject *type)
for (i = 0; i < len; i++) {
PyTypeObject *t;
cls = PyTuple_GET_ITEM(tuple, i);
- if (PyClass_Check(cls))
- continue;
- else if (!PyType_Check(cls)) {
+ if (!PyType_Check(cls)) {
PyErr_Format(PyExc_TypeError,
"mro() returned a non-class ('%.500s')",
cls->ob_type->tp_name);
@@ -1356,8 +1309,6 @@ best_base(PyObject *bases)
winner = NULL;
for (i = 0; i < n; i++) {
base_proto = PyTuple_GET_ITEM(bases, i);
- if (PyClass_Check(base_proto))
- continue;
if (!PyType_Check(base_proto)) {
PyErr_SetString(
PyExc_TypeError,
@@ -1636,8 +1587,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
for (i = 0; i < nbases; i++) {
tmp = PyTuple_GET_ITEM(bases, i);
tmptype = tmp->ob_type;
- if (tmptype == &PyClass_Type)
- continue; /* Special case classic classes */
if (PyType_IsSubtype(winner, tmptype))
continue;
if (PyType_IsSubtype(tmptype, winner)) {
@@ -1793,14 +1742,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
tmp = PyTuple_GET_ITEM(bases, i);
if (tmp == (PyObject *)base)
continue; /* Skip primary base */
- if (PyClass_Check(tmp)) {
- /* Classic base class provides both */
- if (may_add_dict && !add_dict)
- add_dict++;
- if (may_add_weak && !add_weak)
- add_weak++;
- break;
- }
assert(PyType_Check(tmp));
tmptype = (PyTypeObject *)tmp;
if (may_add_dict && !add_dict &&
@@ -2006,12 +1947,8 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
n = PyTuple_GET_SIZE(mro);
for (i = 0; i < n; i++) {
base = PyTuple_GET_ITEM(mro, i);
- if (PyClass_Check(base))
- dict = ((PyClassObject *)base)->cl_dict;
- else {
- assert(PyType_Check(base));
- dict = ((PyTypeObject *)base)->tp_dict;
- }
+ assert(PyType_Check(base));
+ dict = ((PyTypeObject *)base)->tp_dict;
assert(dict && PyDict_Check(dict));
res = PyDict_GetItem(dict, name);
if (res != NULL)
@@ -2364,12 +2301,6 @@ object_str(PyObject *self)
return f(self);
}
-static long
-object_hash(PyObject *self)
-{
- return _Py_HashPointer(self);
-}
-
static PyObject *
object_get_class(PyObject *self, void *closure)
{
@@ -2762,7 +2693,7 @@ PyTypeObject PyBaseObject_Type = {
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
- object_hash, /* tp_hash */
+ (hashfunc)_Py_HashPointer, /* tp_hash */
0, /* tp_call */
object_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
@@ -2791,7 +2722,7 @@ PyTypeObject PyBaseObject_Type = {
};
-/* Initialize the __dict__ in a type object */
+/* Add the methods from tp_methods to the __dict__ in a type object */
static int
add_methods(PyTypeObject *type, PyMethodDef *meth)
@@ -3118,7 +3049,7 @@ PyType_Ready(PyTypeObject *type)
*/
/* Initialize the base class */
- if (base && base->tp_dict == NULL) {
+ if (base != NULL && base->tp_dict == NULL) {
if (PyType_Ready(base) < 0)
goto error;
}
@@ -4490,41 +4421,35 @@ static long
slot_tp_hash(PyObject *self)
{
PyObject *func;
- static PyObject *hash_str, *eq_str, *cmp_str;
+ static PyObject *hash_str;
long h;
func = lookup_method(self, "__hash__", &hash_str);
- if (func != NULL) {
- PyObject *res = PyEval_CallObject(func, NULL);
+ if (func == Py_None) {
Py_DECREF(func);
- if (res == NULL)
- return -1;
- if (PyLong_Check(res))
- h = PyLong_Type.tp_hash(res);
- else
- h = PyInt_AsLong(res);
- Py_DECREF(res);
+ func = NULL;
}
- else {
- PyErr_Clear();
- func = lookup_method(self, "__eq__", &eq_str);
- if (func == NULL) {
- PyErr_Clear();
- func = lookup_method(self, "__cmp__", &cmp_str);
- }
- if (func != NULL) {
- PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
- self->ob_type->tp_name);
- Py_DECREF(func);
- return -1;
- }
+
+ if (func == NULL) {
PyErr_Clear();
- h = _Py_HashPointer((void *)self);
- }
- if (h == -1 && !PyErr_Occurred())
- h = -2;
- return h;
+ PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
+ self->ob_type->tp_name);
+ return -1;
+ }
+
+ PyObject *res = PyEval_CallObject(func, NULL);
+ Py_DECREF(func);
+ if (res == NULL)
+ return -1;
+ if (PyLong_Check(res))
+ h = PyLong_Type.tp_hash(res);
+ else
+ h = PyInt_AsLong(res);
+ Py_DECREF(res);
+ if (h == -1 && !PyErr_Occurred())
+ h = -2;
+ return h;
}
static PyObject *
@@ -5579,8 +5504,6 @@ super_getattro(PyObject *self, PyObject *name)
tmp = PyTuple_GET_ITEM(mro, i);
if (PyType_Check(tmp))
dict = ((PyTypeObject *)tmp)->tp_dict;
- else if (PyClass_Check(tmp))
- dict = ((PyClassObject *)tmp)->cl_dict;
else
continue;
res = PyDict_GetItem(dict, name);
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 9e35b61..a87916c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -7848,6 +7848,8 @@ Create a new Unicode object from the given encoded string.\n\
encoding defaults to the current default string encoding.\n\
errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.");
+static PyObject *unicode_iter(PyObject *seq);
+
PyTypeObject PyUnicode_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
@@ -7876,7 +7878,7 @@ PyTypeObject PyUnicode_Type = {
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
- 0, /* tp_iter */
+ unicode_iter, /* tp_iter */
0, /* tp_iternext */
unicode_methods, /* tp_methods */
0, /* tp_members */
@@ -7961,6 +7963,124 @@ _PyUnicode_Fini(void)
unicode_freelist_size = 0;
}
+
+
+/********************* Unicode Iterator **************************/
+
+typedef struct {
+ PyObject_HEAD
+ long it_index;
+ PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */
+} unicodeiterobject;
+
+static void
+unicodeiter_dealloc(unicodeiterobject *it)
+{
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
+}
+
+static int
+unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg)
+{
+ Py_VISIT(it->it_seq);
+ return 0;
+}
+
+static PyObject *
+unicodeiter_next(unicodeiterobject *it)
+{
+ PyUnicodeObject *seq;
+ PyObject *item;
+
+ assert(it != NULL);
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+ assert(PyUnicode_Check(seq));
+
+ if (it->it_index < PyUnicode_GET_SIZE(seq)) {
+ item = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(seq)+it->it_index, 1);
+ if (item != NULL)
+ ++it->it_index;
+ return item;
+ }
+
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ return NULL;
+}
+
+static PyObject *
+unicodeiter_len(unicodeiterobject *it)
+{
+ Py_ssize_t len = 0;
+ if (it->it_seq)
+ len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index;
+ return PyInt_FromSsize_t(len);
+}
+
+PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
+
+static PyMethodDef unicodeiter_methods[] = {
+ {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
+};
+
+PyTypeObject PyUnicodeIter_Type = {
+ PyObject_HEAD_INIT(&PyType_Type)
+ 0, /* ob_size */
+ "unicodeiterator", /* tp_name */
+ sizeof(unicodeiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)unicodeiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)unicodeiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)unicodeiter_next, /* tp_iternext */
+ unicodeiter_methods, /* tp_methods */
+ 0,
+};
+
+static PyObject *
+unicode_iter(PyObject *seq)
+{
+ unicodeiterobject *it;
+
+ if (!PyUnicode_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = (PyUnicodeObject *)seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/Python/ceval.c b/Python/ceval.c
index 9a9b98e..07dfdc2 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3409,14 +3409,8 @@ PyEval_GetFuncName(PyObject *func)
return PyString_AsString(((PyFunctionObject*)func)->func_name);
else if (PyCFunction_Check(func))
return ((PyCFunctionObject*)func)->m_ml->ml_name;
- else if (PyClass_Check(func))
- return PyString_AsString(((PyClassObject*)func)->cl_name);
- else if (PyInstance_Check(func)) {
- return PyString_AsString(
- ((PyInstanceObject*)func)->in_class->cl_name);
- } else {
+ else
return func->ob_type->tp_name;
- }
}
const char *
@@ -3428,13 +3422,8 @@ PyEval_GetFuncDesc(PyObject *func)
return "()";
else if (PyCFunction_Check(func))
return "()";
- else if (PyClass_Check(func))
- return " constructor";
- else if (PyInstance_Check(func)) {
- return " instance";
- } else {
+ else
return " object";
- }
}
static void
diff --git a/Python/structmember.c b/Python/structmember.c
index 54eb055..688a4b8 100644
--- a/Python/structmember.c
+++ b/Python/structmember.c
@@ -126,6 +126,10 @@ PyMember_GetOne(const char *addr, PyMemberDef *l)
v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
break;
#endif /* HAVE_LONG_LONG */
+ case T_NONE:
+ v = Py_None;
+ Py_INCREF(v);
+ break;
default:
PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
v = NULL;
diff --git a/README b/README
index 72a10cd..c1f0a48 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-This is Python 3000 -- unversioned (branched off 2.5 pre alpha 1)
+This is Python 3000 -- unversioned (branched off 2.5 in various beta stages)
=================================================================
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation.
@@ -30,6 +30,14 @@ All trademarks referenced herein are property of their respective
holders.
+Python 3000 disclaimer
+----------------------
+
+This README hasn't been updated for Python 3000 yet. If you see
+anything that should clearly be deleted, let me know (guido@python.org)
+or submit a patch to the Python 3000 category in SourceForge.
+
+
What's new in this release?
---------------------------