blob: 615a11b28076de216cdd38aa8d2f8e30089687be [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jack Jansen28fc8802000-07-11 21:47:20 +00006#ifdef macintosh
7#include "macglue.h"
8#endif
9
Guido van Rossume92e6102000-04-24 15:40:53 +000010/* just for trashcan: */
11#include "compile.h"
12#include "frameobject.h"
13#include "traceback.h"
14
Guido van Rossum6f9e4331995-03-29 16:57:48 +000015#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000016DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossum3f5da241990-12-20 15:06:42 +000019/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
20 These are used by the individual routines for object creation.
21 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000023#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025extern int tuple_zero_allocs, fast_tuple_allocs;
26extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000027extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000028void
Fred Drake100814d2000-07-09 15:48:49 +000029dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032
33 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000034 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
Tim Peters6d6c1a32001-08-02 04:15:00 +000035 tp->tp_name, tp->tp_allocs, tp->tp_frees,
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000036 tp->tp_maxalloc);
37 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
38 fast_tuple_allocs, tuple_zero_allocs);
39 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
40 quick_int_allocs, quick_neg_int_allocs);
41 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
42 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043}
44
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000046get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047{
48 PyTypeObject *tp;
49 PyObject *result;
50 PyObject *v;
51
52 result = PyList_New(0);
53 if (result == NULL)
54 return NULL;
55 for (tp = type_list; tp; tp = tp->tp_next) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000056 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
57 tp->tp_frees, tp->tp_maxalloc);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000058 if (v == NULL) {
59 Py_DECREF(result);
60 return NULL;
61 }
62 if (PyList_Append(result, v) < 0) {
63 Py_DECREF(v);
64 Py_DECREF(result);
65 return NULL;
66 }
67 Py_DECREF(v);
68 }
69 return result;
70}
71
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000072void
Fred Drake100814d2000-07-09 15:48:49 +000073inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074{
Tim Peters6d6c1a32001-08-02 04:15:00 +000075 if (tp->tp_allocs == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000076 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 tp->tp_next = type_list;
80 type_list = tp;
81 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000082 tp->tp_allocs++;
83 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000085}
86#endif
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000089PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumb18618d2000-05-03 23:44:39 +000091 if (op == NULL) {
92 PyErr_SetString(PyExc_SystemError,
93 "NULL object passed to PyObject_Init");
94 return op;
95 }
96 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 return op;
100}
101
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000103PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000104{
105 if (op == NULL) {
106 PyErr_SetString(PyExc_SystemError,
107 "NULL object passed to PyObject_InitVar");
108 return op;
109 }
110 /* Any changes should be reflected in PyObject_INIT_VAR */
111 op->ob_size = size;
112 op->ob_type = tp;
113 _Py_NewReference((PyObject *)op);
114 return op;
115}
116
117PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000118_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119{
120 PyObject *op;
121 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
122 if (op == NULL)
123 return PyErr_NoMemory();
124 return PyObject_INIT(op, tp);
125}
126
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000127PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000128_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 PyVarObject *op;
131 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000133 return (PyVarObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000134 return PyObject_INIT_VAR(op, tp, size);
135}
136
137void
Fred Drake100814d2000-07-09 15:48:49 +0000138_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000139{
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000140 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
Guido van Rossum90933611991-06-07 16:10:43 +0000143int
Fred Drake100814d2000-07-09 15:48:49 +0000144PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
Guido van Rossum278ef591991-07-27 21:40:24 +0000146 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000148 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000149#ifdef USE_STACKCHECK
150 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000151 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000152 return -1;
153 }
154#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000155 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000156 if (op == NULL) {
157 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 }
Guido van Rossum90933611991-06-07 16:10:43 +0000159 else {
160 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000161 fprintf(fp, "<refcnt %u at %p>",
162 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000163 else if (op->ob_type->tp_print == NULL) {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000164 PyObject *s;
165 if (flags & Py_PRINT_RAW)
166 s = PyObject_Str(op);
167 else
168 s = PyObject_Repr(op);
169 if (s == NULL)
170 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000171 else {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000172 ret = PyObject_Print(s, fp, Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000173 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000174 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000175 }
Guido van Rossum90933611991-06-07 16:10:43 +0000176 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000177 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000178 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000179 if (ret == 0) {
180 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000182 clearerr(fp);
183 ret = -1;
184 }
185 }
186 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
Barry Warsaw9bf16442001-01-23 16:24:35 +0000189/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000190void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000191{
Barry Warsaweefb1072001-02-22 22:39:18 +0000192 if (op == NULL)
193 fprintf(stderr, "NULL\n");
194 else {
195 (void)PyObject_Print(op, stderr, 0);
196 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
197 fprintf(stderr, "address : %p\n", op);
198 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000199}
Barry Warsaw903138f2001-01-23 16:33:18 +0000200
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000202PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000205 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000206#ifdef USE_STACKCHECK
207 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000208 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000209 return NULL;
210 }
211#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000212 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213 return PyString_FromString("<NULL>");
Barry Warsaw7ce36942001-08-24 18:34:26 +0000214 else if (v->ob_type->tp_repr == NULL)
215 return PyString_FromFormat("<%s object at %p",
216 v->ob_type->tp_name, v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000217 else {
218 PyObject *res;
219 res = (*v->ob_type->tp_repr)(v);
220 if (res == NULL)
221 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000222#ifdef Py_USING_UNICODE
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000223 if (PyUnicode_Check(res)) {
224 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000225 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000226 Py_DECREF(res);
227 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000228 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000229 else
230 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000231 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000232#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000233 if (!PyString_Check(res)) {
234 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000235 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000236 res->ob_type->tp_name);
237 Py_DECREF(res);
238 return NULL;
239 }
240 return res;
241 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242}
243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000245PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000246{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000247 PyObject *res;
248
Guido van Rossumc6004111993-11-05 10:22:19 +0000249 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 return PyString_FromString("<NULL>");
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000251 if (PyString_Check(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000253 return v;
254 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000255 if (v->ob_type->tp_str == NULL)
256 return PyObject_Repr(v);
257
258 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 if (res == NULL)
260 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000261#ifdef Py_USING_UNICODE
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000262 if (PyUnicode_Check(res)) {
263 PyObject* str;
264 str = PyUnicode_AsEncodedString(res, NULL, NULL);
265 Py_DECREF(res);
266 if (str)
267 res = str;
268 else
269 return NULL;
270 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000271#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000272 if (!PyString_Check(res)) {
273 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000274 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000275 res->ob_type->tp_name);
276 Py_DECREF(res);
277 return NULL;
278 }
279 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000280}
281
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000282#ifdef Py_USING_UNICODE
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000283PyObject *
284PyObject_Unicode(PyObject *v)
285{
286 PyObject *res;
287
288 if (v == NULL)
289 res = PyString_FromString("<NULL>");
290 else if (PyUnicode_Check(v)) {
291 Py_INCREF(v);
292 return v;
293 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000294 else if (PyString_Check(v)) {
295 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000296 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000297 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000298 else if (v->ob_type->tp_str != NULL)
299 res = (*v->ob_type->tp_str)(v);
300 else {
301 PyObject *func;
302 static PyObject *strstr;
303 if (strstr == NULL) {
304 strstr= PyString_InternFromString("__str__");
305 if (strstr == NULL)
306 return NULL;
307 }
308 if (!PyInstance_Check(v) ||
309 (func = PyObject_GetAttr(v, strstr)) == NULL) {
310 PyErr_Clear();
311 res = PyObject_Repr(v);
312 }
313 else {
314 res = PyEval_CallObject(func, (PyObject *)NULL);
315 Py_DECREF(func);
316 }
317 }
318 if (res == NULL)
319 return NULL;
320 if (!PyUnicode_Check(res)) {
321 PyObject* str;
322 str = PyUnicode_FromObject(res);
323 Py_DECREF(res);
324 if (str)
325 res = str;
326 else
327 return NULL;
328 }
329 return res;
330}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000331#endif
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000332
333
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000334/* Macro to get the tp_richcompare field of a type if defined */
335#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
336 ? (t)->tp_richcompare : NULL)
337
Guido van Rossume797ec12001-01-17 15:24:28 +0000338/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
339static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000340
Guido van Rossume797ec12001-01-17 15:24:28 +0000341/* Try a genuine rich comparison, returning an object. Return:
342 NULL for exception;
343 NotImplemented if this particular rich comparison is not implemented or
344 undefined;
345 some object not equal to NotImplemented if it is implemented
346 (this latter object may not be a Boolean).
347*/
348static PyObject *
349try_rich_compare(PyObject *v, PyObject *w, int op)
350{
351 richcmpfunc f;
352 PyObject *res;
353
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000354 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000355 res = (*f)(v, w, op);
356 if (res != Py_NotImplemented)
357 return res;
358 Py_DECREF(res);
359 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000360 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000361 return (*f)(w, v, swapped_op[op]);
362 }
363 res = Py_NotImplemented;
364 Py_INCREF(res);
365 return res;
366}
367
368/* Try a genuine rich comparison, returning an int. Return:
369 -1 for exception (including the case where try_rich_compare() returns an
370 object that's not a Boolean);
371 0 if the outcome is false;
372 1 if the outcome is true;
373 2 if this particular rich comparison is not implemented or undefined.
374*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000375static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000376try_rich_compare_bool(PyObject *v, PyObject *w, int op)
377{
378 PyObject *res;
379 int ok;
380
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000381 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000382 return 2; /* Shortcut, avoid INCREF+DECREF */
383 res = try_rich_compare(v, w, op);
384 if (res == NULL)
385 return -1;
386 if (res == Py_NotImplemented) {
387 Py_DECREF(res);
388 return 2;
389 }
390 ok = PyObject_IsTrue(res);
391 Py_DECREF(res);
392 return ok;
393}
394
395/* Try rich comparisons to determine a 3-way comparison. Return:
396 -2 for an exception;
397 -1 if v < w;
398 0 if v == w;
399 1 if v > w;
400 2 if this particular rich comparison is not implemented or undefined.
401*/
402static int
403try_rich_to_3way_compare(PyObject *v, PyObject *w)
404{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000405 static struct { int op; int outcome; } tries[3] = {
406 /* Try this operator, and if it is true, use this outcome: */
407 {Py_EQ, 0},
408 {Py_LT, -1},
409 {Py_GT, 1},
410 };
411 int i;
412
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000413 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000414 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000415
416 for (i = 0; i < 3; i++) {
417 switch (try_rich_compare_bool(v, w, tries[i].op)) {
418 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000419 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000420 case 1:
421 return tries[i].outcome;
422 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000423 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000424
425 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000426}
427
428/* Try a 3-way comparison, returning an int. Return:
429 -2 for an exception;
430 -1 if v < w;
431 0 if v == w;
432 1 if v > w;
433 2 if this particular 3-way comparison is not implemented or undefined.
434*/
435static int
436try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000437{
438 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000439 cmpfunc f;
440
441 /* Comparisons involving instances are given to instance_compare,
442 which has the same return conventions as this function. */
443
444 if (PyInstance_Check(v))
445 return (*v->ob_type->tp_compare)(v, w);
446 if (PyInstance_Check(w))
447 return (*w->ob_type->tp_compare)(v, w);
448
Guido van Rossume797ec12001-01-17 15:24:28 +0000449 /* Try coercion; if it fails, give up */
450 c = PyNumber_CoerceEx(&v, &w);
451 if (c < 0)
452 return -2;
453 if (c > 0)
454 return 2;
455
456 /* Try v's comparison, if defined */
457 if ((f = v->ob_type->tp_compare) != NULL) {
458 c = (*f)(v, w);
459 Py_DECREF(v);
460 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000461 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000462 return -2;
463 return c < 0 ? -1 : c > 0 ? 1 : 0;
464 }
465
466 /* Try w's comparison, if defined */
467 if ((f = w->ob_type->tp_compare) != NULL) {
468 c = (*f)(w, v); /* swapped! */
469 Py_DECREF(v);
470 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000471 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000472 return -2;
473 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
474 }
475
476 /* No comparison defined */
477 Py_DECREF(v);
478 Py_DECREF(w);
479 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000480}
481
Guido van Rossume797ec12001-01-17 15:24:28 +0000482/* Final fallback 3-way comparison, returning an int. Return:
483 -2 if an error occurred;
484 -1 if v < w;
485 0 if v == w;
486 1 if v > w.
487*/
488static int
489default_3way_compare(PyObject *v, PyObject *w)
490{
491 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000492 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000493
494 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000495 /* When comparing these pointers, they must be cast to
496 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
497 * uintptr_t). ANSI specifies that pointer compares other
498 * than == and != to non-related structures are undefined.
499 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000500 Py_uintptr_t vv = (Py_uintptr_t)v;
501 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000502 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
503 }
504
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000505#ifdef Py_USING_UNICODE
Guido van Rossume797ec12001-01-17 15:24:28 +0000506 /* Special case for Unicode */
507 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
508 c = PyUnicode_Compare(v, w);
509 if (!PyErr_Occurred())
510 return c;
511 /* TypeErrors are ignored: if Unicode coercion fails due
512 to one of the arguments not having the right type, we
513 continue as defined by the coercion protocol (see
514 above). Luckily, decoding errors are reported as
515 ValueErrors and are not masked by this technique. */
516 if (!PyErr_ExceptionMatches(PyExc_TypeError))
517 return -2;
518 PyErr_Clear();
519 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000520#endif
Guido van Rossume797ec12001-01-17 15:24:28 +0000521
Guido van Rossum0871e932001-01-22 19:28:09 +0000522 /* None is smaller than anything */
523 if (v == Py_None)
524 return -1;
525 if (w == Py_None)
526 return 1;
527
Guido van Rossume797ec12001-01-17 15:24:28 +0000528 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000529 if (v->ob_type->tp_as_number)
530 vname = "";
531 else
532 vname = v->ob_type->tp_name;
533 if (w->ob_type->tp_as_number)
534 wname = "";
535 else
536 wname = w->ob_type->tp_name;
537 c = strcmp(vname, wname);
538 if (c < 0)
539 return -1;
540 if (c > 0)
541 return 1;
542 /* Same type name, or (more likely) incomparable numeric types */
543 return ((Py_uintptr_t)(v->ob_type) < (
544 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000545}
546
547#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
548
Tim Peters6d60b2e2001-05-07 20:53:51 +0000549/* Do a 3-way comparison, by hook or by crook. Return:
550 -2 for an exception;
551 -1 if v < w;
552 0 if v == w;
553 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000554 If the object implements a tp_compare function, it returns
555 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000556*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000557static int
Fred Drake100814d2000-07-09 15:48:49 +0000558do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000559{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000560 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000561 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000562
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000563 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c12001-08-16 08:02:45 +0000564 && (f = v->ob_type->tp_compare) != NULL) {
565 c = (*f)(v, w);
566 if (c != 2 || !PyInstance_Check(v))
567 return c;
568 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000569 c = try_rich_to_3way_compare(v, w);
570 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000571 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000572 c = try_3way_compare(v, w);
573 if (c < 2)
574 return c;
575 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000576}
577
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000578/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000579 some types) and decremented on exit. If the count exceeds the
580 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000581
582 This is a tunable parameter that should only affect the performance
583 of comparisons, nothing else. Setting it high makes comparing deeply
584 nested non-cyclical data structures faster, but makes comparing cyclical
585 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000586*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000587#define NESTING_LIMIT 20
588
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000589static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000590
591static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000592get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000593{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000594 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000595 PyObject *tstate_dict, *inprogress;
596
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000597 if (key == NULL) {
598 key = PyString_InternFromString("cmp_state");
599 if (key == NULL)
600 return NULL;
601 }
602
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000603 tstate_dict = PyThreadState_GetDict();
604 if (tstate_dict == NULL) {
605 PyErr_BadInternalCall();
606 return NULL;
607 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000608
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000609 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000610 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000611 inprogress = PyDict_New();
612 if (inprogress == NULL)
613 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000614 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000615 Py_DECREF(inprogress);
616 return NULL;
617 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000618 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000620
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000621 return inprogress;
622}
623
624static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000625check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000626{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000627 PyObject *inprogress;
628 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000629 Py_uintptr_t iv = (Py_uintptr_t)v;
630 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000631 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000632
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000633 inprogress = get_inprogress_dict();
634 if (inprogress == NULL)
635 return NULL;
636
637 token = PyTuple_New(3);
638 if (token == NULL)
639 return NULL;
640
641 if (iv <= iw) {
642 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
643 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
644 if (op >= 0)
645 op = swapped_op[op];
646 } else {
647 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
648 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
649 }
650 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
651 if (x == NULL || y == NULL || z == NULL) {
652 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000653 return NULL;
654 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000655
656 if (PyDict_GetItem(inprogress, token) != NULL) {
657 Py_DECREF(token);
658 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000659 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000660
661 if (PyDict_SetItem(inprogress, token, token) < 0) {
662 Py_DECREF(token);
663 return NULL;
664 }
665
666 return token;
667}
668
669static void
670delete_token(PyObject *token)
671{
672 PyObject *inprogress;
673
674 if (token == NULL || token == Py_None)
675 return;
676 inprogress = get_inprogress_dict();
677 if (inprogress == NULL)
678 PyErr_Clear();
679 else
680 PyDict_DelItem(inprogress, token);
681 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000682}
683
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684int
Fred Drake100814d2000-07-09 15:48:49 +0000685PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000687 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000688 int result;
689
Jack Jansend49cbe12000-08-22 21:52:51 +0000690#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000691 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000692 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
693 return -1;
694 }
695#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000696 if (v == NULL || w == NULL) {
697 PyErr_BadInternalCall();
698 return -1;
699 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700 if (v == w)
701 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000702 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000703 compare_nesting++;
704 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000705 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000706 || (vtp->tp_as_sequence
707 && !PyString_Check(v)
708 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000709 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000710 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000711
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000712 if (token == NULL) {
713 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000714 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000715 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000716 /* already comparing these objects. assume
717 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000718 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000719 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000720 else {
721 result = do_cmp(v, w);
722 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000723 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000724 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000725 else {
726 result = do_cmp(v, w);
727 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000728 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000729 return result < 0 ? -1 : result;
730}
731
732static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000733convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000734{
Guido van Rossume797ec12001-01-17 15:24:28 +0000735 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000736 switch (op) {
737 case Py_LT: c = c < 0; break;
738 case Py_LE: c = c <= 0; break;
739 case Py_EQ: c = c == 0; break;
740 case Py_NE: c = c != 0; break;
741 case Py_GT: c = c > 0; break;
742 case Py_GE: c = c >= 0; break;
743 }
744 result = c ? Py_True : Py_False;
745 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000746 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000748
749
750static PyObject *
751try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
752{
753 int c;
754
755 c = try_3way_compare(v, w);
756 if (c >= 2)
757 c = default_3way_compare(v, w);
758 if (c <= -2)
759 return NULL;
760 return convert_3way_to_object(op, c);
761}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000763static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000764do_richcmp(PyObject *v, PyObject *w, int op)
765{
766 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000767 cmpfunc f;
768
769 /* If the types are equal, don't bother with coercions etc.
770 Instances are special-cased in try_3way_compare, since
771 a result of 2 does *not* mean one value being greater
772 than the other. */
773 if (v->ob_type == w->ob_type
774 && (f = v->ob_type->tp_compare) != NULL
775 && !PyInstance_Check(v)) {
776 int c;
777 richcmpfunc f1;
778 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
779 /* If the type has richcmp, try it first.
780 try_rich_compare would try it two-sided,
781 which is not needed since we've a single
782 type only. */
783 res = (*f1)(v, w, op);
784 if (res != Py_NotImplemented)
785 return res;
786 Py_DECREF(res);
787 }
788 c = (*f)(v, w);
789 if (c < 0 && PyErr_Occurred())
790 return NULL;
791 return convert_3way_to_object(op, c);
792 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000793
794 res = try_rich_compare(v, w, op);
795 if (res != Py_NotImplemented)
796 return res;
797 Py_DECREF(res);
798
799 return try_3way_to_rich_compare(v, w, op);
800}
801
802PyObject *
803PyObject_RichCompare(PyObject *v, PyObject *w, int op)
804{
805 PyObject *res;
806
807 assert(Py_LT <= op && op <= Py_GE);
808
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000809 compare_nesting++;
810 if (compare_nesting > NESTING_LIMIT &&
811 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000812 || (v->ob_type->tp_as_sequence
813 && !PyString_Check(v)
814 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000815 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000816 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000818 if (token == NULL) {
819 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000820 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000821 else if (token == Py_None) {
822 /* already comparing these objects with this operator.
823 assume they're equal until shown otherwise */
824 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000827 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000828 else {
829 PyErr_SetString(PyExc_ValueError,
830 "can't order recursive values");
831 res = NULL;
832 }
833 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 else {
836 res = do_richcmp(v, w, op);
837 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000838 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000839 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000840 else {
841 res = do_richcmp(v, w, op);
842 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000843 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000844 return res;
845}
846
Tim Petersde9725f2001-05-05 10:06:17 +0000847/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000848int
849PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
850{
851 PyObject *res = PyObject_RichCompare(v, w, op);
852 int ok;
853
854 if (res == NULL)
855 return -1;
856 ok = PyObject_IsTrue(res);
857 Py_DECREF(res);
858 return ok;
859}
Fred Drake13634cf2000-06-29 19:17:04 +0000860
861/* Set of hash utility functions to help maintaining the invariant that
862 iff a==b then hash(a)==hash(b)
863
864 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
865*/
866
867long
Fred Drake100814d2000-07-09 15:48:49 +0000868_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000869{
Tim Peters39dce292000-08-15 03:34:48 +0000870 double intpart, fractpart;
871 int expo;
872 long hipart;
873 long x; /* the final hash value */
874 /* This is designed so that Python numbers of different types
875 * that compare equal hash to the same value; otherwise comparisons
876 * of mapping keys will turn out weird.
877 */
878
879#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
880{
881 extended e;
882 fractpart = modf(v, &e);
883 intpart = e;
884}
885#else
886 fractpart = modf(v, &intpart);
887#endif
888 if (fractpart == 0.0) {
889 /* This must return the same hash as an equal int or long. */
890 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
891 /* Convert to long and use its hash. */
892 PyObject *plong; /* converted to Python long */
893 if (Py_IS_INFINITY(intpart))
894 /* can't convert to long int -- arbitrary */
895 v = v < 0 ? -271828.0 : 314159.0;
896 plong = PyLong_FromDouble(v);
897 if (plong == NULL)
898 return -1;
899 x = PyObject_Hash(plong);
900 Py_DECREF(plong);
901 return x;
902 }
903 /* Fits in a C long == a Python int, so is its own hash. */
904 x = (long)intpart;
905 if (x == -1)
906 x = -2;
907 return x;
908 }
909 /* The fractional part is non-zero, so we don't have to worry about
910 * making this match the hash of some other type.
911 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000912 * Since the VAX D double format has 56 mantissa bits, which is the
913 * most of any double format in use, each of these parts may have as
914 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000915 * So, assuming sizeof(long) >= 4, each part can be broken into two
916 * longs; frexp and multiplication are used to do that.
917 * Also, since the Cray double format has 15 exponent bits, which is
918 * the most of any double format in use, shifting the exponent field
919 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000920 */
Tim Peters39dce292000-08-15 03:34:48 +0000921 v = frexp(v, &expo);
922 v *= 2147483648.0; /* 2**31 */
923 hipart = (long)v; /* take the top 32 bits */
924 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
925 x = hipart + (long)v + (expo << 15);
926 if (x == -1)
927 x = -2;
928 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000929}
930
931long
Fred Drake100814d2000-07-09 15:48:49 +0000932_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000933{
934#if SIZEOF_LONG >= SIZEOF_VOID_P
935 return (long)p;
936#else
937 /* convert to a Python long and hash that */
938 PyObject* longobj;
939 long x;
940
941 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
942 x = -1;
943 goto finally;
944 }
945 x = PyObject_Hash(longobj);
946
947finally:
948 Py_XDECREF(longobj);
949 return x;
950#endif
951}
952
953
Guido van Rossum9bfef441993-03-29 10:43:31 +0000954long
Fred Drake100814d2000-07-09 15:48:49 +0000955PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000956{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000958 if (tp->tp_hash != NULL)
959 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000960 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000961 return _Py_HashPointer(v); /* Use address as hash value */
962 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000963 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000965 return -1;
966}
967
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000968PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000969PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000970{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000971 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000972
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000974 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000975 w = PyString_InternFromString(name);
976 if (w == NULL)
977 return NULL;
978 res = PyObject_GetAttr(v, w);
979 Py_XDECREF(w);
980 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000981}
982
983int
Fred Drake100814d2000-07-09 15:48:49 +0000984PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000985{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000986 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000987 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000989 return 1;
990 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000992 return 0;
993}
994
995int
Fred Drake100814d2000-07-09 15:48:49 +0000996PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998 PyObject *s;
999 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001000
Tim Peters6d6c1a32001-08-02 04:15:00 +00001001 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001002 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 s = PyString_InternFromString(name);
1004 if (s == NULL)
1005 return -1;
1006 res = PyObject_SetAttr(v, s, w);
1007 Py_XDECREF(s);
1008 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001009}
1010
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001011PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001012PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001013{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 PyTypeObject *tp = v->ob_type;
1015
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001016#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001017 /* The Unicode to string conversion is done here because the
1018 existing tp_getattro slots expect a string object as name
1019 and we wouldn't want to break those. */
1020 if (PyUnicode_Check(name)) {
1021 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1022 if (name == NULL)
1023 return NULL;
1024 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001025#endif
1026
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001027 if (!PyString_Check(name)) {
1028 PyErr_SetString(PyExc_TypeError,
1029 "attribute name must be string");
1030 return NULL;
1031 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032 if (tp->tp_getattro != NULL)
1033 return (*tp->tp_getattro)(v, name);
1034 if (tp->tp_getattr != NULL)
1035 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1036 PyErr_Format(PyExc_AttributeError,
1037 "'%.50s' object has no attribute '%.400s'",
1038 tp->tp_name, PyString_AS_STRING(name));
1039 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001040}
1041
1042int
Fred Drake100814d2000-07-09 15:48:49 +00001043PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001044{
1045 PyObject *res = PyObject_GetAttr(v, name);
1046 if (res != NULL) {
1047 Py_DECREF(res);
1048 return 1;
1049 }
1050 PyErr_Clear();
1051 return 0;
1052}
1053
1054int
Fred Drake100814d2000-07-09 15:48:49 +00001055PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001056{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001058 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001059
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001060#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001061 /* The Unicode to string conversion is done here because the
1062 existing tp_setattro slots expect a string object as name
1063 and we wouldn't want to break those. */
1064 if (PyUnicode_Check(name)) {
1065 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1066 if (name == NULL)
1067 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001068 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001069 else
1070#endif
1071 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001072 PyErr_SetString(PyExc_TypeError,
1073 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001074 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001075 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076 else
1077 Py_INCREF(name);
1078
1079 PyString_InternInPlace(&name);
1080 if (tp->tp_setattro != NULL) {
1081 err = (*tp->tp_setattro)(v, name, value);
1082 Py_DECREF(name);
1083 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001084 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001085 if (tp->tp_setattr != NULL) {
1086 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1087 Py_DECREF(name);
1088 return err;
1089 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001090 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1092 PyErr_Format(PyExc_TypeError,
1093 "'%.100s' object has no attributes "
1094 "(%s .%.100s)",
1095 tp->tp_name,
1096 value==NULL ? "del" : "assign to",
1097 PyString_AS_STRING(name));
1098 else
1099 PyErr_Format(PyExc_TypeError,
1100 "'%.100s' object has only read-only attributes "
1101 "(%s .%.100s)",
1102 tp->tp_name,
1103 value==NULL ? "del" : "assign to",
1104 PyString_AS_STRING(name));
1105 return -1;
1106}
1107
1108/* Helper to get a pointer to an object's __dict__ slot, if any */
1109
1110PyObject **
1111_PyObject_GetDictPtr(PyObject *obj)
1112{
1113#define PTRSIZE (sizeof(PyObject *))
1114
1115 long dictoffset;
1116 PyTypeObject *tp = obj->ob_type;
1117
1118 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1119 return NULL;
1120 dictoffset = tp->tp_dictoffset;
1121 if (dictoffset == 0)
1122 return NULL;
1123 if (dictoffset < 0) {
Neil Schemenauerfd343692001-08-29 23:54:03 +00001124 dictoffset += tp->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125 assert(dictoffset > 0); /* Sanity check */
1126 if (tp->tp_itemsize > 0) {
1127 int n = ((PyVarObject *)obj)->ob_size;
1128 if (n > 0) {
1129 dictoffset += tp->tp_itemsize * n;
1130 /* Round up, if necessary */
1131 if (tp->tp_itemsize % PTRSIZE != 0) {
1132 dictoffset += PTRSIZE - 1;
1133 dictoffset /= PTRSIZE;
1134 dictoffset *= PTRSIZE;
1135 }
1136 }
1137 }
1138 }
1139 return (PyObject **) ((char *)obj + dictoffset);
1140}
1141
1142/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1143
1144PyObject *
1145PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1146{
1147 PyTypeObject *tp = obj->ob_type;
1148 PyObject *descr;
1149 descrgetfunc f;
1150 PyObject **dictptr;
1151
1152 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001153 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001154 return NULL;
1155 }
1156
1157 descr = _PyType_Lookup(tp, name);
1158 f = NULL;
1159 if (descr != NULL) {
1160 f = descr->ob_type->tp_descr_get;
1161 if (f != NULL && PyDescr_IsData(descr))
1162 return f(descr, obj, (PyObject *)obj->ob_type);
1163 }
1164
1165 dictptr = _PyObject_GetDictPtr(obj);
1166 if (dictptr != NULL) {
1167 PyObject *dict = *dictptr;
1168 if (dict != NULL) {
1169 PyObject *res = PyDict_GetItem(dict, name);
1170 if (res != NULL) {
1171 Py_INCREF(res);
1172 return res;
1173 }
1174 }
1175 }
1176
1177 if (f != NULL)
1178 return f(descr, obj, (PyObject *)obj->ob_type);
1179
1180 if (descr != NULL) {
1181 Py_INCREF(descr);
1182 return descr;
1183 }
1184
1185 PyErr_Format(PyExc_AttributeError,
1186 "'%.50s' object has no attribute '%.400s'",
1187 tp->tp_name, PyString_AS_STRING(name));
1188 return NULL;
1189}
1190
1191int
1192PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1193{
1194 PyTypeObject *tp = obj->ob_type;
1195 PyObject *descr;
1196 descrsetfunc f;
1197 PyObject **dictptr;
1198
1199 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001200 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001201 return -1;
1202 }
1203
1204 descr = _PyType_Lookup(tp, name);
1205 f = NULL;
1206 if (descr != NULL) {
1207 f = descr->ob_type->tp_descr_set;
1208 if (f != NULL && PyDescr_IsData(descr))
1209 return f(descr, obj, value);
1210 }
1211
1212 dictptr = _PyObject_GetDictPtr(obj);
1213 if (dictptr != NULL) {
1214 PyObject *dict = *dictptr;
1215 if (dict == NULL && value != NULL) {
1216 dict = PyDict_New();
1217 if (dict == NULL)
1218 return -1;
1219 *dictptr = dict;
1220 }
1221 if (dict != NULL) {
1222 int res;
1223 if (value == NULL)
1224 res = PyDict_DelItem(dict, name);
1225 else
1226 res = PyDict_SetItem(dict, name, value);
1227 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1228 PyErr_SetObject(PyExc_AttributeError, name);
1229 return res;
1230 }
1231 }
1232
1233 if (f != NULL)
1234 return f(descr, obj, value);
1235
1236 if (descr == NULL) {
1237 PyErr_Format(PyExc_AttributeError,
1238 "'%.50s' object has no attribute '%.400s'",
1239 tp->tp_name, PyString_AS_STRING(name));
1240 return -1;
1241 }
1242
1243 PyErr_Format(PyExc_AttributeError,
1244 "'%.50s' object attribute '%.400s' is read-only",
1245 tp->tp_name, PyString_AS_STRING(name));
1246 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001247}
1248
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001249/* Test a value used as condition, e.g., in a for or if statement.
1250 Return -1 if an error occurred */
1251
1252int
Fred Drake100814d2000-07-09 15:48:49 +00001253PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001254{
1255 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001257 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001258 else if (v->ob_type->tp_as_number != NULL &&
1259 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001260 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001261 else if (v->ob_type->tp_as_mapping != NULL &&
1262 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001263 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001264 else if (v->ob_type->tp_as_sequence != NULL &&
1265 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001266 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1267 else
1268 res = 1;
1269 if (res > 0)
1270 res = 1;
1271 return res;
1272}
1273
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001274/* equivalent of 'not v'
1275 Return -1 if an error occurred */
1276
1277int
Fred Drake100814d2000-07-09 15:48:49 +00001278PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001279{
1280 int res;
1281 res = PyObject_IsTrue(v);
1282 if (res < 0)
1283 return res;
1284 return res == 0;
1285}
1286
Guido van Rossum5524a591995-01-10 15:26:20 +00001287/* Coerce two numeric types to the "larger" one.
1288 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001289 Return value:
1290 -1 if an error occurred;
1291 0 if the coercion succeeded (and then the reference counts are increased);
1292 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001293*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001294int
Fred Drake100814d2000-07-09 15:48:49 +00001295PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001296{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 register PyObject *v = *pv;
1298 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001299 int res;
1300
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001301 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1302 Py_INCREF(v);
1303 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001304 return 0;
1305 }
1306 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1307 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1308 if (res <= 0)
1309 return res;
1310 }
1311 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1312 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1313 if (res <= 0)
1314 return res;
1315 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001316 return 1;
1317}
1318
Guido van Rossume797ec12001-01-17 15:24:28 +00001319/* Coerce two numeric types to the "larger" one.
1320 Increment the reference count on each argument.
1321 Return -1 and raise an exception if no coercion is possible
1322 (and then no reference count is incremented).
1323*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001324int
Fred Drake100814d2000-07-09 15:48:49 +00001325PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001326{
1327 int err = PyNumber_CoerceEx(pv, pw);
1328 if (err <= 0)
1329 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001331 return -1;
1332}
1333
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001334
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001335/* Test whether an object can be called */
1336
1337int
Fred Drake100814d2000-07-09 15:48:49 +00001338PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001339{
1340 if (x == NULL)
1341 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 if (PyInstance_Check(x)) {
1343 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001344 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001346 return 0;
1347 }
1348 /* Could test recursively but don't, for fear of endless
1349 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001351 return 1;
1352 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001353 else {
1354 return x->ob_type->tp_call != NULL;
1355 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001356}
1357
1358
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359/*
1360NoObject is usable as a non-NULL undefined value, used by the macro None.
1361There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001363(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001364*/
1365
Guido van Rossum0c182a11992-03-27 17:26:13 +00001366/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001368none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001371}
1372
Barry Warsaw9bf16442001-01-23 16:24:35 +00001373/* ARGUSED */
1374static void
1375none_dealloc(PyObject* ignore)
1376{
1377 /* This should never get called, but we also don't want to SEGV if
1378 * we accidently decref None out of existance.
1379 */
1380 abort();
1381}
1382
1383
Guido van Rossumba21a492001-08-16 08:17:26 +00001384static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001386 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001387 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001388 0,
1389 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001390 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001391 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001392 0, /*tp_getattr*/
1393 0, /*tp_setattr*/
1394 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001395 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001396 0, /*tp_as_number*/
1397 0, /*tp_as_sequence*/
1398 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001399 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001400};
1401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001403 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001404};
1405
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001406/* NotImplemented is an object that can be used to signal that an
1407 operation is not implemented for the given type combination. */
1408
1409static PyObject *
1410NotImplemented_repr(PyObject *op)
1411{
1412 return PyString_FromString("NotImplemented");
1413}
1414
1415static PyTypeObject PyNotImplemented_Type = {
1416 PyObject_HEAD_INIT(&PyType_Type)
1417 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001418 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001419 0,
1420 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001421 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001422 0, /*tp_print*/
1423 0, /*tp_getattr*/
1424 0, /*tp_setattr*/
1425 0, /*tp_compare*/
1426 (reprfunc)NotImplemented_repr, /*tp_repr*/
1427 0, /*tp_as_number*/
1428 0, /*tp_as_sequence*/
1429 0, /*tp_as_mapping*/
1430 0, /*tp_hash */
1431};
1432
1433PyObject _Py_NotImplementedStruct = {
1434 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1435};
1436
Guido van Rossumba21a492001-08-16 08:17:26 +00001437void
1438_Py_ReadyTypes(void)
1439{
1440 if (PyType_Ready(&PyType_Type) < 0)
1441 Py_FatalError("Can't initialize 'type'");
1442
1443 if (PyType_Ready(&PyList_Type) < 0)
1444 Py_FatalError("Can't initialize 'list'");
1445
1446 if (PyType_Ready(&PyNone_Type) < 0)
1447 Py_FatalError("Can't initialize type(None)");
1448
1449 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1450 Py_FatalError("Can't initialize type(NotImplemented)");
1451}
1452
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001453
Guido van Rossum84a90321996-05-22 16:34:47 +00001454#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001455
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001456static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001458void
Fred Drake100814d2000-07-09 15:48:49 +00001459_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001460{
1461 refchain._ob_prev = refchain._ob_next = &refchain;
1462 _Py_RefTotal = 0;
1463}
1464
1465void
Fred Drake100814d2000-07-09 15:48:49 +00001466_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001467{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001468 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001469 op->ob_refcnt = 1;
1470 op->_ob_next = refchain._ob_next;
1471 op->_ob_prev = &refchain;
1472 refchain._ob_next->_ob_prev = op;
1473 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001474#ifdef COUNT_ALLOCS
1475 inc_count(op->ob_type);
1476#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001477}
1478
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001479void
Fred Drake100814d2000-07-09 15:48:49 +00001480_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001482#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001483 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001484#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001485 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001487 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001488 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001490#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001491 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1492 if (p == op)
1493 break;
1494 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001495 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001496 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001497#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001498 op->_ob_next->_ob_prev = op->_ob_prev;
1499 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001500 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001501#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001502 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001503#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001504}
1505
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001506void
Fred Drake100814d2000-07-09 15:48:49 +00001507_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001508{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001509 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001510 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001511 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512}
1513
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001514void
Fred Drake100814d2000-07-09 15:48:49 +00001515_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001518 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1520 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 if (PyObject_Print(op, fp, 0) != 0)
1522 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523 putc('\n', fp);
1524 }
1525}
1526
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001527PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001528_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001529{
1530 int i, n;
1531 PyObject *t = NULL;
1532 PyObject *res, *op;
1533
1534 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1535 return NULL;
1536 op = refchain._ob_next;
1537 res = PyList_New(0);
1538 if (res == NULL)
1539 return NULL;
1540 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1541 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001542 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001543 op = op->_ob_next;
1544 if (op == &refchain)
1545 return res;
1546 }
1547 if (PyList_Append(res, op) < 0) {
1548 Py_DECREF(res);
1549 return NULL;
1550 }
1551 op = op->_ob_next;
1552 }
1553 return res;
1554}
1555
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001556#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001557
1558
1559/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001560PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001561
1562
1563/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001564int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001565
1566
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001567/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001568
Thomas Wouters334fb892000-07-25 12:56:38 +00001569void *
Fred Drake100814d2000-07-09 15:48:49 +00001570PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001571{
1572#if _PyMem_EXTRA > 0
1573 if (nbytes == 0)
1574 nbytes = _PyMem_EXTRA;
1575#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001576 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001577}
1578
Thomas Wouters334fb892000-07-25 12:56:38 +00001579void *
1580PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001581{
1582#if _PyMem_EXTRA > 0
1583 if (nbytes == 0)
1584 nbytes = _PyMem_EXTRA;
1585#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001586 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001587}
1588
1589void
Thomas Wouters334fb892000-07-25 12:56:38 +00001590PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001591{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001592 PyMem_FREE(p);
1593}
1594
1595
1596/* Python's object malloc wrappers (see objimpl.h) */
1597
Thomas Wouters334fb892000-07-25 12:56:38 +00001598void *
Fred Drake100814d2000-07-09 15:48:49 +00001599PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001600{
1601 return PyObject_MALLOC(nbytes);
1602}
1603
Thomas Wouters334fb892000-07-25 12:56:38 +00001604void *
1605PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001606{
1607 return PyObject_REALLOC(p, nbytes);
1608}
1609
1610void
Thomas Wouters334fb892000-07-25 12:56:38 +00001611PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001612{
1613 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001614}
Guido van Rossum86610361998-04-10 22:32:46 +00001615
1616
Fred Drake41deb1e2001-02-01 05:27:45 +00001617/* Hook to clear up weak references only once the _weakref module is
1618 imported. We use a dummy implementation to simplify the code at each
1619 call site instead of requiring a test for NULL.
1620*/
1621
Fred Drakeb60654b2001-02-26 18:56:37 +00001622static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001623empty_clear_weak_refs(PyObject *o)
1624{
Fred Drakeb60654b2001-02-26 18:56:37 +00001625 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001626}
1627
Fred Drakeb60654b2001-02-26 18:56:37 +00001628void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001629
1630
1631
Guido van Rossum86610361998-04-10 22:32:46 +00001632/* These methods are used to control infinite recursion in repr, str, print,
1633 etc. Container objects that may recursively contain themselves,
1634 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1635 Py_ReprLeave() to avoid infinite recursion.
1636
1637 Py_ReprEnter() returns 0 the first time it is called for a particular
1638 object and 1 every time thereafter. It returns -1 if an exception
1639 occurred. Py_ReprLeave() has no return value.
1640
1641 See dictobject.c and listobject.c for examples of use.
1642*/
1643
1644#define KEY "Py_Repr"
1645
1646int
Fred Drake100814d2000-07-09 15:48:49 +00001647Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001648{
1649 PyObject *dict;
1650 PyObject *list;
1651 int i;
1652
1653 dict = PyThreadState_GetDict();
1654 if (dict == NULL)
1655 return -1;
1656 list = PyDict_GetItemString(dict, KEY);
1657 if (list == NULL) {
1658 list = PyList_New(0);
1659 if (list == NULL)
1660 return -1;
1661 if (PyDict_SetItemString(dict, KEY, list) < 0)
1662 return -1;
1663 Py_DECREF(list);
1664 }
1665 i = PyList_GET_SIZE(list);
1666 while (--i >= 0) {
1667 if (PyList_GET_ITEM(list, i) == obj)
1668 return 1;
1669 }
1670 PyList_Append(list, obj);
1671 return 0;
1672}
1673
1674void
Fred Drake100814d2000-07-09 15:48:49 +00001675Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001676{
1677 PyObject *dict;
1678 PyObject *list;
1679 int i;
1680
1681 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001682 if (dict == NULL)
1683 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001684 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001685 if (list == NULL || !PyList_Check(list))
1686 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001687 i = PyList_GET_SIZE(list);
1688 /* Count backwards because we always expect obj to be list[-1] */
1689 while (--i >= 0) {
1690 if (PyList_GET_ITEM(list, i) == obj) {
1691 PyList_SetSlice(list, i, i + 1, NULL);
1692 break;
1693 }
1694 }
1695}
Guido van Rossumd724b232000-03-13 16:01:29 +00001696
1697/*
1698 trashcan
1699 CT 2k0130
1700 non-recursively destroy nested objects
1701
1702 CT 2k0223
1703 everything is now done in a macro.
1704
1705 CT 2k0305
1706 modified to use functions, after Tim Peter's suggestion.
1707
1708 CT 2k0309
1709 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001710
1711 CT 2k0325
1712 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001713
1714 CT 2k0422
1715 complete rewrite. We now build a chain via ob_type
1716 and save the limited number of types in ob_refcnt.
1717 This is perfect since we don't need any memory.
1718 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001719*/
1720
Guido van Rossume92e6102000-04-24 15:40:53 +00001721#define Py_TRASHCAN_TUPLE 1
1722#define Py_TRASHCAN_LIST 2
1723#define Py_TRASHCAN_DICT 3
1724#define Py_TRASHCAN_FRAME 4
1725#define Py_TRASHCAN_TRACEBACK 5
1726/* extend here if other objects want protection */
1727
Guido van Rossumd724b232000-03-13 16:01:29 +00001728int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001729
Guido van Rossumd724b232000-03-13 16:01:29 +00001730PyObject * _PyTrash_delete_later = NULL;
1731
1732void
Fred Drake100814d2000-07-09 15:48:49 +00001733_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001734{
Guido van Rossume92e6102000-04-24 15:40:53 +00001735 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001736
Guido van Rossume92e6102000-04-24 15:40:53 +00001737 if (PyTuple_Check(op))
1738 typecode = Py_TRASHCAN_TUPLE;
1739 else if (PyList_Check(op))
1740 typecode = Py_TRASHCAN_LIST;
1741 else if (PyDict_Check(op))
1742 typecode = Py_TRASHCAN_DICT;
1743 else if (PyFrame_Check(op))
1744 typecode = Py_TRASHCAN_FRAME;
1745 else if (PyTraceBack_Check(op))
1746 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001747 else /* We have a bug here -- those are the only types in GC */ {
1748 Py_FatalError("Type not supported in GC -- internal bug");
1749 return; /* pacify compiler -- execution never here */
1750 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001751 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001752
Guido van Rossume92e6102000-04-24 15:40:53 +00001753 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1754 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001755}
1756
1757void
Fred Drake100814d2000-07-09 15:48:49 +00001758_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001759{
1760 while (_PyTrash_delete_later) {
1761 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001762 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1763
1764 switch (shredder->ob_refcnt) {
1765 case Py_TRASHCAN_TUPLE:
1766 shredder->ob_type = &PyTuple_Type;
1767 break;
1768 case Py_TRASHCAN_LIST:
1769 shredder->ob_type = &PyList_Type;
1770 break;
1771 case Py_TRASHCAN_DICT:
1772 shredder->ob_type = &PyDict_Type;
1773 break;
1774 case Py_TRASHCAN_FRAME:
1775 shredder->ob_type = &PyFrame_Type;
1776 break;
1777 case Py_TRASHCAN_TRACEBACK:
1778 shredder->ob_type = &PyTraceBack_Type;
1779 break;
1780 }
1781 _Py_NewReference(shredder);
1782
Guido van Rossumd724b232000-03-13 16:01:29 +00001783 ++_PyTrash_delete_nesting;
1784 Py_DECREF(shredder);
1785 --_PyTrash_delete_nesting;
1786 }
1787}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001788
1789#ifdef WITH_PYMALLOC
1790#include "obmalloc.c"
1791#endif