blob: 1a1ed52208c7ebdac25b75b999622a2d1aea2f4f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum6f9e4331995-03-29 16:57:48 +000036#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037long _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000038#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossum3f5da241990-12-20 15:06:42 +000040/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
41 These are used by the individual routines for object creation.
42 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000044#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000046extern int tuple_zero_allocs, fast_tuple_allocs;
47extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000048extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000049void
50dump_counts()
51{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000053
54 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000055 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
56 tp->tp_name, tp->tp_alloc, tp->tp_free,
57 tp->tp_maxalloc);
58 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
59 fast_tuple_allocs, tuple_zero_allocs);
60 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
61 quick_int_allocs, quick_neg_int_allocs);
62 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
63 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000064}
65
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000066PyObject *
67get_counts()
68{
69 PyTypeObject *tp;
70 PyObject *result;
71 PyObject *v;
72
73 result = PyList_New(0);
74 if (result == NULL)
75 return NULL;
76 for (tp = type_list; tp; tp = tp->tp_next) {
77 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
78 tp->tp_free, tp->tp_maxalloc);
79 if (v == NULL) {
80 Py_DECREF(result);
81 return NULL;
82 }
83 if (PyList_Append(result, v) < 0) {
84 Py_DECREF(v);
85 Py_DECREF(result);
86 return NULL;
87 }
88 Py_DECREF(v);
89 }
90 return result;
91}
92
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000093void
94inc_count(tp)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000096{
97 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000098 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000099 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000101 tp->tp_next = type_list;
102 type_list = tp;
103 }
104 tp->tp_alloc++;
105 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
106 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
107}
108#endif
109
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000110#ifndef MS_COREDLL
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111PyObject *
112_PyObject_New(tp)
113 PyTypeObject *tp;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000114#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115PyObject *
116_PyObject_New(tp,op)
117 PyTypeObject *tp;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000118 PyObject *op;
119#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000121#ifndef MS_COREDLL
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000123#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return op;
129}
130
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000131#ifndef MS_COREDLL
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000132PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133_PyObject_NewVar(tp, size)
134 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000135 int size;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000136#else
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000137PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138_PyObject_NewVar(tp, size, op)
139 PyTypeObject *tp;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000140 int size;
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000141 PyVarObject *op;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000142#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000144#ifndef MS_COREDLL
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000145 PyVarObject *op = (PyVarObject *)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000147#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000149 return (PyVarObject *)PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 op->ob_type = tp;
151 op->ob_size = size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return op;
154}
155
Guido van Rossum90933611991-06-07 16:10:43 +0000156int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyObject_Print(op, fp, flags)
158 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 FILE *fp;
160 int flags;
161{
Guido van Rossum278ef591991-07-27 21:40:24 +0000162 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000164 return -1;
Guido van Rossum90933611991-06-07 16:10:43 +0000165 if (op == NULL) {
166 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 }
Guido van Rossum90933611991-06-07 16:10:43 +0000168 else {
169 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000170 fprintf(fp, "<refcnt %u at %lx>",
171 op->ob_refcnt, (long)op);
172 else if (op->ob_type->tp_print == NULL) {
173 if (op->ob_type->tp_repr == NULL) {
174 fprintf(fp, "<%s object at %lx>",
175 op->ob_type->tp_name, (long)op);
176 }
177 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 PyObject *s;
179 if (flags & Py_PRINT_RAW)
180 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000181 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000183 if (s == NULL)
184 ret = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 else if (!PyString_Check(s)) {
186 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000187 "repr not string");
188 ret = -1;
189 }
190 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 fprintf(fp, "%s",
192 PyString_AsString(s));
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000193 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000195 }
196 }
Guido van Rossum90933611991-06-07 16:10:43 +0000197 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000198 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000199 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000200 if (ret == 0) {
201 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000203 clearerr(fp);
204 ret = -1;
205 }
206 }
207 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208}
209
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210PyObject *
211PyObject_Repr(v)
212 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000215 return NULL;
Guido van Rossum90933611991-06-07 16:10:43 +0000216 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000218 else if (v->ob_type->tp_repr == NULL) {
219 char buf[120];
220 sprintf(buf, "<%.80s object at %lx>",
221 v->ob_type->tp_name, (long)v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 }
Guido van Rossum90933611991-06-07 16:10:43 +0000224 else
225 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228PyObject *
229PyObject_Str(v)
230 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000231{
232 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000233 return PyString_FromString("<NULL>");
234 else if (PyString_Check(v)) {
235 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000236 return v;
237 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000238 else if (v->ob_type->tp_str != NULL)
239 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000240 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241 PyObject *func;
242 PyObject *res;
243 if (!PyInstance_Check(v) ||
244 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
245 PyErr_Clear();
246 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000247 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 res = PyEval_CallObject(func, (PyObject *)NULL);
249 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000250 return res;
251 }
252}
253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000255do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000257{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000258 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000259 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
260 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261 This is intentional -- it makes no sense to define cmp(x,y)
262 different than -cmp(y,x). */
263 if (PyInstance_Check(v) || PyInstance_Check(w))
264 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000265 c = PyObject_Compare(v, w);
266 if (c && PyErr_Occurred())
267 return NULL;
268 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000269}
270
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000272PyObject_Compare(v, w)
273 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 PyTypeObject *tp;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000276 if (v == NULL || w == NULL) {
277 PyErr_BadInternalCall();
278 return -1;
279 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280 if (v == w)
281 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 if (PyInstance_Check(v) || PyInstance_Check(w)) {
283 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000284 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 if (!PyInstance_Check(v))
286 return -PyObject_Compare(w, v);
Guido van Rossum20566841995-01-12 11:26:10 +0000287 res = do_cmp(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000288 if (res == NULL)
289 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 if (!PyInt_Check(res)) {
291 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000292 PyErr_SetString(PyExc_TypeError,
293 "comparison did not return an int");
294 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 c = PyInt_AsLong(res);
297 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000298 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
299 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000300 if ((tp = v->ob_type) != w->ob_type) {
301 if (tp->tp_as_number != NULL &&
302 w->ob_type->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000303 int err;
304 err = PyNumber_CoerceEx(&v, &w);
305 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000306 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000307 else if (err == 0) {
Guido van Rossum9fb03681991-07-01 18:48:04 +0000308 int cmp = (*v->ob_type->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 Py_DECREF(v);
310 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000311 return cmp;
312 }
313 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000315 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316 if (tp->tp_compare == NULL)
317 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000318 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319}
320
Guido van Rossum9bfef441993-03-29 10:43:31 +0000321long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000322PyObject_Hash(v)
323 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000324{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000326 if (tp->tp_hash != NULL)
327 return (*tp->tp_hash)(v);
328 if (tp->tp_compare == NULL)
329 return (long) v; /* Use address as hash value */
330 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000332 return -1;
333}
334
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335PyObject *
336PyObject_GetAttrString(v, name)
337 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000338 char *name;
339{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000340 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000342 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000343 if (w == NULL)
344 return NULL;
345 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000347 return res;
348 }
349
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000351 PyErr_Format(PyExc_AttributeError,
352 "'%.50s' object has no attribute '%.400s'",
353 v->ob_type->tp_name,
354 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000355 return NULL;
356 }
357 else {
358 return (*v->ob_type->tp_getattr)(v, name);
359 }
360}
361
362int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363PyObject_HasAttrString(v, name)
364 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000365 char *name;
366{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000368 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000370 return 1;
371 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000373 return 0;
374}
375
376int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377PyObject_SetAttrString(v, name, w)
378 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000382 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000384 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000385 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000386 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000387 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000388 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000390 return res;
391 }
392
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393 if (v->ob_type->tp_setattr == NULL) {
394 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000396 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000397 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000399 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000400 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000401 }
402 else {
403 return (*v->ob_type->tp_setattr)(v, name, w);
404 }
405}
406
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000407PyObject *
408PyObject_GetAttr(v, name)
409 PyObject *v;
410 PyObject *name;
411{
412 if (v->ob_type->tp_getattro != NULL)
413 return (*v->ob_type->tp_getattro)(v, name);
414 else
415 return PyObject_GetAttrString(v, PyString_AsString(name));
416}
417
418int
419PyObject_HasAttr(v, name)
420 PyObject *v;
421 PyObject *name;
422{
423 PyObject *res = PyObject_GetAttr(v, name);
424 if (res != NULL) {
425 Py_DECREF(res);
426 return 1;
427 }
428 PyErr_Clear();
429 return 0;
430}
431
432int
433PyObject_SetAttr(v, name, value)
434 PyObject *v;
435 PyObject *name;
436 PyObject *value;
437{
438 int err;
439 Py_INCREF(name);
440 PyString_InternInPlace(&name);
441 if (v->ob_type->tp_setattro != NULL)
442 err = (*v->ob_type->tp_setattro)(v, name, value);
443 else
444 err = PyObject_SetAttrString(
445 v, PyString_AsString(name), value);
446 Py_DECREF(name);
447 return err;
448}
449
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000450/* Test a value used as condition, e.g., in a for or if statement.
451 Return -1 if an error occurred */
452
453int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454PyObject_IsTrue(v)
455 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000456{
457 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000459 res = 0;
460 else if (v->ob_type->tp_as_number != NULL)
461 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
462 else if (v->ob_type->tp_as_mapping != NULL)
463 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
464 else if (v->ob_type->tp_as_sequence != NULL)
465 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
466 else
467 res = 1;
468 if (res > 0)
469 res = 1;
470 return res;
471}
472
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000473/* equivalent of 'not v'
474 Return -1 if an error occurred */
475
476int
477PyObject_Not(v)
478 PyObject *v;
479{
480 int res;
481 res = PyObject_IsTrue(v);
482 if (res < 0)
483 return res;
484 return res == 0;
485}
486
Guido van Rossum5524a591995-01-10 15:26:20 +0000487/* Coerce two numeric types to the "larger" one.
488 Increment the reference count on each argument.
489 Return -1 and raise an exception if no coercion is possible
490 (and then no reference count is incremented).
491*/
492
493int
Guido van Rossum242c6421997-11-19 16:03:17 +0000494PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000496{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497 register PyObject *v = *pv;
498 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000499 int res;
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
502 Py_INCREF(v);
503 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000504 return 0;
505 }
506 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
507 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
508 if (res <= 0)
509 return res;
510 }
511 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
512 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
513 if (res <= 0)
514 return res;
515 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000516 return 1;
517}
518
519int
520PyNumber_Coerce(pv, pw)
521 PyObject **pv, **pw;
522{
523 int err = PyNumber_CoerceEx(pv, pw);
524 if (err <= 0)
525 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000527 return -1;
528}
529
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000531/* Test whether an object can be called */
532
533int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534PyCallable_Check(x)
535 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000536{
537 if (x == NULL)
538 return 0;
539 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 PyFunction_Check(x) ||
541 PyMethod_Check(x) ||
542 PyCFunction_Check(x) ||
543 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000544 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 if (PyInstance_Check(x)) {
546 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000547 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000549 return 0;
550 }
551 /* Could test recursively but don't, for fear of endless
552 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000554 return 1;
555 }
556 return 0;
557}
558
559
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560/*
561NoObject is usable as a non-NULL undefined value, used by the macro None.
562There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564*/
565
Guido van Rossum0c182a11992-03-27 17:26:13 +0000566/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000568none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000570{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572}
573
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574static PyTypeObject PyNothing_Type = {
575 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000577 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578 0,
579 0,
580 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000581 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000582 0, /*tp_getattr*/
583 0, /*tp_setattr*/
584 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000585 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586 0, /*tp_as_number*/
587 0, /*tp_as_sequence*/
588 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000589 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590};
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592PyObject _Py_NoneStruct = {
593 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594};
595
596
Guido van Rossum84a90321996-05-22 16:34:47 +0000597#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000601void
Guido van Rossume09fb551997-08-05 02:04:34 +0000602_Py_ResetReferences()
603{
604 refchain._ob_prev = refchain._ob_next = &refchain;
605 _Py_RefTotal = 0;
606}
607
608void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609_Py_NewReference(op)
610 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613 op->ob_refcnt = 1;
614 op->_ob_next = refchain._ob_next;
615 op->_ob_prev = &refchain;
616 refchain._ob_next->_ob_prev = op;
617 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000618#ifdef COUNT_ALLOCS
619 inc_count(op->ob_type);
620#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621}
622
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000623void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624_Py_ForgetReference(op)
625 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 register PyObject *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000628 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000630 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000631 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000633#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000634 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
635 if (p == op)
636 break;
637 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000638 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000640#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641 op->_ob_next->_ob_prev = op->_ob_prev;
642 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000643 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000644#ifdef COUNT_ALLOCS
645 op->ob_type->tp_free++;
646#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000647}
648
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000649void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650_Py_Dealloc(op)
651 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000652{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000653 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 _Py_ForgetReference(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000655 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000656 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000657}
658
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000659void
Guido van Rossumded690f1996-05-24 20:48:31 +0000660_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000661 FILE *fp;
662{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000664 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
666 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 if (PyObject_Print(op, fp, 0) != 0)
668 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669 putc('\n', fp);
670 }
671}
672
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000673PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000674_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000675 PyObject *self;
676 PyObject *args;
677{
678 int i, n;
679 PyObject *t = NULL;
680 PyObject *res, *op;
681
682 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
683 return NULL;
684 op = refchain._ob_next;
685 res = PyList_New(0);
686 if (res == NULL)
687 return NULL;
688 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
689 while (op == self || op == args || op == res || op == t ||
690 t != NULL && op->ob_type != (PyTypeObject *) t) {
691 op = op->_ob_next;
692 if (op == &refchain)
693 return res;
694 }
695 if (PyList_Append(res, op) < 0) {
696 Py_DECREF(res);
697 return NULL;
698 }
699 op = op->_ob_next;
700 }
701 return res;
702}
703
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000705
706
707/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000708PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000709
710
711/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000713
714
715/* Malloc wrappers (see mymalloc.h) */
716
717/* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
718
719ANY *
720Py_Malloc(nbytes)
721 size_t nbytes;
722{
723 ANY *p;
724#if _PyMem_EXTRA > 0
725 if (nbytes == 0)
726 nbytes = _PyMem_EXTRA;
727#endif
728 p = malloc(nbytes);
729 if (p != NULL)
730 return p;
Guido van Rossumea46e4d1997-08-12 14:54:54 +0000731 else {
732 PyErr_NoMemory();
733 return NULL;
734 }
Guido van Rossume09fb551997-08-05 02:04:34 +0000735}
736
737ANY *
738Py_Realloc(p, nbytes)
739 ANY *p;
740 size_t nbytes;
741{
742#if _PyMem_EXTRA > 0
743 if (nbytes == 0)
744 nbytes = _PyMem_EXTRA;
745#endif
746 p = realloc(p, nbytes);
747 if (p != NULL)
748 return p;
Guido van Rossumea46e4d1997-08-12 14:54:54 +0000749 else {
750 PyErr_NoMemory();
751 return NULL;
752 }
Guido van Rossume09fb551997-08-05 02:04:34 +0000753}
754
755void
756Py_Free(p)
757 ANY *p;
758{
759 free(p);
760}
761
762/* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
763
764ANY *
765PyMem_Malloc(nbytes)
766 size_t nbytes;
767{
768#if _PyMem_EXTRA > 0
769 if (nbytes == 0)
770 nbytes = _PyMem_EXTRA;
771#endif
772 return malloc(nbytes);
773}
774
775ANY *
776PyMem_Realloc(p, nbytes)
777 ANY *p;
778 size_t nbytes;
779{
780#if _PyMem_EXTRA > 0
781 if (nbytes == 0)
782 nbytes = _PyMem_EXTRA;
783#endif
784 return realloc(p, nbytes);
785}
786
787void
788PyMem_Free(p)
789 ANY *p;
790{
791 free(p);
792}
Guido van Rossum86610361998-04-10 22:32:46 +0000793
794
795/* These methods are used to control infinite recursion in repr, str, print,
796 etc. Container objects that may recursively contain themselves,
797 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
798 Py_ReprLeave() to avoid infinite recursion.
799
800 Py_ReprEnter() returns 0 the first time it is called for a particular
801 object and 1 every time thereafter. It returns -1 if an exception
802 occurred. Py_ReprLeave() has no return value.
803
804 See dictobject.c and listobject.c for examples of use.
805*/
806
807#define KEY "Py_Repr"
808
809int
810Py_ReprEnter(obj)
811 PyObject *obj;
812{
813 PyObject *dict;
814 PyObject *list;
815 int i;
816
817 dict = PyThreadState_GetDict();
818 if (dict == NULL)
819 return -1;
820 list = PyDict_GetItemString(dict, KEY);
821 if (list == NULL) {
822 list = PyList_New(0);
823 if (list == NULL)
824 return -1;
825 if (PyDict_SetItemString(dict, KEY, list) < 0)
826 return -1;
827 Py_DECREF(list);
828 }
829 i = PyList_GET_SIZE(list);
830 while (--i >= 0) {
831 if (PyList_GET_ITEM(list, i) == obj)
832 return 1;
833 }
834 PyList_Append(list, obj);
835 return 0;
836}
837
838void
839Py_ReprLeave(obj)
840 PyObject *obj;
841{
842 PyObject *dict;
843 PyObject *list;
844 int i;
845
846 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +0000847 if (dict == NULL)
848 return;
Guido van Rossum86610361998-04-10 22:32:46 +0000849 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +0000850 if (list == NULL || !PyList_Check(list))
851 return;
Guido van Rossum86610361998-04-10 22:32:46 +0000852 i = PyList_GET_SIZE(list);
853 /* Count backwards because we always expect obj to be list[-1] */
854 while (--i >= 0) {
855 if (PyList_GET_ITEM(list, i) == obj) {
856 PyList_SetSlice(list, i, i + 1, NULL);
857 break;
858 }
859 }
860}