Mercurial > cpython-lru_cache-weakref
annotate Objects/object.c @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | cca84745da3e |
| children | 54bffdddc0ef |
| rev | line source |
|---|---|
| 145 | 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 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4288
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 86 | 32 /* Generic object operations; and implementation of None (NoObject) */ |
| 6 | 33 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
34 #include "Python.h" |
| 6 | 35 |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
36 #include "mymath.h" |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
37 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
38 /* just for trashcan: */ |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
39 #include "compile.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
40 #include "frameobject.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
41 #include "traceback.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
42 |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2307
diff
changeset
|
43 #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) |
|
11673
5e6e9e893acd
The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents:
11649
diff
changeset
|
44 DL_IMPORT(long) _Py_RefTotal; |
| 86 | 45 #endif |
| 6 | 46 |
| 86 | 47 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 48 These are used by the individual routines for object creation. | |
| 49 Do not call them otherwise, they do not initialize the object! */ | |
| 6 | 50 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
51 #ifdef COUNT_ALLOCS |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
52 static PyTypeObject *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
|
53 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
|
54 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
|
55 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
|
56 void |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
57 dump_counts() |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
58 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
59 PyTypeObject *tp; |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
60 |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 null_strings, one_strings); |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
71 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
72 |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
73 PyObject * |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
74 get_counts() |
|
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 PyTypeObject *tp; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
77 PyObject *result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
78 PyObject *v; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
79 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
80 result = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
81 if (result == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
82 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
83 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
|
84 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
|
85 tp->tp_free, tp->tp_maxalloc); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
86 if (v == NULL) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
87 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
88 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
89 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
90 if (PyList_Append(result, v) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
91 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
92 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
93 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
94 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
95 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
96 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
97 return result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
98 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
99 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
100 void |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
101 inc_count(tp) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
102 PyTypeObject *tp; |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
103 { |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
104 if (tp->tp_alloc == 0) { |
|
2639
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
105 /* 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
|
106 if (tp->tp_next != NULL) /* sanity check */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
107 Py_FatalError("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
|
108 tp->tp_next = type_list; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
109 type_list = tp; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
110 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
111 tp->tp_alloc++; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
112 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
|
113 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
|
114 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
115 #endif |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
116 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
117 PyObject * |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
118 PyObject_Init(op, tp) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
119 PyObject *op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
120 PyTypeObject *tp; |
| 6 | 121 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
122 if (op == NULL) { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
123 PyErr_SetString(PyExc_SystemError, |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
124 "NULL object passed to PyObject_Init"); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
125 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
126 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
127 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
128 op->ob_type = tp; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
129 _Py_NewReference(op); |
| 6 | 130 return op; |
| 131 } | |
| 132 | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
133 PyVarObject * |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
134 PyObject_InitVar(op, tp, size) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
135 PyVarObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
136 PyTypeObject *tp; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
137 int size; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
138 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
139 if (op == NULL) { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
140 PyErr_SetString(PyExc_SystemError, |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
141 "NULL object passed to PyObject_InitVar"); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
142 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
143 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
144 /* Any changes should be reflected in PyObject_INIT_VAR */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
145 op->ob_size = size; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
146 op->ob_type = tp; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
147 _Py_NewReference((PyObject *)op); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
148 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
149 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
150 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
151 PyObject * |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
152 _PyObject_New(tp) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
153 PyTypeObject *tp; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
154 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
155 PyObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
156 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
157 if (op == NULL) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
158 return PyErr_NoMemory(); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
159 return PyObject_INIT(op, tp); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
160 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
161 |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
162 PyVarObject * |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
163 _PyObject_NewVar(tp, size) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
164 PyTypeObject *tp; |
| 2307 | 165 int size; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
166 { |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
167 PyVarObject *op; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
168 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); |
| 6 | 169 if (op == NULL) |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
170 return (PyVarObject *)PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
171 return PyObject_INIT_VAR(op, tp, size); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
172 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
173 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
174 void |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
175 _PyObject_Del(op) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
176 PyObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
177 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
178 PyObject_FREE(op); |
| 6 | 179 } |
| 180 | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
181 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
182 PyObject_Print(op, fp, flags) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
183 PyObject *op; |
| 6 | 184 FILE *fp; |
| 185 int flags; | |
| 186 { | |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
187 int ret = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
188 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
189 return -1; |
|
8181
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
190 #ifdef USE_STACKCHECK |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
191 if (PyOS_CheckStack()) { |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
192 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
193 return -1; |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
194 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
195 #endif |
|
11649
38560a3a3249
On Linux, one sometimes sees spurious errors after interrupting
Guido van Rossum <guido@python.org>
parents:
8695
diff
changeset
|
196 clearerr(fp); /* Clear any previous error condition */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
197 if (op == NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
198 fprintf(fp, "<nil>"); |
| 6 | 199 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
200 else { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 } |
|
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
|
209 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
210 PyObject *s; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
211 if (flags & Py_PRINT_RAW) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
212 s = PyObject_Str(op); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
213 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
214 s = PyObject_Repr(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
|
215 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
|
216 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
|
217 else { |
|
8111
5c9be98f5347
Be less naive about null characters in an object's repr().
Guido van Rossum <guido@python.org>
parents:
8006
diff
changeset
|
218 ret = PyObject_Print(s, fp, |
|
5c9be98f5347
Be less naive about null characters in an object's repr().
Guido van Rossum <guido@python.org>
parents:
8006
diff
changeset
|
219 Py_PRINT_RAW); |
|
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
|
220 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
221 Py_XDECREF(s); |
|
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
|
222 } |
|
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
|
223 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
224 else |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
225 ret = (*op->ob_type->tp_print)(op, fp, flags); |
| 6 | 226 } |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
227 if (ret == 0) { |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
228 if (ferror(fp)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
229 PyErr_SetFromErrno(PyExc_IOError); |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
230 clearerr(fp); |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
231 ret = -1; |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
232 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
233 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
234 return ret; |
| 6 | 235 } |
| 236 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
237 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
238 PyObject_Repr(v) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
239 PyObject *v; |
| 6 | 240 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
241 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
242 return NULL; |
|
8181
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
243 #ifdef USE_STACKCHECK |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
244 if (PyOS_CheckStack()) { |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
245 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
246 return NULL; |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
247 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
248 #endif |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
249 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
250 return PyString_FromString("<NULL>"); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
251 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
|
252 char buf[120]; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
253 sprintf(buf, "<%.80s object at %lx>", |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
254 v->ob_type->tp_name, (long)v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
255 return PyString_FromString(buf); |
| 6 | 256 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
257 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
258 PyObject *res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
259 res = (*v->ob_type->tp_repr)(v); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
260 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
261 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
262 if (!PyString_Check(res)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
263 PyErr_Format(PyExc_TypeError, |
|
12320
5de0b939fe61
Skip Montanaro: add string precisions to calls to PyErr_Format
Guido van Rossum <guido@python.org>
parents:
12052
diff
changeset
|
264 "__repr__ returned non-string (type %.200s)", |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
265 res->ob_type->tp_name); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
266 Py_DECREF(res); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
267 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
268 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
269 return res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
270 } |
| 6 | 271 } |
| 272 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
273 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
274 PyObject_Str(v) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
275 PyObject *v; |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
276 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
277 PyObject *res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
278 |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
279 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
280 return PyString_FromString("<NULL>"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
281 else if (PyString_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
282 Py_INCREF(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
283 return v; |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
284 } |
|
2160
899b4e3dfd4c
fix strobject() behavior
Guido van Rossum <guido@python.org>
parents:
2124
diff
changeset
|
285 else if (v->ob_type->tp_str != NULL) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
286 res = (*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
|
287 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
288 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
289 if (!PyInstance_Check(v) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
290 (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
291 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
292 return PyObject_Repr(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
293 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
294 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
295 Py_DECREF(func); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
296 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
297 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
298 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
299 if (!PyString_Check(res)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
300 PyErr_Format(PyExc_TypeError, |
|
12320
5de0b939fe61
Skip Montanaro: add string precisions to calls to PyErr_Format
Guido van Rossum <guido@python.org>
parents:
12052
diff
changeset
|
301 "__str__ returned non-string (type %.200s)", |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
302 res->ob_type->tp_name); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
303 Py_DECREF(res); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
304 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
305 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
306 return res; |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
307 } |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
308 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
309 static PyObject * |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
310 do_cmp(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
311 PyObject *v, *w; |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
312 { |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
313 long c; |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
314 /* __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
|
315 because the check in cmpobject() reverses the objects first. |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
316 This is intentional -- it makes no sense to define cmp(x,y) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
317 different than -cmp(y,x). */ |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
318 if (PyInstance_Check(v) || PyInstance_Check(w)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
319 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
320 c = PyObject_Compare(v, w); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
321 if (c && PyErr_Occurred()) |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
322 return NULL; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
323 return PyInt_FromLong(c); |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
324 } |
|
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
325 |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
326 PyObject *_PyCompareState_Key; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
327 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
328 /* _PyCompareState_nesting is incremented beforing call compare (for |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
329 some types) and decremented on exit. If the count exceeds the |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
330 nesting limit, enable code to detect circular data structures. |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
331 */ |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
332 #define NESTING_LIMIT 500 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
333 int _PyCompareState_nesting = 0; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
334 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
335 static PyObject* |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
336 get_inprogress_dict() |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
337 { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
338 PyObject *tstate_dict, *inprogress; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
339 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
340 tstate_dict = PyThreadState_GetDict(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
341 if (tstate_dict == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
342 PyErr_BadInternalCall(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
343 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
344 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
345 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
346 if (inprogress == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
347 PyErr_Clear(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
348 inprogress = PyDict_New(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
349 if (inprogress == NULL) |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
350 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
351 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key, |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
352 inprogress) == -1) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
353 Py_DECREF(inprogress); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
354 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
355 } |
|
12904
752aa8dff520
the PyDict_SetItem does not borrow a reference, so we need to decref
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
356 Py_DECREF(inprogress); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
357 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
358 return inprogress; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
359 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
360 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
361 static PyObject * |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
362 make_pair(v, w) |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
363 PyObject *v, *w; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
364 { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
365 PyObject *pair; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
366 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
367 pair = PyTuple_New(2); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
368 if (pair == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
369 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
370 } |
|
13089
bf1ebe869878
Trent Mick: change a few casts for Win64 compatibility.
Guido van Rossum <guido@python.org>
parents:
12991
diff
changeset
|
371 if (v <= w) { |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
372 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v)); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
373 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w)); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
374 } else { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
375 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w)); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
376 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v)); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
377 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
378 return pair; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
379 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
380 |
| 6 | 381 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
382 PyObject_Compare(v, w) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
383 PyObject *v, *w; |
| 6 | 384 { |
|
8472
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
385 PyTypeObject *vtp, *wtp; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
386 int result; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
387 |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
388 if (v == NULL || w == NULL) { |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
389 PyErr_BadInternalCall(); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
390 return -1; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
391 } |
| 6 | 392 if (v == w) |
| 393 return 0; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
394 if (PyInstance_Check(v) || PyInstance_Check(w)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
395 PyObject *res; |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
396 int c; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
397 if (!PyInstance_Check(v)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
398 return -PyObject_Compare(w, v); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
399 if (++_PyCompareState_nesting > NESTING_LIMIT) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
400 PyObject *inprogress, *pair; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
401 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
402 inprogress = get_inprogress_dict(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
403 if (inprogress == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
404 return -1; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
405 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
406 pair = make_pair(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
407 if (PyDict_GetItem(inprogress, pair)) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
408 /* already comparing these objects. assume |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
409 they're equal until shown otherwise */ |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
410 Py_DECREF(pair); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
411 --_PyCompareState_nesting; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
412 return 0; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
413 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
414 if (PyDict_SetItem(inprogress, pair, pair) == -1) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
415 return -1; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
416 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
417 res = do_cmp(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
418 _PyCompareState_nesting--; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
419 /* XXX DelItem shouldn't fail */ |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
420 PyDict_DelItem(inprogress, pair); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
421 Py_DECREF(pair); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
422 } else { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
423 res = do_cmp(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
424 } |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
425 if (res == NULL) |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
426 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
427 if (!PyInt_Check(res)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
428 Py_DECREF(res); |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
429 PyErr_SetString(PyExc_TypeError, |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
430 "comparison did not return an int"); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
431 return -1; |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
432 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
433 c = PyInt_AsLong(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
434 Py_DECREF(res); |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
435 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
|
436 } |
|
8472
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
437 if ((vtp = v->ob_type) != (wtp = w->ob_type)) { |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
438 char *vname = vtp->tp_name; |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
439 char *wname = wtp->tp_name; |
|
8695
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
440 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) { |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
441 int err; |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
442 err = PyNumber_CoerceEx(&v, &w); |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
443 if (err < 0) |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
444 return -1; |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
445 else if (err == 0) { |
|
8695
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
446 int cmp; |
|
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
447 vtp = v->ob_type; |
|
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
448 if (vtp->tp_compare == NULL) |
|
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
449 cmp = (v < w) ? -1 : 1; |
|
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
450 else |
|
6d2168a9641f
When comparing objects, always check that tp_compare is not NULL
Guido van Rossum <guido@python.org>
parents:
8472
diff
changeset
|
451 cmp = (*vtp->tp_compare)(v, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
452 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
453 Py_DECREF(w); |
|
307
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
454 return cmp; |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
455 } |
|
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
456 } |
| 12327 | 457 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 458 int result = PyUnicode_Compare(v, w); | |
| 459 if (result == -1 && PyErr_Occurred() && | |
| 460 PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 461 /* TypeErrors are ignored: if Unicode coercion | |
| 462 fails due to one of the arguments not | |
| 463 having the right type, we continue as | |
| 464 defined by the coercion protocol (see | |
| 465 above). Luckily, decoding errors are | |
| 466 reported as ValueErrors and are not masked | |
| 467 by this technique. */ | |
| 468 PyErr_Clear(); | |
| 469 else | |
| 470 return result; | |
| 471 } | |
|
8472
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
472 else if (vtp->tp_as_number != NULL) |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
473 vname = ""; |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
474 else if (wtp->tp_as_number != NULL) |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
475 wname = ""; |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
476 /* Numerical types compare smaller than all other types */ |
|
c555c72f090d
When comparing objects of different types (which is done by comparing
Guido van Rossum <guido@python.org>
parents:
8404
diff
changeset
|
477 return strcmp(vname, wname); |
|
307
35a2528f8a73
Change cmpobject() to coerce numerical values before comparing them
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
478 } |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
479 if (vtp->tp_compare == NULL) { |
| 6 | 480 return (v < w) ? -1 : 1; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
481 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
482 if (++_PyCompareState_nesting > NESTING_LIMIT |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
483 && (vtp->tp_as_mapping |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
484 || (vtp->tp_as_sequence && !PyString_Check(v)))) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
485 PyObject *inprogress, *pair; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
486 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
487 inprogress = get_inprogress_dict(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
488 if (inprogress == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
489 return -1; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
490 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
491 pair = make_pair(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
492 if (PyDict_GetItem(inprogress, pair)) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
493 /* already comparing these objects. assume |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
494 they're equal until shown otherwise */ |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
495 _PyCompareState_nesting--; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
496 Py_DECREF(pair); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
497 return 0; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
498 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
499 if (PyDict_SetItem(inprogress, pair, pair) == -1) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
500 return -1; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
501 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
502 result = (*vtp->tp_compare)(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
503 _PyCompareState_nesting--; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
504 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */ |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
505 Py_DECREF(pair); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
506 } else { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
507 result = (*vtp->tp_compare)(v, w); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
508 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
509 return result; |
| 6 | 510 } |
| 511 | |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
512 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
513 /* Set of hash utility functions to help maintaining the invariant that |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
514 iff a==b then hash(a)==hash(b) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
515 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
516 All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
517 */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
518 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
519 long |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
520 _Py_HashDouble(v) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
521 double v; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
522 { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
523 /* Use frexp to get at the bits in the double. |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
524 * Since the VAX D double format has 56 mantissa bits, which is the |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
525 * most of any double format in use, each of these parts may have as |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
526 * many as (but no more than) 56 significant bits. |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
527 * So, assuming sizeof(long) >= 4, each part can be broken into two longs; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
528 * frexp and multiplication are used to do that. |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
529 * Also, since the Cray double format has 15 exponent bits, which is the |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
530 * most of any double format in use, shifting the exponent field left by |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
531 * 15 won't overflow a long (again assuming sizeof(long) >= 4). |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
532 */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
533 int expo; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
534 long hipart; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
535 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
536 v = frexp(v, &expo); |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
537 v = v * 2147483648.0; /* 2**31 */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
538 hipart = (long)v; /* Take the top 32 bits */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
539 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
540 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
541 return hipart + (long)v + (expo << 15); /* Combine everything */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
542 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
543 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
544 long |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
545 _Py_HashPointer(p) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
546 void *p; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
547 { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
548 #if SIZEOF_LONG >= SIZEOF_VOID_P |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
549 return (long)p; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
550 #else |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
551 /* convert to a Python long and hash that */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
552 PyObject* longobj; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
553 long x; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
554 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
555 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
556 x = -1; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
557 goto finally; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
558 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
559 x = PyObject_Hash(longobj); |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
560 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
561 finally: |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
562 Py_XDECREF(longobj); |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
563 return x; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
564 #endif |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
565 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
566 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
567 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
568 long |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
569 PyObject_Hash(v) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
570 PyObject *v; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
571 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
572 PyTypeObject *tp = v->ob_type; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
573 if (tp->tp_hash != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
574 return (*tp->tp_hash)(v); |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
575 if (tp->tp_compare == NULL) { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
576 return _Py_HashPointer(v); /* Use address as hash value */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
577 } |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
578 /* If there's a cmp but no hash defined, the object can't be hashed */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
579 PyErr_SetString(PyExc_TypeError, "unhashable type"); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
580 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
581 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
582 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
583 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
584 PyObject_GetAttrString(v, name) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
585 PyObject *v; |
| 86 | 586 char *name; |
| 587 { | |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
588 if (v->ob_type->tp_getattro != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
589 PyObject *w, *res; |
|
5006
5f4b23e3d97c
Intern the strings created in getattr() and setattr().
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
590 w = PyString_InternFromString(name); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
591 if (w == NULL) |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
592 return NULL; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
593 res = (*v->ob_type->tp_getattro)(v, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
594 Py_XDECREF(w); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
595 return res; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
596 } |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
597 |
| 86 | 598 if (v->ob_type->tp_getattr == NULL) { |
|
7119
516189984b0c
Instead of "attribute-less object", issue an error message that
Guido van Rossum <guido@python.org>
parents:
6597
diff
changeset
|
599 PyErr_Format(PyExc_AttributeError, |
|
516189984b0c
Instead of "attribute-less object", issue an error message that
Guido van Rossum <guido@python.org>
parents:
6597
diff
changeset
|
600 "'%.50s' object has no attribute '%.400s'", |
|
516189984b0c
Instead of "attribute-less object", issue an error message that
Guido van Rossum <guido@python.org>
parents:
6597
diff
changeset
|
601 v->ob_type->tp_name, |
|
516189984b0c
Instead of "attribute-less object", issue an error message that
Guido van Rossum <guido@python.org>
parents:
6597
diff
changeset
|
602 name); |
| 86 | 603 return NULL; |
| 604 } | |
| 605 else { | |
| 606 return (*v->ob_type->tp_getattr)(v, name); | |
| 607 } | |
| 608 } | |
| 609 | |
| 610 int | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
611 PyObject_HasAttrString(v, name) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
612 PyObject *v; |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
613 char *name; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
614 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
615 PyObject *res = PyObject_GetAttrString(v, name); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
616 if (res != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
617 Py_DECREF(res); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
618 return 1; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
619 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
620 PyErr_Clear(); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
621 return 0; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
622 } |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
623 |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
624 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
625 PyObject_SetAttrString(v, name, w) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
626 PyObject *v; |
| 86 | 627 char *name; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
628 PyObject *w; |
| 86 | 629 { |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
630 if (v->ob_type->tp_setattro != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
631 PyObject *s; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
632 int res; |
|
5006
5f4b23e3d97c
Intern the strings created in getattr() and setattr().
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
633 s = PyString_InternFromString(name); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
634 if (s == NULL) |
|
4288
43ae045aa10e
Correct typo in setattr: return -1 for error, not NULL
Guido van Rossum <guido@python.org>
parents:
4002
diff
changeset
|
635 return -1; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
636 res = (*v->ob_type->tp_setattro)(v, s, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
637 Py_XDECREF(s); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
638 return res; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
639 } |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
640 |
| 86 | 641 if (v->ob_type->tp_setattr == NULL) { |
| 642 if (v->ob_type->tp_getattr == NULL) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
643 PyErr_SetString(PyExc_TypeError, |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
644 "attribute-less object (assign or del)"); |
| 86 | 645 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
646 PyErr_SetString(PyExc_TypeError, |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
647 "object has read-only attributes"); |
| 95 | 648 return -1; |
| 86 | 649 } |
| 650 else { | |
| 651 return (*v->ob_type->tp_setattr)(v, name, w); | |
| 652 } | |
| 653 } | |
| 654 | |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
655 PyObject * |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
656 PyObject_GetAttr(v, name) |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
657 PyObject *v; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
658 PyObject *name; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
659 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
660 if (v->ob_type->tp_getattro != NULL) |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
661 return (*v->ob_type->tp_getattro)(v, name); |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
662 |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
663 if (!PyString_Check(name)) { |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
664 PyErr_SetString(PyExc_TypeError, |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
665 "attribute name must be string"); |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
666 return NULL; |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
667 } |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
668 return PyObject_GetAttrString(v, PyString_AS_STRING(name)); |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
669 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
670 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
671 int |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
672 PyObject_HasAttr(v, name) |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
673 PyObject *v; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
674 PyObject *name; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
675 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
676 PyObject *res = PyObject_GetAttr(v, name); |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
677 if (res != NULL) { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
678 Py_DECREF(res); |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
679 return 1; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
680 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
681 PyErr_Clear(); |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
682 return 0; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
683 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
684 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
685 int |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
686 PyObject_SetAttr(v, name, value) |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
687 PyObject *v; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
688 PyObject *name; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
689 PyObject *value; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
690 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
691 int err; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
692 Py_INCREF(name); |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
693 if (PyString_Check(name)) |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
694 PyString_InternInPlace(&name); |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
695 if (v->ob_type->tp_setattro != NULL) |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
696 err = (*v->ob_type->tp_setattro)(v, name, value); |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
697 else if (PyString_Check(name)) { |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
698 err = PyObject_SetAttrString( |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
699 v, PyString_AS_STRING(name), value); |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
700 } |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
701 else { |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
702 PyErr_SetString(PyExc_TypeError, |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
703 "attribute name must be string"); |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
704 err = -1; |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
705 } |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
706 Py_DECREF(name); |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
707 return err; |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
708 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
709 |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
710 /* 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
|
711 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
|
712 |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
713 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
714 PyObject_IsTrue(v) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
715 PyObject *v; |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
716 { |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
717 int res; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
718 if (v == Py_None) |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
719 res = 0; |
|
8404
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
720 else if (v->ob_type->tp_as_number != NULL && |
|
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
721 v->ob_type->tp_as_number->nb_nonzero != NULL) |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
722 res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
|
8404
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
723 else if (v->ob_type->tp_as_mapping != NULL && |
|
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
724 v->ob_type->tp_as_mapping->mp_length != NULL) |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
725 res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
|
8404
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
726 else if (v->ob_type->tp_as_sequence != NULL && |
|
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
727 v->ob_type->tp_as_sequence->sq_length != NULL) |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
728 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
|
729 else |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
730 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
731 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
|
732 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
733 return res; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
734 } |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
735 |
| 7935 | 736 /* equivalent of 'not v' |
| 737 Return -1 if an error occurred */ | |
| 738 | |
| 739 int | |
| 740 PyObject_Not(v) | |
| 741 PyObject *v; | |
| 742 { | |
| 743 int res; | |
| 744 res = PyObject_IsTrue(v); | |
| 745 if (res < 0) | |
| 746 return res; | |
| 747 return res == 0; | |
| 748 } | |
| 749 | |
|
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
|
750 /* 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
|
751 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
|
752 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
|
753 (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
|
754 */ |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
755 |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
756 int |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
757 PyNumber_CoerceEx(pv, pw) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
758 PyObject **pv, **pw; |
|
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
|
759 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
760 register PyObject *v = *pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
761 register PyObject *w = *pw; |
|
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
|
762 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
|
763 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
764 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
765 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
766 Py_INCREF(w); |
|
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
|
767 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
|
768 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
769 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
|
770 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
|
771 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
|
772 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
|
773 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
774 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
|
775 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
|
776 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
|
777 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
|
778 } |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
779 return 1; |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
780 } |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
781 |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
782 int |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
783 PyNumber_Coerce(pv, pw) |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
784 PyObject **pv, **pw; |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
785 { |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
786 int err = PyNumber_CoerceEx(pv, pw); |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
787 if (err <= 0) |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
788 return err; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
789 PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
|
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
|
790 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
|
791 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
792 |
| 6 | 793 |
| 2237 | 794 /* Test whether an object can be called */ |
| 795 | |
| 796 int | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
797 PyCallable_Check(x) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
798 PyObject *x; |
| 2237 | 799 { |
| 800 if (x == NULL) | |
| 801 return 0; | |
| 802 if (x->ob_type->tp_call != NULL || | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
803 PyFunction_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
804 PyMethod_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
805 PyCFunction_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
806 PyClass_Check(x)) |
| 2237 | 807 return 1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
808 if (PyInstance_Check(x)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
809 PyObject *call = PyObject_GetAttrString(x, "__call__"); |
| 2237 | 810 if (call == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
811 PyErr_Clear(); |
| 2237 | 812 return 0; |
| 813 } | |
| 814 /* Could test recursively but don't, for fear of endless | |
| 815 recursion if some joker sets self.__call__ = self */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
816 Py_DECREF(call); |
| 2237 | 817 return 1; |
| 818 } | |
| 819 return 0; | |
| 820 } | |
| 821 | |
| 822 | |
| 6 | 823 /* |
| 824 NoObject is usable as a non-NULL undefined value, used by the macro None. | |
| 825 There is (and should be!) no way to create other objects of this type, | |
| 86 | 826 so there is exactly one (which is indestructible, by the way). |
| 6 | 827 */ |
| 828 | |
| 739 | 829 /* ARGSUSED */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
830 static PyObject * |
| 86 | 831 none_repr(op) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
832 PyObject *op; |
| 86 | 833 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
834 return PyString_FromString("None"); |
| 6 | 835 } |
| 836 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
837 static PyTypeObject PyNothing_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
838 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 839 0, |
| 86 | 840 "None", |
| 6 | 841 0, |
| 842 0, | |
| 843 0, /*tp_dealloc*/ /*never called*/ | |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1017
diff
changeset
|
844 0, /*tp_print*/ |
| 86 | 845 0, /*tp_getattr*/ |
| 846 0, /*tp_setattr*/ | |
| 847 0, /*tp_compare*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
848 (reprfunc)none_repr, /*tp_repr*/ |
| 86 | 849 0, /*tp_as_number*/ |
| 850 0, /*tp_as_sequence*/ | |
| 851 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
852 0, /*tp_hash */ |
| 6 | 853 }; |
| 854 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
855 PyObject _Py_NoneStruct = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
856 PyObject_HEAD_INIT(&PyNothing_Type) |
| 6 | 857 }; |
| 858 | |
| 859 | |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
860 #ifdef Py_TRACE_REFS |
| 6 | 861 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
862 static PyObject refchain = {&refchain, &refchain}; |
| 6 | 863 |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
864 void |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
865 _Py_ResetReferences() |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
866 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
867 refchain._ob_prev = refchain._ob_next = &refchain; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
868 _Py_RefTotal = 0; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
869 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
870 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
871 void |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
872 _Py_NewReference(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
873 PyObject *op; |
| 6 | 874 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
875 _Py_RefTotal++; |
| 6 | 876 op->ob_refcnt = 1; |
| 877 op->_ob_next = refchain._ob_next; | |
| 878 op->_ob_prev = &refchain; | |
| 879 refchain._ob_next->_ob_prev = op; | |
| 880 refchain._ob_next = op; | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
881 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
882 inc_count(op->ob_type); |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
883 #endif |
| 6 | 884 } |
| 885 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
886 void |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
887 _Py_ForgetReference(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
888 register PyObject *op; |
| 6 | 889 { |
|
11673
5e6e9e893acd
The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents:
11649
diff
changeset
|
890 #ifdef SLOW_UNREF_CHECK |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
891 register PyObject *p; |
|
11673
5e6e9e893acd
The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents:
11649
diff
changeset
|
892 #endif |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1869
diff
changeset
|
893 if (op->ob_refcnt < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
894 Py_FatalError("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
|
895 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
|
896 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
897 Py_FatalError("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
|
898 #ifdef SLOW_UNREF_CHECK |
| 86 | 899 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 900 if (p == op) | |
| 901 break; | |
| 902 } | |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1869
diff
changeset
|
903 if (p == &refchain) /* Not found */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
904 Py_FatalError("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
|
905 #endif |
| 6 | 906 op->_ob_next->_ob_prev = op->_ob_prev; |
| 907 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
|
908 op->_ob_next = op->_ob_prev = NULL; |
|
2639
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
909 #ifdef COUNT_ALLOCS |
|
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
910 op->ob_type->tp_free++; |
|
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
911 #endif |
| 86 | 912 } |
| 913 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
914 void |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
915 _Py_Dealloc(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
916 PyObject *op; |
| 86 | 917 { |
|
1869
2cede61e9b1b
rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
918 destructor dealloc = op->ob_type->tp_dealloc; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
919 _Py_ForgetReference(op); |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
920 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
921 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
|
922 (*dealloc)(op); |
| 6 | 923 } |
| 924 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
925 void |
|
3609
0b0c4ed1936a
rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents:
3574
diff
changeset
|
926 _Py_PrintReferences(fp) |
| 6 | 927 FILE *fp; |
| 928 { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
929 PyObject *op; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
930 fprintf(fp, "Remaining objects:\n"); |
| 6 | 931 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 932 fprintf(fp, "[%d] ", op->ob_refcnt); | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
933 if (PyObject_Print(op, fp, 0) != 0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
934 PyErr_Clear(); |
| 6 | 935 putc('\n', fp); |
| 936 } | |
| 937 } | |
| 938 | |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
939 PyObject * |
|
3609
0b0c4ed1936a
rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents:
3574
diff
changeset
|
940 _Py_GetObjects(self, args) |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
941 PyObject *self; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
942 PyObject *args; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
943 { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
944 int i, n; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
945 PyObject *t = NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
946 PyObject *res, *op; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
947 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
948 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
949 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
950 op = refchain._ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
951 res = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
952 if (res == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
953 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
954 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
|
955 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
|
956 t != NULL && op->ob_type != (PyTypeObject *) t) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
957 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
958 if (op == &refchain) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
959 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
960 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
961 if (PyList_Append(res, op) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
962 Py_DECREF(res); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
963 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
964 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
965 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
966 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
967 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
968 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
969 |
| 6 | 970 #endif |
|
3330
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
971 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
972 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
973 /* Hack to force loading of cobject.o */ |
| 4588 | 974 PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
975 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
976 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
977 /* Hack to force loading of abstract.o */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
978 int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
979 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
980 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
981 /* Python's malloc wrappers (see mymalloc.h) */ |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
982 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
983 ANY * |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
984 PyMem_Malloc(nbytes) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
985 size_t nbytes; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
986 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
987 #if _PyMem_EXTRA > 0 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
988 if (nbytes == 0) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
989 nbytes = _PyMem_EXTRA; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
990 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
991 return PyMem_MALLOC(nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
992 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
993 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
994 ANY * |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
995 PyMem_Realloc(p, nbytes) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
996 ANY *p; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
997 size_t nbytes; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
998 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
999 #if _PyMem_EXTRA > 0 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1000 if (nbytes == 0) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1001 nbytes = _PyMem_EXTRA; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1002 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1003 return PyMem_REALLOC(p, nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1004 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1005 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1006 void |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1007 PyMem_Free(p) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1008 ANY *p; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1009 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1010 PyMem_FREE(p); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1011 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1012 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1013 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1014 /* Python's object malloc wrappers (see objimpl.h) */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1015 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1016 ANY * |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1017 PyObject_Malloc(nbytes) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1018 size_t nbytes; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1019 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1020 return PyObject_MALLOC(nbytes); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1021 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1022 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1023 ANY * |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1024 PyObject_Realloc(p, nbytes) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1025 ANY *p; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1026 size_t nbytes; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1027 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1028 return PyObject_REALLOC(p, nbytes); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1029 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1030 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1031 void |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1032 PyObject_Free(p) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1033 ANY *p; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1034 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1035 PyObject_FREE(p); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1036 } |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1037 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1038 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1039 /* These methods are used to control infinite recursion in repr, str, print, |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1040 etc. Container objects that may recursively contain themselves, |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1041 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1042 Py_ReprLeave() to avoid infinite recursion. |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1043 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1044 Py_ReprEnter() returns 0 the first time it is called for a particular |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1045 object and 1 every time thereafter. It returns -1 if an exception |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1046 occurred. Py_ReprLeave() has no return value. |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1047 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1048 See dictobject.c and listobject.c for examples of use. |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1049 */ |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1050 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1051 #define KEY "Py_Repr" |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1052 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1053 int |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1054 Py_ReprEnter(obj) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1055 PyObject *obj; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1056 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1057 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1058 PyObject *list; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1059 int i; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1060 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1061 dict = PyThreadState_GetDict(); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1062 if (dict == NULL) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1063 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1064 list = PyDict_GetItemString(dict, KEY); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1065 if (list == NULL) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1066 list = PyList_New(0); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1067 if (list == NULL) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1068 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1069 if (PyDict_SetItemString(dict, KEY, list) < 0) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1070 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1071 Py_DECREF(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1072 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1073 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1074 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1075 if (PyList_GET_ITEM(list, i) == obj) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1076 return 1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1077 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1078 PyList_Append(list, obj); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1079 return 0; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1080 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1081 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1082 void |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1083 Py_ReprLeave(obj) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1084 PyObject *obj; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1085 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1086 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1087 PyObject *list; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1088 int i; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1089 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1090 dict = PyThreadState_GetDict(); |
|
8006
a842c9e63280
Some robustness checks in Py_ReprLeave() in the unlikely event someone
Guido van Rossum <guido@python.org>
parents:
7984
diff
changeset
|
1091 if (dict == NULL) |
|
a842c9e63280
Some robustness checks in Py_ReprLeave() in the unlikely event someone
Guido van Rossum <guido@python.org>
parents:
7984
diff
changeset
|
1092 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1093 list = PyDict_GetItemString(dict, KEY); |
|
8006
a842c9e63280
Some robustness checks in Py_ReprLeave() in the unlikely event someone
Guido van Rossum <guido@python.org>
parents:
7984
diff
changeset
|
1094 if (list == NULL || !PyList_Check(list)) |
|
a842c9e63280
Some robustness checks in Py_ReprLeave() in the unlikely event someone
Guido van Rossum <guido@python.org>
parents:
7984
diff
changeset
|
1095 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1096 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1097 /* Count backwards because we always expect obj to be list[-1] */ |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1098 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1099 if (PyList_GET_ITEM(list, i) == obj) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1100 PyList_SetSlice(list, i, i + 1, NULL); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1101 break; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1102 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1103 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1104 } |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1105 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1106 /* |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1107 trashcan |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1108 CT 2k0130 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1109 non-recursively destroy nested objects |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1110 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1111 CT 2k0223 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1112 everything is now done in a macro. |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1113 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1114 CT 2k0305 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1115 modified to use functions, after Tim Peter's suggestion. |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1116 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1117 CT 2k0309 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1118 modified to restore a possible error. |
| 12052 | 1119 |
| 1120 CT 2k0325 | |
| 1121 added better safe than sorry check for threadstate | |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1122 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1123 CT 2k0422 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1124 complete rewrite. We now build a chain via ob_type |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1125 and save the limited number of types in ob_refcnt. |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1126 This is perfect since we don't need any memory. |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1127 A patch for free-threading would need just a lock. |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1128 */ |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1129 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1130 #define Py_TRASHCAN_TUPLE 1 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1131 #define Py_TRASHCAN_LIST 2 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1132 #define Py_TRASHCAN_DICT 3 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1133 #define Py_TRASHCAN_FRAME 4 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1134 #define Py_TRASHCAN_TRACEBACK 5 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1135 /* extend here if other objects want protection */ |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1136 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1137 int _PyTrash_delete_nesting = 0; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1138 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1139 PyObject * _PyTrash_delete_later = NULL; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1140 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1141 void |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1142 _PyTrash_deposit_object(op) |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1143 PyObject *op; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1144 { |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1145 int typecode; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1146 PyObject *hold = _PyTrash_delete_later; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1147 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1148 if (PyTuple_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1149 typecode = Py_TRASHCAN_TUPLE; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1150 else if (PyList_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1151 typecode = Py_TRASHCAN_LIST; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1152 else if (PyDict_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1153 typecode = Py_TRASHCAN_DICT; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1154 else if (PyFrame_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1155 typecode = Py_TRASHCAN_FRAME; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1156 else if (PyTraceBack_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1157 typecode = Py_TRASHCAN_TRACEBACK; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1158 op->ob_refcnt = typecode; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1159 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1160 op->ob_type = (PyTypeObject*)_PyTrash_delete_later; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1161 _PyTrash_delete_later = op; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1162 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1163 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1164 void |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1165 _PyTrash_destroy_chain() |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1166 { |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1167 while (_PyTrash_delete_later) { |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1168 PyObject *shredder = _PyTrash_delete_later; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1169 _PyTrash_delete_later = (PyObject*) shredder->ob_type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1170 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1171 switch (shredder->ob_refcnt) { |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1172 case Py_TRASHCAN_TUPLE: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1173 shredder->ob_type = &PyTuple_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1174 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1175 case Py_TRASHCAN_LIST: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1176 shredder->ob_type = &PyList_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1177 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1178 case Py_TRASHCAN_DICT: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1179 shredder->ob_type = &PyDict_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1180 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1181 case Py_TRASHCAN_FRAME: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1182 shredder->ob_type = &PyFrame_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1183 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1184 case Py_TRASHCAN_TRACEBACK: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1185 shredder->ob_type = &PyTraceBack_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1186 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1187 } |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1188 _Py_NewReference(shredder); |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1189 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1190 ++_PyTrash_delete_nesting; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1191 Py_DECREF(shredder); |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1192 --_PyTrash_delete_nesting; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1193 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1194 } |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1195 |
