annotate Objects/object.c @ 3216:d00a79ebb027 legacy-trunk

Added missing imgpbm and imgsgi modules
author Jack Jansen <jack.jansen@cwi.nl>
date Thu, 12 Oct 1995 10:23:27 +0000
parents 54aa87b1ccee
children 9f1b6e2db8e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
24
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
25 /* Generic object operations; and implementation of None (NoObject) */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
27 #include "allobjects.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2307
diff changeset
29 #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
30 long ref_total;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
31 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
33 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
34 These are used by the individual routines for object creation.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
35 Do not call them otherwise, they do not initialize the object! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
37 #ifdef COUNT_ALLOCS
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
38 static typeobject *type_list;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
39 extern int tuple_zero_allocs, fast_tuple_allocs;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
40 extern int quick_int_allocs, quick_neg_int_allocs;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1352
diff changeset
41 extern int null_strings, one_strings;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
42 void
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
43 dump_counts()
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
44 {
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
45 typeobject *tp;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
46
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
47 for (tp = type_list; tp; tp = tp->tp_next)
1361
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
48 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
49 tp->tp_name, tp->tp_alloc, tp->tp_free,
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
50 tp->tp_maxalloc);
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
51 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
52 fast_tuple_allocs, tuple_zero_allocs);
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
53 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
54 quick_int_allocs, quick_neg_int_allocs);
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
55 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
56 null_strings, one_strings);
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
57 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
58
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
59 PyObject *
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
60 get_counts()
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
61 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
62 PyTypeObject *tp;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
63 PyObject *result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
64 PyObject *v;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
65
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
66 result = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
67 if (result == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
68 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
69 for (tp = type_list; tp; tp = tp->tp_next) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
70 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
71 tp->tp_free, tp->tp_maxalloc);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
72 if (v == NULL) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
73 Py_DECREF(result);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
74 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
75 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
76 if (PyList_Append(result, v) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
77 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
78 Py_DECREF(result);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
79 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
80 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
81 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
82 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
83 return result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
84 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
85
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
86 void
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
87 inc_count(tp)
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
88 typeobject *tp;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
89 {
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
90 if (tp->tp_alloc == 0) {
2639
f11ba9cc342f change in counting freed objects
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
91 /* first time; insert in linked list */
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
92 if (tp->tp_next != NULL) /* sanity check */
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
93 fatal("XXX inc_count sanity check");
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
94 tp->tp_next = type_list;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
95 type_list = tp;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
96 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
97 tp->tp_alloc++;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
98 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
99 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
100 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
101 #endif
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
102
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 newobject(tp)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 typeobject *tp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 object *op = (object *) malloc(tp->tp_basicsize);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if (op == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return err_nomem();
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
110 op->ob_type = tp;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 NEWREF(op);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 return op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 varobject *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 newvarobject(tp, size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 typeobject *tp;
2307
5aee2c5d8dff make size arg signed
Guido van Rossum <guido@python.org>
parents: 2237
diff changeset
118 int size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 varobject *op = (varobject *)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if (op == NULL)
231
b31abf420e61 Enabled (and fixed) newvarobject()
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
123 return (varobject *)err_nomem();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 op->ob_type = tp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 op->ob_size = size;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
126 NEWREF(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 return op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
130 int
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 printobject(op, fp, flags)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 int flags;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
136 int ret = 0;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
137 if (sigcheck())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
138 return -1;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
139 if (op == NULL) {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
140 fprintf(fp, "<nil>");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
142 else {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 }
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
151 else {
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
152 object *s;
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
153 if (flags & PRINT_RAW)
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
154 s = strobject(op);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
155 else
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
156 s = reprobject(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
157 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
158 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
159 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
160 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
161 "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
162 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
163 }
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
164 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
165 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
166 }
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
167 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
168 }
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
169 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
170 else
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
171 ret = (*op->ob_type->tp_print)(op, fp, flags);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 }
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
173 if (ret == 0) {
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
174 if (ferror(fp)) {
473
4a7f0f06cd27 Use IOError.
Guido van Rossum <guido@python.org>
parents: 322
diff changeset
175 err_errno(IOError);
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
176 clearerr(fp);
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
177 ret = -1;
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
178 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
179 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
180 return ret;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 reprobject(v)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 object *v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 {
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
187 if (sigcheck())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
188 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
189 if (v == NULL)
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
190 return newstringobject("<NULL>");
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
191 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
192 char buf[120];
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
193 sprintf(buf, "<%.80s object at %lx>",
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
194 v->ob_type->tp_name, (long)v);
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
195 return newstringobject(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
197 else
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
198 return (*v->ob_type->tp_repr)(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
201 object *
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
202 strobject(v)
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
203 object *v;
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
204 {
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
205 if (v == NULL)
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
206 return newstringobject("<NULL>");
2827
ff1faa4cc1df args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents: 2639
diff changeset
207 else if (is_stringobject(v)) {
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
208 INCREF(v);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
209 return v;
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
210 }
2160
899b4e3dfd4c fix strobject() behavior
Guido van Rossum <guido@python.org>
parents: 2124
diff changeset
211 else if (v->ob_type->tp_str != NULL)
899b4e3dfd4c fix strobject() behavior
Guido van Rossum <guido@python.org>
parents: 2124
diff changeset
212 return (*v->ob_type->tp_str)(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
213 else {
2160
899b4e3dfd4c fix strobject() behavior
Guido van Rossum <guido@python.org>
parents: 2124
diff changeset
214 object *func;
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
215 object *res;
2827
ff1faa4cc1df args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents: 2639
diff changeset
216 if (!is_instanceobject(v) ||
ff1faa4cc1df args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents: 2639
diff changeset
217 (func = getattr(v, "__str__")) == NULL) {
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
218 err_clear();
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
219 return reprobject(v);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
220 }
2827
ff1faa4cc1df args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents: 2639
diff changeset
221 res = call_object(func, (object *)NULL);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
222 DECREF(func);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
223 return res;
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
224 }
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
225 }
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
226
2124
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
227 static object *
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
228 do_cmp(v, w)
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
229 object *v, *w;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
230 {
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
231 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
232 because the check in cmpobject() reverses the objects first.
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
233 This is intentional -- it makes no sense to define cmp(x,y) different
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
234 than -cmp(y,x). */
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
235 if (is_instanceobject(v) || is_instanceobject(w))
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
236 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
237 return newintobject((long)cmpobject(v, w));
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
238 }
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
239
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 cmpobject(v, w)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 object *v, *w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 typeobject *tp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 if (v == w)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 if (v == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 if (w == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 return 1;
2124
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
251 if (is_instanceobject(v) || is_instanceobject(w)) {
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
252 object *res;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
253 int c;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
254 if (!is_instanceobject(v))
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
255 return -cmpobject(w, v);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
256 res = do_cmp(v, w);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
257 if (res == NULL) {
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
258 err_clear();
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
259 return (v < w) ? -1 : 1;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
260 }
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
261 if (!is_intobject(res)) {
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
262 DECREF(res);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
263 return (v < w) ? -1 : 1;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
264 }
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
265 c = getintvalue(res);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
266 DECREF(res);
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
267 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
268 }
307
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
269 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
270 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
271 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
272 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
273 err_clear();
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
274 /* 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
275 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
276 }
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
277 else {
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
278 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
279 DECREF(v);
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
280 DECREF(w);
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
281 return cmp;
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
282 }
35a2528f8a73 Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
283 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 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
285 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 if (tp->tp_compare == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 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
288 return (*tp->tp_compare)(v, w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
291 long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
292 hashobject(v)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
293 object *v;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
294 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
295 typeobject *tp = v->ob_type;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
296 if (tp->tp_hash != NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
297 return (*tp->tp_hash)(v);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
298 if (tp->tp_compare == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
299 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
300 /* 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
301 err_setstr(TypeError, "unhashable type");
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
302 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
303 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
304
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
305 object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
306 getattr(v, name)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
307 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
308 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
309 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
310 if (v->ob_type->tp_getattr == NULL) {
2079
5bbf0a9c9a42 attribute-less object is AttributeError, not TypeError
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
311 err_setstr(AttributeError, "attribute-less object");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
312 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
313 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
314 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
315 return (*v->ob_type->tp_getattr)(v, name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
316 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
317 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
318
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
319 int
1324
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
320 hasattr(v, name)
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
321 object *v;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
322 char *name;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
323 {
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
324 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
325 if (res != NULL) {
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
326 DECREF(res);
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
327 return 1;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
328 }
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
329 err_clear();
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
330 return 0;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
331 }
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
332
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
333 int
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
334 setattr(v, name, w)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
335 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
336 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
337 object *w;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
338 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
339 if (v->ob_type->tp_setattr == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
340 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
341 err_setstr(TypeError,
8414e1f9a5fe Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents: 473
diff changeset
342 "attribute-less object (assign or del)");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
343 else
512
8414e1f9a5fe Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents: 473
diff changeset
344 err_setstr(TypeError,
8414e1f9a5fe Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents: 473
diff changeset
345 "object has read-only attributes");
95
a5bf7d5959ce Return -1 for errors.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
346 return -1;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
347 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
348 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
349 return (*v->ob_type->tp_setattr)(v, name, w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
350 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
351 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
352
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
353 /* 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
354 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
355
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
356 int
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
357 testbool(v)
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
358 object *v;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
359 {
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
360 int res;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
361 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
362 res = 0;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
363 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
364 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
365 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
366 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
367 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
368 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
369 else
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
370 res = 1;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
371 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
372 res = 1;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
373 return res;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
374 }
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
375
2113
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
376 /* Coerce two numeric types to the "larger" one.
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
377 Increment the reference count on each argument.
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
378 Return -1 and raise an exception if no coercion is possible
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
379 (and then no reference count is incremented).
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
380 */
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
381
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
382 int
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
383 coerce(pv, pw)
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
384 object **pv, **pw;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
385 {
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
386 register object *v = *pv;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
387 register object *w = *pw;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
388 int res;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
389
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
390 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
391 INCREF(v);
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
392 INCREF(w);
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
393 return 0;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
394 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
395 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
396 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
397 if (res <= 0)
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
398 return res;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
399 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
400 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
401 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
402 if (res <= 0)
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
403 return res;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
404 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
405 err_setstr(TypeError, "number coercion failed");
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
406 return -1;
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
407 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
408
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
410 /* Test whether an object can be called */
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
411
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
412 int
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
413 callable(x)
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
414 object *x;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
415 {
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
416 if (x == NULL)
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
417 return 0;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
418 if (x->ob_type->tp_call != NULL ||
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
419 is_funcobject(x) ||
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
420 is_instancemethodobject(x) ||
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
421 is_methodobject(x) ||
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
422 is_classobject(x))
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
423 return 1;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
424 if (is_instanceobject(x)) {
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
425 object *call = getattr(x, "__call__");
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
426 if (call == NULL) {
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
427 err_clear();
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
428 return 0;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
429 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
430 /* Could test recursively but don't, for fear of endless
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
431 recursion if some joker sets self.__call__ = self */
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
432 DECREF(call);
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
433 return 1;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
434 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
435 return 0;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
436 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
437
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
438
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 /*
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 NoObject is usable as a non-NULL undefined value, used by the macro None.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 There is (and should be!) no way to create other objects of this type,
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
442 so there is exactly one (which is indestructible, by the way).
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444
739
ab1b3486b226 Fix lint bug
Guido van Rossum <guido@python.org>
parents: 512
diff changeset
445 /* ARGSUSED */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
446 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
447 none_repr(op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
448 object *op;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
449 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
450 return newstringobject("None");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 static typeobject Notype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 0,
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
456 "None",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 0, /*tp_dealloc*/ /*never called*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 1017
diff changeset
460 0, /*tp_print*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
461 0, /*tp_getattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
462 0, /*tp_setattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
463 0, /*tp_compare*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
464 (reprfunc)none_repr, /*tp_repr*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
465 0, /*tp_as_number*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
466 0, /*tp_as_sequence*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
467 0, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
468 0, /*tp_hash */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 object NoObject = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 OB_HEAD_INIT(&Notype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
475
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 #ifdef TRACE_REFS
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 static object refchain = {&refchain, &refchain};
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
479
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 NEWREF(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 ref_total++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 op->ob_refcnt = 1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 op->_ob_next = refchain._ob_next;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 op->_ob_prev = &refchain;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 refchain._ob_next->_ob_prev = op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 refchain._ob_next = op;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
489 #ifdef COUNT_ALLOCS
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
490 inc_count(op->ob_type);
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
491 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
493
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
494 UNREF(op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
495 register object *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
497 register object *p;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
498 if (op->ob_refcnt < 0)
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
499 fatal("UNREF negative refcnt");
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
500 if (op == &refchain ||
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
501 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
502 fatal("UNREF invalid object");
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
503 #ifdef SLOW_UNREF_CHECK
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
504 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
505 if (p == op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
506 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
507 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
508 if (p == &refchain) /* Not found */
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
509 fatal("UNREF unknown object");
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
510 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 op->_ob_next->_ob_prev = op->_ob_prev;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 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
513 op->_ob_next = op->_ob_prev = NULL;
2639
f11ba9cc342f change in counting freed objects
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
514 #ifdef COUNT_ALLOCS
f11ba9cc342f change in counting freed objects
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
515 op->ob_type->tp_free++;
f11ba9cc342f change in counting freed objects
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
516 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
517 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
518
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
519 DELREF(op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
520 object *op;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
521 {
1869
2cede61e9b1b rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
522 destructor dealloc = op->ob_type->tp_dealloc;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
523 UNREF(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
524 op->ob_type = NULL;
1869
2cede61e9b1b rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
525 (*dealloc)(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 printrefs(fp)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 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
532 fprintf(fp, "Remaining objects (except strings referenced once):\n");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 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
534 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
535 continue; /* Will be printed elsewhere */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 fprintf(fp, "[%d] ", op->ob_refcnt);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
537 if (printobject(op, fp, 0) != 0)
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
538 err_clear();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 putc('\n', fp);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
543 PyObject *
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
544 getobjects(self, args)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
545 PyObject *self;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
546 PyObject *args;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
547 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
548 int i, n;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
549 PyObject *t = NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
550 PyObject *res, *op;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
551
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
552 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
553 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
554 op = refchain._ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
555 res = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
556 if (res == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
557 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
558 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
559 while (op == self || op == args || op == res || op == t ||
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
560 t != NULL && op->ob_type != (PyTypeObject *) t) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
561 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
562 if (op == &refchain)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
563 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
564 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
565 if (PyList_Append(res, op) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
566 Py_DECREF(res);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
567 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
568 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
569 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
570 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
571 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
572 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
573
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 #endif