object.c 40 KB
Newer Older
1

Guido van Rossum's avatar
Guido van Rossum committed
2
/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum's avatar
Guido van Rossum committed
3

4
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
5

6 7 8 9
#ifdef macintosh
#include "macglue.h"
#endif

10 11 12 13 14
/* just for trashcan: */
#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

15
#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
16
DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum's avatar
Guido van Rossum committed
17
#endif
Guido van Rossum's avatar
Guido van Rossum committed
18

Guido van Rossum's avatar
Guido van Rossum committed
19 20 21
/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
   These are used by the individual routines for object creation.
   Do not call them otherwise, they do not initialize the object! */
Guido van Rossum's avatar
Guido van Rossum committed
22

23
#ifdef COUNT_ALLOCS
24
static PyTypeObject *type_list;
25 26
extern int tuple_zero_allocs, fast_tuple_allocs;
extern int quick_int_allocs, quick_neg_int_allocs;
27
extern int null_strings, one_strings;
28
void
29
dump_counts(void)
30
{
31
	PyTypeObject *tp;
32 33

	for (tp = type_list; tp; tp = tp->tp_next)
34
		fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
35
			tp->tp_name, tp->tp_allocs, tp->tp_frees,
36 37 38 39 40 41 42
			tp->tp_maxalloc);
	fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
		fast_tuple_allocs, tuple_zero_allocs);
	fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
		quick_int_allocs, quick_neg_int_allocs);
	fprintf(stderr, "null strings: %d, 1-strings: %d\n",
		null_strings, one_strings);
43 44
}

45
PyObject *
46
get_counts(void)
47 48 49 50 51 52 53 54 55
{
	PyTypeObject *tp;
	PyObject *result;
	PyObject *v;

	result = PyList_New(0);
	if (result == NULL)
		return NULL;
	for (tp = type_list; tp; tp = tp->tp_next) {
56 57
		v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
				  tp->tp_frees, tp->tp_maxalloc);
58 59 60 61 62 63 64 65 66 67 68 69 70 71
		if (v == NULL) {
			Py_DECREF(result);
			return NULL;
		}
		if (PyList_Append(result, v) < 0) {
			Py_DECREF(v);
			Py_DECREF(result);
			return NULL;
		}
		Py_DECREF(v);
	}
	return result;
}

72
void
73
inc_count(PyTypeObject *tp)
74
{
75
	if (tp->tp_allocs == 0) {
76
		/* first time; insert in linked list */
77
		if (tp->tp_next != NULL) /* sanity check */
78
			Py_FatalError("XXX inc_count sanity check");
79 80 81
		tp->tp_next = type_list;
		type_list = tp;
	}
82 83 84
	tp->tp_allocs++;
	if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
		tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
85 86 87
}
#endif

88
PyObject *
89
PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum's avatar
Guido van Rossum committed
90
{
91 92 93 94 95
	if (op == NULL) {
		PyErr_SetString(PyExc_SystemError,
				"NULL object passed to PyObject_Init");
		return op;
  	}
96 97
	if (PyType_IS_GC(tp))
		op = (PyObject *) PyObject_FROM_GC(op);
98
	/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum's avatar
Guido van Rossum committed
99
	op->ob_type = tp;
100
	_Py_NewReference(op);
Guido van Rossum's avatar
Guido van Rossum committed
101 102 103
	return op;
}

104
PyVarObject *
105
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
106 107 108 109 110 111
{
	if (op == NULL) {
		PyErr_SetString(PyExc_SystemError,
				"NULL object passed to PyObject_InitVar");
		return op;
	}
112 113
	if (PyType_IS_GC(tp))
		op = (PyVarObject *) PyObject_FROM_GC(op);
114 115 116 117 118 119 120 121
	/* Any changes should be reflected in PyObject_INIT_VAR */
	op->ob_size = size;
	op->ob_type = tp;
	_Py_NewReference((PyObject *)op);
	return op;
}

PyObject *
122
_PyObject_New(PyTypeObject *tp)
123 124 125 126 127
{
	PyObject *op;
	op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
	if (op == NULL)
		return PyErr_NoMemory();
128 129
	if (PyType_IS_GC(tp))
		op = (PyObject *) PyObject_FROM_GC(op);
130 131 132
	return PyObject_INIT(op, tp);
}

133
PyVarObject *
134
_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum's avatar
Guido van Rossum committed
135
{
136 137
	PyVarObject *op;
	op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum's avatar
Guido van Rossum committed
138
	if (op == NULL)
139
		return (PyVarObject *)PyErr_NoMemory();
140 141
	if (PyType_IS_GC(tp))
		op = (PyVarObject *) PyObject_FROM_GC(op);
142 143 144 145
	return PyObject_INIT_VAR(op, tp, size);
}

void
146
_PyObject_Del(PyObject *op)
147
{
148 149
	if (op && PyType_IS_GC(op->ob_type)) {
		op = (PyObject *) PyObject_AS_GC(op);
150
	}
151
	PyObject_FREE(op);
Guido van Rossum's avatar
Guido van Rossum committed
152 153
}

154 155 156 157 158 159
#ifndef WITH_CYCLE_GC
/* extension modules might need these */
void _PyGC_Insert(PyObject *op) { }
void _PyGC_Remove(PyObject *op) { }
#endif

160
int
161
PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum's avatar
Guido van Rossum committed
162
{
163
	int ret = 0;
164
	if (PyErr_CheckSignals())
165
		return -1;
166 167
#ifdef USE_STACKCHECK
	if (PyOS_CheckStack()) {
Fred Drake's avatar
Fred Drake committed
168
		PyErr_SetString(PyExc_MemoryError, "stack overflow");
169 170 171
		return -1;
	}
#endif
172
	clearerr(fp); /* Clear any previous error condition */
173 174
	if (op == NULL) {
		fprintf(fp, "<nil>");
Guido van Rossum's avatar
Guido van Rossum committed
175
	}
176 177
	else {
		if (op->ob_refcnt <= 0)
178 179
			fprintf(fp, "<refcnt %u at %p>",
				op->ob_refcnt, op);
180
		else if (op->ob_type->tp_print == NULL) {
181 182 183 184 185 186 187
			PyObject *s;
			if (flags & Py_PRINT_RAW)
				s = PyObject_Str(op);
			else
				s = PyObject_Repr(op);
			if (s == NULL)
				ret = -1;
188
			else {
189
				ret = PyObject_Print(s, fp, Py_PRINT_RAW);
190
			}
191
			Py_XDECREF(s);
192
		}
193
		else
194
			ret = (*op->ob_type->tp_print)(op, fp, flags);
195
	}
196 197
	if (ret == 0) {
		if (ferror(fp)) {
198
			PyErr_SetFromErrno(PyExc_IOError);
199 200 201 202 203
			clearerr(fp);
			ret = -1;
		}
	}
	return ret;
Guido van Rossum's avatar
Guido van Rossum committed
204 205
}

206
/* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */
207
void _PyObject_Dump(PyObject* op) 
208
{
209 210 211 212 213 214 215
	if (op == NULL)
		fprintf(stderr, "NULL\n");
	else {
		(void)PyObject_Print(op, stderr, 0);
		fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
		fprintf(stderr, "address    : %p\n", op);
	}
216
}
217 218

