Commits

Anonymous committed 8f5b998

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.)

  • Participants
  • Parent commits 8e88ecb

Comments (0)

Files changed (22)

File Include/Python.h

 #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

File Include/classobject.h

-
-/* Class object interface */
+/* Former class object interface -- now only (un)bound methods are here  */
 
 /* Revealing some structures (not for general use) */
 
 
 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) \
 #define PyMethod_GET_CLASS(meth) \
 	(((PyMethodObject *)meth) -> im_class)
 
-PyAPI_FUNC(int) PyClass_IsSubclass(PyObject *, PyObject *);
-
-
 #ifdef __cplusplus
 }
 #endif

File Include/methodobject.h

 #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. */

File Include/object.h

 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. */
 
 	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;
 	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;
 
 	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;
 	/* weak reference enabler */
 	Py_ssize_t tp_weaklistoffset;
 
-	/* Added in release 2.2 */
 	/* Iterators */
 	getiterfunc tp_iter;
 	iternextfunc tp_iternext;

File Include/structmember.h

 				   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 */

File Modules/_sre.c

     {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)

File Modules/arraymodule.c

 {
 	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)

File Modules/cPickle.c

 }
 
 
-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)
 		}
 		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);
 	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)
 	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;
 	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);
 
 	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;

File Modules/gcmodule.c

 #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;
 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;
 }
 
 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);
 	}
 "  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");
 	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);

File Objects/abstract.c

 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;
 }
 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 && 

File Objects/bytesobject.c

     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)
 {
     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 */

File Objects/classobject.c

-
-/* Class object implementation */
+/* Class object implementation (dead now except for methods) */
 
 #include "Python.h"
 #include "structmember.h"
 #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)
 {
 	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;