| Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 |  | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Generic object operations; and implementation of None (NoObject) */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 |  | 
| Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 6 | #ifdef Py_REF_DEBUG | 
| Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 7 | long _Py_RefTotal; | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 |  | 
| Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 10 | int Py_DivisionWarningFlag; | 
| Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 11 |  | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. | 
 | 13 |    These are used by the individual routines for object creation. | 
 | 14 |    Do not call them otherwise, they do not initialize the object! */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 |  | 
| Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 16 | #ifdef Py_TRACE_REFS | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 17 | /* Head of circular doubly-linked list of all objects.  These are linked | 
 | 18 |  * together via the _ob_prev and _ob_next members of a PyObject, which | 
 | 19 |  * exist only in a Py_TRACE_REFS build. | 
 | 20 |  */ | 
| Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 21 | static PyObject refchain = {&refchain, &refchain}; | 
| Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 22 |  | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 23 | /* Insert op at the front of the list of all objects.  If force is true, | 
 | 24 |  * op is added even if _ob_prev and _ob_next are non-NULL already.  If | 
 | 25 |  * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. | 
 | 26 |  * force should be true if and only if op points to freshly allocated, | 
 | 27 |  * uninitialized memory, or you've unlinked op from the list and are | 
| Tim Peters | 51f8d38 | 2003-03-23 18:06:08 +0000 | [diff] [blame] | 28 |  * relinking it into the front. | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 29 |  * Note that objects are normally added to the list via _Py_NewReference, | 
 | 30 |  * which is called by PyObject_Init.  Not all objects are initialized that | 
 | 31 |  * way, though; exceptions include statically allocated type objects, and | 
 | 32 |  * statically allocated singletons (like Py_True and Py_None). | 
 | 33 |  */ | 
| Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 34 | void | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 35 | _Py_AddToAllObjects(PyObject *op, int force) | 
| Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 36 | { | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 37 | #ifdef  Py_DEBUG | 
 | 38 | 	if (!force) { | 
 | 39 | 		/* If it's initialized memory, op must be in or out of | 
 | 40 | 		 * the list unambiguously. | 
 | 41 | 		 */ | 
 | 42 | 		assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); | 
 | 43 | 	} | 
| Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 44 | #endif | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 45 | 	if (force || op->_ob_prev == NULL) { | 
 | 46 | 		op->_ob_next = refchain._ob_next; | 
 | 47 | 		op->_ob_prev = &refchain; | 
 | 48 | 		refchain._ob_next->_ob_prev = op; | 
 | 49 | 		refchain._ob_next = op; | 
 | 50 | 	} | 
 | 51 | } | 
 | 52 | #endif	/* Py_TRACE_REFS */ | 
| Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 53 |  | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 54 | #ifdef COUNT_ALLOCS | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | static PyTypeObject *type_list; | 
| Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 56 | extern int tuple_zero_allocs, fast_tuple_allocs; | 
 | 57 | extern int quick_int_allocs, quick_neg_int_allocs; | 
| Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 58 | extern int null_strings, one_strings; | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 59 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 60 | dump_counts(void) | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 61 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | 	PyTypeObject *tp; | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 63 |  | 
 | 64 | 	for (tp = type_list; tp; tp = tp->tp_next) | 
| Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 65 | 		fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 66 | 			tp->tp_name, tp->tp_allocs, tp->tp_frees, | 
| Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 67 | 			tp->tp_maxalloc); | 
 | 68 | 	fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", | 
 | 69 | 		fast_tuple_allocs, tuple_zero_allocs); | 
 | 70 | 	fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", | 
 | 71 | 		quick_int_allocs, quick_neg_int_allocs); | 
 | 72 | 	fprintf(stderr, "null strings: %d, 1-strings: %d\n", | 
 | 73 | 		null_strings, one_strings); | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 74 | } | 
 | 75 |  | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 76 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 77 | get_counts(void) | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 78 | { | 
 | 79 | 	PyTypeObject *tp; | 
 | 80 | 	PyObject *result; | 
 | 81 | 	PyObject *v; | 
 | 82 |  | 
 | 83 | 	result = PyList_New(0); | 
 | 84 | 	if (result == NULL) | 
 | 85 | 		return NULL; | 
 | 86 | 	for (tp = type_list; tp; tp = tp->tp_next) { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | 		v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs, | 
 | 88 | 				  tp->tp_frees, tp->tp_maxalloc); | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 89 | 		if (v == NULL) { | 
 | 90 | 			Py_DECREF(result); | 
 | 91 | 			return NULL; | 
 | 92 | 		} | 
 | 93 | 		if (PyList_Append(result, v) < 0) { | 
 | 94 | 			Py_DECREF(v); | 
 | 95 | 			Py_DECREF(result); | 
 | 96 | 			return NULL; | 
 | 97 | 		} | 
 | 98 | 		Py_DECREF(v); | 
 | 99 | 	} | 
 | 100 | 	return result; | 
 | 101 | } | 
 | 102 |  | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 103 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 104 | inc_count(PyTypeObject *tp) | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 105 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 106 | 	if (tp->tp_allocs == 0) { | 
| Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 107 | 		/* first time; insert in linked list */ | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 108 | 		if (tp->tp_next != NULL) /* sanity check */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | 			Py_FatalError("XXX inc_count sanity check"); | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 110 | 		tp->tp_next = type_list; | 
| Tim Peters | c6a3ff6 | 2002-07-08 22:11:52 +0000 | [diff] [blame] | 111 | 		/* Note that as of Python 2.2, heap-allocated type objects | 
 | 112 | 		 * can go away, but this code requires that they stay alive | 
 | 113 | 		 * until program exit.  That's why we're careful with | 
 | 114 | 		 * refcounts here.  type_list gets a new reference to tp, | 
 | 115 | 		 * while ownership of the reference type_list used to hold | 
 | 116 | 		 * (if any) was transferred to tp->tp_next in the line above. | 
 | 117 | 		 * tp is thus effectively immortal after this. | 
 | 118 | 		 */ | 
 | 119 | 		Py_INCREF(tp); | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 120 | 		type_list = tp; | 
| Tim Peters | 3e40c7f | 2003-03-23 03:04:32 +0000 | [diff] [blame] | 121 | #ifdef Py_TRACE_REFS | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 122 | 		/* Also insert in the doubly-linked list of all objects, | 
 | 123 | 		 * if not already there. | 
 | 124 | 		 */ | 
 | 125 | 		_Py_AddToAllObjects((PyObject *)tp, 0); | 
| Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 126 | #endif | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 127 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 128 | 	tp->tp_allocs++; | 
 | 129 | 	if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) | 
 | 130 | 		tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; | 
| Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 131 | } | 
 | 132 | #endif | 
 | 133 |  | 
| Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 134 | #ifdef Py_REF_DEBUG | 
 | 135 | /* Log a fatal error; doesn't return. */ | 
 | 136 | void | 
 | 137 | _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) | 
 | 138 | { | 
 | 139 | 	char buf[300]; | 
 | 140 |  | 
 | 141 | 	PyOS_snprintf(buf, sizeof(buf), | 
 | 142 | 		      "%s:%i object at %p has negative ref count %i", | 
 | 143 | 		      fname, lineno, op, op->ob_refcnt); | 
 | 144 | 	Py_FatalError(buf); | 
 | 145 | } | 
 | 146 |  | 
 | 147 | #endif /* Py_REF_DEBUG */ | 
 | 148 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 149 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 150 | PyObject_Init(PyObject *op, PyTypeObject *tp) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | { | 
| Guido van Rossum | 6e08c14 | 2002-10-11 20:37:24 +0000 | [diff] [blame] | 152 | 	if (op == NULL) | 
 | 153 | 		return PyErr_NoMemory(); | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 154 | 	/* Any changes should be reflected in PyObject_INIT (objimpl.h) */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | 	op->ob_type = tp; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 156 | 	_Py_NewReference(op); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | 	return op; | 
 | 158 | } | 
 | 159 |  | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 160 | PyVarObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 161 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 162 | { | 
| Guido van Rossum | 6e08c14 | 2002-10-11 20:37:24 +0000 | [diff] [blame] | 163 | 	if (op == NULL) | 
 | 164 | 		return (PyVarObject *) PyErr_NoMemory(); | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 165 | 	/* Any changes should be reflected in PyObject_INIT_VAR */ | 
 | 166 | 	op->ob_size = size; | 
 | 167 | 	op->ob_type = tp; | 
 | 168 | 	_Py_NewReference((PyObject *)op); | 
 | 169 | 	return op; | 
 | 170 | } | 
 | 171 |  | 
 | 172 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 173 | _PyObject_New(PyTypeObject *tp) | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 174 | { | 
 | 175 | 	PyObject *op; | 
 | 176 | 	op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); | 
 | 177 | 	if (op == NULL) | 
 | 178 | 		return PyErr_NoMemory(); | 
 | 179 | 	return PyObject_INIT(op, tp); | 
 | 180 | } | 
 | 181 |  | 
| Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 182 | PyVarObject * | 
| Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 183 | _PyObject_NewVar(PyTypeObject *tp, int nitems) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 185 | 	PyVarObject *op; | 
| Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 186 | 	const size_t size = _PyObject_VAR_SIZE(tp, nitems); | 
| Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 187 | 	op = (PyVarObject *) PyObject_MALLOC(size); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | 	if (op == NULL) | 
| Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 189 | 		return (PyVarObject *)PyErr_NoMemory(); | 
| Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 190 | 	return PyObject_INIT_VAR(op, tp, nitems); | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 191 | } | 
 | 192 |  | 
| Neil Schemenauer | bdf0eed | 2002-04-12 03:08:42 +0000 | [diff] [blame] | 193 | /* for binary compatibility with 2.2 */ | 
 | 194 | #undef _PyObject_Del | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 195 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 196 | _PyObject_Del(PyObject *op) | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 197 | { | 
| Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 198 | 	PyObject_FREE(op); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 199 | } | 
 | 200 |  | 
| Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 201 | /* Implementation of PyObject_Print with recursion checking */ | 
 | 202 | static int | 
 | 203 | internal_print(PyObject *op, FILE *fp, int flags, int nesting) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | { | 
| Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 205 | 	int ret = 0; | 
| Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 206 | 	if (nesting > 10) { | 
 | 207 | 		PyErr_SetString(PyExc_RuntimeError, "print recursion"); | 
 | 208 | 		return -1; | 
 | 209 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | 	if (PyErr_CheckSignals()) | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 211 | 		return -1; | 
| Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 212 | #ifdef USE_STACKCHECK | 
 | 213 | 	if (PyOS_CheckStack()) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 214 | 		PyErr_SetString(PyExc_MemoryError, "stack overflow"); | 
| Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 215 | 		return -1; | 
 | 216 | 	} | 
 | 217 | #endif | 
| Guido van Rossum | 687ef6e | 2000-01-12 16:28:58 +0000 | [diff] [blame] | 218 | 	clearerr(fp); /* Clear any previous error condition */ | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 219 | 	if (op == NULL) { | 
 | 220 | 		fprintf(fp, "<nil>"); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | 	} | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 222 | 	else { | 
 | 223 | 		if (op->ob_refcnt <= 0) | 
| Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 224 | 			fprintf(fp, "<refcnt %u at %p>", | 
 | 225 | 				op->ob_refcnt, op); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 226 | 		else if (op->ob_type->tp_print == NULL) { | 
| Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 227 | 			PyObject *s; | 
 | 228 | 			if (flags & Py_PRINT_RAW) | 
 | 229 | 				s = PyObject_Str(op); | 
 | 230 | 			else | 
 | 231 | 				s = PyObject_Repr(op); | 
 | 232 | 			if (s == NULL) | 
 | 233 | 				ret = -1; | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 234 | 			else { | 
| Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 235 | 				ret = internal_print(s, fp, Py_PRINT_RAW, | 
 | 236 | 						     nesting+1); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 237 | 			} | 
| Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 238 | 			Py_XDECREF(s); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 239 | 		} | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 240 | 		else | 
| Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 241 | 			ret = (*op->ob_type->tp_print)(op, fp, flags); | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 242 | 	} | 
| Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 243 | 	if (ret == 0) { | 
 | 244 | 		if (ferror(fp)) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | 			PyErr_SetFromErrno(PyExc_IOError); | 
| Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 246 | 			clearerr(fp); | 
 | 247 | 			ret = -1; | 
 | 248 | 		} | 
 | 249 | 	} | 
 | 250 | 	return ret; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | } | 
 | 252 |  | 
| Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 253 | int | 
 | 254 | PyObject_Print(PyObject *op, FILE *fp, int flags) | 
 | 255 | { | 
 | 256 | 	return internal_print(op, fp, flags, 0); | 
 | 257 | } | 
 | 258 |  | 
 | 259 |  | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 260 | /* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */ | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 261 | void _PyObject_Dump(PyObject* op) | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 262 | { | 
| Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 263 | 	if (op == NULL) | 
 | 264 | 		fprintf(stderr, "NULL\n"); | 
 | 265 | 	else { | 
| Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 266 | 		fprintf(stderr, "object  : "); | 
| Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 267 | 		(void)PyObject_Print(op, stderr, 0); | 
| Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 268 | 		fprintf(stderr, "\n" | 
 | 269 | 			"type    : %s\n" | 
 | 270 | 			"refcount: %d\n" | 
 | 271 | 			"address : %p\n", | 
 | 272 | 			op->ob_type==NULL ? "NULL" : op->ob_type->tp_name, | 
 | 273 | 			op->ob_refcnt, | 
 | 274 | 			op); | 
| Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 275 | 	} | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 276 | } | 
| Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 277 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 278 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 279 | PyObject_Repr(PyObject *v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 281 | 	if (PyErr_CheckSignals()) | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 282 | 		return NULL; | 
| Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 283 | #ifdef USE_STACKCHECK | 
 | 284 | 	if (PyOS_CheckStack()) { | 
| Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 285 | 		PyErr_SetString(PyExc_MemoryError, "stack overflow"); | 
| Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 286 | 		return NULL; | 
 | 287 | 	} | 
 | 288 | #endif | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 289 | 	if (v == NULL) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | 		return PyString_FromString("<NULL>"); | 
| Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 291 | 	else if (v->ob_type->tp_repr == NULL) | 
| Guido van Rossum | 21922aa | 2001-08-30 20:26:05 +0000 | [diff] [blame] | 292 | 		return PyString_FromFormat("<%s object at %p>", | 
| Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 293 | 					   v->ob_type->tp_name, v); | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 294 | 	else { | 
 | 295 | 		PyObject *res; | 
 | 296 | 		res = (*v->ob_type->tp_repr)(v); | 
 | 297 | 		if (res == NULL) | 
 | 298 | 			return NULL; | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 299 | #ifdef Py_USING_UNICODE | 
| Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 300 | 		if (PyUnicode_Check(res)) { | 
 | 301 | 			PyObject* str; | 
| Fredrik Lundh | 2a1e060 | 2000-07-08 17:43:32 +0000 | [diff] [blame] | 302 | 			str = PyUnicode_AsUnicodeEscapeString(res); | 
| Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 303 | 			Py_DECREF(res); | 
 | 304 | 			if (str) | 
| Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 305 | 				res = str; | 
| Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 306 | 			else | 
 | 307 | 				return NULL; | 
| Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 308 | 		} | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 309 | #endif | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 310 | 		if (!PyString_Check(res)) { | 
 | 311 | 			PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 312 | 				     "__repr__ returned non-string (type %.200s)", | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 313 | 				     res->ob_type->tp_name); | 
 | 314 | 			Py_DECREF(res); | 
 | 315 | 			return NULL; | 
 | 316 | 		} | 
 | 317 | 		return res; | 
 | 318 | 	} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | } | 
 | 320 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 322 | PyObject_Str(PyObject *v) | 
| Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 323 | { | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 324 | 	PyObject *res; | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 325 |  | 
| Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 326 | 	if (v == NULL) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 327 | 		return PyString_FromString("<NULL>"); | 
| Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 328 | 	if (PyString_CheckExact(v)) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 329 | 		Py_INCREF(v); | 
| Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 330 | 		return v; | 
 | 331 | 	} | 
| Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 332 | 	if (v->ob_type->tp_str == NULL) | 
 | 333 | 		return PyObject_Repr(v); | 
 | 334 |  | 
 | 335 | 	res = (*v->ob_type->tp_str)(v); | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 336 | 	if (res == NULL) | 
 | 337 | 		return NULL; | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 338 | #ifdef Py_USING_UNICODE | 
| Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 339 | 	if (PyUnicode_Check(res)) { | 
 | 340 | 		PyObject* str; | 
 | 341 | 		str = PyUnicode_AsEncodedString(res, NULL, NULL); | 
 | 342 | 		Py_DECREF(res); | 
 | 343 | 		if (str) | 
 | 344 | 			res = str; | 
 | 345 | 		else | 
 | 346 | 		    	return NULL; | 
 | 347 | 	} | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 348 | #endif | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 349 | 	if (!PyString_Check(res)) { | 
 | 350 | 		PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 351 | 			     "__str__ returned non-string (type %.200s)", | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 352 | 			     res->ob_type->tp_name); | 
 | 353 | 		Py_DECREF(res); | 
 | 354 | 		return NULL; | 
 | 355 | 	} | 
 | 356 | 	return res; | 
| Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 357 | } | 
 | 358 |  | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 359 | #ifdef Py_USING_UNICODE | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 360 | PyObject * | 
 | 361 | PyObject_Unicode(PyObject *v) | 
 | 362 | { | 
 | 363 | 	PyObject *res; | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 364 |  | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 365 | 	if (v == NULL) | 
 | 366 | 		res = PyString_FromString("<NULL>"); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 367 | 	if (PyUnicode_CheckExact(v)) { | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 368 | 		Py_INCREF(v); | 
 | 369 | 		return v; | 
 | 370 | 	} | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 371 | 	if (PyUnicode_Check(v)) { | 
 | 372 | 		/* For a Unicode subtype that's not a Unicode object, | 
 | 373 | 		   return a true Unicode object with the same data. */ | 
 | 374 | 		return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), | 
 | 375 | 					     PyUnicode_GET_SIZE(v)); | 
 | 376 | 	} | 
 | 377 | 	if (PyString_Check(v)) { | 
| Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 378 | 		Py_INCREF(v); | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 379 | 	    	res = v; | 
| Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 380 |     	} | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 381 | 	else { | 
 | 382 | 		PyObject *func; | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 383 | 		static PyObject *unicodestr; | 
 | 384 | 		/* XXX As soon as we have a tp_unicode slot, we should | 
 | 385 | 		       check this before trying the __unicode__ | 
 | 386 | 		       method. */ | 
 | 387 | 		if (unicodestr == NULL) { | 
 | 388 | 			unicodestr= PyString_InternFromString( | 
 | 389 | 						       "__unicode__"); | 
 | 390 | 			if (unicodestr == NULL) | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 391 | 				return NULL; | 
 | 392 | 		} | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 393 | 		func = PyObject_GetAttr(v, unicodestr); | 
 | 394 | 		if (func != NULL) { | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 395 | 		    	res = PyEval_CallObject(func, (PyObject *)NULL); | 
 | 396 | 			Py_DECREF(func); | 
 | 397 | 		} | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 398 | 		else { | 
 | 399 | 			PyErr_Clear(); | 
 | 400 | 			if (v->ob_type->tp_str != NULL) | 
 | 401 | 				res = (*v->ob_type->tp_str)(v); | 
 | 402 | 			else | 
 | 403 | 				res = PyObject_Repr(v); | 
 | 404 | 		} | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 405 | 	} | 
 | 406 | 	if (res == NULL) | 
 | 407 | 		return NULL; | 
 | 408 | 	if (!PyUnicode_Check(res)) { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 409 | 		PyObject *str; | 
 | 410 | 		str = PyUnicode_FromEncodedObject(res, NULL, "strict"); | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 411 | 		Py_DECREF(res); | 
 | 412 | 		if (str) | 
 | 413 | 			res = str; | 
 | 414 | 		else | 
 | 415 | 		    	return NULL; | 
 | 416 | 	} | 
 | 417 | 	return res; | 
 | 418 | } | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 419 | #endif | 
| Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 420 |  | 
 | 421 |  | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 422 | /* Helper to warn about deprecated tp_compare return values.  Return: | 
 | 423 |    -2 for an exception; | 
 | 424 |    -1 if v <  w; | 
 | 425 |     0 if v == w; | 
 | 426 |     1 if v  > w. | 
 | 427 |    (This function cannot return 2.) | 
 | 428 | */ | 
 | 429 | static int | 
 | 430 | adjust_tp_compare(int c) | 
 | 431 | { | 
 | 432 | 	if (PyErr_Occurred()) { | 
 | 433 | 		if (c != -1 && c != -2) { | 
 | 434 | 			PyObject *t, *v, *tb; | 
 | 435 | 			PyErr_Fetch(&t, &v, &tb); | 
 | 436 | 			if (PyErr_Warn(PyExc_RuntimeWarning, | 
 | 437 | 				       "tp_compare didn't return -1 or -2 " | 
 | 438 | 				       "for exception") < 0) { | 
 | 439 | 				Py_XDECREF(t); | 
 | 440 | 				Py_XDECREF(v); | 
 | 441 | 				Py_XDECREF(tb); | 
 | 442 | 			} | 
 | 443 | 			else | 
 | 444 | 				PyErr_Restore(t, v, tb); | 
 | 445 | 		} | 
 | 446 | 		return -2; | 
 | 447 | 	} | 
 | 448 | 	else if (c < -1 || c > 1) { | 
 | 449 | 		if (PyErr_Warn(PyExc_RuntimeWarning, | 
 | 450 | 			       "tp_compare didn't return -1, 0 or 1") < 0) | 
 | 451 | 			return -2; | 
 | 452 | 		else | 
 | 453 | 			return c < -1 ? -1 : 1; | 
 | 454 | 	} | 
 | 455 | 	else { | 
 | 456 | 		assert(c >= -1 && c <= 1); | 
 | 457 | 		return c; | 
 | 458 | 	} | 
 | 459 | } | 
 | 460 |  | 
 | 461 |  | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 462 | /* Macro to get the tp_richcompare field of a type if defined */ | 
 | 463 | #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ | 
 | 464 |                          ? (t)->tp_richcompare : NULL) | 
 | 465 |  | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 466 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ | 
 | 467 | static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 468 |  | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 469 | /* Try a genuine rich comparison, returning an object.  Return: | 
 | 470 |    NULL for exception; | 
 | 471 |    NotImplemented if this particular rich comparison is not implemented or | 
 | 472 |      undefined; | 
 | 473 |    some object not equal to NotImplemented if it is implemented | 
 | 474 |      (this latter object may not be a Boolean). | 
 | 475 | */ | 
 | 476 | static PyObject * | 
 | 477 | try_rich_compare(PyObject *v, PyObject *w, int op) | 
 | 478 | { | 
 | 479 | 	richcmpfunc f; | 
 | 480 | 	PyObject *res; | 
 | 481 |  | 
| Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 482 | 	if (v->ob_type != w->ob_type && | 
 | 483 | 	    PyType_IsSubtype(w->ob_type, v->ob_type) && | 
 | 484 | 	    (f = RICHCOMPARE(w->ob_type)) != NULL) { | 
 | 485 | 		res = (*f)(w, v, swapped_op[op]); | 
 | 486 | 		if (res != Py_NotImplemented) | 
 | 487 | 			return res; | 
 | 488 | 		Py_DECREF(res); | 
 | 489 | 	} | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 490 | 	if ((f = RICHCOMPARE(v->ob_type)) != NULL) { | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 491 | 		res = (*f)(v, w, op); | 
 | 492 | 		if (res != Py_NotImplemented) | 
 | 493 | 			return res; | 
 | 494 | 		Py_DECREF(res); | 
 | 495 | 	} | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 496 | 	if ((f = RICHCOMPARE(w->ob_type)) != NULL) { | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 497 | 		return (*f)(w, v, swapped_op[op]); | 
 | 498 | 	} | 
 | 499 | 	res = Py_NotImplemented; | 
 | 500 | 	Py_INCREF(res); | 
 | 501 | 	return res; | 
 | 502 | } | 
 | 503 |  | 
 | 504 | /* Try a genuine rich comparison, returning an int.  Return: | 
 | 505 |    -1 for exception (including the case where try_rich_compare() returns an | 
 | 506 |       object that's not a Boolean); | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 507 |     0 if the outcome is false; | 
 | 508 |     1 if the outcome is true; | 
 | 509 |     2 if this particular rich comparison is not implemented or undefined. | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 510 | */ | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 511 | static int | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 512 | try_rich_compare_bool(PyObject *v, PyObject *w, int op) | 
 | 513 | { | 
 | 514 | 	PyObject *res; | 
 | 515 | 	int ok; | 
 | 516 |  | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 517 | 	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 518 | 		return 2; /* Shortcut, avoid INCREF+DECREF */ | 
 | 519 | 	res = try_rich_compare(v, w, op); | 
 | 520 | 	if (res == NULL) | 
 | 521 | 		return -1; | 
 | 522 | 	if (res == Py_NotImplemented) { | 
 | 523 | 		Py_DECREF(res); | 
 | 524 | 		return 2; | 
 | 525 | 	} | 
 | 526 | 	ok = PyObject_IsTrue(res); | 
 | 527 | 	Py_DECREF(res); | 
 | 528 | 	return ok; | 
 | 529 | } | 
 | 530 |  | 
 | 531 | /* Try rich comparisons to determine a 3-way comparison.  Return: | 
 | 532 |    -2 for an exception; | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 533 |    -1 if v  < w; | 
 | 534 |     0 if v == w; | 
 | 535 |     1 if v  > w; | 
 | 536 |     2 if this particular rich comparison is not implemented or undefined. | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 537 | */ | 
 | 538 | static int | 
 | 539 | try_rich_to_3way_compare(PyObject *v, PyObject *w) | 
 | 540 | { | 
| Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 541 | 	static struct { int op; int outcome; } tries[3] = { | 
 | 542 | 		/* Try this operator, and if it is true, use this outcome: */ | 
 | 543 | 		{Py_EQ, 0}, | 
 | 544 | 		{Py_LT, -1}, | 
 | 545 | 		{Py_GT, 1}, | 
 | 546 | 	}; | 
 | 547 | 	int i; | 
 | 548 |  | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 549 | 	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 550 | 		return 2; /* Shortcut */ | 
| Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 551 |  | 
 | 552 | 	for (i = 0; i < 3; i++) { | 
 | 553 | 		switch (try_rich_compare_bool(v, w, tries[i].op)) { | 
 | 554 | 		case -1: | 
| Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 555 | 			return -2; | 
| Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 556 | 		case 1: | 
 | 557 | 			return tries[i].outcome; | 
 | 558 | 		} | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 559 | 	} | 
| Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 560 |  | 
 | 561 | 	return 2; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 562 | } | 
 | 563 |  | 
 | 564 | /* Try a 3-way comparison, returning an int.  Return: | 
 | 565 |    -2 for an exception; | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 566 |    -1 if v <  w; | 
 | 567 |     0 if v == w; | 
 | 568 |     1 if v  > w; | 
 | 569 |     2 if this particular 3-way comparison is not implemented or undefined. | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 570 | */ | 
 | 571 | static int | 
 | 572 | try_3way_compare(PyObject *v, PyObject *w) | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 573 | { | 
 | 574 | 	int c; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 575 | 	cmpfunc f; | 
 | 576 |  | 
 | 577 | 	/* Comparisons involving instances are given to instance_compare, | 
 | 578 | 	   which has the same return conventions as this function. */ | 
 | 579 |  | 
| Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 580 | 	f = v->ob_type->tp_compare; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 581 | 	if (PyInstance_Check(v)) | 
| Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 582 | 		return (*f)(v, w); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 583 | 	if (PyInstance_Check(w)) | 
 | 584 | 		return (*w->ob_type->tp_compare)(v, w); | 
 | 585 |  | 
| Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 586 | 	/* If both have the same (non-NULL) tp_compare, use it. */ | 
 | 587 | 	if (f != NULL && f == w->ob_type->tp_compare) { | 
 | 588 | 		c = (*f)(v, w); | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 589 | 		return adjust_tp_compare(c); | 
| Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 590 | 	} | 
 | 591 |  | 
 | 592 | 	/* If either tp_compare is _PyObject_SlotCompare, that's safe. */ | 
 | 593 | 	if (f == _PyObject_SlotCompare || | 
 | 594 | 	    w->ob_type->tp_compare == _PyObject_SlotCompare) | 
 | 595 | 		return _PyObject_SlotCompare(v, w); | 
 | 596 |  | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 597 | 	/* Try coercion; if it fails, give up */ | 
 | 598 | 	c = PyNumber_CoerceEx(&v, &w); | 
 | 599 | 	if (c < 0) | 
 | 600 | 		return -2; | 
 | 601 | 	if (c > 0) | 
 | 602 | 		return 2; | 
 | 603 |  | 
 | 604 | 	/* Try v's comparison, if defined */ | 
 | 605 | 	if ((f = v->ob_type->tp_compare) != NULL) { | 
 | 606 | 		c = (*f)(v, w); | 
 | 607 | 		Py_DECREF(v); | 
 | 608 | 		Py_DECREF(w); | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 609 | 		return adjust_tp_compare(c); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 610 | 	} | 
 | 611 |  | 
 | 612 | 	/* Try w's comparison, if defined */ | 
 | 613 | 	if ((f = w->ob_type->tp_compare) != NULL) { | 
 | 614 | 		c = (*f)(w, v); /* swapped! */ | 
 | 615 | 		Py_DECREF(v); | 
 | 616 | 		Py_DECREF(w); | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 617 | 		c = adjust_tp_compare(c); | 
 | 618 | 		if (c >= -1) | 
 | 619 | 			return -c; /* Swapped! */ | 
 | 620 | 		else | 
 | 621 | 			return c; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 622 | 	} | 
 | 623 |  | 
 | 624 | 	/* No comparison defined */ | 
 | 625 | 	Py_DECREF(v); | 
 | 626 | 	Py_DECREF(w); | 
 | 627 | 	return 2; | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 628 | } | 
 | 629 |  | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 630 | /* Final fallback 3-way comparison, returning an int.  Return: | 
 | 631 |    -2 if an error occurred; | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 632 |    -1 if v <  w; | 
 | 633 |     0 if v == w; | 
 | 634 |     1 if v >  w. | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 635 | */ | 
 | 636 | static int | 
 | 637 | default_3way_compare(PyObject *v, PyObject *w) | 
 | 638 | { | 
 | 639 | 	int c; | 
| Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 640 | 	char *vname, *wname; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 641 |  | 
 | 642 | 	if (v->ob_type == w->ob_type) { | 
| Barry Warsaw | b0e754d | 2001-01-20 06:24:55 +0000 | [diff] [blame] | 643 | 		/* When comparing these pointers, they must be cast to | 
 | 644 | 		 * integer types (i.e. Py_uintptr_t, our spelling of C9X's | 
 | 645 | 		 * uintptr_t).  ANSI specifies that pointer compares other | 
 | 646 | 		 * than == and != to non-related structures are undefined. | 
 | 647 | 		 */ | 
| Barry Warsaw | 71ff8d5 | 2001-01-20 06:08:10 +0000 | [diff] [blame] | 648 | 		Py_uintptr_t vv = (Py_uintptr_t)v; | 
 | 649 | 		Py_uintptr_t ww = (Py_uintptr_t)w; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 650 | 		return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; | 
 | 651 | 	} | 
 | 652 |  | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 653 | #ifdef Py_USING_UNICODE | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 654 | 	/* Special case for Unicode */ | 
 | 655 | 	if (PyUnicode_Check(v) || PyUnicode_Check(w)) { | 
 | 656 | 		c = PyUnicode_Compare(v, w); | 
 | 657 | 		if (!PyErr_Occurred()) | 
 | 658 | 			return c; | 
 | 659 | 		/* TypeErrors are ignored: if Unicode coercion fails due | 
 | 660 | 		   to one of the arguments not having the right type, we | 
 | 661 | 		   continue as defined by the coercion protocol (see | 
 | 662 | 		   above).  Luckily, decoding errors are reported as | 
 | 663 | 		   ValueErrors and are not masked by this technique. */ | 
 | 664 | 		if (!PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 665 | 			return -2; | 
 | 666 | 		PyErr_Clear(); | 
 | 667 | 	} | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 668 | #endif | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 669 |  | 
| Guido van Rossum | 0871e93 | 2001-01-22 19:28:09 +0000 | [diff] [blame] | 670 | 	/* None is smaller than anything */ | 
 | 671 | 	if (v == Py_None) | 
 | 672 | 		return -1; | 
 | 673 | 	if (w == Py_None) | 
 | 674 | 		return 1; | 
 | 675 |  | 
| Guido van Rossum | fb50d3f | 2003-02-18 16:40:09 +0000 | [diff] [blame] | 676 | 	/* different type: compare type names; numbers are smaller */ | 
 | 677 | 	if (PyNumber_Check(v)) | 
| Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 678 | 		vname = ""; | 
 | 679 | 	else | 
 | 680 | 		vname = v->ob_type->tp_name; | 
| Guido van Rossum | fb50d3f | 2003-02-18 16:40:09 +0000 | [diff] [blame] | 681 | 	if (PyNumber_Check(w)) | 
| Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 682 | 		wname = ""; | 
 | 683 | 	else | 
 | 684 | 		wname = w->ob_type->tp_name; | 
 | 685 | 	c = strcmp(vname, wname); | 
 | 686 | 	if (c < 0) | 
 | 687 | 		return -1; | 
 | 688 | 	if (c > 0) | 
 | 689 | 		return 1; | 
 | 690 | 	/* Same type name, or (more likely) incomparable numeric types */ | 
 | 691 | 	return ((Py_uintptr_t)(v->ob_type) < ( | 
 | 692 | 		Py_uintptr_t)(w->ob_type)) ? -1 : 1; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 693 | } | 
 | 694 |  | 
 | 695 | #define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES) | 
 | 696 |  | 
| Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 697 | /* Do a 3-way comparison, by hook or by crook.  Return: | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 698 |    -2 for an exception (but see below); | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 699 |    -1 if v <  w; | 
| Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 700 |     0 if v == w; | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 701 |     1 if v >  w; | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 702 |    BUT: if the object implements a tp_compare function, it returns | 
| Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 703 |    whatever this function returns (whether with an exception or not). | 
| Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 704 | */ | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 705 | static int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 706 | do_cmp(PyObject *v, PyObject *w) | 
| Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 707 | { | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 708 | 	int c; | 
| Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 709 | 	cmpfunc f; | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 710 |  | 
| Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 711 | 	if (v->ob_type == w->ob_type | 
| Guido van Rossum | 82fc51c1 | 2001-08-16 08:02:45 +0000 | [diff] [blame] | 712 | 	    && (f = v->ob_type->tp_compare) != NULL) { | 
 | 713 | 		c = (*f)(v, w); | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 714 | 		if (PyInstance_Check(v)) { | 
 | 715 | 			/* Instance tp_compare has a different signature. | 
 | 716 | 			   But if it returns undefined we fall through. */ | 
 | 717 | 			if (c != 2) | 
 | 718 | 				return c; | 
| Neal Norwitz | 3f8dae7 | 2002-05-31 20:23:33 +0000 | [diff] [blame] | 719 | 			/* Else fall through to try_rich_to_3way_compare() */ | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 720 | 		} | 
 | 721 | 		else | 
 | 722 | 			return adjust_tp_compare(c); | 
| Guido van Rossum | 82fc51c1 | 2001-08-16 08:02:45 +0000 | [diff] [blame] | 723 | 	} | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 724 | 	/* We only get here if one of the following is true: | 
 | 725 | 	   a) v and w have different types | 
 | 726 | 	   b) v and w have the same type, which doesn't have tp_compare | 
 | 727 | 	   c) v and w are instances, and either __cmp__ is not defined or | 
 | 728 | 	      __cmp__ returns NotImplemented | 
 | 729 | 	*/ | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 730 | 	c = try_rich_to_3way_compare(v, w); | 
 | 731 | 	if (c < 2) | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 732 | 		return c; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 733 | 	c = try_3way_compare(v, w); | 
 | 734 | 	if (c < 2) | 
 | 735 | 		return c; | 
 | 736 | 	return default_3way_compare(v, w); | 
| Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 737 | } | 
 | 738 |  | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 739 | /* Compare v to w.  Return | 
 | 740 |    -1 if v <  w or exception (PyErr_Occurred() true in latter case). | 
 | 741 |     0 if v == w. | 
 | 742 |     1 if v > w. | 
 | 743 |    XXX The docs (C API manual) say the return value is undefined in case | 
 | 744 |    XXX of error. | 
 | 745 | */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 746 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 747 | PyObject_Compare(PyObject *v, PyObject *w) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 748 | { | 
| Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 749 | 	int result; | 
 | 750 |  | 
| Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 751 | 	if (v == NULL || w == NULL) { | 
 | 752 | 		PyErr_BadInternalCall(); | 
 | 753 | 		return -1; | 
 | 754 | 	} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 755 | 	if (v == w) | 
 | 756 | 		return 0; | 
| Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 757 | 	if (Py_EnterRecursiveCall(" in cmp")) | 
 | 758 | 		return -1; | 
 | 759 | 	result = do_cmp(v, w); | 
 | 760 | 	Py_LeaveRecursiveCall(); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 761 | 	return result < 0 ? -1 : result; | 
 | 762 | } | 
 | 763 |  | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 764 | /* Return (new reference to) Py_True or Py_False. */ | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 765 | static PyObject * | 
| Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 766 | convert_3way_to_object(int op, int c) | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 767 | { | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 768 | 	PyObject *result; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 769 | 	switch (op) { | 
 | 770 | 	case Py_LT: c = c <  0; break; | 
 | 771 | 	case Py_LE: c = c <= 0; break; | 
 | 772 | 	case Py_EQ: c = c == 0; break; | 
 | 773 | 	case Py_NE: c = c != 0; break; | 
 | 774 | 	case Py_GT: c = c >  0; break; | 
 | 775 | 	case Py_GE: c = c >= 0; break; | 
 | 776 | 	} | 
 | 777 | 	result = c ? Py_True : Py_False; | 
 | 778 | 	Py_INCREF(result); | 
| Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 779 | 	return result; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | } | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 781 |  | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 782 | /* We want a rich comparison but don't have one.  Try a 3-way cmp instead. | 
 | 783 |    Return | 
 | 784 |    NULL      if error | 
 | 785 |    Py_True   if v op w | 
 | 786 |    Py_False  if not (v op w) | 
 | 787 | */ | 
| Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 788 | static PyObject * | 
 | 789 | try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) | 
 | 790 | { | 
 | 791 | 	int c; | 
 | 792 |  | 
 | 793 | 	c = try_3way_compare(v, w); | 
 | 794 | 	if (c >= 2) | 
 | 795 | 		c = default_3way_compare(v, w); | 
 | 796 | 	if (c <= -2) | 
 | 797 | 		return NULL; | 
 | 798 | 	return convert_3way_to_object(op, c); | 
 | 799 | } | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 |  | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 801 | /* Do rich comparison on v and w.  Return | 
 | 802 |    NULL      if error | 
 | 803 |    Else a new reference to an object other than Py_NotImplemented, usually(?): | 
 | 804 |    Py_True   if v op w | 
 | 805 |    Py_False  if not (v op w) | 
 | 806 | */ | 