#ifdef WITH_CYCLE_GC
219
void _PyGC_Dump(PyGC_Head* op)
220
{
221
	_PyObject_Dump(PyObject_FROM_GC(op));
222
}
223
#endif /* WITH_CYCLE_GC */
224

225
PyObject *
226
PyObject_Repr(PyObject *v)
Guido van Rossum's avatar
Guido van Rossum committed
227
{
228
	if (PyErr_CheckSignals())
229
		return NULL;
230 231
#ifdef USE_STACKCHECK
	if (PyOS_CheckStack()) {
Fred Drake's avatar
Fred Drake committed
232
		PyErr_SetString(PyExc_MemoryError, "stack overflow");
233 234 235
		return NULL;
	}
#endif
236
	if (v == NULL)
237
		return PyString_FromString("<NULL>");
238 239
	else if (v->ob_type->tp_repr == NULL) {
		char buf[120];
240 241
		sprintf(buf, "<%.80s object at %p>",
			v->ob_type->tp_name, v);
242
		return PyString_FromString(buf);
Guido van Rossum's avatar
Guido van Rossum committed
243
	}
244 245 246 247 248
	else {
		PyObject *res;
		res = (*v->ob_type->tp_repr)(v);
		if (res == NULL)
			return NULL;
249
#ifdef Py_USING_UNICODE
250 251
		if (PyUnicode_Check(res)) {
			PyObject* str;
252
			str = PyUnicode_AsUnicodeEscapeString(res);
253 254
			Py_DECREF(res);
			if (str)
255
				res = str;
256 257
			else
				return NULL;
258
		}
259
#endif
260 261
		if (!PyString_Check(res)) {
			PyErr_Format(PyExc_TypeError,
262
				     "__repr__ returned non-string (type %.200s)",
263 264 265 266 267 268
				     res->ob_type->tp_name);
			Py_DECREF(res);
			return NULL;
		}
		return res;
	}
Guido van Rossum's avatar
Guido van Rossum committed
269 270
}

271
PyObject *
272
PyObject_Str(PyObject *v)
273
{
274 275
	PyObject *res;
	
276
	if (v == NULL)
277
		return PyString_FromString("<NULL>");
278
	if (PyString_Check(v)) {
279
		Py_INCREF(v);
280 281
		return v;
	}
282 283 284 285
	if (v->ob_type->tp_str == NULL)
		return PyObject_Repr(v);

	res = (*v->ob_type->tp_str)(v);
286 287
	if (res == NULL)
		return NULL;
288
#ifdef Py_USING_UNICODE
289 290 291 292 293 294 295 296 297
	if (PyUnicode_Check(res)) {
		PyObject* str;
		str = PyUnicode_AsEncodedString(res, NULL, NULL);
		Py_DECREF(res);
		if (str)
			res = str;
		else
		    	return NULL;
	}
298
#endif
299 300
	if (!PyString_Check(res)) {
		PyErr_Format(PyExc_TypeError,
301
			     "__str__ returned non-string (type %.200s)",
302 303 304 305 306
			     res->ob_type->tp_name);
		Py_DECREF(res);
		return NULL;
	}
	return res;
307 308
}

309
#ifdef Py_USING_UNICODE
310 311 312 313 314 315 316 317 318 319 320
PyObject *
PyObject_Unicode(PyObject *v)
{
	PyObject *res;
	
	if (v == NULL)
		res = PyString_FromString("<NULL>");
	else if (PyUnicode_Check(v)) {
		Py_INCREF(v);
		return v;
	}
321 322
	else if (PyString_Check(v)) {
		Py_INCREF(v);
323
	    	res = v;
324
    	}
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	else if (v->ob_type->tp_str != NULL)
		res = (*v->ob_type->tp_str)(v);
	else {
		PyObject *func;
		static PyObject *strstr;
		if (strstr == NULL) {
			strstr= PyString_InternFromString("__str__");
			if (strstr == NULL)
				return NULL;
		}
		if (!PyInstance_Check(v) ||
		    (func = PyObject_GetAttr(v, strstr)) == NULL) {
			PyErr_Clear();
			res = PyObject_Repr(v);
		}
		else {
		    	res = PyEval_CallObject(func, (PyObject *)NULL);
			Py_DECREF(func);
		}
	}
	if (res == NULL)
		return NULL;
	if (!PyUnicode_Check(res)) {
		PyObject* str;
		str = PyUnicode_FromObject(res);
		Py_DECREF(res);
		if (str)
			res = str;
		else
		    	return NULL;
	}
	return res;
}
358
#endif
359 360


361 362 363 364
/* Macro to get the tp_richcompare field of a type if defined */
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
                         ? (t)->tp_richcompare : NULL)

365 366
/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
367

368 369 370 371 372 373 374 375 376 377 378 379 380
/* Try a genuine rich comparison, returning an object.  Return:
   NULL for exception;
   NotImplemented if this particular rich comparison is not implemented or
     undefined;
   some object not equal to NotImplemented if it is implemented
     (this latter object may not be a Boolean).
*/
static PyObject *
try_rich_compare(PyObject *v, PyObject *w, int op)
{
	richcmpfunc f;
	PyObject *res;

381
	if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
382 383 384 385 386
		res = (*f)(v, w, op);
		if (res != Py_NotImplemented)
			return res;
		Py_DECREF(res);
	}
387
	if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
388 389 390 391 392 393 394 395 396 397 398 399 400 401
		return (*f)(w, v, swapped_op[op]);
	}
	res = Py_NotImplemented;
	Py_INCREF(res);
	return res;
}

/* Try a genuine rich comparison, returning an int.  Return:
   -1 for exception (including the case where try_rich_compare() returns an
      object that's not a Boolean);
   0 if the outcome is false;
   1 if the outcome is true;
   2 if this particular rich comparison is not implemented or undefined.
*/
402
static int
403
try_rich_compare_bool(PyObject *v, PyObject *w, int op)
404
{
405 406 407
	PyObject *res;
	int ok;

408
	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
409 410 411
		return 2; /* Shortcut, avoid INCREF+DECREF */
	res = try_rich_compare(v, w, op);
	if (res == NULL)
412
		return -1;
413 414 415
	if (res == Py_NotImplemented) {
		Py_DECREF(res);
		return 2;
416
	}
417 418 419
	ok = PyObject_IsTrue(res);
	Py_DECREF(res);
	return ok;
420 421
}

422 423 424 425 426 427 428
/* Try rich comparisons to determine a 3-way comparison.  Return:
   -2 for an exception;
   -1 if v < w;
   0 if v == w;
   1 if v > w;
   2 if this particular rich comparison is not implemented or undefined.
*/
429
static int
430 431
try_rich_to_3way_compare(PyObject *v, PyObject *w)
{
432 433 434 435 436 437 438 439
	static struct { int op; int outcome; } tries[3] = {
		/* Try this operator, and if it is true, use this outcome: */
		{Py_EQ, 0},
		{Py_LT, -1},
		{Py_GT, 1},
	};
	int i;

440
	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
441
		return 2; /* Shortcut */
442 443 444 445

	for (i = 0; i < 3; i++) {
		switch (try_rich_compare_bool(v, w, tries[i].op)) {
		case -1:
446
			return -2;
447 448 449
		case 1:
			return tries[i].outcome;
		}
450
	}
451 452

	return 2;
453 454 455 456 457 458 459 460 461 462 463
}

