blob: b11df72193018ec21d4bc54cdc1cf8abf7325a39 [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",
35 tp->tp_name, tp->tp_alloc, tp->tp_free,
36 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) {
56 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
57 tp->tp_free, tp->tp_maxalloc);
58 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{
75 if (tp->tp_alloc == 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 }
82 tp->tp_alloc++;
83 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
85}
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 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096#ifdef WITH_CYCLE_GC
97 if (PyType_IS_GC(tp))
98 op = (PyObject *) PyObject_FROM_GC(op);
99#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return op;
104}
105
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000107PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108{
109 if (op == NULL) {
110 PyErr_SetString(PyExc_SystemError,
111 "NULL object passed to PyObject_InitVar");
112 return op;
113 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000114#ifdef WITH_CYCLE_GC
115 if (PyType_IS_GC(tp))
116 op = (PyVarObject *) PyObject_FROM_GC(op);
117#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 /* Any changes should be reflected in PyObject_INIT_VAR */
119 op->ob_size = size;
120 op->ob_type = tp;
121 _Py_NewReference((PyObject *)op);
122 return op;
123}
124
125PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000126_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127{
128 PyObject *op;
129 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
130 if (op == NULL)
131 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132#ifdef WITH_CYCLE_GC
133 if (PyType_IS_GC(tp))
134 op = (PyObject *) PyObject_FROM_GC(op);
135#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 return PyObject_INIT(op, tp);
137}
138
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000139PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 PyVarObject *op;
143 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000145 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146#ifdef WITH_CYCLE_GC
147 if (PyType_IS_GC(tp))
148 op = (PyVarObject *) PyObject_FROM_GC(op);
149#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 return PyObject_INIT_VAR(op, tp, size);
151}
152
153void
Fred Drake100814d2000-07-09 15:48:49 +0000154_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000157 if (op && PyType_IS_GC(op->ob_type)) {
158 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160#endif
161 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifndef WITH_CYCLE_GC
165/* extension modules might need these */
166void _PyGC_Insert(PyObject *op) { }
167void _PyGC_Remove(PyObject *op) { }
168#endif
169
Guido van Rossum90933611991-06-07 16:10:43 +0000170int
Fred Drake100814d2000-07-09 15:48:49 +0000171PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
Guido van Rossum278ef591991-07-27 21:40:24 +0000173 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000175 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000176#ifdef USE_STACKCHECK
177 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000178 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000179 return -1;
180 }
181#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000182 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000183 if (op == NULL) {
184 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 }
Guido van Rossum90933611991-06-07 16:10:43 +0000186 else {
187 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000188 fprintf(fp, "<refcnt %u at %p>",
189 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000190 else if (op->ob_type->tp_print == NULL) {
191 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000192 fprintf(fp, "<%s object at %p>",
193 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000194 }
195 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyObject *s;
197 if (flags & Py_PRINT_RAW)
198 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000199 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 if (s == NULL)
202 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000204 ret = PyObject_Print(s, fp,
205 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 }
209 }
Guido van Rossum90933611991-06-07 16:10:43 +0000210 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000211 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000212 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000213 if (ret == 0) {
214 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 clearerr(fp);
217 ret = -1;
218 }
219 }
220 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000224PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000227 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000228#ifdef USE_STACKCHECK
229 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000230 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000231 return NULL;
232 }
233#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000234 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000236 else if (v->ob_type->tp_repr == NULL) {
237 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000238 sprintf(buf, "<%.80s object at %p>",
239 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000242 else {
243 PyObject *res;
244 res = (*v->ob_type->tp_repr)(v);
245 if (res == NULL)
246 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000247 if (PyUnicode_Check(res)) {
248 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000249 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000250 Py_DECREF(res);
251 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000252 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000253 else
254 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000255 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000256 if (!PyString_Check(res)) {
257 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000258 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 res->ob_type->tp_name);
260 Py_DECREF(res);
261 return NULL;
262 }
263 return res;
264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000268PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000269{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000270 PyObject *res;
271
Guido van Rossumc6004111993-11-05 10:22:19 +0000272 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 return PyString_FromString("<NULL>");
274 else if (PyString_Check(v)) {
275 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000276 return v;
277 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000278 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000280 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 if (!PyInstance_Check(v) ||
283 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
284 PyErr_Clear();
285 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000286 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 res = PyEval_CallObject(func, (PyObject *)NULL);
288 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000290 if (res == NULL)
291 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000292 if (PyUnicode_Check(res)) {
293 PyObject* str;
294 str = PyUnicode_AsEncodedString(res, NULL, NULL);
295 Py_DECREF(res);
296 if (str)
297 res = str;
298 else
299 return NULL;
300 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 if (!PyString_Check(res)) {
302 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000303 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304 res->ob_type->tp_name);
305 Py_DECREF(res);
306 return NULL;
307 }
308 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000309}
310
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000311#define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
312 Py_TPFLAGS_NEWSTYLENUMBER)
313
314static int
315cmp_to_int(PyObject *result)
316{
317 int c;
318 if (result == NULL)
319 return -1;
320 if (!PyInt_Check(result)) {
321 PyErr_SetString(PyExc_TypeError,
322 "comparison did not return an int");
323 return -1;
324 }
325 c = PyInt_AS_LONG(result);
326 Py_DECREF(result);
327 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
328}
329
330static int
Fred Drake100814d2000-07-09 15:48:49 +0000331do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000332{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000333 PyNumberMethods *mv, *mw;
334 PyObject *x;
335 int c;
336
337 /* new style nb_cmp gets priority */
338 mv = v->ob_type->tp_as_number;
339 if (mv != NULL && NEW_STYLE_NUMBER(v) && mv->nb_cmp) {
340 x = (*mv->nb_cmp)(v, w);
341 if (x != Py_NotImplemented)
342 return cmp_to_int(x);
343 Py_DECREF(x);
344 }
345 mw = w->ob_type->tp_as_number;
346 if (mw != NULL && NEW_STYLE_NUMBER(w) && mw->nb_cmp) {
347 x = (*mw->nb_cmp)(v, w);
348 if (x != Py_NotImplemented)
349 return cmp_to_int(x);
350 Py_DECREF(x);
351 }
352 /* fall back to tp_compare */
353 if (v->ob_type == w->ob_type) {
354 if (v->ob_type->tp_compare != NULL) {
355 return (*v->ob_type->tp_compare)(v, w);
356 }
357 else {
358 Py_uintptr_t iv = (Py_uintptr_t)v;
359 Py_uintptr_t iw = (Py_uintptr_t)w;
360 return (iv < iw) ? -1 : (iv > iw) ? 1 : 0;
361 }
362 }
363 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
364 c = PyUnicode_Compare(v, w);
365 if (c == -1 &&
366 PyErr_Occurred() &&
367 PyErr_ExceptionMatches(PyExc_TypeError))
368 /* TypeErrors are ignored: if Unicode coercion
369 fails due to one of the arguments not having
370 the right type, we continue as defined by the
371 coercion protocol (see above). Luckily,
372 decoding errors are reported as ValueErrors and
373 are not masked by this technique. */
374 PyErr_Clear();
375 else
376 return c;
377 }
378 /* fall back to coercion */
379 if (mv && mw && (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w))) {
380 /* old style operand, both operations numeric, coerce */
381 int err = PyNumber_CoerceEx(&v, &w);
382 if (err < 0)
383 return -1;
384 if (err == 0) {
385 if (v->ob_type->tp_compare) {
386 c = (*v->ob_type->tp_compare)(v, w);
387 }
388 else {
389 Py_uintptr_t iv = (Py_uintptr_t)v;
390 Py_uintptr_t iw = (Py_uintptr_t)w;
391 c = (iv < iw) ? -1 : (iv > iw) ? 1 : 0;
392 }
393 Py_DECREF(v);
394 Py_DECREF(w);
395 return c;
396 }
397 }
398 /* last resort, use type names */
399 c = strcmp(v->ob_type->tp_name, w->ob_type->tp_name);
400 return (c < 0) ? -1: (c > 0) ? 1 : 0;
Guido van Rossum20566841995-01-12 11:26:10 +0000401}
402
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000403PyObject *_PyCompareState_Key;
404
Thomas Wouters7e474022000-07-16 12:04:32 +0000405/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000406 some types) and decremented on exit. If the count exceeds the
407 nesting limit, enable code to detect circular data structures.
408*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000409#ifdef macintosh
410#define NESTING_LIMIT 60
411#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000412#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000413#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000414int _PyCompareState_nesting = 0;
415
416static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000417get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000418{
419 PyObject *tstate_dict, *inprogress;
420
421 tstate_dict = PyThreadState_GetDict();
422 if (tstate_dict == NULL) {
423 PyErr_BadInternalCall();
424 return NULL;
425 }
426 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
427 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000428 inprogress = PyDict_New();
429 if (inprogress == NULL)
430 return NULL;
431 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
432 inprogress) == -1) {
433 Py_DECREF(inprogress);
434 return NULL;
435 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000436 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000437 }
438 return inprogress;
439}
440
441static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000442make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000443{
444 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000445 Py_uintptr_t iv = (Py_uintptr_t)v;
446 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000447
448 pair = PyTuple_New(2);
449 if (pair == NULL) {
450 return NULL;
451 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000452 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000453 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
454 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
455 } else {
456 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
457 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
458 }
459 return pair;
460}
461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462int
Fred Drake100814d2000-07-09 15:48:49 +0000463PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000465 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000466 int result;
467
Jack Jansend49cbe12000-08-22 21:52:51 +0000468#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000469 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000470 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
471 return -1;
472 }
473#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000474 if (v == NULL || w == NULL) {
475 PyErr_BadInternalCall();
476 return -1;
477 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 if (v == w)
479 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000480 vtp = v->ob_type;
481 wtp = w->ob_type;
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000482 _PyCompareState_nesting++;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000483 if (_PyCompareState_nesting > NESTING_LIMIT &&
484 (vtp->tp_as_mapping
485 || PyInstance_Check(v)
486 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
487 /* try to detect circular data structures */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000488 PyObject *inprogress, *pair;
489
490 inprogress = get_inprogress_dict();
491 if (inprogress == NULL) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000492 result = -1;
493 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000494 }
495 pair = make_pair(v, w);
496 if (PyDict_GetItem(inprogress, pair)) {
497 /* already comparing these objects. assume
498 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000499 Py_DECREF(pair);
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000500 result = 0;
501 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000502 }
503 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000504 result = -1;
505 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000506 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000507 result = do_cmp(v, w);
508 /* XXX DelItem shouldn't fail */
509 PyDict_DelItem(inprogress, pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000510 Py_DECREF(pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000511 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000512 else {
513 result = do_cmp(v, w);
514 }
515exit_cmp:
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000516 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000517 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518}
519
Fred Drake13634cf2000-06-29 19:17:04 +0000520
521/* Set of hash utility functions to help maintaining the invariant that
522 iff a==b then hash(a)==hash(b)
523
524 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
525*/
526
527long
Fred Drake100814d2000-07-09 15:48:49 +0000528_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000529{
Tim Peters39dce292000-08-15 03:34:48 +0000530 double intpart, fractpart;
531 int expo;
532 long hipart;
533 long x; /* the final hash value */
534 /* This is designed so that Python numbers of different types
535 * that compare equal hash to the same value; otherwise comparisons
536 * of mapping keys will turn out weird.
537 */
538
539#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
540{
541 extended e;
542 fractpart = modf(v, &e);
543 intpart = e;
544}
545#else
546 fractpart = modf(v, &intpart);
547#endif
548 if (fractpart == 0.0) {
549 /* This must return the same hash as an equal int or long. */
550 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
551 /* Convert to long and use its hash. */
552 PyObject *plong; /* converted to Python long */
553 if (Py_IS_INFINITY(intpart))
554 /* can't convert to long int -- arbitrary */
555 v = v < 0 ? -271828.0 : 314159.0;
556 plong = PyLong_FromDouble(v);
557 if (plong == NULL)
558 return -1;
559 x = PyObject_Hash(plong);
560 Py_DECREF(plong);
561 return x;
562 }
563 /* Fits in a C long == a Python int, so is its own hash. */
564 x = (long)intpart;
565 if (x == -1)
566 x = -2;
567 return x;
568 }
569 /* The fractional part is non-zero, so we don't have to worry about
570 * making this match the hash of some other type.
571 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000572 * Since the VAX D double format has 56 mantissa bits, which is the
573 * most of any double format in use, each of these parts may have as
574 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000575 * So, assuming sizeof(long) >= 4, each part can be broken into two
576 * longs; frexp and multiplication are used to do that.
577 * Also, since the Cray double format has 15 exponent bits, which is
578 * the most of any double format in use, shifting the exponent field
579 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000580 */
Tim Peters39dce292000-08-15 03:34:48 +0000581 v = frexp(v, &expo);
582 v *= 2147483648.0; /* 2**31 */
583 hipart = (long)v; /* take the top 32 bits */
584 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
585 x = hipart + (long)v + (expo << 15);
586 if (x == -1)
587 x = -2;
588 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000589}
590
591long
Fred Drake100814d2000-07-09 15:48:49 +0000592_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000593{
594#if SIZEOF_LONG >= SIZEOF_VOID_P
595 return (long)p;
596#else
597 /* convert to a Python long and hash that */
598 PyObject* longobj;
599 long x;
600
601 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
602 x = -1;
603 goto finally;
604 }
605 x = PyObject_Hash(longobj);
606
607finally:
608 Py_XDECREF(longobj);
609 return x;
610#endif
611}
612
613
Guido van Rossum9bfef441993-03-29 10:43:31 +0000614long
Fred Drake100814d2000-07-09 15:48:49 +0000615PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000616{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000618 if (tp->tp_hash != NULL)
619 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000620 if (tp->tp_compare == NULL) {
621 return _Py_HashPointer(v); /* Use address as hash value */
622 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000623 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000625 return -1;
626}
627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000629PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000630{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000631 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000633 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000634 if (w == NULL)
635 return NULL;
636 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000638 return res;
639 }
640
Guido van Rossum3f5da241990-12-20 15:06:42 +0000641 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000642 PyErr_Format(PyExc_AttributeError,
643 "'%.50s' object has no attribute '%.400s'",
644 v->ob_type->tp_name,
645 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000646 return NULL;
647 }
648 else {
649 return (*v->ob_type->tp_getattr)(v, name);
650 }
651}
652
653int
Fred Drake100814d2000-07-09 15:48:49 +0000654PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000655{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000657 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000659 return 1;
660 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000662 return 0;
663}
664
665int
Fred Drake100814d2000-07-09 15:48:49 +0000666PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000667{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000668 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000670 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000671 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000672 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000673 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000674 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000676 return res;
677 }
678
Guido van Rossum3f5da241990-12-20 15:06:42 +0000679 if (v->ob_type->tp_setattr == NULL) {
680 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000682 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000683 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000685 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000686 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000687 }
688 else {
689 return (*v->ob_type->tp_setattr)(v, name, w);
690 }
691}
692
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000693/* Internal API needed by PyObject_GetAttr(): */
694extern
695PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
696 const char *errors);
697
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000698PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000699PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000700{
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000701 /* The Unicode to string conversion is done here because the
702 existing tp_getattro slots expect a string object as name
703 and we wouldn't want to break those. */
704 if (PyUnicode_Check(name)) {
705 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
706 if (name == NULL)
707 return NULL;
708 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000709
710 if (!PyString_Check(name)) {
711 PyErr_SetString(PyExc_TypeError,
712 "attribute name must be string");
713 return NULL;
714 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000715 if (v->ob_type->tp_getattro != NULL)
716 return (*v->ob_type->tp_getattro)(v, name);
717 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000718 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000719}
720
721int
Fred Drake100814d2000-07-09 15:48:49 +0000722PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000723{
724 PyObject *res = PyObject_GetAttr(v, name);
725 if (res != NULL) {
726 Py_DECREF(res);
727 return 1;
728 }
729 PyErr_Clear();
730 return 0;
731}
732
733int
Fred Drake100814d2000-07-09 15:48:49 +0000734PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000735{
736 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000737
738 /* The Unicode to string conversion is done here because the
739 existing tp_setattro slots expect a string object as name
740 and we wouldn't want to break those. */
741 if (PyUnicode_Check(name)) {
742 name = PyUnicode_AsEncodedString(name, NULL, NULL);
743 if (name == NULL)
744 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000745 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000746 else
747 Py_INCREF(name);
748
749 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000750 PyErr_SetString(PyExc_TypeError,
751 "attribute name must be string");
752 err = -1;
753 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000754 else {
755 PyString_InternInPlace(&name);
756 if (v->ob_type->tp_setattro != NULL)
757 err = (*v->ob_type->tp_setattro)(v, name, value);
758 else
759 err = PyObject_SetAttrString(v,
760 PyString_AS_STRING(name), value);
761 }
762
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000763 Py_DECREF(name);
764 return err;
765}
766
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000767/* Test a value used as condition, e.g., in a for or if statement.
768 Return -1 if an error occurred */
769
770int
Fred Drake100814d2000-07-09 15:48:49 +0000771PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000772{
773 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000775 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000776 else if (v->ob_type->tp_as_number != NULL &&
777 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000778 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000779 else if (v->ob_type->tp_as_mapping != NULL &&
780 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000781 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000782 else if (v->ob_type->tp_as_sequence != NULL &&
783 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000784 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
785 else
786 res = 1;
787 if (res > 0)
788 res = 1;
789 return res;
790}
791
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000792/* equivalent of 'not v'
793 Return -1 if an error occurred */
794
795int
Fred Drake100814d2000-07-09 15:48:49 +0000796PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000797{
798 int res;
799 res = PyObject_IsTrue(v);
800 if (res < 0)
801 return res;
802 return res == 0;
803}
804
Guido van Rossum5524a591995-01-10 15:26:20 +0000805/* Coerce two numeric types to the "larger" one.
806 Increment the reference count on each argument.
807 Return -1 and raise an exception if no coercion is possible
808 (and then no reference count is incremented).
809*/
810
811int
Fred Drake100814d2000-07-09 15:48:49 +0000812PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000813{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 register PyObject *v = *pv;
815 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000816 int res;
817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
819 Py_INCREF(v);
820 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000821 return 0;
822 }
823 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
824 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
825 if (res <= 0)
826 return res;
827 }
828 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
829 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
830 if (res <= 0)
831 return res;
832 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000833 return 1;
834}
835
836int
Fred Drake100814d2000-07-09 15:48:49 +0000837PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000838{
839 int err = PyNumber_CoerceEx(pv, pw);
840 if (err <= 0)
841 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000843 return -1;
844}
845
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000847/* Test whether an object can be called */
848
849int
Fred Drake100814d2000-07-09 15:48:49 +0000850PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000851{
852 if (x == NULL)
853 return 0;
854 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855 PyFunction_Check(x) ||
856 PyMethod_Check(x) ||
857 PyCFunction_Check(x) ||
858 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000859 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860 if (PyInstance_Check(x)) {
861 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000862 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000864 return 0;
865 }
866 /* Could test recursively but don't, for fear of endless
867 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000869 return 1;
870 }
871 return 0;
872}
873
874
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000875/*
876NoObject is usable as a non-NULL undefined value, used by the macro None.
877There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000878so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000879*/
880
Guido van Rossum0c182a11992-03-27 17:26:13 +0000881/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000883none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000884{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000886}
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888static PyTypeObject PyNothing_Type = {
889 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000890 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000891 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000892 0,
893 0,
894 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000895 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000896 0, /*tp_getattr*/
897 0, /*tp_setattr*/
898 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000899 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000900 0, /*tp_as_number*/
901 0, /*tp_as_sequence*/
902 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000903 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904};
905
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906PyObject _Py_NoneStruct = {
907 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908};
909
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000910/* NotImplemented is an object that can be used to signal that an
911 operation is not implemented for the given type combination. */
912
913static PyObject *
914NotImplemented_repr(PyObject *op)
915{
916 return PyString_FromString("NotImplemented");
917}
918
919static PyTypeObject PyNotImplemented_Type = {
920 PyObject_HEAD_INIT(&PyType_Type)
921 0,
922 "NotImplemented",
923 0,
924 0,
925 0, /*tp_dealloc*/ /*never called*/
926 0, /*tp_print*/
927 0, /*tp_getattr*/
928 0, /*tp_setattr*/
929 0, /*tp_compare*/
930 (reprfunc)NotImplemented_repr, /*tp_repr*/
931 0, /*tp_as_number*/
932 0, /*tp_as_sequence*/
933 0, /*tp_as_mapping*/
934 0, /*tp_hash */
935};
936
937PyObject _Py_NotImplementedStruct = {
938 PyObject_HEAD_INIT(&PyNotImplemented_Type)
939};
940
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000941
Guido van Rossum84a90321996-05-22 16:34:47 +0000942#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000944static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000946void
Fred Drake100814d2000-07-09 15:48:49 +0000947_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000948{
949 refchain._ob_prev = refchain._ob_next = &refchain;
950 _Py_RefTotal = 0;
951}
952
953void
Fred Drake100814d2000-07-09 15:48:49 +0000954_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957 op->ob_refcnt = 1;
958 op->_ob_next = refchain._ob_next;
959 op->_ob_prev = &refchain;
960 refchain._ob_next->_ob_prev = op;
961 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000962#ifdef COUNT_ALLOCS
963 inc_count(op->ob_type);
964#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965}
966
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000967void
Fred Drake100814d2000-07-09 15:48:49 +0000968_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000969{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000970#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000971 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000972#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000973 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000975 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000976 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000978#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000979 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
980 if (p == op)
981 break;
982 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000983 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000985#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986 op->_ob_next->_ob_prev = op->_ob_prev;
987 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000988 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000989#ifdef COUNT_ALLOCS
990 op->ob_type->tp_free++;
991#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000992}
993
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000994void
Fred Drake100814d2000-07-09 15:48:49 +0000995_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000996{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000997 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000998 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +0000999 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000}
1001
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001002void
Fred Drake100814d2000-07-09 15:48:49 +00001003_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001004{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001006 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001007 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1008 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001009 if (PyObject_Print(op, fp, 0) != 0)
1010 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011 putc('\n', fp);
1012 }
1013}
1014
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001015PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001016_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001017{
1018 int i, n;
1019 PyObject *t = NULL;
1020 PyObject *res, *op;
1021
1022 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1023 return NULL;
1024 op = refchain._ob_next;
1025 res = PyList_New(0);
1026 if (res == NULL)
1027 return NULL;
1028 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1029 while (op == self || op == args || op == res || op == t ||
1030 t != NULL && op->ob_type != (PyTypeObject *) t) {
1031 op = op->_ob_next;
1032 if (op == &refchain)
1033 return res;
1034 }
1035 if (PyList_Append(res, op) < 0) {
1036 Py_DECREF(res);
1037 return NULL;
1038 }
1039 op = op->_ob_next;
1040 }
1041 return res;
1042}
1043
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001045
1046
1047/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001048PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001049
1050
1051/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001052int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001053
1054
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001055/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001056
Thomas Wouters334fb892000-07-25 12:56:38 +00001057void *
Fred Drake100814d2000-07-09 15:48:49 +00001058PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001059{
1060#if _PyMem_EXTRA > 0
1061 if (nbytes == 0)
1062 nbytes = _PyMem_EXTRA;
1063#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001064 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001065}
1066
Thomas Wouters334fb892000-07-25 12:56:38 +00001067void *
1068PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001069{
1070#if _PyMem_EXTRA > 0
1071 if (nbytes == 0)
1072 nbytes = _PyMem_EXTRA;
1073#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001074 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001075}
1076
1077void
Thomas Wouters334fb892000-07-25 12:56:38 +00001078PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001079{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001080 PyMem_FREE(p);
1081}
1082
1083
1084/* Python's object malloc wrappers (see objimpl.h) */
1085
Thomas Wouters334fb892000-07-25 12:56:38 +00001086void *
Fred Drake100814d2000-07-09 15:48:49 +00001087PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001088{
1089 return PyObject_MALLOC(nbytes);
1090}
1091
Thomas Wouters334fb892000-07-25 12:56:38 +00001092void *
1093PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001094{
1095 return PyObject_REALLOC(p, nbytes);
1096}
1097
1098void
Thomas Wouters334fb892000-07-25 12:56:38 +00001099PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001100{
1101 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001102}
Guido van Rossum86610361998-04-10 22:32:46 +00001103
1104
1105/* These methods are used to control infinite recursion in repr, str, print,
1106 etc. Container objects that may recursively contain themselves,
1107 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1108 Py_ReprLeave() to avoid infinite recursion.
1109
1110 Py_ReprEnter() returns 0 the first time it is called for a particular
1111 object and 1 every time thereafter. It returns -1 if an exception
1112 occurred. Py_ReprLeave() has no return value.
1113
1114 See dictobject.c and listobject.c for examples of use.
1115*/
1116
1117#define KEY "Py_Repr"
1118
1119int
Fred Drake100814d2000-07-09 15:48:49 +00001120Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001121{
1122 PyObject *dict;
1123 PyObject *list;
1124 int i;
1125
1126 dict = PyThreadState_GetDict();
1127 if (dict == NULL)
1128 return -1;
1129 list = PyDict_GetItemString(dict, KEY);
1130 if (list == NULL) {
1131 list = PyList_New(0);
1132 if (list == NULL)
1133 return -1;
1134 if (PyDict_SetItemString(dict, KEY, list) < 0)
1135 return -1;
1136 Py_DECREF(list);
1137 }
1138 i = PyList_GET_SIZE(list);
1139 while (--i >= 0) {
1140 if (PyList_GET_ITEM(list, i) == obj)
1141 return 1;
1142 }
1143 PyList_Append(list, obj);
1144 return 0;
1145}
1146
1147void
Fred Drake100814d2000-07-09 15:48:49 +00001148Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001149{
1150 PyObject *dict;
1151 PyObject *list;
1152 int i;
1153
1154 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001155 if (dict == NULL)
1156 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001157 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001158 if (list == NULL || !PyList_Check(list))
1159 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001160 i = PyList_GET_SIZE(list);
1161 /* Count backwards because we always expect obj to be list[-1] */
1162 while (--i >= 0) {
1163 if (PyList_GET_ITEM(list, i) == obj) {
1164 PyList_SetSlice(list, i, i + 1, NULL);
1165 break;
1166 }
1167 }
1168}
Guido van Rossumd724b232000-03-13 16:01:29 +00001169
1170/*
1171 trashcan
1172 CT 2k0130
1173 non-recursively destroy nested objects
1174
1175 CT 2k0223
1176 everything is now done in a macro.
1177
1178 CT 2k0305
1179 modified to use functions, after Tim Peter's suggestion.
1180
1181 CT 2k0309
1182 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001183
1184 CT 2k0325
1185 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001186
1187 CT 2k0422
1188 complete rewrite. We now build a chain via ob_type
1189 and save the limited number of types in ob_refcnt.
1190 This is perfect since we don't need any memory.
1191 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001192*/
1193
Guido van Rossume92e6102000-04-24 15:40:53 +00001194#define Py_TRASHCAN_TUPLE 1
1195#define Py_TRASHCAN_LIST 2
1196#define Py_TRASHCAN_DICT 3
1197#define Py_TRASHCAN_FRAME 4
1198#define Py_TRASHCAN_TRACEBACK 5
1199/* extend here if other objects want protection */
1200
Guido van Rossumd724b232000-03-13 16:01:29 +00001201int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001202
Guido van Rossumd724b232000-03-13 16:01:29 +00001203PyObject * _PyTrash_delete_later = NULL;
1204
1205void
Fred Drake100814d2000-07-09 15:48:49 +00001206_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001207{
Guido van Rossume92e6102000-04-24 15:40:53 +00001208 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001209
Guido van Rossume92e6102000-04-24 15:40:53 +00001210 if (PyTuple_Check(op))
1211 typecode = Py_TRASHCAN_TUPLE;
1212 else if (PyList_Check(op))
1213 typecode = Py_TRASHCAN_LIST;
1214 else if (PyDict_Check(op))
1215 typecode = Py_TRASHCAN_DICT;
1216 else if (PyFrame_Check(op))
1217 typecode = Py_TRASHCAN_FRAME;
1218 else if (PyTraceBack_Check(op))
1219 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001220 else /* We have a bug here -- those are the only types in GC */ {
1221 Py_FatalError("Type not supported in GC -- internal bug");
1222 return; /* pacify compiler -- execution never here */
1223 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001224 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001225
Guido van Rossume92e6102000-04-24 15:40:53 +00001226 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1227 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001228}
1229
1230void
Fred Drake100814d2000-07-09 15:48:49 +00001231_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001232{
1233 while (_PyTrash_delete_later) {
1234 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001235 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1236
1237 switch (shredder->ob_refcnt) {
1238 case Py_TRASHCAN_TUPLE:
1239 shredder->ob_type = &PyTuple_Type;
1240 break;
1241 case Py_TRASHCAN_LIST:
1242 shredder->ob_type = &PyList_Type;
1243 break;
1244 case Py_TRASHCAN_DICT:
1245 shredder->ob_type = &PyDict_Type;
1246 break;
1247 case Py_TRASHCAN_FRAME:
1248 shredder->ob_type = &PyFrame_Type;
1249 break;
1250 case Py_TRASHCAN_TRACEBACK:
1251 shredder->ob_type = &PyTraceBack_Type;
1252 break;
1253 }
1254 _Py_NewReference(shredder);
1255
Guido van Rossumd724b232000-03-13 16:01:29 +00001256 ++_PyTrash_delete_nesting;
1257 Py_DECREF(shredder);
1258 --_PyTrash_delete_nesting;
1259 }
1260}