blob: 964c5c59f29e97375dbe05f7bcf15200003d279b [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 Rossum9b00dfa1998-04-28 16:06:54 +0000165#ifdef USE_STACKCHECK
166 if (PyOS_CheckStack()) {
167 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
168 return -1;
169 }
170#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000171 if (op == NULL) {
172 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 }
Guido van Rossum90933611991-06-07 16:10:43 +0000174 else {
175 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000176 fprintf(fp, "<refcnt %u at %lx>",
177 op->ob_refcnt, (long)op);
178 else if (op->ob_type->tp_print == NULL) {
179 if (op->ob_type->tp_repr == NULL) {
180 fprintf(fp, "<%s object at %lx>",
181 op->ob_type->tp_name, (long)op);
182 }
183 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 PyObject *s;
185 if (flags & Py_PRINT_RAW)
186 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000187 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000189 if (s == NULL)
190 ret = -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 else if (!PyString_Check(s)) {
192 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000193 "repr not string");
194 ret = -1;
195 }
196 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000197 ret = PyObject_Print(s, fp,
198 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000199 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 }
202 }
Guido van Rossum90933611991-06-07 16:10:43 +0000203 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000204 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000205 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000206 if (ret == 0) {
207 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000209 clearerr(fp);
210 ret = -1;
211 }
212 }
213 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214}
215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216PyObject *
217PyObject_Repr(v)
218 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000221 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000222#ifdef USE_STACKCHECK
223 if (PyOS_CheckStack()) {
224 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
225 return NULL;
226 }
227#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000228 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000230 else if (v->ob_type->tp_repr == NULL) {
231 char buf[120];
232 sprintf(buf, "<%.80s object at %lx>",
233 v->ob_type->tp_name, (long)v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 }
Guido van Rossum90933611991-06-07 16:10:43 +0000236 else
237 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238}
239
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240PyObject *
241PyObject_Str(v)
242 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000243{
244 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245 return PyString_FromString("<NULL>");
246 else if (PyString_Check(v)) {
247 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000248 return v;
249 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000250 else if (v->ob_type->tp_str != NULL)
251 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000252 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253 PyObject *func;
254 PyObject *res;
255 if (!PyInstance_Check(v) ||
256 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
257 PyErr_Clear();
258 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000259 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 res = PyEval_CallObject(func, (PyObject *)NULL);
261 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000262 return res;
263 }
264}
265
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000267do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000269{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000270 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000271 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
272 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 This is intentional -- it makes no sense to define cmp(x,y)
274 different than -cmp(y,x). */
275 if (PyInstance_Check(v) || PyInstance_Check(w))
276 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000277 c = PyObject_Compare(v, w);
278 if (c && PyErr_Occurred())
279 return NULL;
280 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000281}
282
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000284PyObject_Compare(v, w)
285 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 PyTypeObject *tp;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000288 if (v == NULL || w == NULL) {
289 PyErr_BadInternalCall();
290 return -1;
291 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292 if (v == w)
293 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 if (PyInstance_Check(v) || PyInstance_Check(w)) {
295 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000296 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 if (!PyInstance_Check(v))
298 return -PyObject_Compare(w, v);
Guido van Rossum20566841995-01-12 11:26:10 +0000299 res = do_cmp(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000300 if (res == NULL)
301 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 if (!PyInt_Check(res)) {
303 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000304 PyErr_SetString(PyExc_TypeError,
305 "comparison did not return an int");
306 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000307 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 c = PyInt_AsLong(res);
309 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000310 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
311 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000312 if ((tp = v->ob_type) != w->ob_type) {
313 if (tp->tp_as_number != NULL &&
314 w->ob_type->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000315 int err;
316 err = PyNumber_CoerceEx(&v, &w);
317 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000318 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000319 else if (err == 0) {
Guido van Rossum9fb03681991-07-01 18:48:04 +0000320 int cmp = (*v->ob_type->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321 Py_DECREF(v);
322 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000323 return cmp;
324 }
325 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000327 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328 if (tp->tp_compare == NULL)
329 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000330 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331}
332
Guido van Rossum9bfef441993-03-29 10:43:31 +0000333long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334PyObject_Hash(v)
335 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000336{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000338 if (tp->tp_hash != NULL)
339 return (*tp->tp_hash)(v);
340 if (tp->tp_compare == NULL)
341 return (long) v; /* Use address as hash value */
342 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000344 return -1;
345}
346
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347PyObject *
348PyObject_GetAttrString(v, name)
349 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000350 char *name;
351{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000352 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000354 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000355 if (w == NULL)
356 return NULL;
357 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000359 return res;
360 }
361
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000363 PyErr_Format(PyExc_AttributeError,
364 "'%.50s' object has no attribute '%.400s'",
365 v->ob_type->tp_name,
366 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000367 return NULL;
368 }
369 else {
370 return (*v->ob_type->tp_getattr)(v, name);
371 }
372}
373
374int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375PyObject_HasAttrString(v, name)
376 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000377 char *name;
378{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000380 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000382 return 1;
383 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000385 return 0;
386}
387
388int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389PyObject_SetAttrString(v, name, w)
390 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000394 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000396 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000397 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000398 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000399 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000400 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000402 return res;
403 }
404
Guido van Rossum3f5da241990-12-20 15:06:42 +0000405 if (v->ob_type->tp_setattr == NULL) {
406 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000408 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000411 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000412 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000413 }
414 else {
415 return (*v->ob_type->tp_setattr)(v, name, w);
416 }
417}
418
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000419PyObject *
420PyObject_GetAttr(v, name)
421 PyObject *v;
422 PyObject *name;
423{
424 if (v->ob_type->tp_getattro != NULL)
425 return (*v->ob_type->tp_getattro)(v, name);
426 else
427 return PyObject_GetAttrString(v, PyString_AsString(name));
428}
429
430int
431PyObject_HasAttr(v, name)
432 PyObject *v;
433 PyObject *name;
434{
435 PyObject *res = PyObject_GetAttr(v, name);
436 if (res != NULL) {
437 Py_DECREF(res);
438 return 1;
439 }
440 PyErr_Clear();
441 return 0;
442}
443
444int
445PyObject_SetAttr(v, name, value)
446 PyObject *v;
447 PyObject *name;
448 PyObject *value;
449{
450 int err;
451 Py_INCREF(name);
452 PyString_InternInPlace(&name);
453 if (v->ob_type->tp_setattro != NULL)
454 err = (*v->ob_type->tp_setattro)(v, name, value);
455 else
456 err = PyObject_SetAttrString(
457 v, PyString_AsString(name), value);
458 Py_DECREF(name);
459 return err;
460}
461
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000462/* Test a value used as condition, e.g., in a for or if statement.
463 Return -1 if an error occurred */
464
465int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466PyObject_IsTrue(v)
467 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000468{
469 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000471 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000472 else if (v->ob_type->tp_as_number != NULL &&
473 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000474 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000475 else if (v->ob_type->tp_as_mapping != NULL &&
476 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000477 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000478 else if (v->ob_type->tp_as_sequence != NULL &&
479 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000480 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
481 else
482 res = 1;
483 if (res > 0)
484 res = 1;
485 return res;
486}
487
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000488/* equivalent of 'not v'
489 Return -1 if an error occurred */
490
491int
492PyObject_Not(v)
493 PyObject *v;
494{
495 int res;
496 res = PyObject_IsTrue(v);
497 if (res < 0)
498 return res;
499 return res == 0;
500}
501
Guido van Rossum5524a591995-01-10 15:26:20 +0000502/* Coerce two numeric types to the "larger" one.
503 Increment the reference count on each argument.
504 Return -1 and raise an exception if no coercion is possible
505 (and then no reference count is incremented).
506*/
507
508int
Guido van Rossum242c6421997-11-19 16:03:17 +0000509PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000511{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 register PyObject *v = *pv;
513 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000514 int res;
515
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
517 Py_INCREF(v);
518 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000519 return 0;
520 }
521 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
522 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
523 if (res <= 0)
524 return res;
525 }
526 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
527 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
528 if (res <= 0)
529 return res;
530 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000531 return 1;
532}
533
534int
535PyNumber_Coerce(pv, pw)
536 PyObject **pv, **pw;
537{
538 int err = PyNumber_CoerceEx(pv, pw);
539 if (err <= 0)
540 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000542 return -1;
543}
544
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000546/* Test whether an object can be called */
547
548int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549PyCallable_Check(x)
550 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000551{
552 if (x == NULL)
553 return 0;
554 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyFunction_Check(x) ||
556 PyMethod_Check(x) ||
557 PyCFunction_Check(x) ||
558 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000559 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 if (PyInstance_Check(x)) {
561 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000562 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000564 return 0;
565 }
566 /* Could test recursively but don't, for fear of endless
567 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000569 return 1;
570 }
571 return 0;
572}
573
574
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575/*
576NoObject is usable as a non-NULL undefined value, used by the macro None.
577There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000578so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579*/
580
Guido van Rossum0c182a11992-03-27 17:26:13 +0000581/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000583none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000585{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587}
588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589static PyTypeObject PyNothing_Type = {
590 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000592 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593 0,
594 0,
595 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000596 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000597 0, /*tp_getattr*/
598 0, /*tp_setattr*/
599 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000600 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601 0, /*tp_as_number*/
602 0, /*tp_as_sequence*/
603 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000604 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605};
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607PyObject _Py_NoneStruct = {
608 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609};
610
611
Guido van Rossum84a90321996-05-22 16:34:47 +0000612#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000616void
Guido van Rossume09fb551997-08-05 02:04:34 +0000617_Py_ResetReferences()
618{
619 refchain._ob_prev = refchain._ob_next = &refchain;
620 _Py_RefTotal = 0;
621}
622
623void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624_Py_NewReference(op)
625 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628 op->ob_refcnt = 1;
629 op->_ob_next = refchain._ob_next;
630 op->_ob_prev = &refchain;
631 refchain._ob_next->_ob_prev = op;
632 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000633#ifdef COUNT_ALLOCS
634 inc_count(op->ob_type);
635#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636}
637
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000638void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639_Py_ForgetReference(op)
640 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 register PyObject *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000643 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000645 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000646 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000648#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000649 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
650 if (p == op)
651 break;
652 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000653 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000655#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656 op->_ob_next->_ob_prev = op->_ob_prev;
657 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000658 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000659#ifdef COUNT_ALLOCS
660 op->ob_type->tp_free++;
661#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000662}
663
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000664void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665_Py_Dealloc(op)
666 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000667{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000668 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 _Py_ForgetReference(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000670 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000671 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672}
673
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000674void
Guido van Rossumded690f1996-05-24 20:48:31 +0000675_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676 FILE *fp;
677{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000679 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000680 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
681 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 if (PyObject_Print(op, fp, 0) != 0)
683 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684 putc('\n', fp);
685 }
686}
687
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000688PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000689_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000690 PyObject *self;
691 PyObject *args;
692{
693 int i, n;
694 PyObject *t = NULL;
695 PyObject *res, *op;
696
697 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
698 return NULL;
699 op = refchain._ob_next;
700 res = PyList_New(0);
701 if (res == NULL)
702 return NULL;
703 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
704 while (op == self || op == args || op == res || op == t ||
705 t != NULL && op->ob_type != (PyTypeObject *) t) {
706 op = op->_ob_next;
707 if (op == &refchain)
708 return res;
709 }
710 if (PyList_Append(res, op) < 0) {
711 Py_DECREF(res);
712 return NULL;
713 }
714 op = op->_ob_next;
715 }
716 return res;
717}
718
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000720
721
722/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000723PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000724
725
726/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000728
729
730/* Malloc wrappers (see mymalloc.h) */
731
732/* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
733
734ANY *
735Py_Malloc(nbytes)
736 size_t nbytes;
737{
738 ANY *p;
739#if _PyMem_EXTRA > 0
740 if (nbytes == 0)
741 nbytes = _PyMem_EXTRA;
742#endif
743 p = malloc(nbytes);
744 if (p != NULL)
745 return p;
Guido van Rossumea46e4d1997-08-12 14:54:54 +0000746 else {
747 PyErr_NoMemory();
748 return NULL;
749 }
Guido van Rossume09fb551997-08-05 02:04:34 +0000750}
751
752ANY *
753Py_Realloc(p, nbytes)
754 ANY *p;
755 size_t nbytes;
756{
757#if _PyMem_EXTRA > 0
758 if (nbytes == 0)
759 nbytes = _PyMem_EXTRA;
760#endif
761 p = realloc(p, nbytes);
762 if (p != NULL)
763 return p;
Guido van Rossumea46e4d1997-08-12 14:54:54 +0000764 else {
765 PyErr_NoMemory();
766 return NULL;
767 }
Guido van Rossume09fb551997-08-05 02:04:34 +0000768}
769
770void
771Py_Free(p)
772 ANY *p;
773{
774 free(p);
775}
776
777/* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
778
779ANY *
780PyMem_Malloc(nbytes)
781 size_t nbytes;
782{
783#if _PyMem_EXTRA > 0
784 if (nbytes == 0)
785 nbytes = _PyMem_EXTRA;
786#endif
787 return malloc(nbytes);
788}
789
790ANY *
791PyMem_Realloc(p, nbytes)
792 ANY *p;
793 size_t nbytes;
794{
795#if _PyMem_EXTRA > 0
796 if (nbytes == 0)
797 nbytes = _PyMem_EXTRA;
798#endif
799 return realloc(p, nbytes);
800}
801
802void
803PyMem_Free(p)
804 ANY *p;
805{
806 free(p);
807}
Guido van Rossum86610361998-04-10 22:32:46 +0000808
809
810/* These methods are used to control infinite recursion in repr, str, print,
811 etc. Container objects that may recursively contain themselves,
812 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
813 Py_ReprLeave() to avoid infinite recursion.
814
815 Py_ReprEnter() returns 0 the first time it is called for a particular
816 object and 1 every time thereafter. It returns -1 if an exception
817 occurred. Py_ReprLeave() has no return value.
818
819 See dictobject.c and listobject.c for examples of use.
820*/
821
822#define KEY "Py_Repr"
823
824int
825Py_ReprEnter(obj)
826 PyObject *obj;
827{
828 PyObject *dict;
829 PyObject *list;
830 int i;
831
832 dict = PyThreadState_GetDict();
833 if (dict == NULL)
834 return -1;
835 list = PyDict_GetItemString(dict, KEY);
836 if (list == NULL) {
837 list = PyList_New(0);
838 if (list == NULL)
839 return -1;
840 if (PyDict_SetItemString(dict, KEY, list) < 0)
841 return -1;
842 Py_DECREF(list);
843 }
844 i = PyList_GET_SIZE(list);
845 while (--i >= 0) {
846 if (PyList_GET_ITEM(list, i) == obj)
847 return 1;
848 }
849 PyList_Append(list, obj);
850 return 0;
851}
852
853void
854Py_ReprLeave(obj)
855 PyObject *obj;
856{
857 PyObject *dict;
858 PyObject *list;
859 int i;
860
861 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +0000862 if (dict == NULL)
863 return;
Guido van Rossum86610361998-04-10 22:32:46 +0000864 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +0000865 if (list == NULL || !PyList_Check(list))
866 return;
Guido van Rossum86610361998-04-10 22:32:46 +0000867 i = PyList_GET_SIZE(list);
868 /* Count backwards because we always expect obj to be list[-1] */
869 while (--i >= 0) {
870 if (PyList_GET_ITEM(list, i) == obj) {
871 PyList_SetSlice(list, i, i + 1, NULL);
872 break;
873 }
874 }
875}