/* Try a 3-way comparison, returning an int.  Return:
   -2 for an exception;
   -1 if v < w;
   0 if v == w;
   1 if v > w;
   2 if this particular 3-way comparison is not implemented or undefined.
*/
static int
try_3way_compare(PyObject *v, PyObject *w)
464
{
465
	int c;
466
	cmpfunc f;
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	/* Comparisons involving instances are given to instance_compare,
	   which has the same return conventions as this function. */

	if (PyInstance_Check(v))
		return (*v->ob_type->tp_compare)(v, w);
	if (PyInstance_Check(w))
		return (*w->ob_type->tp_compare)(v, w);

	/* Try coercion; if it fails, give up */
	c = PyNumber_CoerceEx(&v, &w);
	if (c < 0)
		return -2;
	if (c > 0)
		return 2;

	/* Try v's comparison, if defined */
	if ((f = v->ob_type->tp_compare) != NULL) {
		c = (*f)(v, w);
		Py_DECREF(v);
		Py_DECREF(w);
488
		if (c < 0 && PyErr_Occurred())
489 490
			return -2;
		return c < 0 ? -1 : c > 0 ? 1 : 0;
491
	}
492 493 494 495 496 497

	/* Try w's comparison, if defined */
	if ((f = w->ob_type->tp_compare) != NULL) {
		c = (*f)(w, v); /* swapped! */
		Py_DECREF(v);
		Py_DECREF(w);
498
		if (c < 0 && PyErr_Occurred())
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
			return -2;
		return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
	}

	/* No comparison defined */
	Py_DECREF(v);
	Py_DECREF(w);
	return 2;
}

/* Final fallback 3-way comparison, returning an int.  Return:
   -2 if an error occurred;
   -1 if v < w;
   0 if v == w;
   1 if v > w.
*/
static int
default_3way_compare(PyObject *v, PyObject *w)
{
	int c;
519
	char *vname, *wname;
520 521

	if (v->ob_type == w->ob_type) {
Barry Warsaw's avatar
Barry Warsaw committed
522 523 524 525 526
		/* When comparing these pointers, they must be cast to
		 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
		 * uintptr_t).  ANSI specifies that pointer compares other
		 * than == and != to non-related structures are undefined.
		 */
527 528
		Py_uintptr_t vv = (Py_uintptr_t)v;
		Py_uintptr_t ww = (Py_uintptr_t)w;
529 530 531
		return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
	}

532
#ifdef Py_USING_UNICODE
533 534 535 536
	/* Special case for Unicode */
	if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
		c = PyUnicode_Compare(v, w);
		if (!PyErr_Occurred())
537
			return c;
538 539 540 541 542 543 544 545
		/* TypeErrors are ignored: if Unicode coercion fails due
		   to one of the arguments not having the right type, we
		   continue as defined by the coercion protocol (see
		   above).  Luckily, decoding errors are reported as
		   ValueErrors and are not masked by this technique. */
		if (!PyErr_ExceptionMatches(PyExc_TypeError))
			return -2;
		PyErr_Clear();
546
	}
547
#endif
548

549 550 551 552 553 554
	/* None is smaller than anything */
	if (v == Py_None)
		return -1;
	if (w == Py_None)
		return 1;

555
	/* different type: compare type names */
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	if (v->ob_type->tp_as_number)
		vname = "";
	else
		vname = v->ob_type->tp_name;
	if (w->ob_type->tp_as_number)
		wname = "";
	else
		wname = w->ob_type->tp_name;
	c = strcmp(vname, wname);
	if (c < 0)
		return -1;
	if (c > 0)
		return 1;
	/* Same type name, or (more likely) incomparable numeric types */
	return ((Py_uintptr_t)(v->ob_type) < (
		Py_uintptr_t)(w->ob_type)) ? -1 : 1;
572 573 574 575
}

#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)

576 577 578 579 580
/* Do a 3-way comparison, by hook or by crook.  Return:
   -2 for an exception;
   -1 if v < w;
    0 if v == w;
    1 if v > w;
581 582
   If the object implements a tp_compare function, it returns
   whatever this function returns (whether with an exception or not).
583
*/
584 585 586 587
static int
do_cmp(PyObject *v, PyObject *w)
{
	int c;
588
	cmpfunc f;
589

590
	if (v->ob_type == w->ob_type
591 592 593 594 595
	    && (f = v->ob_type->tp_compare) != NULL) {
		c = (*f)(v, w);
		if (c != 2 || !PyInstance_Check(v))
			return c;
	}
596 597 598 599 600 601 602
	c = try_rich_to_3way_compare(v, w);
	if (c < 2)
		return c;
	c = try_3way_compare(v, w);
	if (c < 2)
		return c;
	return default_3way_compare(v, w);
603 604
}

605
/* compare_nesting is incremented before calling compare (for
606 607
   some types) and decremented on exit.  If the count exceeds the
   nesting limit, enable code to detect circular data structures.
608 609 610 611 612

   This is a tunable parameter that should only affect the performance
   of comparisons, nothing else.  Setting it high makes comparing deeply
   nested non-cyclical data structures faster, but makes comparing cyclical
   data structures slower.
613
*/
614 615
#define NESTING_LIMIT 20

616
static int compare_nesting = 0;
617 618

static PyObject*
619
get_inprogress_dict(void)
620
{
621
	static PyObject *key;
622 623
	PyObject *tstate_dict, *inprogress;

624 625 626 627 628 629
	if (key == NULL) {
		key = PyString_InternFromString("cmp_state");
		if (key == NULL)
			return NULL;
	}

630 631 632 633 634
	tstate_dict = PyThreadState_GetDict();
	if (tstate_dict == NULL) {
		PyErr_BadInternalCall();
		return NULL;
	} 
635

636
	inprogress = PyDict_GetItem(tstate_dict, key); 
637 638 639 640
	if (inprogress == NULL) {
		inprogress = PyDict_New();
		if (inprogress == NULL)
			return NULL;
641
		if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
642 643 644
		    Py_DECREF(inprogress);
		    return NULL;
		}
645
		Py_DECREF(inprogress);
646 647
	}

648
	return inprogress;
649 650
}

651
static PyObject *
652
check_recursion(PyObject *v, PyObject *w, int op)
653
{
654 655
	PyObject *inprogress;
	PyObject *token;
656 657
	Py_uintptr_t iv = (Py_uintptr_t)v;
	Py_uintptr_t iw = (Py_uintptr_t)w;
658
	PyObject *x, *y, *z;
659

660 661
	inprogress = get_inprogress_dict();
	if (inprogress == NULL)
662
		return NULL;
663 664 665 666 667

	token = PyTuple_New(3);
	if (token == NULL)
		return NULL;

668
	if (iv <= iw) {
669 670 671 672
		PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
		PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
		if (op >= 0)
			op = swapped_op[op];
673
	} else {
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
		PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
	}
	PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
	if (x == NULL || y == NULL || z == NULL) {
		Py_DECREF(token);
		return NULL;
	}

	if (PyDict_GetItem(inprogress, token) != NULL) {
		Py_DECREF(token);
		return Py_None; /* Without INCREF! */
	}

	if (PyDict_SetItem(inprogress, token, token) < 0) {
		Py_DECREF(token);
		return NULL;
691
	}
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

	return token;
}

