Mercurial > cpython-lru_cache-weakref
annotate Objects/object.c @ 1330:409ea74feb21 legacy-trunk
Minor fixes / changes for Mac compatibility.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 29 Jul 1993 08:25:09 +0000 |
| parents | 716be765a79b |
| children | a22a350d3a5d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 86 | 25 /* Generic object operations; and implementation of None (NoObject) */ |
| 6 | 26 |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 86 | 29 #ifdef REF_DEBUG |
| 30 long ref_total; | |
| 31 #endif | |
| 6 | 32 |
| 86 | 33 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 34 These are used by the individual routines for object creation. | |
| 35 Do not call them otherwise, they do not initialize the object! */ | |
| 6 | 36 |
| 37 object * | |
| 38 newobject(tp) | |
| 39 typeobject *tp; | |
| 40 { | |
| 41 object *op = (object *) malloc(tp->tp_basicsize); | |
| 42 if (op == NULL) | |
| 43 return err_nomem(); | |
| 44 NEWREF(op); | |
| 45 op->ob_type = tp; | |
| 46 return op; | |
| 47 } | |
| 48 | |
| 49 varobject * | |
| 50 newvarobject(tp, size) | |
| 51 typeobject *tp; | |
| 52 unsigned int size; | |
| 53 { | |
| 54 varobject *op = (varobject *) | |
| 55 malloc(tp->tp_basicsize + size * tp->tp_itemsize); | |
| 56 if (op == NULL) | |
|
231
b31abf420e61
Enabled (and fixed) newvarobject()
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
57 return (varobject *)err_nomem(); |
| 6 | 58 NEWREF(op); |
| 59 op->ob_type = tp; | |
| 60 op->ob_size = size; | |
| 61 return op; | |
| 62 } | |
| 63 | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
64 int |
| 6 | 65 printobject(op, fp, flags) |
| 66 object *op; | |
| 67 FILE *fp; | |
| 68 int flags; | |
| 69 { | |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
70 int ret = 0; |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
71 if (intrcheck()) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
72 err_set(KeyboardInterrupt); |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
73 return -1; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
74 } |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
75 if (op == NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
76 fprintf(fp, "<nil>"); |
| 6 | 77 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
78 else { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
79 if (op->ob_refcnt <= 0) |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
80 fprintf(fp, "<refcnt %u at %lx>", |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
81 op->ob_refcnt, (long)op); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
82 else if (op->ob_type->tp_print == NULL) { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
83 if (op->ob_type->tp_repr == NULL) { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
84 fprintf(fp, "<%s object at %lx>", |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
85 op->ob_type->tp_name, (long)op); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
86 } |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
87 else { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
88 object *s = reprobject(op); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
89 if (s == NULL) |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
90 ret = -1; |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
91 else if (!is_stringobject(s)) { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
92 err_setstr(TypeError, |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
93 "repr not string"); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
94 ret = -1; |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
95 } |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
96 else { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
97 fprintf(fp, "%s", getstringvalue(s)); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
98 } |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
99 XDECREF(s); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
100 } |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
101 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
102 else |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
103 ret = (*op->ob_type->tp_print)(op, fp, flags); |
| 6 | 104 } |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
105 if (ret == 0) { |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
106 if (ferror(fp)) { |
| 473 | 107 err_errno(IOError); |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
108 clearerr(fp); |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
109 ret = -1; |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
110 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
111 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
112 return ret; |
| 6 | 113 } |
| 114 | |
| 115 object * | |
| 116 reprobject(v) | |
| 117 object *v; | |
| 118 { | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
119 if (intrcheck()) { |
| 6 | 120 err_set(KeyboardInterrupt); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
121 return NULL; |
| 6 | 122 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
123 if (v == NULL) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
124 return newstringobject("<NULL>"); |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
125 else if (v->ob_type->tp_repr == NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
126 char buf[120]; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
127 sprintf(buf, "<%.80s object at %lx>", |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
128 v->ob_type->tp_name, (long)v); |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
129 return newstringobject(buf); |
| 6 | 130 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
131 else |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
132 return (*v->ob_type->tp_repr)(v); |
| 6 | 133 } |
| 134 | |
| 135 int | |
| 136 cmpobject(v, w) | |
| 137 object *v, *w; | |
| 138 { | |
| 139 typeobject *tp; | |
| 140 if (v == w) | |
| 141 return 0; | |
| 142 if (v == NULL) | |
| 143 return -1; | |
| 144 if (w == NULL) | |
| 145 return 1; | |
|
307
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
146 if ((tp = v->ob_type) != w->ob_type) { |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
147 if (tp->tp_as_number != NULL && |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
148 w->ob_type->tp_as_number != NULL) { |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
149 if (coerce(&v, &w) != 0) { |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
150 err_clear(); |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
151 /* XXX Should report the error, |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
152 XXX but the interface isn't there... */ |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
153 } |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
154 else { |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
155 int cmp = (*v->ob_type->tp_compare)(v, w); |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
156 DECREF(v); |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
157 DECREF(w); |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
158 return cmp; |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
159 } |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
160 } |
| 6 | 161 return strcmp(tp->tp_name, w->ob_type->tp_name); |
|
307
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
162 } |
| 6 | 163 if (tp->tp_compare == NULL) |
| 164 return (v < w) ? -1 : 1; | |
|
307
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
165 return (*tp->tp_compare)(v, w); |
| 6 | 166 } |
| 167 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
168 long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
169 hashobject(v) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
170 object *v; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
171 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
172 typeobject *tp = v->ob_type; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
173 if (tp->tp_hash != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
174 return (*tp->tp_hash)(v); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
175 if (tp->tp_compare == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
176 return (long) v; /* Use address as hash value */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
177 /* If there's a cmp but no hash defined, the object can't be hashed */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
178 err_setstr(TypeError, "unhashable type"); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
179 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
180 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
181 |
| 86 | 182 object * |
| 183 getattr(v, name) | |
| 184 object *v; | |
| 185 char *name; | |
| 186 { | |
| 187 if (v->ob_type->tp_getattr == NULL) { | |
| 188 err_setstr(TypeError, "attribute-less object"); | |
| 189 return NULL; | |
| 190 } | |
| 191 else { | |
| 192 return (*v->ob_type->tp_getattr)(v, name); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 int | |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
197 hasattr(v, name) |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
198 object *v; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
199 char *name; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
200 { |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
201 object *res = getattr(v, name); |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
202 if (res != NULL) { |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
203 DECREF(res); |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
204 return 1; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
205 } |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
206 err_clear(); |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
207 return 0; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
208 } |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
209 |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
210 int |
| 86 | 211 setattr(v, name, w) |
| 212 object *v; | |
| 213 char *name; | |
| 214 object *w; | |
| 215 { | |
| 216 if (v->ob_type->tp_setattr == NULL) { | |
| 217 if (v->ob_type->tp_getattr == NULL) | |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
218 err_setstr(TypeError, |
|
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
219 "attribute-less object (assign or del)"); |
| 86 | 220 else |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
221 err_setstr(TypeError, |
|
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
222 "object has read-only attributes"); |
| 95 | 223 return -1; |
| 86 | 224 } |
| 225 else { | |
| 226 return (*v->ob_type->tp_setattr)(v, name, w); | |
| 227 } | |
| 228 } | |
| 229 | |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
230 /* Test a value used as condition, e.g., in a for or if statement. |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
231 Return -1 if an error occurred */ |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
232 |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
233 int |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
234 testbool(v) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
235 object *v; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
236 { |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
237 int res; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
238 if (v == None) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
239 res = 0; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
240 else if (v->ob_type->tp_as_number != NULL) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
241 res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
242 else if (v->ob_type->tp_as_mapping != NULL) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
243 res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
244 else if (v->ob_type->tp_as_sequence != NULL) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
245 res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
246 else |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
247 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
248 if (res > 0) |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
249 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
250 return res; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
251 } |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
252 |
| 6 | 253 |
| 254 /* | |
| 255 NoObject is usable as a non-NULL undefined value, used by the macro None. | |
| 256 There is (and should be!) no way to create other objects of this type, | |
| 86 | 257 so there is exactly one (which is indestructible, by the way). |
| 6 | 258 */ |
| 259 | |
| 739 | 260 /* ARGSUSED */ |
| 86 | 261 static object * |
| 262 none_repr(op) | |
| 263 object *op; | |
| 264 { | |
| 265 return newstringobject("None"); | |
| 6 | 266 } |
| 267 | |
| 268 static typeobject Notype = { | |
| 269 OB_HEAD_INIT(&Typetype) | |
| 270 0, | |
| 86 | 271 "None", |
| 6 | 272 0, |
| 273 0, | |
| 274 0, /*tp_dealloc*/ /*never called*/ | |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1017
diff
changeset
|
275 0, /*tp_print*/ |
| 86 | 276 0, /*tp_getattr*/ |
| 277 0, /*tp_setattr*/ | |
| 278 0, /*tp_compare*/ | |
| 279 none_repr, /*tp_repr*/ | |
| 280 0, /*tp_as_number*/ | |
| 281 0, /*tp_as_sequence*/ | |
| 282 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
283 0, /*tp_hash */ |
| 6 | 284 }; |
| 285 | |
| 286 object NoObject = { | |
| 287 OB_HEAD_INIT(&Notype) | |
| 288 }; | |
| 289 | |
| 290 | |
| 291 #ifdef TRACE_REFS | |
| 292 | |
| 293 static object refchain = {&refchain, &refchain}; | |
| 294 | |
| 295 NEWREF(op) | |
| 296 object *op; | |
| 297 { | |
| 298 ref_total++; | |
| 299 op->ob_refcnt = 1; | |
| 300 op->_ob_next = refchain._ob_next; | |
| 301 op->_ob_prev = &refchain; | |
| 302 refchain._ob_next->_ob_prev = op; | |
| 303 refchain._ob_next = op; | |
| 304 } | |
| 305 | |
| 86 | 306 UNREF(op) |
| 307 register object *op; | |
| 6 | 308 { |
| 86 | 309 register object *p; |
|
58
4975f7990b2b
Removed redundant prototype for err_nomem().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
310 if (op->ob_refcnt < 0) { |
| 86 | 311 fprintf(stderr, "UNREF negative refcnt\n"); |
| 312 abort(); | |
| 313 } | |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
314 if (op == &refchain || |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
315 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
316 fprintf(stderr, "UNREF invalid object\n"); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
317 abort(); |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
318 } |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
319 #ifdef SLOW_UNREF_CHECK |
| 86 | 320 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 321 if (p == op) | |
| 322 break; | |
| 323 } | |
| 324 if (p == &refchain) { /* Not found */ | |
| 325 fprintf(stderr, "UNREF unknown object\n"); | |
|
58
4975f7990b2b
Removed redundant prototype for err_nomem().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
326 abort(); |
|
4975f7990b2b
Removed redundant prototype for err_nomem().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
327 } |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
328 #endif |
| 6 | 329 op->_ob_next->_ob_prev = op->_ob_prev; |
| 330 op->_ob_prev->_ob_next = op->_ob_next; | |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
331 op->_ob_next = op->_ob_prev = NULL; |
| 86 | 332 } |
| 333 | |
| 334 DELREF(op) | |
| 335 object *op; | |
| 336 { | |
| 337 UNREF(op); | |
| 6 | 338 (*(op)->ob_type->tp_dealloc)(op); |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
339 op->ob_type = NULL; |
| 6 | 340 } |
| 341 | |
| 342 printrefs(fp) | |
| 343 FILE *fp; | |
| 344 { | |
| 345 object *op; | |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
346 fprintf(fp, "Remaining objects (except strings referenced once):\n"); |
| 6 | 347 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
|
1017
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
348 if (op->ob_refcnt == 1 && is_stringobject(op)) |
|
cb7ef9bd9ede
If a type has a repr function but no print function, printing it now
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
349 continue; /* Will be printed elsewhere */ |
| 6 | 350 fprintf(fp, "[%d] ", op->ob_refcnt); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
351 if (printobject(op, fp, 0) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
352 err_clear(); |
| 6 | 353 putc('\n', fp); |
| 354 } | |
| 355 } | |
| 356 | |
| 357 #endif |