| Neil Schemenauer | d38855c | 2001-01-21 16:25:18 +0000 | [diff] [blame] | 807 | static PyObject * | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 808 | do_richcmp(PyObject *v, PyObject *w, int op) | 
 | 809 | { | 
 | 810 | 	PyObject *res; | 
 | 811 |  | 
 | 812 | 	res = try_rich_compare(v, w, op); | 
 | 813 | 	if (res != Py_NotImplemented) | 
 | 814 | 		return res; | 
 | 815 | 	Py_DECREF(res); | 
 | 816 |  | 
 | 817 | 	return try_3way_to_rich_compare(v, w, op); | 
 | 818 | } | 
 | 819 |  | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 820 | /* Return: | 
 | 821 |    NULL for exception; | 
| Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 822 |    some object not equal to NotImplemented if it is implemented | 
 | 823 |      (this latter object may not be a Boolean). | 
 | 824 | */ | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 825 | PyObject * | 
 | 826 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) | 
 | 827 | { | 
 | 828 | 	PyObject *res; | 
 | 829 |  | 
 | 830 | 	assert(Py_LT <= op && op <= Py_GE); | 
| Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 831 | 	if (Py_EnterRecursiveCall(" in cmp")) | 
 | 832 | 		return NULL; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 833 |  | 
| Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 834 | 	/* If the types are equal, and not old-style instances, try to | 
| Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 835 | 	   get out cheap (don't bother with coercions etc.). */ | 
 | 836 | 	if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { | 
 | 837 | 		cmpfunc fcmp; | 
 | 838 | 		richcmpfunc frich = RICHCOMPARE(v->ob_type); | 
 | 839 | 		/* If the type has richcmp, try it first.  try_rich_compare | 
 | 840 | 		   tries it two-sided, which is not needed since we've a | 
 | 841 | 		   single type only. */ | 
 | 842 | 		if (frich != NULL) { | 
 | 843 | 			res = (*frich)(v, w, op); | 
 | 844 | 			if (res != Py_NotImplemented) | 
 | 845 | 				goto Done; | 
 | 846 | 			Py_DECREF(res); | 
 | 847 | 		} | 
 | 848 | 		/* No richcmp, or this particular richmp not implemented. | 
 | 849 | 		   Try 3-way cmp. */ | 
 | 850 | 		fcmp = v->ob_type->tp_compare; | 
 | 851 | 		if (fcmp != NULL) { | 
 | 852 | 			int c = (*fcmp)(v, w); | 
| Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 853 | 			c = adjust_tp_compare(c); | 
 | 854 | 			if (c == -2) { | 
| Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 855 | 				res = NULL; | 
 | 856 | 				goto Done; | 
 | 857 | 			} | 
 | 858 | 			res = convert_3way_to_object(op, c); | 
 | 859 | 			goto Done; | 
 | 860 | 		} | 
| Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 861 | 	} | 
| Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 862 |  | 
 | 863 | 	/* Fast path not taken, or couldn't deliver a useful result. */ | 
 | 864 | 	res = do_richcmp(v, w, op); | 
 | 865 | Done: | 
| Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 866 | 	Py_LeaveRecursiveCall(); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 867 | 	return res; | 
 | 868 | } | 
 | 869 |  | 
| Tim Peters | de9725f | 2001-05-05 10:06:17 +0000 | [diff] [blame] | 870 | /* Return -1 if error; 1 if v op w; 0 if not (v op w). */ | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 871 | int | 
 | 872 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) | 
 | 873 | { | 
| Raymond Hettinger | 93d4481 | 2004-03-21 17:01:44 +0000 | [diff] [blame] | 874 | 	PyObject *res; | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 875 | 	int ok; | 
 | 876 |  | 
| Raymond Hettinger | 93d4481 | 2004-03-21 17:01:44 +0000 | [diff] [blame] | 877 | 	/* Quick result when objects are the same. | 
 | 878 | 	   Guarantees that identity implies equality. */ | 
 | 879 | 	if (v == w) { | 
 | 880 | 		if (op == Py_EQ) | 
 | 881 | 			return 1; | 
 | 882 | 		else if (op == Py_NE) | 
 | 883 | 			return 0; | 
 | 884 | 	} | 
 | 885 |  | 
 | 886 | 	res = PyObject_RichCompare(v, w, op); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 887 | 	if (res == NULL) | 
 | 888 | 		return -1; | 
| Guido van Rossum | 81912d4 | 2002-08-24 05:33:28 +0000 | [diff] [blame] | 889 | 	if (PyBool_Check(res)) | 
 | 890 | 		ok = (res == Py_True); | 
 | 891 | 	else | 
 | 892 | 		ok = PyObject_IsTrue(res); | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 893 | 	Py_DECREF(res); | 
 | 894 | 	return ok; | 
 | 895 | } | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 896 |  | 
 | 897 | /* Set of hash utility functions to help maintaining the invariant that | 
| Raymond Hettinger | 8183fa4 | 2004-03-21 17:35:06 +0000 | [diff] [blame] | 898 | 	if a==b then hash(a)==hash(b) | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 899 |  | 
 | 900 |    All the utility functions (_Py_Hash*()) return "-1" to signify an error. | 
 | 901 | */ | 
 | 902 |  | 
 | 903 | long | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 904 | _Py_HashDouble(double v) | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 905 | { | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 906 | 	double intpart, fractpart; | 
 | 907 | 	int expo; | 
 | 908 | 	long hipart; | 
 | 909 | 	long x;		/* the final hash value */ | 
 | 910 | 	/* This is designed so that Python numbers of different types | 
 | 911 | 	 * that compare equal hash to the same value; otherwise comparisons | 
 | 912 | 	 * of mapping keys will turn out weird. | 
 | 913 | 	 */ | 
 | 914 |  | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 915 | 	fractpart = modf(v, &intpart); | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 916 | 	if (fractpart == 0.0) { | 
 | 917 | 		/* This must return the same hash as an equal int or long. */ | 
 | 918 | 		if (intpart > LONG_MAX || -intpart > LONG_MAX) { | 
 | 919 | 			/* Convert to long and use its hash. */ | 
 | 920 | 			PyObject *plong;	/* converted to Python long */ | 
 | 921 | 			if (Py_IS_INFINITY(intpart)) | 
 | 922 | 				/* can't convert to long int -- arbitrary */ | 
 | 923 | 				v = v < 0 ? -271828.0 : 314159.0; | 
 | 924 | 			plong = PyLong_FromDouble(v); | 
 | 925 | 			if (plong == NULL) | 
 | 926 | 				return -1; | 
 | 927 | 			x = PyObject_Hash(plong); | 
 | 928 | 			Py_DECREF(plong); | 
 | 929 | 			return x; | 
 | 930 | 		} | 
 | 931 | 		/* Fits in a C long == a Python int, so is its own hash. */ | 
 | 932 | 		x = (long)intpart; | 
 | 933 | 		if (x == -1) | 
 | 934 | 			x = -2; | 
 | 935 | 		return x; | 
 | 936 | 	} | 
 | 937 | 	/* The fractional part is non-zero, so we don't have to worry about | 
 | 938 | 	 * making this match the hash of some other type. | 
 | 939 | 	 * Use frexp to get at the bits in the double. | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 940 | 	 * Since the VAX D double format has 56 mantissa bits, which is the | 
 | 941 | 	 * most of any double format in use, each of these parts may have as | 
 | 942 | 	 * many as (but no more than) 56 significant bits. | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 943 | 	 * So, assuming sizeof(long) >= 4, each part can be broken into two | 
 | 944 | 	 * longs; frexp and multiplication are used to do that. | 
 | 945 | 	 * Also, since the Cray double format has 15 exponent bits, which is | 
 | 946 | 	 * the most of any double format in use, shifting the exponent field | 
 | 947 | 	 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 948 | 	 */ | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 949 | 	v = frexp(v, &expo); | 
 | 950 | 	v *= 2147483648.0;	/* 2**31 */ | 
 | 951 | 	hipart = (long)v;	/* take the top 32 bits */ | 
 | 952 | 	v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ | 
 | 953 | 	x = hipart + (long)v + (expo << 15); | 
 | 954 | 	if (x == -1) | 
 | 955 | 		x = -2; | 
 | 956 | 	return x; | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 957 | } | 
 | 958 |  | 
 | 959 | long | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 960 | _Py_HashPointer(void *p) | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 961 | { | 
 | 962 | #if SIZEOF_LONG >= SIZEOF_VOID_P | 
 | 963 | 	return (long)p; | 
 | 964 | #else | 
 | 965 | 	/* convert to a Python long and hash that */ | 
 | 966 | 	PyObject* longobj; | 
 | 967 | 	long x; | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 968 |  | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 969 | 	if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { | 
 | 970 | 		x = -1; | 
 | 971 | 		goto finally; | 
 | 972 | 	} | 
 | 973 | 	x = PyObject_Hash(longobj); | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 974 |  | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 975 | finally: | 
 | 976 | 	Py_XDECREF(longobj); | 
 | 977 | 	return x; | 
 | 978 | #endif | 
 | 979 | } | 
 | 980 |  | 
 | 981 |  | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 982 | long | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 983 | PyObject_Hash(PyObject *v) | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 984 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | 	PyTypeObject *tp = v->ob_type; | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 986 | 	if (tp->tp_hash != NULL) | 
 | 987 | 		return (*tp->tp_hash)(v); | 
| Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 988 | 	if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { | 
| Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 989 | 		return _Py_HashPointer(v); /* Use address as hash value */ | 
 | 990 | 	} | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 991 | 	/* If there's a cmp but no hash defined, the object can't be hashed */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 992 | 	PyErr_SetString(PyExc_TypeError, "unhashable type"); | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 993 | 	return -1; | 
 | 994 | } | 
 | 995 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 996 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 997 | PyObject_GetAttrString(PyObject *v, char *name) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 998 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 999 | 	PyObject *w, *res; | 
| Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1000 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1001 | 	if (v->ob_type->tp_getattr != NULL) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1002 | 		return (*v->ob_type->tp_getattr)(v, name); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1003 | 	w = PyString_InternFromString(name); | 
 | 1004 | 	if (w == NULL) | 
 | 1005 | 		return NULL; | 
 | 1006 | 	res = PyObject_GetAttr(v, w); | 
 | 1007 | 	Py_XDECREF(w); | 
 | 1008 | 	return res; | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1009 | } | 
 | 1010 |  | 
 | 1011 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1012 | PyObject_HasAttrString(PyObject *v, char *name) | 
| Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1013 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1014 | 	PyObject *res = PyObject_GetAttrString(v, name); | 
| Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1015 | 	if (res != NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1016 | 		Py_DECREF(res); | 
| Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1017 | 		return 1; | 
 | 1018 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1019 | 	PyErr_Clear(); | 
| Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1020 | 	return 0; | 
 | 1021 | } | 
 | 1022 |  | 
 | 1023 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1024 | PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1025 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1026 | 	PyObject *s; | 
 | 1027 | 	int res; | 
| Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1028 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1029 | 	if (v->ob_type->tp_setattr != NULL) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1030 | 		return (*v->ob_type->tp_setattr)(v, name, w); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1031 | 	s = PyString_InternFromString(name); | 
 | 1032 | 	if (s == NULL) | 
 | 1033 | 		return -1; | 
 | 1034 | 	res = PyObject_SetAttr(v, s, w); | 
 | 1035 | 	Py_XDECREF(s); | 
 | 1036 | 	return res; | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1037 | } | 
 | 1038 |  | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1039 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1040 | PyObject_GetAttr(PyObject *v, PyObject *name) | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1041 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1042 | 	PyTypeObject *tp = v->ob_type; | 
 | 1043 |  | 
| Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1044 | 	if (!PyString_Check(name)) { | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1045 | #ifdef Py_USING_UNICODE | 
 | 1046 | 		/* The Unicode to string conversion is done here because the | 
 | 1047 | 		   existing tp_getattro slots expect a string object as name | 
 | 1048 | 		   and we wouldn't want to break those. */ | 
 | 1049 | 		if (PyUnicode_Check(name)) { | 
 | 1050 | 			name = _PyUnicode_AsDefaultEncodedString(name, NULL); | 
 | 1051 | 			if (name == NULL) | 
 | 1052 | 				return NULL; | 
 | 1053 | 		} | 
 | 1054 | 		else | 
 | 1055 | #endif | 
 | 1056 | 		{ | 
 | 1057 | 			PyErr_SetString(PyExc_TypeError, | 
 | 1058 | 					"attribute name must be string"); | 
 | 1059 | 			return NULL; | 
 | 1060 | 		} | 
| Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1061 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1062 | 	if (tp->tp_getattro != NULL) | 
 | 1063 | 		return (*tp->tp_getattro)(v, name); | 
 | 1064 | 	if (tp->tp_getattr != NULL) | 
 | 1065 | 		return (*tp->tp_getattr)(v, PyString_AS_STRING(name)); | 
 | 1066 | 	PyErr_Format(PyExc_AttributeError, | 
 | 1067 | 		     "'%.50s' object has no attribute '%.400s'", | 
 | 1068 | 		     tp->tp_name, PyString_AS_STRING(name)); | 
 | 1069 | 	return NULL; | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1070 | } | 
 | 1071 |  | 
 | 1072 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1073 | PyObject_HasAttr(PyObject *v, PyObject *name) | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1074 | { | 
 | 1075 | 	PyObject *res = PyObject_GetAttr(v, name); | 
 | 1076 | 	if (res != NULL) { | 
 | 1077 | 		Py_DECREF(res); | 
 | 1078 | 		return 1; | 
 | 1079 | 	} | 
 | 1080 | 	PyErr_Clear(); | 
 | 1081 | 	return 0; | 
 | 1082 | } | 
 | 1083 |  | 
 | 1084 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1085 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1086 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1087 | 	PyTypeObject *tp = v->ob_type; | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1088 | 	int err; | 
| Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1089 |  | 
| Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1090 | 	if (!PyString_Check(name)){ | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1091 | #ifdef Py_USING_UNICODE | 
 | 1092 | 		/* The Unicode to string conversion is done here because the | 
 | 1093 | 		   existing tp_setattro slots expect a string object as name | 
 | 1094 | 		   and we wouldn't want to break those. */ | 
 | 1095 | 		if (PyUnicode_Check(name)) { | 
 | 1096 | 			name = PyUnicode_AsEncodedString(name, NULL, NULL); | 
 | 1097 | 			if (name == NULL) | 
 | 1098 | 				return -1; | 
 | 1099 | 		} | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1100 | 		else | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1101 | #endif | 
 | 1102 | 		{ | 
 | 1103 | 			PyErr_SetString(PyExc_TypeError, | 
 | 1104 | 					"attribute name must be string"); | 
 | 1105 | 			return -1; | 
 | 1106 | 		} | 
| Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1107 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1108 | 	else | 
 | 1109 | 		Py_INCREF(name); | 
 | 1110 |  | 
 | 1111 | 	PyString_InternInPlace(&name); | 
 | 1112 | 	if (tp->tp_setattro != NULL) { | 
 | 1113 | 		err = (*tp->tp_setattro)(v, name, value); | 
 | 1114 | 		Py_DECREF(name); | 
 | 1115 | 		return err; | 
| Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1116 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1117 | 	if (tp->tp_setattr != NULL) { | 
 | 1118 | 		err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value); | 
 | 1119 | 		Py_DECREF(name); | 
 | 1120 | 		return err; | 
 | 1121 | 	} | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1122 | 	Py_DECREF(name); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1123 | 	if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) | 
 | 1124 | 		PyErr_Format(PyExc_TypeError, | 
 | 1125 | 			     "'%.100s' object has no attributes " | 
 | 1126 | 			     "(%s .%.100s)", | 
 | 1127 | 			     tp->tp_name, | 
 | 1128 | 			     value==NULL ? "del" : "assign to", | 
 | 1129 | 			     PyString_AS_STRING(name)); | 
 | 1130 | 	else | 
 | 1131 | 		PyErr_Format(PyExc_TypeError, | 
 | 1132 | 			     "'%.100s' object has only read-only attributes " | 
 | 1133 | 			     "(%s .%.100s)", | 
 | 1134 | 			     tp->tp_name, | 
 | 1135 | 			     value==NULL ? "del" : "assign to", | 
 | 1136 | 			     PyString_AS_STRING(name)); | 
 | 1137 | 	return -1; | 
 | 1138 | } | 
 | 1139 |  | 
 | 1140 | /* Helper to get a pointer to an object's __dict__ slot, if any */ | 
 | 1141 |  | 
 | 1142 | PyObject ** | 
 | 1143 | _PyObject_GetDictPtr(PyObject *obj) | 
 | 1144 | { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1145 | 	long dictoffset; | 
 | 1146 | 	PyTypeObject *tp = obj->ob_type; | 
 | 1147 |  | 
 | 1148 | 	if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS)) | 
 | 1149 | 		return NULL; | 
 | 1150 | 	dictoffset = tp->tp_dictoffset; | 
 | 1151 | 	if (dictoffset == 0) | 
 | 1152 | 		return NULL; | 
 | 1153 | 	if (dictoffset < 0) { | 
| Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 1154 | 		int tsize; | 
 | 1155 | 		size_t size; | 
 | 1156 |  | 
 | 1157 | 		tsize = ((PyVarObject *)obj)->ob_size; | 
 | 1158 | 		if (tsize < 0) | 
 | 1159 | 			tsize = -tsize; | 
 | 1160 | 		size = _PyObject_VAR_SIZE(tp, tsize); | 
 | 1161 |  | 
| Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1162 | 		dictoffset += (long)size; | 
 | 1163 | 		assert(dictoffset > 0); | 
 | 1164 | 		assert(dictoffset % SIZEOF_VOID_P == 0); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1165 | 	} | 
 | 1166 | 	return (PyObject **) ((char *)obj + dictoffset); | 
 | 1167 | } | 
 | 1168 |  | 
 | 1169 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ | 
 | 1170 |  | 
 | 1171 | PyObject * | 
| Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1172 | PyObject_SelfIter(PyObject *obj) | 
| Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 1173 | { | 
 | 1174 | 	Py_INCREF(obj); | 
 | 1175 | 	return obj; | 
 | 1176 | } | 
 | 1177 |  | 
 | 1178 | PyObject * | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1179 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) | 
 | 1180 | { | 
 | 1181 | 	PyTypeObject *tp = obj->ob_type; | 
| Guido van Rossum | 056fbf4 | 2002-08-19 19:22:50 +0000 | [diff] [blame] | 1182 | 	PyObject *descr = NULL; | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1183 | 	PyObject *res = NULL; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1184 | 	descrgetfunc f; | 
| Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1185 | 	long dictoffset; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1186 | 	PyObject **dictptr; | 
 | 1187 |  | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1188 | 	if (!PyString_Check(name)){ | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1189 | #ifdef Py_USING_UNICODE | 
 | 1190 | 		/* The Unicode to string conversion is done here because the | 
 | 1191 | 		   existing tp_setattro slots expect a string object as name | 
 | 1192 | 		   and we wouldn't want to break those. */ | 
 | 1193 | 		if (PyUnicode_Check(name)) { | 
 | 1194 | 			name = PyUnicode_AsEncodedString(name, NULL, NULL); | 
 | 1195 | 			if (name == NULL) | 
 | 1196 | 				return NULL; | 
 | 1197 | 		} | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1198 | 		else | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1199 | #endif | 
 | 1200 | 		{ | 
 | 1201 | 			PyErr_SetString(PyExc_TypeError, | 
 | 1202 | 					"attribute name must be string"); | 
 | 1203 | 			return NULL; | 
 | 1204 | 		} | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1205 | 	} | 
 | 1206 | 	else | 
 | 1207 | 		Py_INCREF(name); | 
 | 1208 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1209 | 	if (tp->tp_dict == NULL) { | 
| Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1210 | 		if (PyType_Ready(tp) < 0) | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1211 | 			goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1212 | 	} | 
 | 1213 |  | 
| Guido van Rossum | 056fbf4 | 2002-08-19 19:22:50 +0000 | [diff] [blame] | 1214 | 	/* Inline _PyType_Lookup */ | 
 | 1215 | 	{ | 
 | 1216 | 		int i, n; | 
 | 1217 | 		PyObject *mro, *base, *dict; | 
 | 1218 |  | 
 | 1219 | 		/* Look in tp_dict of types in MRO */ | 
 | 1220 | 		mro = tp->tp_mro; | 
 | 1221 | 		assert(mro != NULL); | 
 | 1222 | 		assert(PyTuple_Check(mro)); | 
 | 1223 | 		n = PyTuple_GET_SIZE(mro); | 
 | 1224 | 		for (i = 0; i < n; i++) { | 
 | 1225 | 			base = PyTuple_GET_ITEM(mro, i); | 
 | 1226 | 			if (PyClass_Check(base)) | 
 | 1227 | 				dict = ((PyClassObject *)base)->cl_dict; | 
 | 1228 | 			else { | 
 | 1229 | 				assert(PyType_Check(base)); | 
 | 1230 | 				dict = ((PyTypeObject *)base)->tp_dict; | 
 | 1231 | 			} | 
 | 1232 | 			assert(dict && PyDict_Check(dict)); | 
 | 1233 | 			descr = PyDict_GetItem(dict, name); | 
 | 1234 | 			if (descr != NULL) | 
 | 1235 | 				break; | 
 | 1236 | 		} | 
 | 1237 | 	} | 
 | 1238 |  | 
| Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1239 | 	Py_XINCREF(descr); | 
 | 1240 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1241 | 	f = NULL; | 
| Guido van Rossum | 90195e2 | 2003-02-19 03:19:29 +0000 | [diff] [blame] | 1242 | 	if (descr != NULL && | 
 | 1243 | 	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1244 | 		f = descr->ob_type->tp_descr_get; | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1245 | 		if (f != NULL && PyDescr_IsData(descr)) { | 
 | 1246 | 			res = f(descr, obj, (PyObject *)obj->ob_type); | 
| Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1247 | 			Py_DECREF(descr); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1248 | 			goto done; | 
 | 1249 | 		} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1250 | 	} | 
 | 1251 |  | 
| Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1252 | 	/* Inline _PyObject_GetDictPtr */ | 
 | 1253 | 	dictoffset = tp->tp_dictoffset; | 
 | 1254 | 	if (dictoffset != 0) { | 
 | 1255 | 		PyObject *dict; | 
 | 1256 | 		if (dictoffset < 0) { | 
 | 1257 | 			int tsize; | 
 | 1258 | 			size_t size; | 
 | 1259 |  | 
 | 1260 | 			tsize = ((PyVarObject *)obj)->ob_size; | 
 | 1261 | 			if (tsize < 0) | 
 | 1262 | 				tsize = -tsize; | 
 | 1263 | 			size = _PyObject_VAR_SIZE(tp, tsize); | 
 | 1264 |  | 
 | 1265 | 			dictoffset += (long)size; | 
 | 1266 | 			assert(dictoffset > 0); | 
 | 1267 | 			assert(dictoffset % SIZEOF_VOID_P == 0); | 
 | 1268 | 		} | 
 | 1269 | 		dictptr = (PyObject **) ((char *)obj + dictoffset); | 
 | 1270 | 		dict = *dictptr; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1271 | 		if (dict != NULL) { | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1272 | 			res = PyDict_GetItem(dict, name); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1273 | 			if (res != NULL) { | 
 | 1274 | 				Py_INCREF(res); | 
| Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1275 | 				Py_XDECREF(descr); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1276 | 				goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1277 | 			} | 
 | 1278 | 		} | 
 | 1279 | 	} | 
 | 1280 |  | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1281 | 	if (f != NULL) { | 
 | 1282 | 		res = f(descr, obj, (PyObject *)obj->ob_type); | 
| Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1283 | 		Py_DECREF(descr); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1284 | 		goto done; | 
 | 1285 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1286 |  | 
 | 1287 | 	if (descr != NULL) { | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1288 | 		res = descr; | 
| Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1289 | 		/* descr was already increfed above */ | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1290 | 		goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1291 | 	} | 
 | 1292 |  | 
 | 1293 | 	PyErr_Format(PyExc_AttributeError, | 
 | 1294 | 		     "'%.50s' object has no attribute '%.400s'", | 
 | 1295 | 		     tp->tp_name, PyString_AS_STRING(name)); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1296 |   done: | 
 | 1297 | 	Py_DECREF(name); | 
 | 1298 | 	return res; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1299 | } | 
 | 1300 |  | 
 | 1301 | int | 
 | 1302 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) | 
 | 1303 | { | 
 | 1304 | 	PyTypeObject *tp = obj->ob_type; | 
 | 1305 | 	PyObject *descr; | 
 | 1306 | 	descrsetfunc f; | 
 | 1307 | 	PyObject **dictptr; | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1308 | 	int res = -1; | 
 | 1309 |  | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1310 | 	if (!PyString_Check(name)){ | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1311 | #ifdef Py_USING_UNICODE | 
 | 1312 | 		/* The Unicode to string conversion is done here because the | 
 | 1313 | 		   existing tp_setattro slots expect a string object as name | 
 | 1314 | 		   and we wouldn't want to break those. */ | 
 | 1315 | 		if (PyUnicode_Check(name)) { | 
 | 1316 | 			name = PyUnicode_AsEncodedString(name, NULL, NULL); | 
 | 1317 | 			if (name == NULL) | 
 | 1318 | 				return -1; | 
 | 1319 | 		} | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1320 | 		else | 
| Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1321 | #endif | 
 | 1322 | 		{ | 
 | 1323 | 			PyErr_SetString(PyExc_TypeError, | 
 | 1324 | 					"attribute name must be string"); | 
 | 1325 | 			return -1; | 
 | 1326 | 		} | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1327 | 	} | 
 | 1328 | 	else | 
 | 1329 | 		Py_INCREF(name); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1330 |  | 
 | 1331 | 	if (tp->tp_dict == NULL) { | 
| Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1332 | 		if (PyType_Ready(tp) < 0) | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1333 | 			goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1334 | 	} | 
 | 1335 |  | 
 | 1336 | 	descr = _PyType_Lookup(tp, name); | 
 | 1337 | 	f = NULL; | 
| Guido van Rossum | 90195e2 | 2003-02-19 03:19:29 +0000 | [diff] [blame] | 1338 | 	if (descr != NULL && | 
 | 1339 | 	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1340 | 		f = descr->ob_type->tp_descr_set; | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1341 | 		if (f != NULL && PyDescr_IsData(descr)) { | 
 | 1342 | 			res = f(descr, obj, value); | 
 | 1343 | 			goto done; | 
 | 1344 | 		} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1345 | 	} | 
 | 1346 |  | 
 | 1347 | 	dictptr = _PyObject_GetDictPtr(obj); | 
 | 1348 | 	if (dictptr != NULL) { | 
 | 1349 | 		PyObject *dict = *dictptr; | 
 | 1350 | 		if (dict == NULL && value != NULL) { | 
 | 1351 | 			dict = PyDict_New(); | 
 | 1352 | 			if (dict == NULL) | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1353 | 				goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1354 | 			*dictptr = dict; | 
 | 1355 | 		} | 
 | 1356 | 		if (dict != NULL) { | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1357 | 			if (value == NULL) | 
 | 1358 | 				res = PyDict_DelItem(dict, name); | 
 | 1359 | 			else | 
 | 1360 | 				res = PyDict_SetItem(dict, name, value); | 
 | 1361 | 			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | 
 | 1362 | 				PyErr_SetObject(PyExc_AttributeError, name); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1363 | 			goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1364 | 		} | 
 | 1365 | 	} | 
 | 1366 |  | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1367 | 	if (f != NULL) { | 
 | 1368 | 		res = f(descr, obj, value); | 
 | 1369 | 		goto done; | 
 | 1370 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1371 |  | 
 | 1372 | 	if (descr == NULL) { | 
 | 1373 | 		PyErr_Format(PyExc_AttributeError, | 
 | 1374 | 			     "'%.50s' object has no attribute '%.400s'", | 
 | 1375 | 			     tp->tp_name, PyString_AS_STRING(name)); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1376 | 		goto done; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1377 | 	} | 
 | 1378 |  | 
 | 1379 | 	PyErr_Format(PyExc_AttributeError, | 
 | 1380 | 		     "'%.50s' object attribute '%.400s' is read-only", | 
 | 1381 | 		     tp->tp_name, PyString_AS_STRING(name)); | 
| Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1382 |   done: | 
 | 1383 | 	Py_DECREF(name); | 
 | 1384 | 	return res; | 
| Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1385 | } | 
 | 1386 |  | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1387 | /* Test a value used as condition, e.g., in a for or if statement. | 
 | 1388 |    Return -1 if an error occurred */ | 
 | 1389 |  | 
 | 1390 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1391 | PyObject_IsTrue(PyObject *v) | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1392 | { | 
 | 1393 | 	int res; | 
| Guido van Rossum | 6248f44 | 2002-08-24 06:31:34 +0000 | [diff] [blame] | 1394 | 	if (v == Py_True) | 
 | 1395 | 		return 1; | 
 | 1396 | 	if (v == Py_False) | 
 | 1397 | 		return 0; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1398 | 	if (v == Py_None) | 
| Neal Norwitz | 51290d3 | 2002-06-13 21:32:44 +0000 | [diff] [blame] | 1399 | 		return 0; | 
| Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1400 | 	else if (v->ob_type->tp_as_number != NULL && | 
 | 1401 | 		 v->ob_type->tp_as_number->nb_nonzero != NULL) | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1402 | 		res = (*v->ob_type->tp_as_number->nb_nonzero)(v); | 
| Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1403 | 	else if (v->ob_type->tp_as_mapping != NULL && | 
 | 1404 | 		 v->ob_type->tp_as_mapping->mp_length != NULL) | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1405 | 		res = (*v->ob_type->tp_as_mapping->mp_length)(v); | 
| Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1406 | 	else if (v->ob_type->tp_as_sequence != NULL && | 
 | 1407 | 		 v->ob_type->tp_as_sequence->sq_length != NULL) | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1408 | 		res = (*v->ob_type->tp_as_sequence->sq_length)(v); | 
 | 1409 | 	else | 
| Neal Norwitz | 51290d3 | 2002-06-13 21:32:44 +0000 | [diff] [blame] | 1410 | 		return 1; | 
 | 1411 | 	return (res > 0) ? 1 : res; | 
| Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1412 | } | 
 | 1413 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1414 | /* equivalent of 'not v' | 
| Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1415 |    Return -1 if an error occurred */ | 
 | 1416 |  | 
 | 1417 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1418 | PyObject_Not(PyObject *v) | 
| Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1419 | { | 
 | 1420 | 	int res; | 
 | 1421 | 	res = PyObject_IsTrue(v); | 
 | 1422 | 	if (res < 0) | 
 | 1423 | 		return res; | 
 | 1424 | 	return res == 0; | 
 | 1425 | } | 
 | 1426 |  | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1427 | /* Coerce two numeric types to the "larger" one. | 
 | 1428 |    Increment the reference count on each argument. | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1429 |    Return value: | 
 | 1430 |    -1 if an error occurred; | 
 | 1431 |    0 if the coercion succeeded (and then the reference counts are increased); | 
 | 1432 |    1 if no coercion is possible (and no error is raised). | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1433 | */ | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1434 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1435 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1436 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1437 | 	register PyObject *v = *pv; | 
 | 1438 | 	register PyObject *w = *pw; | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1439 | 	int res; | 
 | 1440 |  | 
| Guido van Rossum | 517c7d4 | 2002-04-26 02:49:14 +0000 | [diff] [blame] | 1441 | 	/* Shortcut only for old-style types */ | 
 | 1442 | 	if (v->ob_type == w->ob_type && | 
 | 1443 | 	    !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES)) | 
 | 1444 | 	{ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1445 | 		Py_INCREF(v); | 
 | 1446 | 		Py_INCREF(w); | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1447 | 		return 0; | 
 | 1448 | 	} | 
 | 1449 | 	if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { | 
 | 1450 | 		res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); | 
 | 1451 | 		if (res <= 0) | 
 | 1452 | 			return res; | 
 | 1453 | 	} | 
 | 1454 | 	if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { | 
 | 1455 | 		res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); | 
 | 1456 | 		if (res <= 0) | 
 | 1457 | 			return res; | 
 | 1458 | 	} | 
| Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1459 | 	return 1; | 
 | 1460 | } | 
 | 1461 |  | 
| Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1462 | /* Coerce two numeric types to the "larger" one. | 
 | 1463 |    Increment the reference count on each argument. | 
 | 1464 |    Return -1 and raise an exception if no coercion is possible | 
 | 1465 |    (and then no reference count is incremented). | 
 | 1466 | */ | 
| Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1467 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1468 | PyNumber_Coerce(PyObject **pv, PyObject **pw) | 
| Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1469 | { | 
 | 1470 | 	int err = PyNumber_CoerceEx(pv, pw); | 
 | 1471 | 	if (err <= 0) | 
 | 1472 | 		return err; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1473 | 	PyErr_SetString(PyExc_TypeError, "number coercion failed"); | 
| Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1474 | 	return -1; | 
 | 1475 | } | 
 | 1476 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1477 |  | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1478 | /* Test whether an object can be called */ | 
 | 1479 |  | 
 | 1480 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1481 | PyCallable_Check(PyObject *x) | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1482 | { | 
 | 1483 | 	if (x == NULL) | 
 | 1484 | 		return 0; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1485 | 	if (PyInstance_Check(x)) { | 
 | 1486 | 		PyObject *call = PyObject_GetAttrString(x, "__call__"); | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1487 | 		if (call == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1488 | 			PyErr_Clear(); | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1489 | 			return 0; | 
 | 1490 | 		} | 
 | 1491 | 		/* Could test recursively but don't, for fear of endless | 
 | 1492 | 		   recursion if some joker sets self.__call__ = self */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1493 | 		Py_DECREF(call); | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1494 | 		return 1; | 
 | 1495 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1496 | 	else { | 
 | 1497 | 		return x->ob_type->tp_call != NULL; | 
 | 1498 | 	} | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1499 | } | 
 | 1500 |  | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1501 | /* Helper for PyObject_Dir. | 
 | 1502 |    Merge the __dict__ of aclass into dict, and recursively also all | 
 | 1503 |    the __dict__s of aclass's base classes.  The order of merging isn't | 
 | 1504 |    defined, as it's expected that only the final set of dict keys is | 
 | 1505 |    interesting. | 
 | 1506 |    Return 0 on success, -1 on error. | 
 | 1507 | */ | 
 | 1508 |  | 
 | 1509 | static int | 
 | 1510 | merge_class_dict(PyObject* dict, PyObject* aclass) | 
 | 1511 | { | 
 | 1512 | 	PyObject *classdict; | 
 | 1513 | 	PyObject *bases; | 
 | 1514 |  | 
 | 1515 | 	assert(PyDict_Check(dict)); | 
 | 1516 | 	assert(aclass); | 
 | 1517 |  | 
 | 1518 | 	/* Merge in the type's dict (if any). */ | 
 | 1519 | 	classdict = PyObject_GetAttrString(aclass, "__dict__"); | 
 | 1520 | 	if (classdict == NULL) | 
 | 1521 | 		PyErr_Clear(); | 
 | 1522 | 	else { | 
 | 1523 | 		int status = PyDict_Update(dict, classdict); | 
 | 1524 | 		Py_DECREF(classdict); | 
 | 1525 | 		if (status < 0) | 
 | 1526 | 			return -1; | 
 | 1527 | 	} | 
 | 1528 |  | 
 | 1529 | 	/* Recursively merge in the base types' (if any) dicts. */ | 
 | 1530 | 	bases = PyObject_GetAttrString(aclass, "__bases__"); | 
| Tim Peters | bc7e863 | 2001-09-16 20:33:22 +0000 | [diff] [blame] | 1531 | 	if (bases == NULL) | 
 | 1532 | 		PyErr_Clear(); | 
 | 1533 | 	else { | 
| Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1534 | 		/* We have no guarantee that bases is a real tuple */ | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1535 | 		int i, n; | 
| Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1536 | 		n = PySequence_Size(bases); /* This better be right */ | 
 | 1537 | 		if (n < 0) | 
 | 1538 | 			PyErr_Clear(); | 
 | 1539 | 		else { | 
 | 1540 | 			for (i = 0; i < n; i++) { | 
| Tim Peters | 18e7083 | 2003-02-05 19:35:19 +0000 | [diff] [blame] | 1541 | 				int status; | 
| Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1542 | 				PyObject *base = PySequence_GetItem(bases, i); | 
 | 1543 | 				if (base == NULL) { | 
 | 1544 | 					Py_DECREF(bases); | 
 | 1545 | 					return -1; | 
 | 1546 | 				} | 
| Tim Peters | 18e7083 | 2003-02-05 19:35:19 +0000 | [diff] [blame] | 1547 | 				status = merge_class_dict(dict, base); | 
 | 1548 | 				Py_DECREF(base); | 
 | 1549 | 				if (status < 0) { | 
| Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1550 | 					Py_DECREF(bases); | 
 | 1551 | 					return -1; | 
 | 1552 | 				} | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1553 | 			} | 
 | 1554 | 		} | 
 | 1555 | 		Py_DECREF(bases); | 
 | 1556 | 	} | 
 | 1557 | 	return 0; | 
 | 1558 | } | 
 | 1559 |  | 
| Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1560 | /* Helper for PyObject_Dir. | 
 | 1561 |    If obj has an attr named attrname that's a list, merge its string | 
 | 1562 |    elements into keys of dict. | 
 | 1563 |    Return 0 on success, -1 on error.  Errors due to not finding the attr, | 
 | 1564 |    or the attr not being a list, are suppressed. | 
 | 1565 | */ | 
 | 1566 |  | 
 | 1567 | static int | 
 | 1568 | merge_list_attr(PyObject* dict, PyObject* obj, char *attrname) | 
 | 1569 | { | 
 | 1570 | 	PyObject *list; | 
 | 1571 | 	int result = 0; | 
 | 1572 |  | 
 | 1573 | 	assert(PyDict_Check(dict)); | 
 | 1574 | 	assert(obj); | 
 | 1575 | 	assert(attrname); | 
 | 1576 |  | 
 | 1577 | 	list = PyObject_GetAttrString(obj, attrname); | 
 | 1578 | 	if (list == NULL) | 
 | 1579 | 		PyErr_Clear(); | 
 | 1580 |  | 
 | 1581 | 	else if (PyList_Check(list)) { | 
 | 1582 | 		int i; | 
 | 1583 | 		for (i = 0; i < PyList_GET_SIZE(list); ++i) { | 
 | 1584 | 			PyObject *item = PyList_GET_ITEM(list, i); | 
 | 1585 | 			if (PyString_Check(item)) { | 
 | 1586 | 				result = PyDict_SetItem(dict, item, Py_None); | 
 | 1587 | 				if (result < 0) | 
 | 1588 | 					break; | 
 | 1589 | 			} | 
 | 1590 | 		} | 
 | 1591 | 	} | 
 | 1592 |  | 
 | 1593 | 	Py_XDECREF(list); | 
 | 1594 | 	return result; | 
 | 1595 | } | 
 | 1596 |  | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1597 | /* Like __builtin__.dir(arg).  See bltinmodule.c's builtin_dir for the | 
 | 1598 |    docstring, which should be kept in synch with this implementation. */ | 
 | 1599 |  | 
 | 1600 | PyObject * | 
 | 1601 | PyObject_Dir(PyObject *arg) | 
 | 1602 | { | 
 | 1603 | 	/* Set exactly one of these non-NULL before the end. */ | 
 | 1604 | 	PyObject *result = NULL;	/* result list */ | 
 | 1605 | 	PyObject *masterdict = NULL;	/* result is masterdict.keys() */ | 
 | 1606 |  | 
 | 1607 | 	/* If NULL arg, return the locals. */ | 
 | 1608 | 	if (arg == NULL) { | 
 | 1609 | 		PyObject *locals = PyEval_GetLocals(); | 
 | 1610 | 		if (locals == NULL) | 
 | 1611 | 			goto error; | 
 | 1612 | 		result = PyDict_Keys(locals); | 
 | 1613 | 		if (result == NULL) | 
 | 1614 | 			goto error; | 
 | 1615 | 	} | 
 | 1616 |  | 
 | 1617 | 	/* Elif this is some form of module, we only want its dict. */ | 
| Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1618 | 	else if (PyModule_Check(arg)) { | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1619 | 		masterdict = PyObject_GetAttrString(arg, "__dict__"); | 
 | 1620 | 		if (masterdict == NULL) | 
 | 1621 | 			goto error; | 
| Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1622 | 		if (!PyDict_Check(masterdict)) { | 
 | 1623 | 			PyErr_SetString(PyExc_TypeError, | 
 | 1624 | 					"module.__dict__ is not a dictionary"); | 
 | 1625 | 			goto error; | 
 | 1626 | 		} | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1627 | 	} | 
 | 1628 |  | 
 | 1629 | 	/* Elif some form of type or class, grab its dict and its bases. | 
 | 1630 | 	   We deliberately don't suck up its __class__, as methods belonging | 
 | 1631 | 	   to the metaclass would probably be more confusing than helpful. */ | 
 | 1632 | 	else if (PyType_Check(arg) || PyClass_Check(arg)) { | 
 | 1633 | 		masterdict = PyDict_New(); | 
 | 1634 | 		if (masterdict == NULL) | 
 | 1635 | 			goto error; | 
 | 1636 | 		if (merge_class_dict(masterdict, arg) < 0) | 
 | 1637 | 			goto error; | 
 | 1638 | 	} | 
 | 1639 |  | 
 | 1640 | 	/* Else look at its dict, and the attrs reachable from its class. */ | 
 | 1641 | 	else { | 
 | 1642 | 		PyObject *itsclass; | 
 | 1643 | 		/* Create a dict to start with.  CAUTION:  Not everything | 
 | 1644 | 		   responding to __dict__ returns a dict! */ | 
 | 1645 | 		masterdict = PyObject_GetAttrString(arg, "__dict__"); | 
 | 1646 | 		if (masterdict == NULL) { | 
 | 1647 | 			PyErr_Clear(); | 
 | 1648 | 			masterdict = PyDict_New(); | 
 | 1649 | 		} | 
 | 1650 | 		else if (!PyDict_Check(masterdict)) { | 
 | 1651 | 			Py_DECREF(masterdict); | 
 | 1652 | 			masterdict = PyDict_New(); | 
 | 1653 | 		} | 
 | 1654 | 		else { | 
 | 1655 | 			/* The object may have returned a reference to its | 
 | 1656 | 			   dict, so copy it to avoid mutating it. */ | 
 | 1657 | 			PyObject *temp = PyDict_Copy(masterdict); | 
 | 1658 | 			Py_DECREF(masterdict); | 
 | 1659 | 			masterdict = temp; | 
 | 1660 | 		} | 
 | 1661 | 		if (masterdict == NULL) | 
 | 1662 | 			goto error; | 
 | 1663 |  | 
| Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1664 | 		/* Merge in __members__ and __methods__ (if any). | 
 | 1665 | 		   XXX Would like this to go away someday; for now, it's | 
 | 1666 | 		   XXX needed to get at im_self etc of method objects. */ | 
 | 1667 | 		if (merge_list_attr(masterdict, arg, "__members__") < 0) | 
 | 1668 | 			goto error; | 
 | 1669 | 		if (merge_list_attr(masterdict, arg, "__methods__") < 0) | 
 | 1670 | 			goto error; | 
 | 1671 |  | 
| Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1672 | 		/* Merge in attrs reachable from its class. | 
 | 1673 | 		   CAUTION:  Not all objects have a __class__ attr. */ | 
 | 1674 | 		itsclass = PyObject_GetAttrString(arg, "__class__"); | 
 | 1675 | 		if (itsclass == NULL) | 
 | 1676 | 			PyErr_Clear(); | 
 | 1677 | 		else { | 
 | 1678 | 			int status = merge_class_dict(masterdict, itsclass); | 
 | 1679 | 			Py_DECREF(itsclass); | 
 | 1680 | 			if (status < 0) | 
 | 1681 | 				goto error; | 
 | 1682 | 		} | 
 | 1683 | 	} | 
 | 1684 |  | 
 | 1685 | 	assert((result == NULL) ^ (masterdict == NULL)); | 
 | 1686 | 	if (masterdict != NULL) { | 
 | 1687 | 		/* The result comes from its keys. */ | 
 | 1688 | 		assert(result == NULL); | 
 | 1689 | 		result = PyDict_Keys(masterdict); | 
 | 1690 | 		if (result == NULL) | 
 | 1691 | 			goto error; | 
 | 1692 | 	} | 
 | 1693 |  | 
 | 1694 | 	assert(result); | 
 | 1695 | 	if (PyList_Sort(result) != 0) | 
 | 1696 | 		goto error; | 
 | 1697 | 	else | 
 | 1698 | 		goto normal_return; | 
 | 1699 |  | 
 | 1700 |   error: | 
 | 1701 | 	Py_XDECREF(result); | 
 | 1702 | 	result = NULL; | 
 | 1703 | 	/* fall through */ | 
 | 1704 |   normal_return: | 
 | 1705 |   	Py_XDECREF(masterdict); | 
 | 1706 | 	return result; | 
 | 1707 | } | 
| Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1708 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1709 | /* | 
 | 1710 | NoObject is usable as a non-NULL undefined value, used by the macro None. | 
 | 1711 | There is (and should be!) no way to create other objects of this type, | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1712 | so there is exactly one (which is indestructible, by the way). | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1713 | (XXX This type and the type of NotImplemented below should be unified.) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1714 | */ | 
 | 1715 |  | 
| Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1716 | /* ARGSUSED */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1717 | static PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1718 | none_repr(PyObject *op) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1719 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1720 | 	return PyString_FromString("None"); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1721 | } | 
 | 1722 |  | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1723 | /* ARGUSED */ | 
 | 1724 | static void | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1725 | none_dealloc(PyObject* ignore) | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1726 | { | 
 | 1727 | 	/* This should never get called, but we also don't want to SEGV if | 
 | 1728 | 	 * we accidently decref None out of existance. | 
 | 1729 | 	 */ | 
| Martin v. Löwis | 3f19b10 | 2002-08-07 16:21:51 +0000 | [diff] [blame] | 1730 | 	Py_FatalError("deallocating None"); | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1731 | } | 
 | 1732 |  | 
 | 1733 |  | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1734 | static PyTypeObject PyNone_Type = { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1735 | 	PyObject_HEAD_INIT(&PyType_Type) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1736 | 	0, | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1737 | 	"NoneType", | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1738 | 	0, | 
 | 1739 | 	0, | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1740 | 	(destructor)none_dealloc,	     /*tp_dealloc*/ /*never called*/ | 
| Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1741 | 	0,		/*tp_print*/ | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1742 | 	0,		/*tp_getattr*/ | 
 | 1743 | 	0,		/*tp_setattr*/ | 
 | 1744 | 	0,		/*tp_compare*/ | 
| Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1745 | 	(reprfunc)none_repr, /*tp_repr*/ | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1746 | 	0,		/*tp_as_number*/ | 
 | 1747 | 	0,		/*tp_as_sequence*/ | 
 | 1748 | 	0,		/*tp_as_mapping*/ | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1749 | 	0,		/*tp_hash */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1750 | }; | 
 | 1751 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1752 | PyObject _Py_NoneStruct = { | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1753 | 	PyObject_HEAD_INIT(&PyNone_Type) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1754 | }; | 
 | 1755 |  | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1756 | /* NotImplemented is an object that can be used to signal that an | 
 | 1757 |    operation is not implemented for the given type combination. */ | 
 | 1758 |  | 
 | 1759 | static PyObject * | 
 | 1760 | NotImplemented_repr(PyObject *op) | 
 | 1761 | { | 
 | 1762 | 	return PyString_FromString("NotImplemented"); | 
 | 1763 | } | 
 | 1764 |  | 
 | 1765 | static PyTypeObject PyNotImplemented_Type = { | 
 | 1766 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 1767 | 	0, | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1768 | 	"NotImplementedType", | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1769 | 	0, | 
 | 1770 | 	0, | 
| Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1771 | 	(destructor)none_dealloc,	     /*tp_dealloc*/ /*never called*/ | 
| Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1772 | 	0,		/*tp_print*/ | 
 | 1773 | 	0,		/*tp_getattr*/ | 
 | 1774 | 	0,		/*tp_setattr*/ | 
 | 1775 | 	0,		/*tp_compare*/ | 
 | 1776 | 	(reprfunc)NotImplemented_repr, /*tp_repr*/ | 
 | 1777 | 	0,		/*tp_as_number*/ | 
 | 1778 | 	0,		/*tp_as_sequence*/ | 
 | 1779 | 	0,		/*tp_as_mapping*/ | 
 | 1780 | 	0,		/*tp_hash */ | 
 | 1781 | }; | 
 | 1782 |  | 
 | 1783 | PyObject _Py_NotImplementedStruct = { | 
 | 1784 | 	PyObject_HEAD_INIT(&PyNotImplemented_Type) | 
 | 1785 | }; | 
 | 1786 |  | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1787 | void | 
 | 1788 | _Py_ReadyTypes(void) | 
 | 1789 | { | 
 | 1790 | 	if (PyType_Ready(&PyType_Type) < 0) | 
 | 1791 | 		Py_FatalError("Can't initialize 'type'"); | 
 | 1792 |  | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1793 | 	if (PyType_Ready(&PyBool_Type) < 0) | 
 | 1794 | 		Py_FatalError("Can't initialize 'bool'"); | 
 | 1795 |  | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 1796 | 	if (PyType_Ready(&PyString_Type) < 0) | 
 | 1797 | 		Py_FatalError("Can't initialize 'str'"); | 
 | 1798 |  | 
| Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1799 | 	if (PyType_Ready(&PyList_Type) < 0) | 
 | 1800 | 		Py_FatalError("Can't initialize 'list'"); | 
 | 1801 |  | 
 | 1802 | 	if (PyType_Ready(&PyNone_Type) < 0) | 
 | 1803 | 		Py_FatalError("Can't initialize type(None)"); | 
 | 1804 |  | 
 | 1805 | 	if (PyType_Ready(&PyNotImplemented_Type) < 0) | 
 | 1806 | 		Py_FatalError("Can't initialize type(NotImplemented)"); | 
 | 1807 | } | 
 | 1808 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1809 |  | 
| Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1810 | #ifdef Py_TRACE_REFS | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1811 |  | 
| Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1812 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1813 | _Py_NewReference(PyObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1814 | { | 
| Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1815 | 	_Py_INC_REFTOTAL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1816 | 	op->ob_refcnt = 1; | 
| Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1817 | 	_Py_AddToAllObjects(op, 1); | 
| Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1818 | 	_Py_INC_TPALLOCS(op); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1819 | } | 
 | 1820 |  | 
| Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1821 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1822 | _Py_ForgetReference(register PyObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1823 | { | 
| Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1824 | #ifdef SLOW_UNREF_CHECK | 
| Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1825 |         register PyObject *p; | 
| Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1826 | #endif | 
| Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1827 | 	if (op->ob_refcnt < 0) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1828 | 		Py_FatalError("UNREF negative refcnt"); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1829 | 	if (op == &refchain || | 
| Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1830 | 	    op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1831 | 		Py_FatalError("UNREF invalid object"); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1832 | #ifdef SLOW_UNREF_CHECK | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1833 | 	for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { | 
 | 1834 | 		if (p == op) | 
 | 1835 | 			break; | 
 | 1836 | 	} | 
| Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1837 | 	if (p == &refchain) /* Not found */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1838 | 		Py_FatalError("UNREF unknown object"); | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1839 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1840 | 	op->_ob_next->_ob_prev = op->_ob_prev; | 
 | 1841 | 	op->_ob_prev->_ob_next = op->_ob_next; | 
| Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1842 | 	op->_ob_next = op->_ob_prev = NULL; | 
| Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1843 | 	_Py_INC_TPFREES(op); | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1844 | } | 
 | 1845 |  | 
| Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1846 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1847 | _Py_Dealloc(PyObject *op) | 
| Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1848 | { | 
| Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1849 | 	destructor dealloc = op->ob_type->tp_dealloc; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1850 | 	_Py_ForgetReference(op); | 
| Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1851 | 	(*dealloc)(op); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1852 | } | 
 | 1853 |  | 
| Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1854 | /* Print all live objects.  Because PyObject_Print is called, the | 
 | 1855 |  * interpreter must be in a healthy state. | 
 | 1856 |  */ | 
| Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1857 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1858 | _Py_PrintReferences(FILE *fp) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1859 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1860 | 	PyObject *op; | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1861 | 	fprintf(fp, "Remaining objects:\n"); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1862 | 	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { | 
| Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1863 | 		fprintf(fp, "%p [%d] ", op, op->ob_refcnt); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1864 | 		if (PyObject_Print(op, fp, 0) != 0) | 
 | 1865 | 			PyErr_Clear(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1866 | 		putc('\n', fp); | 
 | 1867 | 	} | 
 | 1868 | } | 
 | 1869 |  | 
| Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1870 | /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this | 
 | 1871 |  * doesn't make any calls to the Python C API, so is always safe to call. | 
 | 1872 |  */ | 
 | 1873 | void | 
 | 1874 | _Py_PrintReferenceAddresses(FILE *fp) | 
 | 1875 | { | 
 | 1876 | 	PyObject *op; | 
 | 1877 | 	fprintf(fp, "Remaining object addresses:\n"); | 
 | 1878 | 	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) | 
| Tim Peters | 21d7d4d | 2003-04-18 00:45:59 +0000 | [diff] [blame] | 1879 | 		fprintf(fp, "%p [%d] %s\n", op, op->ob_refcnt, | 
 | 1880 | 					    op->ob_type->tp_name); | 
| Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1881 | } | 
 | 1882 |  | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1883 | PyObject * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1884 | _Py_GetObjects(PyObject *self, PyObject *args) | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1885 | { | 
 | 1886 | 	int i, n; | 
 | 1887 | 	PyObject *t = NULL; | 
 | 1888 | 	PyObject *res, *op; | 
 | 1889 |  | 
 | 1890 | 	if (!PyArg_ParseTuple(args, "i|O", &n, &t)) | 
 | 1891 | 		return NULL; | 
 | 1892 | 	op = refchain._ob_next; | 
 | 1893 | 	res = PyList_New(0); | 
 | 1894 | 	if (res == NULL) | 
 | 1895 | 		return NULL; | 
 | 1896 | 	for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { | 
 | 1897 | 		while (op == self || op == args || op == res || op == t || | 
| Guido van Rossum | 3c29602 | 2001-07-14 17:58:00 +0000 | [diff] [blame] | 1898 | 		       (t != NULL && op->ob_type != (PyTypeObject *) t)) { | 
| Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1899 | 			op = op->_ob_next; | 
 | 1900 | 			if (op == &refchain) | 
 | 1901 | 				return res; | 
 | 1902 | 		} | 
 | 1903 | 		if (PyList_Append(res, op) < 0) { | 
 | 1904 | 			Py_DECREF(res); | 
 | 1905 | 			return NULL; | 
 | 1906 | 		} | 
 | 1907 | 		op = op->_ob_next; | 
 | 1908 | 	} | 
 | 1909 | 	return res; | 
 | 1910 | } | 
 | 1911 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1912 | #endif | 
| Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1913 |  | 
 | 1914 |  | 
 | 1915 | /* Hack to force loading of cobject.o */ | 
| Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1916 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; | 
| Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1917 |  | 
 | 1918 |  | 
 | 1919 | /* Hack to force loading of abstract.o */ | 
| Neal Norwitz | 4178515 | 2002-06-13 21:42:51 +0000 | [diff] [blame] | 1920 | int (*_Py_abstract_hack)(PyObject *) = PyObject_Size; | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1921 |  | 
 | 1922 |  | 
| Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1923 | /* Python's malloc wrappers (see pymem.h) */ | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1924 |  | 
| Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1925 | void * | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1926 | PyMem_Malloc(size_t nbytes) | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1927 | { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1928 | 	return PyMem_MALLOC(nbytes); | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1929 | } | 
 | 1930 |  | 
| Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1931 | void * | 
 | 1932 | PyMem_Realloc(void *p, size_t nbytes) | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1933 | { | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 1934 | 	return PyMem_REALLOC(p, nbytes); | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1935 | } | 
 | 1936 |  | 
 | 1937 | void | 
| Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1938 | PyMem_Free(void *p) | 
| Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1939 | { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1940 | 	PyMem_FREE(p); | 
 | 1941 | } | 
 | 1942 |  | 
 | 1943 |  | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1944 | /* These methods are used to control infinite recursion in repr, str, print, | 
 | 1945 |    etc.  Container objects that may recursively contain themselves, | 
 | 1946 |    e.g. builtin dictionaries and lists, should used Py_ReprEnter() and | 
 | 1947 |    Py_ReprLeave() to avoid infinite recursion. | 
 | 1948 |  | 
 | 1949 |    Py_ReprEnter() returns 0 the first time it is called for a particular | 
 | 1950 |    object and 1 every time thereafter.  It returns -1 if an exception | 
 | 1951 |    occurred.  Py_ReprLeave() has no return value. | 
 | 1952 |  | 
 | 1953 |    See dictobject.c and listobject.c for examples of use. | 
 | 1954 | */ | 
 | 1955 |  | 
 | 1956 | #define KEY "Py_Repr" | 
 | 1957 |  | 
 | 1958 | int | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1959 | Py_ReprEnter(PyObject *obj) | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1960 | { | 
 | 1961 | 	PyObject *dict; | 
 | 1962 | 	PyObject *list; | 
 | 1963 | 	int i; | 
 | 1964 |  | 
 | 1965 | 	dict = PyThreadState_GetDict(); | 
 | 1966 | 	if (dict == NULL) | 
| Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 1967 | 		return 0; | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1968 | 	list = PyDict_GetItemString(dict, KEY); | 
 | 1969 | 	if (list == NULL) { | 
 | 1970 | 		list = PyList_New(0); | 
 | 1971 | 		if (list == NULL) | 
 | 1972 | 			return -1; | 
 | 1973 | 		if (PyDict_SetItemString(dict, KEY, list) < 0) | 
 | 1974 | 			return -1; | 
 | 1975 | 		Py_DECREF(list); | 
 | 1976 | 	} | 
 | 1977 | 	i = PyList_GET_SIZE(list); | 
 | 1978 | 	while (--i >= 0) { | 
 | 1979 | 		if (PyList_GET_ITEM(list, i) == obj) | 
 | 1980 | 			return 1; | 
 | 1981 | 	} | 
 | 1982 | 	PyList_Append(list, obj); | 
 | 1983 | 	return 0; | 
 | 1984 | } | 
 | 1985 |  | 
 | 1986 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1987 | Py_ReprLeave(PyObject *obj) | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1988 | { | 
 | 1989 | 	PyObject *dict; | 
 | 1990 | 	PyObject *list; | 
 | 1991 | 	int i; | 
 | 1992 |  | 
 | 1993 | 	dict = PyThreadState_GetDict(); | 
| Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1994 | 	if (dict == NULL) | 
 | 1995 | 		return; | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1996 | 	list = PyDict_GetItemString(dict, KEY); | 
| Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1997 | 	if (list == NULL || !PyList_Check(list)) | 
 | 1998 | 		return; | 
| Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1999 | 	i = PyList_GET_SIZE(list); | 
 | 2000 | 	/* Count backwards because we always expect obj to be list[-1] */ | 
 | 2001 | 	while (--i >= 0) { | 
 | 2002 | 		if (PyList_GET_ITEM(list, i) == obj) { | 
 | 2003 | 			PyList_SetSlice(list, i, i + 1, NULL); | 
 | 2004 | 			break; | 
 | 2005 | 		} | 
 | 2006 | 	} | 
 | 2007 | } | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2008 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2009 | /* Trashcan support. */ | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2010 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2011 | /* Current call-stack depth of tp_dealloc calls. */ | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2012 | int _PyTrash_delete_nesting = 0; | 
| Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 2013 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2014 | /* List of objects that still need to be cleaned up, singly linked via their | 
 | 2015 |  * gc headers' gc_prev pointers. | 
 | 2016 |  */ | 
 | 2017 | PyObject *_PyTrash_delete_later = NULL; | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2018 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2019 | /* Add op to the _PyTrash_delete_later list.  Called when the current | 
 | 2020 |  * call-stack depth gets large.  op must be a currently untracked gc'ed | 
 | 2021 |  * object, with refcount 0.  Py_DECREF must already have been called on it. | 
 | 2022 |  */ | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2023 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2024 | _PyTrash_deposit_object(PyObject *op) | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2025 | { | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2026 | 	assert(PyObject_IS_GC(op)); | 
 | 2027 | 	assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); | 
 | 2028 | 	assert(op->ob_refcnt == 0); | 
| Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2029 | 	_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; | 
| Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 2030 | 	_PyTrash_delete_later = op; | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2031 | } | 
 | 2032 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2033 | /* Dealloccate all the objects in the _PyTrash_delete_later list.  Called when | 
 | 2034 |  * the call-stack unwinds again. | 
 | 2035 |  */ | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2036 | void | 
| Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2037 | _PyTrash_destroy_chain(void) | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2038 | { | 
 | 2039 | 	while (_PyTrash_delete_later) { | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2040 | 		PyObject *op = _PyTrash_delete_later; | 
 | 2041 | 		destructor dealloc = op->ob_type->tp_dealloc; | 
| Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2042 |  | 
| Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2043 | 		_PyTrash_delete_later = | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2044 | 			(PyObject*) _Py_AS_GC(op)->gc.gc_prev; | 
| Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2045 |  | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2046 | 		/* Call the deallocator directly.  This used to try to | 
 | 2047 | 		 * fool Py_DECREF into calling it indirectly, but | 
 | 2048 | 		 * Py_DECREF was already called on this object, and in | 
 | 2049 | 		 * assorted non-release builds calling Py_DECREF again ends | 
 | 2050 | 		 * up distorting allocation statistics. | 
 | 2051 | 		 */ | 
 | 2052 | 		assert(op->ob_refcnt == 0); | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2053 | 		++_PyTrash_delete_nesting; | 
| Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2054 | 		(*dealloc)(op); | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2055 | 		--_PyTrash_delete_nesting; | 
 | 2056 | 	} | 
 | 2057 | } |