static void
delete_token(PyObject *token)
{
	PyObject *inprogress;

	if (token == NULL || token == Py_None)
		return;
	inprogress = get_inprogress_dict();
	if (inprogress == NULL)
		PyErr_Clear();
	else
		PyDict_DelItem(inprogress, token);
	Py_DECREF(token);
709 710
}

Guido van Rossum's avatar
Guido van Rossum committed
711
int
712
PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum's avatar
Guido van Rossum committed
713
{
714
	PyTypeObject *vtp;
715 716
	int result;

717
#if defined(USE_STACKCHECK)
718
	if (PyOS_CheckStack()) {
719 720 721 722
		PyErr_SetString(PyExc_MemoryError, "Stack overflow");
        return -1;
	}
#endif
723 724 725 726
	if (v == NULL || w == NULL) {
		PyErr_BadInternalCall();
		return -1;
	}
Guido van Rossum's avatar
Guido van Rossum committed
727 728
	if (v == w)
		return 0;
729
	vtp = v->ob_type;
730 731
	compare_nesting++;
	if (compare_nesting > NESTING_LIMIT &&
732
		(vtp->tp_as_mapping
733 734 735
		 || (vtp->tp_as_sequence
		     && !PyString_Check(v)
		     && !PyTuple_Check(v)))) {
736
		/* try to detect circular data structures */
737
		PyObject *token = check_recursion(v, w, -1);
738

739 740
		if (token == NULL) {
			result = -1;
741
		}
742
		else if (token == Py_None) {
743 744
			/* already comparing these objects.  assume
			   they're equal until shown otherwise */
745
                        result = 0;
746
		}
747 748 749
		else {
			result = do_cmp(v, w);
			delete_token(token);
750 751
		}
	}
752 753 754
	else {
		result = do_cmp(v, w);
	}
755
	compare_nesting--;
756 757 758 759
	return result < 0 ? -1 : result;
}

static PyObject *
760
convert_3way_to_object(int op, int c)
761 762 763 764 765 766 767 768 769 770 771 772
{
	PyObject *result;
	switch (op) {
	case Py_LT: c = c <  0; break;
	case Py_LE: c = c <= 0; break;
	case Py_EQ: c = c == 0; break;
	case Py_NE: c = c != 0; break;
	case Py_GT: c = c >  0; break;
	case Py_GE: c = c >= 0; break;
	}
	result = c ? Py_True : Py_False;
	Py_INCREF(result);
773
	return result;
Guido van Rossum's avatar
Guido van Rossum committed
774
}
775 776 777 778 779 780 781 782 783 784 785 786 787 788
	

static PyObject *
try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
{
	int c;

	c = try_3way_compare(v, w);
	if (c >= 2)
		c = default_3way_compare(v, w);
	if (c <= -2)
		return NULL;
	return convert_3way_to_object(op, c);
}
Guido van Rossum's avatar
Guido van Rossum committed
789

790
static PyObject *
791 792 793
do_richcmp(PyObject *v, PyObject *w, int op)
{
	PyObject *res;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	cmpfunc f;

	/* If the types are equal, don't bother with coercions etc. 
	   Instances are special-cased in try_3way_compare, since
	   a result of 2 does *not* mean one value being greater
	   than the other. */
	if (v->ob_type == w->ob_type
	    && (f = v->ob_type->tp_compare) != NULL
	    && !PyInstance_Check(v)) {
		int c;
		richcmpfunc f1;
		if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
			/* If the type has richcmp, try it first.
			   try_rich_compare would try it two-sided,
			   which is not needed since we've a single
			   type only. */
			res = (*f1)(v, w, op);
			if (res != Py_NotImplemented)
				return res;
			Py_DECREF(res);
		}
		c = (*f)(v, w);
		if (c < 0 && PyErr_Occurred())
			return NULL;
		return convert_3way_to_object(op, c);
	}
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

	res = try_rich_compare(v, w, op);
	if (res != Py_NotImplemented)
		return res;
	Py_DECREF(res);

	return try_3way_to_rich_compare(v, w, op);
}

PyObject *
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
{
	PyObject *res;

	assert(Py_LT <= op && op <= Py_GE);

836 837 838
	compare_nesting++;
	if (compare_nesting > NESTING_LIMIT &&
		(v->ob_type->tp_as_mapping
839 840 841
		 || (v->ob_type->tp_as_sequence
		     && !PyString_Check(v)
		     && !PyTuple_Check(v)))) {
842
		/* try to detect circular data structures */
843
		PyObject *token = check_recursion(v, w, op);
844

845 846
		if (token == NULL) {
			res = NULL;
847
		}
848 849 850 851
		else if (token == Py_None) {
			/* already comparing these objects with this operator.
			   assume they're equal until shown otherwise */
			if (op == Py_EQ)
852
				res = Py_True;
853
			else if (op == Py_NE)
854
				res = Py_False;
855 856 857 858 859 860
			else {
				PyErr_SetString(PyExc_ValueError,
					"can't order recursive values");
				res = NULL;
			}
			Py_XINCREF(res);
861
		}
862 863 864
		else {
			res = do_richcmp(v, w, op);
			delete_token(token);
865
		}
866
	}
867 868 869 870
	else {
		res = do_richcmp(v, w, op);
	}
	compare_nesting--;
871 872 873
	return res;
}

874
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
875 876 877 878 879 880 881 882 883 884 885 886
int
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
{
	PyObject *res = PyObject_RichCompare(v, w, op);
	int ok;

	if (res == NULL)
		return -1;
	ok = PyObject_IsTrue(res);
	Py_DECREF(res);
	return ok;
}
887 888 889 890 891 892 893 894

/* Set of hash utility functions to help maintaining the invariant that
	iff a==b then hash(a)==hash(b)

   All the utility functions (_Py_Hash*()) return "-1" to signify an error.
*/

long
895
_Py_HashDouble(double v)
896
{
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	double intpart, fractpart;
	int expo;
	long hipart;
	long x;		/* the final hash value */
	/* This is designed so that Python numbers of different types
	 * that compare equal hash to the same value; otherwise comparisons
	 * of mapping keys will turn out weird.
	 */

#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
{
	extended e;
	fractpart = modf(v, &e);
	intpart = e;
}
#else
	fractpart = modf(v, &intpart);
#endif
	if (fractpart == 0.0) {
		/* This must return the same hash as an equal int or long. */
		if (intpart > LONG_MAX || -intpart > LONG_MAX) {
			/* Convert to long and use its hash. */
			PyObject *plong;	/* converted to Python long */
			if (Py_IS_INFINITY(intpart))
				/* can't convert to long int -- arbitrary */
				v = v < 0 ? -271828.0 : 314159.0;
			plong = PyLong_FromDouble(v);
			if (plong == NULL)
				return -1;
			x = PyObject_Hash(plong);
			Py_DECREF(plong);
			return x;
		}
		/* Fits in a C long == a Python int, so is its own hash. */
		x = (long)intpart;
		if (x == -1)
			x = -2;
		return x;
	}
	/* The fractional part is non-zero, so we don't have to worry about
	 * making this match the hash of some other type.
	 * Use frexp to get at the bits in the double.
939 940 941
	 * Since the VAX D double format has 56 mantissa bits, which is the
	 * most of any double format in use, each of these parts may have as
	 * many as (but no more than) 56 significant bits.
942 943 944 945 946
	 * So, assuming sizeof(long) >= 4, each part can be broken into two
	 * longs; frexp and multiplication are used to do that.
	 * Also, since the Cray double format has 15 exponent bits, which is
	 * the most of any double format in use, shifting the exponent field
	 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
947
	 */
948 949 950 951 952 953 954 955
	v = frexp(v, &expo);
	v *= 2147483648.0;	/* 2**31 */
	hipart = (long)v;	/* take the top 32 bits */
	v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
	x = hipart + (long)v + (expo << 15);
	if (x == -1)
		x = -2;
	return x;
956 957 958
}

