blob: b15e76e37c5a359d27575be15a58541105447def [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
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000311PyObject *
312PyObject_Unicode(PyObject *v)
313{
314 PyObject *res;
315
316 if (v == NULL)
317 res = PyString_FromString("<NULL>");
318 else if (PyUnicode_Check(v)) {
319 Py_INCREF(v);
320 return v;
321 }
322 else if (PyString_Check(v))
323 res = v;
324 else if (v->ob_type->tp_str != NULL)
325 res = (*v->ob_type->tp_str)(v);
326 else {
327 PyObject *func;
328 static PyObject *strstr;
329 if (strstr == NULL) {
330 strstr= PyString_InternFromString("__str__");
331 if (strstr == NULL)
332 return NULL;
333 }
334 if (!PyInstance_Check(v) ||
335 (func = PyObject_GetAttr(v, strstr)) == NULL) {
336 PyErr_Clear();
337 res = PyObject_Repr(v);
338 }
339 else {
340 res = PyEval_CallObject(func, (PyObject *)NULL);
341 Py_DECREF(func);
342 }
343 }
344 if (res == NULL)
345 return NULL;
346 if (!PyUnicode_Check(res)) {
347 PyObject* str;
348 str = PyUnicode_FromObject(res);
349 Py_DECREF(res);
350 if (str)
351 res = str;
352 else
353 return NULL;
354 }
355 return res;
356}
357
358
Guido van Rossume797ec12001-01-17 15:24:28 +0000359/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
360static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000361
Guido van Rossume797ec12001-01-17 15:24:28 +0000362/* Try a genuine rich comparison, returning an object. Return:
363 NULL for exception;
364 NotImplemented if this particular rich comparison is not implemented or
365 undefined;
366 some object not equal to NotImplemented if it is implemented
367 (this latter object may not be a Boolean).
368*/
369static PyObject *
370try_rich_compare(PyObject *v, PyObject *w, int op)
371{
372 richcmpfunc f;
373 PyObject *res;
374
375 if ((f = v->ob_type->tp_richcompare) != NULL) {
376 res = (*f)(v, w, op);
377 if (res != Py_NotImplemented)
378 return res;
379 Py_DECREF(res);
380 }
381 if ((f = w->ob_type->tp_richcompare) != NULL) {
382 return (*f)(w, v, swapped_op[op]);
383 }
384 res = Py_NotImplemented;
385 Py_INCREF(res);
386 return res;
387}
388
389/* Try a genuine rich comparison, returning an int. Return:
390 -1 for exception (including the case where try_rich_compare() returns an
391 object that's not a Boolean);
392 0 if the outcome is false;
393 1 if the outcome is true;
394 2 if this particular rich comparison is not implemented or undefined.
395*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000396static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000397try_rich_compare_bool(PyObject *v, PyObject *w, int op)
398{
399 PyObject *res;
400 int ok;
401
402 if (v->ob_type->tp_richcompare == NULL &&
403 w->ob_type->tp_richcompare == NULL)
404 return 2; /* Shortcut, avoid INCREF+DECREF */
405 res = try_rich_compare(v, w, op);
406 if (res == NULL)
407 return -1;
408 if (res == Py_NotImplemented) {
409 Py_DECREF(res);
410 return 2;
411 }
412 ok = PyObject_IsTrue(res);
413 Py_DECREF(res);
414 return ok;
415}
416
417/* Try rich comparisons to determine a 3-way comparison. Return:
418 -2 for an exception;
419 -1 if v < w;
420 0 if v == w;
421 1 if v > w;
422 2 if this particular rich comparison is not implemented or undefined.
423*/
424static int
425try_rich_to_3way_compare(PyObject *v, PyObject *w)
426{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000427 static struct { int op; int outcome; } tries[3] = {
428 /* Try this operator, and if it is true, use this outcome: */
429 {Py_EQ, 0},
430 {Py_LT, -1},
431 {Py_GT, 1},
432 };
433 int i;
434
Guido van Rossume797ec12001-01-17 15:24:28 +0000435 if (v->ob_type->tp_richcompare == NULL &&
436 w->ob_type->tp_richcompare == NULL)
437 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000438
439 for (i = 0; i < 3; i++) {
440 switch (try_rich_compare_bool(v, w, tries[i].op)) {
441 case -1:
442 return -1;
443 case 1:
444 return tries[i].outcome;
445 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000446 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000447
448 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000449}
450
451/* Try a 3-way comparison, returning an int. Return:
452 -2 for an exception;
453 -1 if v < w;
454 0 if v == w;
455 1 if v > w;
456 2 if this particular 3-way comparison is not implemented or undefined.
457*/
458static int
459try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000460{
461 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000462 cmpfunc f;
463
464 /* Comparisons involving instances are given to instance_compare,
465 which has the same return conventions as this function. */
466
467 if (PyInstance_Check(v))
468 return (*v->ob_type->tp_compare)(v, w);
469 if (PyInstance_Check(w))
470 return (*w->ob_type->tp_compare)(v, w);
471
472 /* If the types are equal, don't bother with coercions etc. */
473 if (v->ob_type == w->ob_type) {
474 if ((f = v->ob_type->tp_compare) == NULL)
475 return 2;
476 c = (*f)(v, w);
477 if (PyErr_Occurred())
478 return -2;
479 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000480 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000481
482 /* Try coercion; if it fails, give up */
483 c = PyNumber_CoerceEx(&v, &w);
484 if (c < 0)
485 return -2;
486 if (c > 0)
487 return 2;
488
489 /* Try v's comparison, if defined */
490 if ((f = v->ob_type->tp_compare) != NULL) {
491 c = (*f)(v, w);
492 Py_DECREF(v);
493 Py_DECREF(w);
494 if (PyErr_Occurred())
495 return -2;
496 return c < 0 ? -1 : c > 0 ? 1 : 0;
497 }
498
499 /* Try w's comparison, if defined */
500 if ((f = w->ob_type->tp_compare) != NULL) {
501 c = (*f)(w, v); /* swapped! */
502 Py_DECREF(v);
503 Py_DECREF(w);
504 if (PyErr_Occurred())
505 return -2;
506 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
507 }
508
509 /* No comparison defined */
510 Py_DECREF(v);
511 Py_DECREF(w);
512 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000513}
514
Guido van Rossume797ec12001-01-17 15:24:28 +0000515/* Final fallback 3-way comparison, returning an int. Return:
516 -2 if an error occurred;
517 -1 if v < w;
518 0 if v == w;
519 1 if v > w.
520*/
521static int
522default_3way_compare(PyObject *v, PyObject *w)
523{
524 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000525 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000526
527 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000528 /* When comparing these pointers, they must be cast to
529 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
530 * uintptr_t). ANSI specifies that pointer compares other
531 * than == and != to non-related structures are undefined.
532 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000533 Py_uintptr_t vv = (Py_uintptr_t)v;
534 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000535 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
536 }
537
538 /* Special case for Unicode */
539 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
540 c = PyUnicode_Compare(v, w);
541 if (!PyErr_Occurred())
542 return c;
543 /* TypeErrors are ignored: if Unicode coercion fails due
544 to one of the arguments not having the right type, we
545 continue as defined by the coercion protocol (see
546 above). Luckily, decoding errors are reported as
547 ValueErrors and are not masked by this technique. */
548 if (!PyErr_ExceptionMatches(PyExc_TypeError))
549 return -2;
550 PyErr_Clear();
551 }
552
553 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000554 if (v->ob_type->tp_as_number)
555 vname = "";
556 else
557 vname = v->ob_type->tp_name;
558 if (w->ob_type->tp_as_number)
559 wname = "";
560 else
561 wname = w->ob_type->tp_name;
562 c = strcmp(vname, wname);
563 if (c < 0)
564 return -1;
565 if (c > 0)
566 return 1;
567 /* Same type name, or (more likely) incomparable numeric types */
568 return ((Py_uintptr_t)(v->ob_type) < (
569 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000570}
571
572#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
573
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000574static int
Fred Drake100814d2000-07-09 15:48:49 +0000575do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000576{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000577 int c;
578
Guido van Rossume797ec12001-01-17 15:24:28 +0000579 c = try_rich_to_3way_compare(v, w);
580 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000581 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000582 c = try_3way_compare(v, w);
583 if (c < 2)
584 return c;
585 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000586}
587
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000588/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000589 some types) and decremented on exit. If the count exceeds the
590 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000591
592 This is a tunable parameter that should only affect the performance
593 of comparisons, nothing else. Setting it high makes comparing deeply
594 nested non-cyclical data structures faster, but makes comparing cyclical
595 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000596*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000597#define NESTING_LIMIT 20
598
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000599static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000600
601static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000602get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000603{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000604 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000605 PyObject *tstate_dict, *inprogress;
606
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000607 if (key == NULL) {
608 key = PyString_InternFromString("cmp_state");
609 if (key == NULL)
610 return NULL;
611 }
612
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000613 tstate_dict = PyThreadState_GetDict();
614 if (tstate_dict == NULL) {
615 PyErr_BadInternalCall();
616 return NULL;
617 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000618
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000619 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000620 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000621 inprogress = PyDict_New();
622 if (inprogress == NULL)
623 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000624 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000625 Py_DECREF(inprogress);
626 return NULL;
627 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000628 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000630
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000631 return inprogress;
632}
633
634static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000635check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000637 PyObject *inprogress;
638 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000639 Py_uintptr_t iv = (Py_uintptr_t)v;
640 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000641 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000642
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000643 inprogress = get_inprogress_dict();
644 if (inprogress == NULL)
645 return NULL;
646
647 token = PyTuple_New(3);
648 if (token == NULL)
649 return NULL;
650
651 if (iv <= iw) {
652 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
653 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
654 if (op >= 0)
655 op = swapped_op[op];
656 } else {
657 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
658 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
659 }
660 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
661 if (x == NULL || y == NULL || z == NULL) {
662 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000663 return NULL;
664 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000665
666 if (PyDict_GetItem(inprogress, token) != NULL) {
667 Py_DECREF(token);
668 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000669 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000670
671 if (PyDict_SetItem(inprogress, token, token) < 0) {
672 Py_DECREF(token);
673 return NULL;
674 }
675
676 return token;
677}
678
679static void
680delete_token(PyObject *token)
681{
682 PyObject *inprogress;
683
684 if (token == NULL || token == Py_None)
685 return;
686 inprogress = get_inprogress_dict();
687 if (inprogress == NULL)
688 PyErr_Clear();
689 else
690 PyDict_DelItem(inprogress, token);
691 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000692}
693
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694int
Fred Drake100814d2000-07-09 15:48:49 +0000695PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000697 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000698 int result;
699
Jack Jansend49cbe12000-08-22 21:52:51 +0000700#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000701 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000702 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
703 return -1;
704 }
705#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000706 if (v == NULL || w == NULL) {
707 PyErr_BadInternalCall();
708 return -1;
709 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710 if (v == w)
711 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000712 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000713 compare_nesting++;
714 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000715 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000716 || (vtp->tp_as_sequence
717 && !PyString_Check(v)
718 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000719 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000720 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000721
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000722 if (token == NULL) {
723 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000724 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000725 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000726 /* already comparing these objects. assume
727 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000728 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000729 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000730 else {
731 result = do_cmp(v, w);
732 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000733 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000734 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000735 else {
736 result = do_cmp(v, w);
737 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000738 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000739 return result < 0 ? -1 : result;
740}
741
742static PyObject *
743try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
744{
745 int c;
746 PyObject *result;
747
748 c = try_3way_compare(v, w);
749 if (c <= -2)
750 return NULL;
751 if (c >= 2)
752 c = default_3way_compare(v, w);
753 switch (op) {
754 case Py_LT: c = c < 0; break;
755 case Py_LE: c = c <= 0; break;
756 case Py_EQ: c = c == 0; break;
757 case Py_NE: c = c != 0; break;
758 case Py_GT: c = c > 0; break;
759 case Py_GE: c = c >= 0; break;
760 }
761 result = c ? Py_True : Py_False;
762 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000763 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764}
765
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000766static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000767do_richcmp(PyObject *v, PyObject *w, int op)
768{
769 PyObject *res;
770
771 res = try_rich_compare(v, w, op);
772 if (res != Py_NotImplemented)
773 return res;
774 Py_DECREF(res);
775
776 return try_3way_to_rich_compare(v, w, op);
777}
778
779PyObject *
780PyObject_RichCompare(PyObject *v, PyObject *w, int op)
781{
782 PyObject *res;
783
784 assert(Py_LT <= op && op <= Py_GE);
785
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000786 compare_nesting++;
787 if (compare_nesting > NESTING_LIMIT &&
788 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000789 || (v->ob_type->tp_as_sequence
790 && !PyString_Check(v)
791 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000792 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000793 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000794
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000795 if (token == NULL) {
796 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000797 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000798 else if (token == Py_None) {
799 /* already comparing these objects with this operator.
800 assume they're equal until shown otherwise */
801 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000802 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000803 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000804 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000805 else {
806 PyErr_SetString(PyExc_ValueError,
807 "can't order recursive values");
808 res = NULL;
809 }
810 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000811 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000812 else {
813 res = do_richcmp(v, w, op);
814 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000815 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000816 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817 else {
818 res = do_richcmp(v, w, op);
819 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000820 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000821 return res;
822}
823
824int
825PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
826{
827 PyObject *res = PyObject_RichCompare(v, w, op);
828 int ok;
829
830 if (res == NULL)
831 return -1;
832 ok = PyObject_IsTrue(res);
833 Py_DECREF(res);
834 return ok;
835}
Fred Drake13634cf2000-06-29 19:17:04 +0000836
837/* Set of hash utility functions to help maintaining the invariant that
838 iff a==b then hash(a)==hash(b)
839
840 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
841*/
842
843long
Fred Drake100814d2000-07-09 15:48:49 +0000844_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000845{
Tim Peters39dce292000-08-15 03:34:48 +0000846 double intpart, fractpart;
847 int expo;
848 long hipart;
849 long x; /* the final hash value */
850 /* This is designed so that Python numbers of different types
851 * that compare equal hash to the same value; otherwise comparisons
852 * of mapping keys will turn out weird.
853 */
854
855#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
856{
857 extended e;
858 fractpart = modf(v, &e);
859 intpart = e;
860}
861#else
862 fractpart = modf(v, &intpart);
863#endif
864 if (fractpart == 0.0) {
865 /* This must return the same hash as an equal int or long. */
866 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
867 /* Convert to long and use its hash. */
868 PyObject *plong; /* converted to Python long */
869 if (Py_IS_INFINITY(intpart))
870 /* can't convert to long int -- arbitrary */
871 v = v < 0 ? -271828.0 : 314159.0;
872 plong = PyLong_FromDouble(v);
873 if (plong == NULL)
874 return -1;
875 x = PyObject_Hash(plong);
876 Py_DECREF(plong);
877 return x;
878 }
879 /* Fits in a C long == a Python int, so is its own hash. */
880 x = (long)intpart;
881 if (x == -1)
882 x = -2;
883 return x;
884 }
885 /* The fractional part is non-zero, so we don't have to worry about
886 * making this match the hash of some other type.
887 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000888 * Since the VAX D double format has 56 mantissa bits, which is the
889 * most of any double format in use, each of these parts may have as
890 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000891 * So, assuming sizeof(long) >= 4, each part can be broken into two
892 * longs; frexp and multiplication are used to do that.
893 * Also, since the Cray double format has 15 exponent bits, which is
894 * the most of any double format in use, shifting the exponent field
895 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000896 */
Tim Peters39dce292000-08-15 03:34:48 +0000897 v = frexp(v, &expo);
898 v *= 2147483648.0; /* 2**31 */
899 hipart = (long)v; /* take the top 32 bits */
900 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
901 x = hipart + (long)v + (expo << 15);
902 if (x == -1)
903 x = -2;
904 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000905}
906
907long
Fred Drake100814d2000-07-09 15:48:49 +0000908_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000909{
910#if SIZEOF_LONG >= SIZEOF_VOID_P
911 return (long)p;
912#else
913 /* convert to a Python long and hash that */
914 PyObject* longobj;
915 long x;
916
917 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
918 x = -1;
919 goto finally;
920 }
921 x = PyObject_Hash(longobj);
922
923finally:
924 Py_XDECREF(longobj);
925 return x;
926#endif
927}
928
929
Guido van Rossum9bfef441993-03-29 10:43:31 +0000930long
Fred Drake100814d2000-07-09 15:48:49 +0000931PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000932{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000934 if (tp->tp_hash != NULL)
935 return (*tp->tp_hash)(v);
Guido van Rossum41c32442001-01-18 23:33:37 +0000936 if (tp->tp_compare == NULL && tp->tp_richcompare == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000937 return _Py_HashPointer(v); /* Use address as hash value */
938 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000939 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000941 return -1;
942}
943
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000944PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000945PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000946{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000947 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000949 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000950 if (w == NULL)
951 return NULL;
952 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000953 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000954 return res;
955 }
956
Guido van Rossum3f5da241990-12-20 15:06:42 +0000957 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000958 PyErr_Format(PyExc_AttributeError,
959 "'%.50s' object has no attribute '%.400s'",
960 v->ob_type->tp_name,
961 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000962 return NULL;
963 }
964 else {
965 return (*v->ob_type->tp_getattr)(v, name);
966 }
967}
968
969int
Fred Drake100814d2000-07-09 15:48:49 +0000970PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000971{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000972 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000973 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000975 return 1;
976 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000978 return 0;
979}
980
981int
Fred Drake100814d2000-07-09 15:48:49 +0000982PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000983{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000984 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000985 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000986 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000987 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000988 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000989 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000990 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000992 return res;
993 }
994
Guido van Rossum3f5da241990-12-20 15:06:42 +0000995 if (v->ob_type->tp_setattr == NULL) {
996 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000998 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001001 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001002 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001003 }
1004 else {
1005 return (*v->ob_type->tp_setattr)(v, name, w);
1006 }
1007}
1008
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001009/* Internal API needed by PyObject_GetAttr(): */
1010extern
1011PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1012 const char *errors);
1013
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001014PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001015PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001016{
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 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001025
1026 if (!PyString_Check(name)) {
1027 PyErr_SetString(PyExc_TypeError,
1028 "attribute name must be string");
1029 return NULL;
1030 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001031 if (v->ob_type->tp_getattro != NULL)
1032 return (*v->ob_type->tp_getattro)(v, name);
1033 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001034 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001035}
1036
1037int
Fred Drake100814d2000-07-09 15:48:49 +00001038PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001039{
1040 PyObject *res = PyObject_GetAttr(v, name);
1041 if (res != NULL) {
1042 Py_DECREF(res);
1043 return 1;
1044 }
1045 PyErr_Clear();
1046 return 0;
1047}
1048
1049int
Fred Drake100814d2000-07-09 15:48:49 +00001050PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001051{
1052 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001053
1054 /* The Unicode to string conversion is done here because the
1055 existing tp_setattro slots expect a string object as name
1056 and we wouldn't want to break those. */
1057 if (PyUnicode_Check(name)) {
1058 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1059 if (name == NULL)
1060 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001061 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001062 else
1063 Py_INCREF(name);
1064
1065 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001066 PyErr_SetString(PyExc_TypeError,
1067 "attribute name must be string");
1068 err = -1;
1069 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001070 else {
1071 PyString_InternInPlace(&name);
1072 if (v->ob_type->tp_setattro != NULL)
1073 err = (*v->ob_type->tp_setattro)(v, name, value);
1074 else
1075 err = PyObject_SetAttrString(v,
1076 PyString_AS_STRING(name), value);
1077 }
1078
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001079 Py_DECREF(name);
1080 return err;
1081}
1082
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001083/* Test a value used as condition, e.g., in a for or if statement.
1084 Return -1 if an error occurred */
1085
1086int
Fred Drake100814d2000-07-09 15:48:49 +00001087PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001088{
1089 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001091 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001092 else if (v->ob_type->tp_as_number != NULL &&
1093 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001094 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001095 else if (v->ob_type->tp_as_mapping != NULL &&
1096 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001097 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001098 else if (v->ob_type->tp_as_sequence != NULL &&
1099 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001100 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1101 else
1102 res = 1;
1103 if (res > 0)
1104 res = 1;
1105 return res;
1106}
1107
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001108/* equivalent of 'not v'
1109 Return -1 if an error occurred */
1110
1111int
Fred Drake100814d2000-07-09 15:48:49 +00001112PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001113{
1114 int res;
1115 res = PyObject_IsTrue(v);
1116 if (res < 0)
1117 return res;
1118 return res == 0;
1119}
1120
Guido van Rossum5524a591995-01-10 15:26:20 +00001121/* Coerce two numeric types to the "larger" one.
1122 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001123 Return value:
1124 -1 if an error occurred;
1125 0 if the coercion succeeded (and then the reference counts are increased);
1126 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001127*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001128int
Fred Drake100814d2000-07-09 15:48:49 +00001129PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001130{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001131 register PyObject *v = *pv;
1132 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001133 int res;
1134
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001135 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1136 Py_INCREF(v);
1137 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001138 return 0;
1139 }
1140 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1141 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1142 if (res <= 0)
1143 return res;
1144 }
1145 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1146 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1147 if (res <= 0)
1148 return res;
1149 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001150 return 1;
1151}
1152
Guido van Rossume797ec12001-01-17 15:24:28 +00001153/* Coerce two numeric types to the "larger" one.
1154 Increment the reference count on each argument.
1155 Return -1 and raise an exception if no coercion is possible
1156 (and then no reference count is incremented).
1157*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001158int
Fred Drake100814d2000-07-09 15:48:49 +00001159PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001160{
1161 int err = PyNumber_CoerceEx(pv, pw);
1162 if (err <= 0)
1163 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001165 return -1;
1166}
1167
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001168
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001169/* Test whether an object can be called */
1170
1171int
Fred Drake100814d2000-07-09 15:48:49 +00001172PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001173{
1174 if (x == NULL)
1175 return 0;
1176 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001177 PyFunction_Check(x) ||
1178 PyMethod_Check(x) ||
1179 PyCFunction_Check(x) ||
1180 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001181 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 if (PyInstance_Check(x)) {
1183 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001184 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001186 return 0;
1187 }
1188 /* Could test recursively but don't, for fear of endless
1189 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001190 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001191 return 1;
1192 }
1193 return 0;
1194}
1195
1196
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197/*
1198NoObject is usable as a non-NULL undefined value, used by the macro None.
1199There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001200so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201*/
1202
Guido van Rossum0c182a11992-03-27 17:26:13 +00001203/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001204static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001205none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001206{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001208}
1209
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001210static PyTypeObject PyNothing_Type = {
1211 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001212 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001213 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214 0,
1215 0,
1216 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001217 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001218 0, /*tp_getattr*/
1219 0, /*tp_setattr*/
1220 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001221 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001222 0, /*tp_as_number*/
1223 0, /*tp_as_sequence*/
1224 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001225 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001226};
1227
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001228PyObject _Py_NoneStruct = {
1229 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230};
1231
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001232/* NotImplemented is an object that can be used to signal that an
1233 operation is not implemented for the given type combination. */
1234
1235static PyObject *
1236NotImplemented_repr(PyObject *op)
1237{
1238 return PyString_FromString("NotImplemented");
1239}
1240
1241static PyTypeObject PyNotImplemented_Type = {
1242 PyObject_HEAD_INIT(&PyType_Type)
1243 0,
1244 "NotImplemented",
1245 0,
1246 0,
1247 0, /*tp_dealloc*/ /*never called*/
1248 0, /*tp_print*/
1249 0, /*tp_getattr*/
1250 0, /*tp_setattr*/
1251 0, /*tp_compare*/
1252 (reprfunc)NotImplemented_repr, /*tp_repr*/
1253 0, /*tp_as_number*/
1254 0, /*tp_as_sequence*/
1255 0, /*tp_as_mapping*/
1256 0, /*tp_hash */
1257};
1258
1259PyObject _Py_NotImplementedStruct = {
1260 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1261};
1262
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263
Guido van Rossum84a90321996-05-22 16:34:47 +00001264#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001265
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001267
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001268void
Fred Drake100814d2000-07-09 15:48:49 +00001269_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001270{
1271 refchain._ob_prev = refchain._ob_next = &refchain;
1272 _Py_RefTotal = 0;
1273}
1274
1275void
Fred Drake100814d2000-07-09 15:48:49 +00001276_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001278 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001279 op->ob_refcnt = 1;
1280 op->_ob_next = refchain._ob_next;
1281 op->_ob_prev = &refchain;
1282 refchain._ob_next->_ob_prev = op;
1283 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001284#ifdef COUNT_ALLOCS
1285 inc_count(op->ob_type);
1286#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287}
1288
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001289void
Fred Drake100814d2000-07-09 15:48:49 +00001290_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001291{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001292#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001293 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001294#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001295 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001297 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001298 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001300#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001301 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1302 if (p == op)
1303 break;
1304 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001305 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001306 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001307#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001308 op->_ob_next->_ob_prev = op->_ob_prev;
1309 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001310 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001311#ifdef COUNT_ALLOCS
1312 op->ob_type->tp_free++;
1313#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001314}
1315
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001316void
Fred Drake100814d2000-07-09 15:48:49 +00001317_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001318{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001319 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001321 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001322}
1323
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001324void
Fred Drake100814d2000-07-09 15:48:49 +00001325_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001326{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001328 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001329 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1330 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 if (PyObject_Print(op, fp, 0) != 0)
1332 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333 putc('\n', fp);
1334 }
1335}
1336
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001337PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001338_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001339{
1340 int i, n;
1341 PyObject *t = NULL;
1342 PyObject *res, *op;
1343
1344 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1345 return NULL;
1346 op = refchain._ob_next;
1347 res = PyList_New(0);
1348 if (res == NULL)
1349 return NULL;
1350 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1351 while (op == self || op == args || op == res || op == t ||
1352 t != NULL && op->ob_type != (PyTypeObject *) t) {
1353 op = op->_ob_next;
1354 if (op == &refchain)
1355 return res;
1356 }
1357 if (PyList_Append(res, op) < 0) {
1358 Py_DECREF(res);
1359 return NULL;
1360 }
1361 op = op->_ob_next;
1362 }
1363 return res;
1364}
1365
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001366#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001367
1368
1369/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001370PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001371
1372
1373/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001374int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001375
1376
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001377/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001378
Thomas Wouters334fb892000-07-25 12:56:38 +00001379void *
Fred Drake100814d2000-07-09 15:48:49 +00001380PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001381{
1382#if _PyMem_EXTRA > 0
1383 if (nbytes == 0)
1384 nbytes = _PyMem_EXTRA;
1385#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001386 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001387}
1388
Thomas Wouters334fb892000-07-25 12:56:38 +00001389void *
1390PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001391{
1392#if _PyMem_EXTRA > 0
1393 if (nbytes == 0)
1394 nbytes = _PyMem_EXTRA;
1395#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001396 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001397}
1398
1399void
Thomas Wouters334fb892000-07-25 12:56:38 +00001400PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001401{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001402 PyMem_FREE(p);
1403}
1404
1405
1406/* Python's object malloc wrappers (see objimpl.h) */
1407
Thomas Wouters334fb892000-07-25 12:56:38 +00001408void *
Fred Drake100814d2000-07-09 15:48:49 +00001409PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001410{
1411 return PyObject_MALLOC(nbytes);
1412}
1413
Thomas Wouters334fb892000-07-25 12:56:38 +00001414void *
1415PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001416{
1417 return PyObject_REALLOC(p, nbytes);
1418}
1419
1420void
Thomas Wouters334fb892000-07-25 12:56:38 +00001421PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001422{
1423 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001424}
Guido van Rossum86610361998-04-10 22:32:46 +00001425
1426
1427/* These methods are used to control infinite recursion in repr, str, print,
1428 etc. Container objects that may recursively contain themselves,
1429 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1430 Py_ReprLeave() to avoid infinite recursion.
1431
1432 Py_ReprEnter() returns 0 the first time it is called for a particular
1433 object and 1 every time thereafter. It returns -1 if an exception
1434 occurred. Py_ReprLeave() has no return value.
1435
1436 See dictobject.c and listobject.c for examples of use.
1437*/
1438
1439#define KEY "Py_Repr"
1440
1441int
Fred Drake100814d2000-07-09 15:48:49 +00001442Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001443{
1444 PyObject *dict;
1445 PyObject *list;
1446 int i;
1447
1448 dict = PyThreadState_GetDict();
1449 if (dict == NULL)
1450 return -1;
1451 list = PyDict_GetItemString(dict, KEY);
1452 if (list == NULL) {
1453 list = PyList_New(0);
1454 if (list == NULL)
1455 return -1;
1456 if (PyDict_SetItemString(dict, KEY, list) < 0)
1457 return -1;
1458 Py_DECREF(list);
1459 }
1460 i = PyList_GET_SIZE(list);
1461 while (--i >= 0) {
1462 if (PyList_GET_ITEM(list, i) == obj)
1463 return 1;
1464 }
1465 PyList_Append(list, obj);
1466 return 0;
1467}
1468
1469void
Fred Drake100814d2000-07-09 15:48:49 +00001470Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001471{
1472 PyObject *dict;
1473 PyObject *list;
1474 int i;
1475
1476 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001477 if (dict == NULL)
1478 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001479 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001480 if (list == NULL || !PyList_Check(list))
1481 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001482 i = PyList_GET_SIZE(list);
1483 /* Count backwards because we always expect obj to be list[-1] */
1484 while (--i >= 0) {
1485 if (PyList_GET_ITEM(list, i) == obj) {
1486 PyList_SetSlice(list, i, i + 1, NULL);
1487 break;
1488 }
1489 }
1490}
Guido van Rossumd724b232000-03-13 16:01:29 +00001491
1492/*
1493 trashcan
1494 CT 2k0130
1495 non-recursively destroy nested objects
1496
1497 CT 2k0223
1498 everything is now done in a macro.
1499
1500 CT 2k0305
1501 modified to use functions, after Tim Peter's suggestion.
1502
1503 CT 2k0309
1504 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001505
1506 CT 2k0325
1507 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001508
1509 CT 2k0422
1510 complete rewrite. We now build a chain via ob_type
1511 and save the limited number of types in ob_refcnt.
1512 This is perfect since we don't need any memory.
1513 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001514*/
1515
Guido van Rossume92e6102000-04-24 15:40:53 +00001516#define Py_TRASHCAN_TUPLE 1
1517#define Py_TRASHCAN_LIST 2
1518#define Py_TRASHCAN_DICT 3
1519#define Py_TRASHCAN_FRAME 4
1520#define Py_TRASHCAN_TRACEBACK 5
1521/* extend here if other objects want protection */
1522
Guido van Rossumd724b232000-03-13 16:01:29 +00001523int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001524
Guido van Rossumd724b232000-03-13 16:01:29 +00001525PyObject * _PyTrash_delete_later = NULL;
1526
1527void
Fred Drake100814d2000-07-09 15:48:49 +00001528_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001529{
Guido van Rossume92e6102000-04-24 15:40:53 +00001530 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001531
Guido van Rossume92e6102000-04-24 15:40:53 +00001532 if (PyTuple_Check(op))
1533 typecode = Py_TRASHCAN_TUPLE;
1534 else if (PyList_Check(op))
1535 typecode = Py_TRASHCAN_LIST;
1536 else if (PyDict_Check(op))
1537 typecode = Py_TRASHCAN_DICT;
1538 else if (PyFrame_Check(op))
1539 typecode = Py_TRASHCAN_FRAME;
1540 else if (PyTraceBack_Check(op))
1541 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001542 else /* We have a bug here -- those are the only types in GC */ {
1543 Py_FatalError("Type not supported in GC -- internal bug");
1544 return; /* pacify compiler -- execution never here */
1545 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001546 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001547
Guido van Rossume92e6102000-04-24 15:40:53 +00001548 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1549 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001550}
1551
1552void
Fred Drake100814d2000-07-09 15:48:49 +00001553_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001554{
1555 while (_PyTrash_delete_later) {
1556 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001557 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1558
1559 switch (shredder->ob_refcnt) {
1560 case Py_TRASHCAN_TUPLE:
1561 shredder->ob_type = &PyTuple_Type;
1562 break;
1563 case Py_TRASHCAN_LIST:
1564 shredder->ob_type = &PyList_Type;
1565 break;
1566 case Py_TRASHCAN_DICT:
1567 shredder->ob_type = &PyDict_Type;
1568 break;
1569 case Py_TRASHCAN_FRAME:
1570 shredder->ob_type = &PyFrame_Type;
1571 break;
1572 case Py_TRASHCAN_TRACEBACK:
1573 shredder->ob_type = &PyTraceBack_Type;
1574 break;
1575 }
1576 _Py_NewReference(shredder);
1577
Guido van Rossumd724b232000-03-13 16:01:29 +00001578 ++_PyTrash_delete_nesting;
1579 Py_DECREF(shredder);
1580 --_PyTrash_delete_nesting;
1581 }
1582}