long
959
_Py_HashPointer(void *p)
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
{
#if SIZEOF_LONG >= SIZEOF_VOID_P
	return (long)p;
#else
	/* convert to a Python long and hash that */
	PyObject* longobj;
	long x;
	
	if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
		x = -1;
		goto finally;
	}
	x = PyObject_Hash(longobj);
	
finally:
	Py_XDECREF(longobj);
	return x;
#endif
}


981
long
982
PyObject_Hash(PyObject *v)
983
{
984
	PyTypeObject *tp = v->ob_type;
985 986
	if (tp->tp_hash != NULL)
		return (*tp->tp_hash)(v);
987
	if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
988 989
		return _Py_HashPointer(v); /* Use address as hash value */
	}
990
	/* If there's a cmp but no hash defined, the object can't be hashed */
991
	PyErr_SetString(PyExc_TypeError, "unhashable type");
992 993 994
	return -1;
}

995
PyObject *
996
PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
997
{
998
	PyObject *w, *res;
999

1000
	if (v->ob_type->tp_getattr != NULL)
Guido van Rossum's avatar
Guido van Rossum committed
1001
		return (*v->ob_type->tp_getattr)(v, name);
1002 1003 1004 1005 1006 1007
	w = PyString_InternFromString(name);
	if (w == NULL)
		return NULL;
	res = PyObject_GetAttr(v, w);
	Py_XDECREF(w);
	return res;
Guido van Rossum's avatar
Guido van Rossum committed
1008 1009
}

1010
int
1011
PyObject_HasAttrString(PyObject *v, char *name)
1012
{
1013
	PyObject *res = PyObject_GetAttrString(v, name);
1014
	if (res != NULL) {
1015
		Py_DECREF(res);
1016 1017
		return 1;
	}
1018
	PyErr_Clear();
1019 1020 1021
	return 0;
}

Guido van Rossum's avatar
Guido van Rossum committed
1022
int
1023
PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum's avatar
Guido van Rossum committed
1024
{
1025 1026
	PyObject *s;
	int res;
1027

1028
	if (v->ob_type->tp_setattr != NULL)
Guido van Rossum's avatar
Guido van Rossum committed
1029
		return (*v->ob_type->tp_setattr)(v, name, w);
1030 1031 1032 1033 1034 1035
	s = PyString_InternFromString(name);
	if (s == NULL)
		return -1;
	res = PyObject_SetAttr(v, s, w);
	Py_XDECREF(s);
	return res;
1036 1037
}

1038
PyObject *
1039
PyObject_GetAttr(PyObject *v, PyObject *name)
1040
{
1041 1042
	PyTypeObject *tp = v->ob_type;

1043
#ifdef Py_USING_UNICODE
1044 1045 1046 1047 1048 1049 1050 1051
	/* The Unicode to string conversion is done here because the
	   existing tp_getattro slots expect a string object as name
	   and we wouldn't want to break those. */
	if (PyUnicode_Check(name)) {
		name = _PyUnicode_AsDefaultEncodedString(name, NULL);
		if (name == NULL)
			return NULL;
	}
1052 1053
#endif

1054 1055 1056 1057 1058
	if (!PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError,
				"attribute name must be string");
		return NULL;
	}
1059 1060 1061 1062 1063 1064 1065 1066
	if (tp->tp_getattro != NULL)
		return (*tp->tp_getattro)(v, name);
	if (tp->tp_getattr != NULL)
		return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object has no attribute '%.400s'",
		     tp->tp_name, PyString_AS_STRING(name));
	return NULL;
1067 1068 1069
}

int
1070
PyObject_HasAttr(PyObject *v, PyObject *name)
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
{
	PyObject *res = PyObject_GetAttr(v, name);
	if (res != NULL) {
		Py_DECREF(res);
		return 1;
	}
	PyErr_Clear();
	return 0;
}

int
1082
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
1083
{
1084
	PyTypeObject *tp = v->ob_type;
1085
	int err;
1086

1087
#ifdef Py_USING_UNICODE
1088 1089 1090 1091 1092 1093 1094
	/* The Unicode to string conversion is done here because the
	   existing tp_setattro slots expect a string object as name
	   and we wouldn't want to break those. */
	if (PyUnicode_Check(name)) {
		name = PyUnicode_AsEncodedString(name, NULL, NULL);
		if (name == NULL)
			return -1;
1095
	}
1096 1097 1098
	else 
#endif
	if (!PyString_Check(name)){
1099 1100
		PyErr_SetString(PyExc_TypeError,
				"attribute name must be string");
1101
		return -1;
1102
	}
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	else
		Py_INCREF(name);

	PyString_InternInPlace(&name);
	if (tp->tp_setattro != NULL) {
		err = (*tp->tp_setattro)(v, name, value);
		Py_DECREF(name);
		return err;
	}
	if (tp->tp_setattr != NULL) {
		err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
		Py_DECREF(name);
		return err;
1116
	}
1117
	Py_DECREF(name);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
		PyErr_Format(PyExc_TypeError,
			     "'%.100s' object has no attributes "
			     "(%s .%.100s)",
			     tp->tp_name,
			     value==NULL ? "del" : "assign to",
			     PyString_AS_STRING(name));
	else
		PyErr_Format(PyExc_TypeError,
			     "'%.100s' object has only read-only attributes "
			     "(%s .%.100s)",
			     tp->tp_name,
			     value==NULL ? "del" : "assign to",
			     PyString_AS_STRING(name));
	return -1;
}

/* Helper to get a pointer to an object's __dict__ slot, if any */

PyObject **
_PyObject_GetDictPtr(PyObject *obj)
{
#define PTRSIZE (sizeof(PyObject *))

	long dictoffset;
	PyTypeObject *tp = obj->ob_type;

	if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
		return NULL;
	dictoffset = tp->tp_dictoffset;
	if (dictoffset == 0)
		return NULL;
	if (dictoffset < 0) {
		dictoffset += PyType_BASICSIZE(tp);
		assert(dictoffset > 0); /* Sanity check */
		if (tp->tp_itemsize > 0) {
			int n = ((PyVarObject *)obj)->ob_size;
			if (n > 0) {
				dictoffset += tp->tp_itemsize * n;
				/* Round up, if necessary */
				if (tp->tp_itemsize % PTRSIZE != 0) {
					dictoffset += PTRSIZE - 1;
					dictoffset /= PTRSIZE;
					dictoffset *= PTRSIZE;
				}
			}
		}
	}
	return (PyObject **) ((char *)obj + dictoffset);
}

/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */

PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{
	PyTypeObject *tp = obj->ob_type;
	PyObject *descr;
	descrgetfunc f;
	PyObject **dictptr;

	if (tp->tp_dict == NULL) {
1180
		if (PyType_Ready(tp) < 0)
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
			return NULL;
	}

	descr = _PyType_Lookup(tp, name);
	f = NULL;
	if (descr != NULL) {
		f = descr->ob_type->tp_descr_get;
		if (f != NULL && PyDescr_IsData(descr))
			return f(descr, obj, (PyObject *)obj->ob_type);
	}

	dictptr = _PyObject_GetDictPtr(obj);
	if (dictptr != NULL) {
		PyObject *dict = *dictptr;
		if (dict != NULL) {
			PyObject *res = PyDict_GetItem(dict, name);
			if (res != NULL) {
				Py_INCREF(res);
				return res;
			}
		}
	}

	if (f != NULL)
		return f(descr, obj, (PyObject *)obj->ob_type);

	if (descr != NULL) {
		Py_INCREF(descr);
		return descr;
	}

	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object has no attribute '%.400s'",
		     tp->tp_name, PyString_AS_STRING(name));
	return NULL;
}

int
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
{
	PyTypeObject *tp = obj->ob_type;
	PyObject *descr;
	descrsetfunc f;
	PyObject **dictptr;

	if (tp->tp_dict == NULL) {
1227
		if (PyType_Ready(tp) < 0)
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
			return -1;
	}

	descr = _PyType_Lookup(tp, name);
	f = NULL;
	if (descr != NULL) {
		f = descr->ob_type->tp_descr_set;
		if (f != NULL && PyDescr_IsData(descr))
			return f(descr, obj, value);
	}

	dictptr = _PyObject_GetDictPtr(obj);
	if (dictptr != NULL) {
		PyObject *dict = *dictptr;
		if (dict == NULL && value != NULL) {
			dict = PyDict_New();
			if (dict == NULL)
				return -1;
			*dictptr = dict;
		}
		if (dict != NULL) {
			int res;
			if (value == NULL)
				res = PyDict_DelItem(dict, name);
			else
				res = PyDict_SetItem(dict, name, value);
			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
				PyErr_SetObject(PyExc_AttributeError, name);
			return res;
		}
	}

	if (f != NULL)
		return f(descr, obj, value);

	if (descr == NULL) {
		PyErr_Format(PyExc_AttributeError,
			     "'%.50s' object has no attribute '%.400s'",
			     tp->tp_name, PyString_AS_STRING(name));
		return -1;
	}

	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object attribute '%.400s' is read-only",
		     tp->tp_name, PyString_AS_STRING(name));
	return -1;
1274 1275
}

1276 1277 1278 1279
/* Test a value used as condition, e.g., in a for or if statement.
   Return -1 if an error occurred */

int
1280
PyObject_IsTrue(PyObject *v)
1281 1282
{
	int res;
1283
	if (v == Py_None)
1284
		res = 0;
1285 1286
	else if (v->ob_type->tp_as_number != NULL &&
		 v->ob_type->tp_as_number->nb_nonzero != NULL)
1287
		res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
1288 1289
	else if (v->ob_type->tp_as_mapping != NULL &&
		 v->ob_type->tp_as_mapping->mp_length != NULL)
1290
		res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1291 1292
	else if (v->ob_type->tp_as_sequence != NULL &&
		 v->ob_type->tp_as_sequence->sq_length != NULL)
1293 1294 1295 1296 1297 1298
		res = (*v->ob_type->tp_as_sequence->sq_length)(v);
	else
		res = 1;
	if (res > 0)
		res = 1;
	return res;
Guido van Rossum's avatar
Guido van Rossum committed
1299 1300
}

Guido van Rossum's avatar
Guido van Rossum committed
1301 1302 1303 1304
/* equivalent of 'not v' 
   Return -1 if an error occurred */

int
1305
PyObject_Not(PyObject *v)
Guido van Rossum's avatar
Guido van Rossum committed
1306 1307 1308 1309 1310 1311 1312 1313
{
	int res;
	res = PyObject_IsTrue(v);
	if (res < 0)
		return res;
	return res == 0;
}

1314 1315
/* Coerce two numeric types to the "larger" one.
   Increment the reference count on each argument.
1316 1317 1318 1319
   Return value:
   -1 if an error occurred;
   0 if the coercion succeeded (and then the reference counts are increased);
   1 if no coercion is possible (and no error is raised).
1320 1321
*/
int
1322
PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
1323
{
1324 1325
	register PyObject *v = *pv;
	register PyObject *w = *pw;
1326 1327
	int res;

1328 1329 1330
	if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
		Py_INCREF(v);
		Py_INCREF(w);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
		return 0;
	}
	if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
		res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
		if (res <= 0)
			return res;
	}
	if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
		res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
		if (res <= 0)
			return res;
	}
1343 1344 1345
	return 1;
}

1346 1347 1348 1349 1350
/* Coerce two numeric types to the "larger" one.
   Increment the reference count on each argument.
   Return -1 and raise an exception if no coercion is possible
   (and then no reference count is incremented).
*/
1351
int
1352
PyNumber_Coerce(PyObject **pv, PyObject **pw)
1353 1354 1355 1356
{
	int err = PyNumber_CoerceEx(pv, pw);
	if (err <= 0)
		return err;
1357
	PyErr_SetString(PyExc_TypeError, "number coercion failed");
1358 1359 1360
	return -1;
}

Guido van Rossum's avatar
Guido van Rossum committed
1361

Guido van Rossum's avatar
Guido van Rossum committed
1362 1363 1364
/* Test whether an object can be called */

int
1365
PyCallable_Check(PyObject *x)
Guido van Rossum's avatar
Guido van Rossum committed
1366 1367 1368
{
	if (x == NULL)
		return 0;
1369 1370
	if (PyInstance_Check(x)) {
		PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum's avatar
Guido van Rossum committed
1371
		if (call == NULL) {
1372
			PyErr_Clear();
Guido van Rossum's avatar
Guido van Rossum committed
1373 1374 1375 1376
			return 0;
		}
		/* Could test recursively but don't, for fear of endless
		   recursion if some joker sets self.__call__ = self */
1377
		Py_DECREF(call);
Guido van Rossum's avatar
Guido van Rossum committed
1378 1379
		return 1;
	}
1380 1381 1382
	else {
		return x->ob_type->tp_call != NULL;
	}
Guido van Rossum's avatar
Guido van Rossum committed
1383 1384 1385
}


Guido van Rossum's avatar
Guido van Rossum committed
1386 1387 1388
/*
NoObject is usable as a non-NULL undefined value, used by the macro None.
There is (and should be!) no way to create other objects of this type,
Guido van Rossum's avatar
Guido van Rossum committed
1389
so there is exactly one (which is indestructible, by the way).
1390
(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum's avatar
Guido van Rossum committed
1391 1392
*/

Guido van Rossum's avatar
Guido van Rossum committed
1393
/* ARGSUSED */
1394
static PyObject *
1395
none_repr(PyObject *op)
Guido van Rossum's avatar
Guido van Rossum committed
1396
{
1397
	return PyString_FromString("None");
Guido van Rossum's avatar
Guido van Rossum committed
1398 1399
}

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
/* ARGUSED */
static void
none_dealloc(PyObject* ignore) 
{
	/* This should never get called, but we also don't want to SEGV if
	 * we accidently decref None out of existance.
	 */
	abort();
}


1411
static PyTypeObject PyNone_Type = {
1412
	PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum's avatar
Guido van Rossum committed
1413
	0,
1414
	"NoneType",
Guido van Rossum's avatar
Guido van Rossum committed
1415 1416
	0,
	0,
1417
	(destructor)none_dealloc,	     /*tp_dealloc*/ /*never called*/
1418
	0,		/*tp_print*/
Guido van Rossum's avatar
Guido van Rossum committed
1419 1420 1421
	0,		/*tp_getattr*/
	0,		/*tp_setattr*/
	0,		/*tp_compare*/
1422
	(reprfunc)none_repr, /*tp_repr*/
Guido van Rossum's avatar
Guido van Rossum committed
1423 1424 1425
	0,		/*tp_as_number*/
	0,		/*tp_as_sequence*/
	0,		/*tp_as_mapping*/
1426
	0,		/*tp_hash */
Guido van Rossum's avatar
Guido van Rossum committed
1427 1428
};

1429
PyObject _Py_NoneStruct = {
1430
	PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum's avatar
Guido van Rossum committed
1431 1432
};

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
/* NotImplemented is an object that can be used to signal that an
   operation is not implemented for the given type combination. */

static PyObject *
NotImplemented_repr(PyObject *op)
{
	return PyString_FromString("NotImplemented");
}

static PyTypeObject PyNotImplemented_Type = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,
1445
	"NotImplementedType",
1446 1447
	0,
	0,
1448
	(destructor)none_dealloc,	     /*tp_dealloc*/ /*never called*/
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	0,		/*tp_print*/
	0,		/*tp_getattr*/
	0,		/*tp_setattr*/
	0,		/*tp_compare*/
	(reprfunc)NotImplemented_repr, /*tp_repr*/
	0,		/*tp_as_number*/
	0,		/*tp_as_sequence*/
	0,		/*tp_as_mapping*/
	0,		/*tp_hash */
};

PyObject _Py_NotImplementedStruct = {
	PyObject_HEAD_INIT(&PyNotImplemented_Type)
};

1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
void
_Py_ReadyTypes(void)
{
	if (PyType_Ready(&PyType_Type) < 0)
		Py_FatalError("Can't initialize 'type'");

	if (PyType_Ready(&PyList_Type) < 0)
		Py_FatalError("Can't initialize 'list'");

	if (PyType_Ready(&PyNone_Type) < 0)
		Py_FatalError("Can't initialize type(None)");

	if (PyType_Ready(&PyNotImplemented_Type) < 0)
		Py_FatalError("Can't initialize type(NotImplemented)");
}

Guido van Rossum's avatar
Guido van Rossum committed
1480

1481
#ifdef Py_TRACE_REFS
Guido van Rossum's avatar
Guido van Rossum committed
1482

1483
static PyObject refchain = {&refchain, &refchain};
Guido van Rossum's avatar
Guido van Rossum committed
1484

1485
void
1486
_Py_ResetReferences(void)
1487 1488 1489 1490 1491
{
	refchain._ob_prev = refchain._ob_next = &refchain;
	_Py_RefTotal = 0;
}

1492
void
1493
_Py_NewReference(PyObject *op)
Guido van Rossum's avatar
Guido van Rossum committed
1494
{
1495
	_Py_RefTotal++;
Guido van Rossum's avatar
Guido van Rossum committed
1496 1497 1498 1499 1500
	op->ob_refcnt = 1;
	op->_ob_next = refchain._ob_next;
	op->_ob_prev = &refchain;
	refchain._ob_next->_ob_prev = op;
	refchain._ob_next = op;
1501 1502 1503
#ifdef COUNT_ALLOCS
	inc_count(op->ob_type);
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1504 1505
}

1506
void
1507
_Py_ForgetReference(register PyObject *op)
Guido van Rossum's avatar
Guido van Rossum committed
1508
{
1509
#ifdef SLOW_UNREF_CHECK
1510
        register PyObject *p;
1511
#endif
1512
	if (op->ob_refcnt < 0)
1513
		Py_FatalError("UNREF negative refcnt");
1514
	if (op == &refchain ||
1515
	    op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1516
		Py_FatalError("UNREF invalid object");
1517
#ifdef SLOW_UNREF_CHECK
Guido van Rossum's avatar
Guido van Rossum committed
1518 1519 1520 1521
	for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
		if (p == op)
			break;
	}
1522
	if (p == &refchain) /* Not found */
1523
		Py_FatalError("UNREF unknown object");
1524
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1525 1526
	op->_ob_next->_ob_prev = op->_ob_prev;
	op->_ob_prev->_ob_next = op->_ob_next;
1527
	op->_ob_next = op->_ob_prev = NULL;
1528
#ifdef COUNT_ALLOCS
1529
	op->ob_type->tp_frees++;
1530
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1531 1532
}

1533
void
1534
_Py_Dealloc(PyObject *op)
Guido van Rossum's avatar
Guido van Rossum committed
1535
{
1536
	destructor dealloc = op->ob_type->tp_dealloc;
1537
	_Py_ForgetReference(op);
1538
	(*dealloc)(op);
Guido van Rossum's avatar
Guido van Rossum committed
1539 1540
}

1541
void
1542
_Py_PrintReferences(FILE *fp)
Guido van Rossum's avatar
Guido van Rossum committed
1543
{
1544
	PyObject *op;
1545
	fprintf(fp, "Remaining objects:\n");
Guido van Rossum's avatar
Guido van Rossum committed
1546 1547
	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
		fprintf(fp, "[%d] ", op->ob_refcnt);
1548 1549
		if (PyObject_Print(op, fp, 0) != 0)
			PyErr_Clear();
Guido van Rossum's avatar
Guido van Rossum committed
1550 1551 1552 1553
		putc('\n', fp);
	}
}

1554
PyObject *
1555
_Py_GetObjects(PyObject *self, PyObject *args)
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
{
	int i, n;
	PyObject *t = NULL;
	PyObject *res, *op;

	if (!PyArg_ParseTuple(args, "i|O", &n, &t))
		return NULL;
	op = refchain._ob_next;
	res = PyList_New(0);
	if (res == NULL)
		return NULL;
	for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
		while (op == self || op == args || op == res || op == t ||
1569
		       (t != NULL && op->ob_type != (PyTypeObject *) t)) {
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
			op = op->_ob_next;
			if (op == &refchain)
				return res;
		}
		if (PyList_Append(res, op) < 0) {
			Py_DECREF(res);
			return NULL;
		}
		op = op->_ob_next;
	}
	return res;
}

Guido van Rossum's avatar
Guido van Rossum committed
1583
#endif
1584 1585 1586


/* Hack to force loading of cobject.o */
Guido van Rossum's avatar
Guido van Rossum committed
1587
PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
1588 1589 1590


/* Hack to force loading of abstract.o */
1591
int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
1592 1593


Andrew M. Kuchling's avatar
Andrew M. Kuchling committed
1594
/* Python's malloc wrappers (see pymem.h) */
1595

1596
void *
1597
PyMem_Malloc(size_t nbytes)
1598 1599 1600 1601 1602
{
#if _PyMem_EXTRA > 0
	if (nbytes == 0)
		nbytes = _PyMem_EXTRA;
#endif
1603
	return PyMem_MALLOC(nbytes);
1604 1605
}

1606 1607
void *
PyMem_Realloc(void *p, size_t nbytes)
1608 1609 1610 1611 1612
{
#if _PyMem_EXTRA > 0
	if (nbytes == 0)
		nbytes = _PyMem_EXTRA;
#endif
1613
	return PyMem_REALLOC(p, nbytes);
1614 1615 1616
}

void
1617
PyMem_Free(void *p)
1618
{
1619
	PyMem_FREE(p);
1620 1621
}

1622 1623

/* Python's object malloc wrappers (see objimpl.h) */
1624

1625
void *
1626
PyObject_Malloc(size_t nbytes)
1627
{
1628
	return PyObject_MALLOC(nbytes);
1629 1630
}

1631 1632
void *
PyObject_Realloc(void *p, size_t nbytes)
1633
{
1634
	return PyObject_REALLOC(p, nbytes);
1635 1636 1637
}

void
1638
PyObject_Free(void *p)
1639
{
1640
	PyObject_FREE(p);
1641
}
1642 1643


1644 1645 1646 1647 1648
/* Hook to clear up weak references only once the _weakref module is
   imported.  We use a dummy implementation to simplify the code at each
   call site instead of requiring a test for NULL.
*/

1649
static void
1650 1651
empty_clear_weak_refs(PyObject *o)
{
1652
    return;
1653 1654
}

1655
void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
1656 1657 1658



1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
/* These methods are used to control infinite recursion in repr, str, print,
   etc.  Container objects that may recursively contain themselves,
   e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
   Py_ReprLeave() to avoid infinite recursion.

   Py_ReprEnter() returns 0 the first time it is called for a particular
   object and 1 every time thereafter.  It returns -1 if an exception
   occurred.  Py_ReprLeave() has no return value.

   See dictobject.c and listobject.c for examples of use.
*/

#define KEY "Py_Repr"

int
1674
Py_ReprEnter(PyObject *obj)
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
{
	PyObject *dict;
	PyObject *list;
	int i;

	dict = PyThreadState_GetDict();
	if (dict == NULL)
		return -1;
	list = PyDict_GetItemString(dict, KEY);
	if (list == NULL) {
		list = PyList_New(0);
		if (list == NULL)
			return -1;
		if (PyDict_SetItemString(dict, KEY, list) < 0)
			return -1;
		Py_DECREF(list);
	}
	i = PyList_GET_SIZE(list);
	while (--i >= 0) {
		if (PyList_GET_ITEM(list, i) == obj)
			return 1;
	}
	PyList_Append(list, obj);
	return 0;
}

void
1702
Py_ReprLeave(PyObject *obj)
1703 1704 1705 1706 1707 1708
{
	PyObject *dict;
	PyObject *list;
	int i;

	dict = PyThreadState_GetDict();
1709 1710
	if (dict == NULL)
		return;
1711
	list = PyDict_GetItemString(dict, KEY);
1712 1713
	if (list == NULL || !PyList_Check(list))
		return;
1714 1715 1716 1717 1718 1719 1720 1721 1722
	i = PyList_GET_SIZE(list);
	/* Count backwards because we always expect obj to be list[-1] */
	while (--i >= 0) {
		if (PyList_GET_ITEM(list, i) == obj) {
			PyList_SetSlice(list, i, i + 1, NULL);
			break;
		}
	}
}
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736

/*
  trashcan
  CT 2k0130
  non-recursively destroy nested objects

  CT 2k0223
  everything is now done in a macro.

  CT 2k0305
  modified to use functions, after Tim Peter's suggestion.

  CT 2k0309
  modified to restore a possible error.
Guido van Rossum's avatar
Guido van Rossum committed
1737 1738 1739

  CT 2k0325
  added better safe than sorry check for threadstate
1740 1741 1742 1743 1744 1745

  CT 2k0422
  complete rewrite. We now build a chain via ob_type
  and save the limited number of types in ob_refcnt.
  This is perfect since we don't need any memory.
  A patch for free-threading would need just a lock.
1746 1747
*/

1748 1749 1750 1751 1752 1753 1754
#define Py_TRASHCAN_TUPLE       1
#define Py_TRASHCAN_LIST        2
#define Py_TRASHCAN_DICT        3
#define Py_TRASHCAN_FRAME       4
#define Py_TRASHCAN_TRACEBACK   5
/* extend here if other objects want protection */

1755
int _PyTrash_delete_nesting = 0;
1756

1757 1758 1759
PyObject * _PyTrash_delete_later = NULL;

void
1760
_PyTrash_deposit_object(PyObject *op)
1761
{
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
	int typecode;

	if (PyTuple_Check(op))
		typecode = Py_TRASHCAN_TUPLE;
	else if (PyList_Check(op))
		typecode = Py_TRASHCAN_LIST;
	else if (PyDict_Check(op))
		typecode = Py_TRASHCAN_DICT;
	else if (PyFrame_Check(op))
		typecode = Py_TRASHCAN_FRAME;
	else if (PyTraceBack_Check(op))
		typecode = Py_TRASHCAN_TRACEBACK;
1774 1775 1776 1777
	else /* We have a bug here -- those are the only types in GC */ {
		Py_FatalError("Type not supported in GC -- internal bug");
		return; /* pacify compiler -- execution never here */
	}
1778 1779 1780 1781
	op->ob_refcnt = typecode;

	op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
	_PyTrash_delete_later = op;
1782 1783 1784
}

void
1785
_PyTrash_destroy_chain(void)
1786 1787 1788
{
	while (_PyTrash_delete_later) {
		PyObject *shredder = _PyTrash_delete_later;
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
		_PyTrash_delete_later = (PyObject*) shredder->ob_type;

		switch (shredder->ob_refcnt) {
		case Py_TRASHCAN_TUPLE:
			shredder->ob_type = &PyTuple_Type;
			break;
		case Py_TRASHCAN_LIST:
			shredder->ob_type = &PyList_Type;
			break;
		case Py_TRASHCAN_DICT:
			shredder->ob_type = &PyDict_Type;
			break;
		case Py_TRASHCAN_FRAME:
			shredder->ob_type = &PyFrame_Type;
			break;
		case Py_TRASHCAN_TRACEBACK:
			shredder->ob_type = &PyTraceBack_Type;
			break;
		}
		_Py_NewReference(shredder);

1810 1811 1812 1813 1814
		++_PyTrash_delete_nesting;
		Py_DECREF(shredder);
		--_PyTrash_delete_nesting;
	}
}
1815 1816 1817 1818

#ifdef WITH_PYMALLOC
#include "obmalloc.c"
#endif