Mercurial > cpython-lru_cache-weakref
annotate Objects/object.c @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | b60096d5001a |
| children | 7948cd4f7663 b125416394fa |
| rev | line source |
|---|---|
| 145 | 1 |
| 86 | 2 /* Generic object operations; and implementation of None (NoObject) */ |
| 6 | 3 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
4 #include "Python.h" |
| 6 | 5 |
|
13551
15b18e0cc3fd
Include macglue.h on the macintosh, so function prototypes are in scope.
Jack Jansen <jack.jansen@cwi.nl>
parents:
13473
diff
changeset
|
6 #ifdef macintosh |
|
15b18e0cc3fd
Include macglue.h on the macintosh, so function prototypes are in scope.
Jack Jansen <jack.jansen@cwi.nl>
parents:
13473
diff
changeset
|
7 #include "macglue.h" |
|
15b18e0cc3fd
Include macglue.h on the macintosh, so function prototypes are in scope.
Jack Jansen <jack.jansen@cwi.nl>
parents:
13473
diff
changeset
|
8 #endif |
|
15b18e0cc3fd
Include macglue.h on the macintosh, so function prototypes are in scope.
Jack Jansen <jack.jansen@cwi.nl>
parents:
13473
diff
changeset
|
9 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
10 /* just for trashcan: */ |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
11 #include "compile.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
12 #include "frameobject.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
13 #include "traceback.h" |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
14 |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2307
diff
changeset
|
15 #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
|
16 DL_IMPORT(long) _Py_RefTotal; |
| 86 | 17 #endif |
| 6 | 18 |
| 86 | 19 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 20 These are used by the individual routines for object creation. | |
| 21 Do not call them otherwise, they do not initialize the object! */ | |
| 6 | 22 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
23 #ifdef COUNT_ALLOCS |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 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
|
28 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
29 dump_counts(void) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
30 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
31 PyTypeObject *tp; |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
32 |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 null_strings, one_strings); |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
43 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
44 |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
45 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
46 get_counts(void) |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
47 { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
48 PyTypeObject *tp; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
49 PyObject *result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
50 PyObject *v; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
51 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
52 result = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
53 if (result == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
54 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
55 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
|
56 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
|
57 tp->tp_free, tp->tp_maxalloc); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
58 if (v == NULL) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
59 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
60 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
61 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
62 if (PyList_Append(result, v) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
63 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
64 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
65 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
66 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
67 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
68 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
69 return result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
70 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
71 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
72 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
73 inc_count(PyTypeObject *tp) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
74 { |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
75 if (tp->tp_alloc == 0) { |
|
2639
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
76 /* 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
|
77 if (tp->tp_next != NULL) /* sanity check */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
78 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
|
79 tp->tp_next = type_list; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
80 type_list = tp; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
81 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
82 tp->tp_alloc++; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
83 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
|
84 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
|
85 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
86 #endif |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
87 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
88 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
89 PyObject_Init(PyObject *op, PyTypeObject *tp) |
| 6 | 90 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
91 if (op == NULL) { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
92 PyErr_SetString(PyExc_SystemError, |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
93 "NULL object passed to PyObject_Init"); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
94 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
95 } |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
96 #ifdef WITH_CYCLE_GC |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
97 if (PyType_IS_GC(tp)) |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
98 op = (PyObject *) PyObject_FROM_GC(op); |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
99 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
100 /* 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
|
101 op->ob_type = tp; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
102 _Py_NewReference(op); |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
103 if (PyType_SUPPORTS_WEAKREFS(tp)) { |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
104 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
105 *weaklist = NULL; |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
106 } |
| 6 | 107 return op; |
| 108 } | |
| 109 | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
110 PyVarObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
111 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
112 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
113 if (op == NULL) { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
114 PyErr_SetString(PyExc_SystemError, |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
115 "NULL object passed to PyObject_InitVar"); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
116 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
117 } |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
118 #ifdef WITH_CYCLE_GC |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
119 if (PyType_IS_GC(tp)) |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
120 op = (PyVarObject *) PyObject_FROM_GC(op); |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
121 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
122 /* 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
|
123 op->ob_size = size; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
124 op->ob_type = tp; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
125 _Py_NewReference((PyObject *)op); |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
126 if (PyType_SUPPORTS_WEAKREFS(tp)) { |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
127 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
128 *weaklist = NULL; |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
129 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
130 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
131 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
132 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
133 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
134 _PyObject_New(PyTypeObject *tp) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
135 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
136 PyObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
137 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
|
138 if (op == NULL) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
139 return PyErr_NoMemory(); |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
140 #ifdef WITH_CYCLE_GC |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
141 if (PyType_IS_GC(tp)) |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
142 op = (PyObject *) PyObject_FROM_GC(op); |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
143 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
144 return PyObject_INIT(op, tp); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
145 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
146 |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
147 PyVarObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
148 _PyObject_NewVar(PyTypeObject *tp, int size) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
149 { |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
150 PyVarObject *op; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
151 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); |
| 6 | 152 if (op == NULL) |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
153 return (PyVarObject *)PyErr_NoMemory(); |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
154 #ifdef WITH_CYCLE_GC |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
155 if (PyType_IS_GC(tp)) |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
156 op = (PyVarObject *) PyObject_FROM_GC(op); |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
157 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
158 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
|
159 } |
|
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 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
162 _PyObject_Del(PyObject *op) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
163 { |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
164 #ifdef WITH_CYCLE_GC |
|
13265
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
165 if (op && PyType_IS_GC(op->ob_type)) { |
|
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
166 op = (PyObject *) PyObject_AS_GC(op); |
|
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
167 } |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
168 #endif |
|
13265
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
169 PyObject_FREE(op); |
| 6 | 170 } |
| 171 | |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
172 #ifndef WITH_CYCLE_GC |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
173 /* extension modules might need these */ |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
174 void _PyGC_Insert(PyObject *op) { } |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
175 void _PyGC_Remove(PyObject *op) { } |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
176 #endif |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13141
diff
changeset
|
177 |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
178 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
179 PyObject_Print(PyObject *op, FILE *fp, int flags) |
| 6 | 180 { |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
181 int ret = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
182 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
183 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
|
184 #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
|
185 if (PyOS_CheckStack()) { |
| 15318 | 186 PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
|
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
|
187 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
|
188 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
189 #endif |
|
11649
38560a3a3249
On Linux, one sometimes sees spurious errors after interrupting
Guido van Rossum <guido@python.org>
parents:
8695
diff
changeset
|
190 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
|
191 if (op == NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
192 fprintf(fp, "<nil>"); |
| 6 | 193 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
194 else { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
195 if (op->ob_refcnt <= 0) |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
196 fprintf(fp, "<refcnt %u at %p>", |
|
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
197 op->ob_refcnt, 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
|
198 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
|
199 if (op->ob_type->tp_repr == NULL) { |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
200 fprintf(fp, "<%s object at %p>", |
|
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
201 op->ob_type->tp_name, 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
|
202 } |
|
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 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
204 PyObject *s; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
205 if (flags & Py_PRINT_RAW) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
206 s = PyObject_Str(op); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
207 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 else { |
|
8111
5c9be98f5347
Be less naive about null characters in an object's repr().
Guido van Rossum <guido@python.org>
parents:
8006
diff
changeset
|
212 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
|
213 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
|
214 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
215 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
|
216 } |
|
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 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
218 else |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
219 ret = (*op->ob_type->tp_print)(op, fp, flags); |
| 6 | 220 } |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
221 if (ret == 0) { |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
222 if (ferror(fp)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
223 PyErr_SetFromErrno(PyExc_IOError); |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
224 clearerr(fp); |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
225 ret = -1; |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
226 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
227 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
228 return ret; |
| 6 | 229 } |
| 230 | |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
231 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
|
16197
1bb7651683e7
PyObject_Dump() -> _PyObject_Dump()
Barry Warsaw <barry@python.org>
parents:
16167
diff
changeset
|
232 void _PyObject_Dump(PyObject* op) |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
233 { |
|
16742
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
234 if (op == NULL) |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
235 fprintf(stderr, "NULL\n"); |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
236 else { |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
237 (void)PyObject_Print(op, stderr, 0); |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
238 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt); |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
239 fprintf(stderr, "address : %p\n", op); |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
240 } |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
241 } |
|
16167
71ceaba3f615
PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents:
16165
diff
changeset
|
242 |
|
71ceaba3f615
PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents:
16165
diff
changeset
|
243 #ifdef WITH_CYCLE_GC |
|
16197
1bb7651683e7
PyObject_Dump() -> _PyObject_Dump()
Barry Warsaw <barry@python.org>
parents:
16167
diff
changeset
|
244 void _PyGC_Dump(PyGC_Head* op) |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
245 { |
|
16197
1bb7651683e7
PyObject_Dump() -> _PyObject_Dump()
Barry Warsaw <barry@python.org>
parents:
16167
diff
changeset
|
246 _PyObject_Dump(PyObject_FROM_GC(op)); |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
247 } |
|
16167
71ceaba3f615
PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents:
16165
diff
changeset
|
248 #endif /* WITH_CYCLE_GC */ |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
249 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
250 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
251 PyObject_Repr(PyObject *v) |
| 6 | 252 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
253 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
254 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
|
255 #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
|
256 if (PyOS_CheckStack()) { |
| 15318 | 257 PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
|
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
|
258 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
|
259 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
260 #endif |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
261 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
262 return PyString_FromString("<NULL>"); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
263 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
|
264 char buf[120]; |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
265 sprintf(buf, "<%.80s object at %p>", |
|
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13183
diff
changeset
|
266 v->ob_type->tp_name, v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
267 return PyString_FromString(buf); |
| 6 | 268 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
269 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
270 PyObject *res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
271 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
|
272 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
273 return NULL; |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
274 if (PyUnicode_Check(res)) { |
|
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
275 PyObject* str; |
|
13436
bc9e22ad5c46
- changed __repr__ to use "unicode escape" encoding for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
13432
diff
changeset
|
276 str = PyUnicode_AsUnicodeEscapeString(res); |
|
13319
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
277 Py_DECREF(res); |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
278 if (str) |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
279 res = str; |
|
13319
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
280 else |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
281 return NULL; |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
282 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
283 if (!PyString_Check(res)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
284 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
|
285 "__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
|
286 res->ob_type->tp_name); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
287 Py_DECREF(res); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
288 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
289 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
290 return res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
291 } |
| 6 | 292 } |
| 293 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
294 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
295 PyObject_Str(PyObject *v) |
|
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 PyObject *res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
298 |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
299 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
300 return PyString_FromString("<NULL>"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
301 else if (PyString_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
302 Py_INCREF(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
303 return v; |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
304 } |
|
2160
899b4e3dfd4c
fix strobject() behavior
Guido van Rossum <guido@python.org>
parents:
2124
diff
changeset
|
305 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
|
306 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
|
307 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
308 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
309 if (!PyInstance_Check(v) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
310 (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
311 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
312 return PyObject_Repr(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
313 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
314 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
315 Py_DECREF(func); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
316 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
317 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
318 return NULL; |
|
13319
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
319 if (PyUnicode_Check(res)) { |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
320 PyObject* str; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
321 str = PyUnicode_AsEncodedString(res, NULL, NULL); |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
322 Py_DECREF(res); |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
323 if (str) |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
324 res = str; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
325 else |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
326 return NULL; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
327 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
328 if (!PyString_Check(res)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
329 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
|
330 "__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
|
331 res->ob_type->tp_name); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
332 Py_DECREF(res); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
333 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
334 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
335 return res; |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
336 } |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
337 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
338 PyObject * |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
339 PyObject_Unicode(PyObject *v) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
340 { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
341 PyObject *res; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
342 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
343 if (v == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
344 res = PyString_FromString("<NULL>"); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
345 else if (PyUnicode_Check(v)) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
346 Py_INCREF(v); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
347 return v; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
348 } |
|
17220
b60096d5001a
Fixed ref count bug. Patch #411191. Found by Walter Dörwald.
Marc-André Lemburg <mal@egenix.com>
parents:
16782
diff
changeset
|
349 else if (PyString_Check(v)) { |
|
b60096d5001a
Fixed ref count bug. Patch #411191. Found by Walter Dörwald.
Marc-André Lemburg <mal@egenix.com>
parents:
16782
diff
changeset
|
350 Py_INCREF(v); |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
351 res = v; |
|
17220
b60096d5001a
Fixed ref count bug. Patch #411191. Found by Walter Dörwald.
Marc-André Lemburg <mal@egenix.com>
parents:
16782
diff
changeset
|
352 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
353 else if (v->ob_type->tp_str != NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
354 res = (*v->ob_type->tp_str)(v); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
355 else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
356 PyObject *func; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
357 static PyObject *strstr; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
358 if (strstr == NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
359 strstr= PyString_InternFromString("__str__"); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
360 if (strstr == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
361 return NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
362 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
363 if (!PyInstance_Check(v) || |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
364 (func = PyObject_GetAttr(v, strstr)) == NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
365 PyErr_Clear(); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
366 res = PyObject_Repr(v); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
367 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
368 else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
369 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
370 Py_DECREF(func); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
371 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
372 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
373 if (res == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
374 return NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
375 if (!PyUnicode_Check(res)) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
376 PyObject* str; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
377 str = PyUnicode_FromObject(res); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
378 Py_DECREF(res); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
379 if (str) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
380 res = str; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
381 else |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
382 return NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
383 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
384 return res; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
385 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
386 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
387 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
388 /* Macro to get the tp_richcompare field of a type if defined */ |
|
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
389 #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ |
|
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
390 ? (t)->tp_richcompare : NULL) |
|
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
391 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
392 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
393 static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
394 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
395 /* Try a genuine rich comparison, returning an object. Return: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
396 NULL for exception; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
397 NotImplemented if this particular rich comparison is not implemented or |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
398 undefined; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
399 some object not equal to NotImplemented if it is implemented |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
400 (this latter object may not be a Boolean). |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
401 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
402 static PyObject * |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
403 try_rich_compare(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
404 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
405 richcmpfunc f; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
406 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
407 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
408 if ((f = RICHCOMPARE(v->ob_type)) != NULL) { |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
409 res = (*f)(v, w, op); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
410 if (res != Py_NotImplemented) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
411 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
412 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
413 } |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
414 if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
415 return (*f)(w, v, swapped_op[op]); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
416 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
417 res = Py_NotImplemented; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
418 Py_INCREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
419 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
420 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
421 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
422 /* Try a genuine rich comparison, returning an int. Return: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
423 -1 for exception (including the case where try_rich_compare() returns an |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
424 object that's not a Boolean); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
425 0 if the outcome is false; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
426 1 if the outcome is true; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
427 2 if this particular rich comparison is not implemented or undefined. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
428 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
429 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
430 try_rich_compare_bool(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
431 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
432 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
433 int ok; |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
434 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
435 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
436 return 2; /* Shortcut, avoid INCREF+DECREF */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
437 res = try_rich_compare(v, w, op); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
438 if (res == NULL) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
439 return -1; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
440 if (res == Py_NotImplemented) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
441 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
442 return 2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
443 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
444 ok = PyObject_IsTrue(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
445 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
446 return ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
447 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
448 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
449 /* Try rich comparisons to determine a 3-way comparison. Return: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
450 -2 for an exception; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
451 -1 if v < w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
452 0 if v == w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
453 1 if v > w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
454 2 if this particular rich comparison is not implemented or undefined. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
455 */ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
456 static int |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
457 try_rich_to_3way_compare(PyObject *v, PyObject *w) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
458 { |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
459 static struct { int op; int outcome; } tries[3] = { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
460 /* Try this operator, and if it is true, use this outcome: */ |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
461 {Py_EQ, 0}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
462 {Py_LT, -1}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
463 {Py_GT, 1}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
464 }; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
465 int i; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
466 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
467 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
468 return 2; /* Shortcut */ |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
469 |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
470 for (i = 0; i < 3; i++) { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
471 switch (try_rich_compare_bool(v, w, tries[i].op)) { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
472 case -1: |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
473 return -1; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
474 case 1: |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
475 return tries[i].outcome; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
476 } |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
477 } |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
478 |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
479 return 2; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
480 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
481 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
482 /* Try a 3-way comparison, returning an int. Return: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
483 -2 for an exception; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
484 -1 if v < w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
485 0 if v == w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
486 1 if v > w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
487 2 if this particular 3-way comparison is not implemented or undefined. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
488 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
489 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
490 try_3way_compare(PyObject *v, PyObject *w) |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
491 { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
492 int c; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
493 cmpfunc f; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
494 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
495 /* Comparisons involving instances are given to instance_compare, |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
496 which has the same return conventions as this function. */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
497 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
498 if (PyInstance_Check(v)) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
499 return (*v->ob_type->tp_compare)(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
500 if (PyInstance_Check(w)) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
501 return (*w->ob_type->tp_compare)(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
502 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
503 /* If the types are equal, don't bother with coercions etc. */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
504 if (v->ob_type == w->ob_type) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
505 if ((f = v->ob_type->tp_compare) == NULL) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
506 return 2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
507 c = (*f)(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
508 if (PyErr_Occurred()) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
509 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
510 return c < 0 ? -1 : c > 0 ? 1 : 0; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
511 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
512 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
513 /* Try coercion; if it fails, give up */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
514 c = PyNumber_CoerceEx(&v, &w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
515 if (c < 0) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
516 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
517 if (c > 0) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
518 return 2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
519 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
520 /* Try v's comparison, if defined */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
521 if ((f = v->ob_type->tp_compare) != NULL) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
522 c = (*f)(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
523 Py_DECREF(v); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
524 Py_DECREF(w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
525 if (PyErr_Occurred()) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
526 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
527 return c < 0 ? -1 : c > 0 ? 1 : 0; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
528 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
529 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
530 /* Try w's comparison, if defined */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
531 if ((f = w->ob_type->tp_compare) != NULL) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
532 c = (*f)(w, v); /* swapped! */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
533 Py_DECREF(v); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
534 Py_DECREF(w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
535 if (PyErr_Occurred()) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
536 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
537 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
538 } |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
539 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
540 /* No comparison defined */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
541 Py_DECREF(v); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
542 Py_DECREF(w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
543 return 2; |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
544 } |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
545 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
546 /* Final fallback 3-way comparison, returning an int. Return: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
547 -2 if an error occurred; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
548 -1 if v < w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
549 0 if v == w; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
550 1 if v > w. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
551 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
552 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
553 default_3way_compare(PyObject *v, PyObject *w) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
554 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
555 int c; |
|
16108
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
556 char *vname, *wname; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
557 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
558 if (v->ob_type == w->ob_type) { |
| 16062 | 559 /* When comparing these pointers, they must be cast to |
| 560 * integer types (i.e. Py_uintptr_t, our spelling of C9X's | |
| 561 * uintptr_t). ANSI specifies that pointer compares other | |
| 562 * than == and != to non-related structures are undefined. | |
| 563 */ | |
|
16061
6b647a5d5b7e
default_3way_compare(): When comparing the pointers, they must be cast
Barry Warsaw <barry@python.org>
parents:
16002
diff
changeset
|
564 Py_uintptr_t vv = (Py_uintptr_t)v; |
|
6b647a5d5b7e
default_3way_compare(): When comparing the pointers, they must be cast
Barry Warsaw <barry@python.org>
parents:
16002
diff
changeset
|
565 Py_uintptr_t ww = (Py_uintptr_t)w; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
566 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
567 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
568 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
569 /* Special case for Unicode */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
570 if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
571 c = PyUnicode_Compare(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
572 if (!PyErr_Occurred()) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
573 return c; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
574 /* TypeErrors are ignored: if Unicode coercion fails due |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
575 to one of the arguments not having the right type, we |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
576 continue as defined by the coercion protocol (see |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
577 above). Luckily, decoding errors are reported as |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
578 ValueErrors and are not masked by this technique. */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
579 if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
580 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
581 PyErr_Clear(); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
582 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
583 |
|
16123
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
584 /* None is smaller than anything */ |
|
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
585 if (v == Py_None) |
|
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
586 return -1; |
|
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
587 if (w == Py_None) |
|
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
588 return 1; |
|
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
589 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
590 /* different type: compare type names */ |
|
16108
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
591 if (v->ob_type->tp_as_number) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
592 vname = ""; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
593 else |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
594 vname = v->ob_type->tp_name; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
595 if (w->ob_type->tp_as_number) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
596 wname = ""; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
597 else |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
598 wname = w->ob_type->tp_name; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
599 c = strcmp(vname, wname); |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
600 if (c < 0) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
601 return -1; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
602 if (c > 0) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
603 return 1; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
604 /* Same type name, or (more likely) incomparable numeric types */ |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
605 return ((Py_uintptr_t)(v->ob_type) < ( |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
606 Py_uintptr_t)(w->ob_type)) ? -1 : 1; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
607 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
608 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
609 #define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
610 |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
611 static int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
612 do_cmp(PyObject *v, PyObject *w) |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
613 { |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
614 int c; |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
615 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
616 c = try_rich_to_3way_compare(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
617 if (c < 2) |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
618 return c; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
619 c = try_3way_compare(v, w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
620 if (c < 2) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
621 return c; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
622 return default_3way_compare(v, w); |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
623 } |
|
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
624 |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
625 /* compare_nesting is incremented before calling compare (for |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
626 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
|
627 nesting limit, enable code to detect circular data structures. |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
628 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
629 This is a tunable parameter that should only affect the performance |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
630 of comparisons, nothing else. Setting it high makes comparing deeply |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
631 nested non-cyclical data structures faster, but makes comparing cyclical |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
632 data structures slower. |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
633 */ |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
634 #define NESTING_LIMIT 20 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
635 |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
636 static int compare_nesting = 0; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
637 |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
638 static PyObject* |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
639 get_inprogress_dict(void) |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
640 { |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
641 static PyObject *key; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
642 PyObject *tstate_dict, *inprogress; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
643 |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
644 if (key == NULL) { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
645 key = PyString_InternFromString("cmp_state"); |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
646 if (key == NULL) |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
647 return NULL; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
648 } |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
649 |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
650 tstate_dict = PyThreadState_GetDict(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
651 if (tstate_dict == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
652 PyErr_BadInternalCall(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
653 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
654 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
655 |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
656 inprogress = PyDict_GetItem(tstate_dict, key); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
657 if (inprogress == NULL) { |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
658 inprogress = PyDict_New(); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
659 if (inprogress == NULL) |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
660 return NULL; |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
661 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) { |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
662 Py_DECREF(inprogress); |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
663 return NULL; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
664 } |
|
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
|
665 Py_DECREF(inprogress); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
666 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
667 |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
668 return inprogress; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
669 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
670 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
671 static PyObject * |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
672 check_recursion(PyObject *v, PyObject *w, int op) |
|
15952
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
673 { |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
674 PyObject *inprogress; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
675 PyObject *token; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
676 Py_uintptr_t iv = (Py_uintptr_t)v; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
677 Py_uintptr_t iw = (Py_uintptr_t)w; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
678 PyObject *x, *y, *z; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
679 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
680 inprogress = get_inprogress_dict(); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
681 if (inprogress == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
682 return NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
683 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
684 token = PyTuple_New(3); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
685 if (token == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
686 return NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
687 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
688 if (iv <= iw) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
689 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v)); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
690 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w)); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
691 if (op >= 0) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
692 op = swapped_op[op]; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
693 } else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
694 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w)); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
695 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v)); |
|
15952
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
696 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
697 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op)); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
698 if (x == NULL || y == NULL || z == NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
699 Py_DECREF(token); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
700 return NULL; |
|
15952
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
701 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
702 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
703 if (PyDict_GetItem(inprogress, token) != NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
704 Py_DECREF(token); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
705 return Py_None; /* Without INCREF! */ |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
706 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
707 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
708 if (PyDict_SetItem(inprogress, token, token) < 0) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
709 Py_DECREF(token); |
|
15952
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
710 return NULL; |
|
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
711 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
712 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
713 return token; |
|
15952
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
714 } |
|
1f92d63b20f0
This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents:
15941
diff
changeset
|
715 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
716 static void |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
717 delete_token(PyObject *token) |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
718 { |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
719 PyObject *inprogress; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
720 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
721 if (token == NULL || token == Py_None) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
722 return; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
723 inprogress = get_inprogress_dict(); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
724 if (inprogress == NULL) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
725 PyErr_Clear(); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
726 else |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
727 PyDict_DelItem(inprogress, token); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
728 Py_DECREF(token); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
729 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
730 |
| 6 | 731 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
732 PyObject_Compare(PyObject *v, PyObject *w) |
| 6 | 733 { |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
734 PyTypeObject *vtp; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
735 int result; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
736 |
|
14126
41eb755e4ed1
Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents:
14051
diff
changeset
|
737 #if defined(USE_STACKCHECK) |
|
14265
1f76fc98b4c8
PyOS_CheckStack() returns 1 when failing, not -1.
Guido van Rossum <guido@python.org>
parents:
14126
diff
changeset
|
738 if (PyOS_CheckStack()) { |
|
14126
41eb755e4ed1
Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents:
14051
diff
changeset
|
739 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
|
41eb755e4ed1
Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents:
14051
diff
changeset
|
740 return -1; |
|
41eb755e4ed1
Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents:
14051
diff
changeset
|
741 } |
|
41eb755e4ed1
Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents:
14051
diff
changeset
|
742 #endif |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
743 if (v == NULL || w == NULL) { |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
744 PyErr_BadInternalCall(); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
745 return -1; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
746 } |
| 6 | 747 if (v == w) |
| 748 return 0; | |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
749 vtp = v->ob_type; |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
750 compare_nesting++; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
751 if (compare_nesting > NESTING_LIMIT && |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
752 (vtp->tp_as_mapping |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
753 || (vtp->tp_as_sequence |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
754 && !PyString_Check(v) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
755 && !PyTuple_Check(v)))) { |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
756 /* try to detect circular data structures */ |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
757 PyObject *token = check_recursion(v, w, -1); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
758 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
759 if (token == NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
760 result = -1; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
761 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
762 else if (token == Py_None) { |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
763 /* already comparing these objects. assume |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
764 they're equal until shown otherwise */ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
765 result = 0; |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
766 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
767 else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
768 result = do_cmp(v, w); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
769 delete_token(token); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
770 } |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
771 } |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
772 else { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
773 result = do_cmp(v, w); |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
774 } |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
775 compare_nesting--; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
776 return result < 0 ? -1 : result; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
777 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
778 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
779 static PyObject * |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
780 try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
781 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
782 int c; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
783 PyObject *result; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
784 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
785 c = try_3way_compare(v, w); |
|
16741
8466bb37289c
In try_3way_to_rich_compare(), swap the call to default_3way_compare()
Guido van Rossum <guido@python.org>
parents:
16385
diff
changeset
|
786 if (c >= 2) |
|
8466bb37289c
In try_3way_to_rich_compare(), swap the call to default_3way_compare()
Guido van Rossum <guido@python.org>
parents:
16385
diff
changeset
|
787 c = default_3way_compare(v, w); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
788 if (c <= -2) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
789 return NULL; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
790 switch (op) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
791 case Py_LT: c = c < 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
792 case Py_LE: c = c <= 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
793 case Py_EQ: c = c == 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
794 case Py_NE: c = c != 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
795 case Py_GT: c = c > 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
796 case Py_GE: c = c >= 0; break; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
797 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
798 result = c ? Py_True : Py_False; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
799 Py_INCREF(result); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
800 return result; |
| 6 | 801 } |
| 802 | |
|
16079
c4355905b044
Remove a smelly export.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16062
diff
changeset
|
803 static PyObject * |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
804 do_richcmp(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
805 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
806 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
807 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
808 res = try_rich_compare(v, w, op); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
809 if (res != Py_NotImplemented) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
810 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
811 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
812 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
813 return try_3way_to_rich_compare(v, w, op); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
814 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
815 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
816 PyObject * |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
817 PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
818 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
819 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
820 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
821 assert(Py_LT <= op && op <= Py_GE); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
822 |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
823 compare_nesting++; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
824 if (compare_nesting > NESTING_LIMIT && |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
825 (v->ob_type->tp_as_mapping |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
826 || (v->ob_type->tp_as_sequence |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
827 && !PyString_Check(v) |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
828 && !PyTuple_Check(v)))) { |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
829 /* try to detect circular data structures */ |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
830 PyObject *token = check_recursion(v, w, op); |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
831 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
832 if (token == NULL) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
833 res = NULL; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
834 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
835 else if (token == Py_None) { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
836 /* already comparing these objects with this operator. |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
837 assume they're equal until shown otherwise */ |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
838 if (op == Py_EQ) |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
839 res = Py_True; |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
840 else if (op == Py_NE) |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
841 res = Py_False; |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
842 else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
843 PyErr_SetString(PyExc_ValueError, |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
844 "can't order recursive values"); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
845 res = NULL; |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
846 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
847 Py_XINCREF(res); |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
848 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
849 else { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
850 res = do_richcmp(v, w, op); |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
851 delete_token(token); |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
852 } |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
853 } |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
854 else { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
855 res = do_richcmp(v, w, op); |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
856 } |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
857 compare_nesting--; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
858 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
859 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
860 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
861 int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
862 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
863 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
864 PyObject *res = PyObject_RichCompare(v, w, op); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
865 int ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
866 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
867 if (res == NULL) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
868 return -1; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
869 ok = PyObject_IsTrue(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
870 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
871 return ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
872 } |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
873 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
874 /* 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
|
875 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
|
876 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
877 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
|
878 */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
879 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
880 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
881 _Py_HashDouble(double v) |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
882 { |
|
13976
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
883 double intpart, fractpart; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
884 int expo; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
885 long hipart; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
886 long x; /* the final hash value */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
887 /* This is designed so that Python numbers of different types |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
888 * that compare equal hash to the same value; otherwise comparisons |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
889 * of mapping keys will turn out weird. |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
890 */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
891 |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
892 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
893 { |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
894 extended e; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
895 fractpart = modf(v, &e); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
896 intpart = e; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
897 } |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
898 #else |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
899 fractpart = modf(v, &intpart); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
900 #endif |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
901 if (fractpart == 0.0) { |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
902 /* This must return the same hash as an equal int or long. */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
903 if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
904 /* Convert to long and use its hash. */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
905 PyObject *plong; /* converted to Python long */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
906 if (Py_IS_INFINITY(intpart)) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
907 /* can't convert to long int -- arbitrary */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
908 v = v < 0 ? -271828.0 : 314159.0; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
909 plong = PyLong_FromDouble(v); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
910 if (plong == NULL) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
911 return -1; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
912 x = PyObject_Hash(plong); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
913 Py_DECREF(plong); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
914 return x; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
915 } |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
916 /* Fits in a C long == a Python int, so is its own hash. */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
917 x = (long)intpart; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
918 if (x == -1) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
919 x = -2; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
920 return x; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
921 } |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
922 /* The fractional part is non-zero, so we don't have to worry about |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
923 * making this match the hash of some other type. |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
924 * Use frexp to get at the bits in the double. |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
925 * 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
|
926 * 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
|
927 * many as (but no more than) 56 significant bits. |
|
13976
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
928 * So, assuming sizeof(long) >= 4, each part can be broken into two |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
929 * longs; frexp and multiplication are used to do that. |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
930 * Also, since the Cray double format has 15 exponent bits, which is |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
931 * the most of any double format in use, shifting the exponent field |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
932 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
933 */ |
|
13976
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
934 v = frexp(v, &expo); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
935 v *= 2147483648.0; /* 2**31 */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
936 hipart = (long)v; /* take the top 32 bits */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
937 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
938 x = hipart + (long)v + (expo << 15); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
939 if (x == -1) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
940 x = -2; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
941 return x; |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
942 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
943 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
944 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
945 _Py_HashPointer(void *p) |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
946 { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
947 #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
|
948 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
|
949 #else |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
950 /* 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
|
951 PyObject* longobj; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
952 long x; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
953 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
954 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
|
955 x = -1; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
956 goto finally; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
957 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
958 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
|
959 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
960 finally: |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
961 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
|
962 return x; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
963 #endif |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
964 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
965 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
966 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
967 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
968 PyObject_Hash(PyObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
969 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
970 PyTypeObject *tp = v->ob_type; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
971 if (tp->tp_hash != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
972 return (*tp->tp_hash)(v); |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
973 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
974 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
|
975 } |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
976 /* 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
|
977 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
|
978 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
979 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
980 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
981 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
982 PyObject_GetAttrString(PyObject *v, char *name) |
| 86 | 983 { |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
984 if (v->ob_type->tp_getattro != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
985 PyObject *w, *res; |
|
5006
5f4b23e3d97c
Intern the strings created in getattr() and setattr().
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
986 w = PyString_InternFromString(name); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
987 if (w == NULL) |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
988 return NULL; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
989 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
|
990 Py_XDECREF(w); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
991 return res; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
992 } |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
993 |
| 86 | 994 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
|
995 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
|
996 "'%.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
|
997 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
|
998 name); |
| 86 | 999 return NULL; |
| 1000 } | |
| 1001 else { | |
| 1002 return (*v->ob_type->tp_getattr)(v, name); | |
| 1003 } | |
| 1004 } | |
| 1005 | |
| 1006 int | |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1007 PyObject_HasAttrString(PyObject *v, char *name) |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1008 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1009 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
|
1010 if (res != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1011 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
|
1012 return 1; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1013 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1014 PyErr_Clear(); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1015 return 0; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1016 } |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1017 |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1018 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1019 PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) |
| 86 | 1020 { |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1021 if (v->ob_type->tp_setattro != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1022 PyObject *s; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1023 int res; |
|
5006
5f4b23e3d97c
Intern the strings created in getattr() and setattr().
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
1024 s = PyString_InternFromString(name); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1025 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
|
1026 return -1; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1027 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
|
1028 Py_XDECREF(s); |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1029 return res; |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1030 } |
|
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1031 |
| 86 | 1032 if (v->ob_type->tp_setattr == NULL) { |
| 1033 if (v->ob_type->tp_getattr == NULL) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1034 PyErr_SetString(PyExc_TypeError, |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
1035 "attribute-less object (assign or del)"); |
| 86 | 1036 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1037 PyErr_SetString(PyExc_TypeError, |
|
512
8414e1f9a5fe
Change error messages for impossible assignment slightly.
Guido van Rossum <guido@python.org>
parents:
473
diff
changeset
|
1038 "object has read-only attributes"); |
| 95 | 1039 return -1; |
| 86 | 1040 } |
| 1041 else { | |
| 1042 return (*v->ob_type->tp_setattr)(v, name, w); | |
| 1043 } | |
| 1044 } | |
| 1045 | |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1046 /* Internal API needed by PyObject_GetAttr(): */ |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1047 extern |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1048 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1049 const char *errors); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1050 |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1051 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1052 PyObject_GetAttr(PyObject *v, PyObject *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
|
1053 { |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1054 /* The Unicode to string conversion is done here because the |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1055 existing tp_getattro slots expect a string object as name |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1056 and we wouldn't want to break those. */ |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1057 if (PyUnicode_Check(name)) { |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1058 name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1059 if (name == NULL) |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1060 return NULL; |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1061 } |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1062 |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1063 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
|
1064 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
|
1065 "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
|
1066 return NULL; |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1067 } |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1068 if (v->ob_type->tp_getattro != NULL) |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1069 return (*v->ob_type->tp_getattro)(v, name); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1070 else |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1071 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
|
1072 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1073 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1074 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1075 PyObject_HasAttr(PyObject *v, PyObject *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
|
1076 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1077 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
|
1078 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
|
1079 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
|
1080 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
|
1081 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1082 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
|
1083 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
|
1084 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1085 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1086 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1087 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1088 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1089 int err; |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1090 |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1091 /* The Unicode to string conversion is done here because the |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1092 existing tp_setattro slots expect a string object as name |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1093 and we wouldn't want to break those. */ |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1094 if (PyUnicode_Check(name)) { |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1095 name = PyUnicode_AsEncodedString(name, NULL, NULL); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1096 if (name == NULL) |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1097 return -1; |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1098 } |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1099 else |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1100 Py_INCREF(name); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1101 |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1102 if (!PyString_Check(name)){ |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1103 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
|
1104 "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
|
1105 err = -1; |
|
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1106 } |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1107 else { |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1108 PyString_InternInPlace(&name); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1109 if (v->ob_type->tp_setattro != NULL) |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1110 err = (*v->ob_type->tp_setattro)(v, name, value); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1111 else |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1112 err = PyObject_SetAttrString(v, |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1113 PyString_AS_STRING(name), value); |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1114 } |
|
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1115 |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1116 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
|
1117 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
|
1118 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1119 |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1120 /* 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
|
1121 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
|
1122 |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1123 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1124 PyObject_IsTrue(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
|
1125 { |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1126 int res; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1127 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
|
1128 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
|
1129 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
|
1130 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
|
1131 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
|
1132 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
|
1133 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
|
1134 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
|
1135 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
|
1136 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
|
1137 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
|
1138 else |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1139 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1140 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
|
1141 res = 1; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1142 return res; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1143 } |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1144 |
| 7935 | 1145 /* equivalent of 'not v' |
| 1146 Return -1 if an error occurred */ | |
| 1147 | |
| 1148 int | |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1149 PyObject_Not(PyObject *v) |
| 7935 | 1150 { |
| 1151 int res; | |
| 1152 res = PyObject_IsTrue(v); | |
| 1153 if (res < 0) | |
| 1154 return res; | |
| 1155 return res == 0; | |
| 1156 } | |
| 1157 | |
|
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
|
1158 /* 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
|
1159 Increment the reference count on each argument. |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1160 Return value: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1161 -1 if an error occurred; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1162 0 if the coercion succeeded (and then the reference counts are increased); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1163 1 if no coercion is possible (and no error is raised). |
|
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
|
1164 */ |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1165 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1166 PyNumber_CoerceEx(PyObject **pv, PyObject **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
|
1167 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1168 register PyObject *v = *pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1169 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
|
1170 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
|
1171 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1172 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
|
1173 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1174 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
|
1175 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
|
1176 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1177 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
|
1178 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
|
1179 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
|
1180 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
|
1181 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1182 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
|
1183 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
|
1184 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
|
1185 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
|
1186 } |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1187 return 1; |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1188 } |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1189 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1190 /* Coerce two numeric types to the "larger" one. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1191 Increment the reference count on each argument. |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1192 Return -1 and raise an exception if no coercion is possible |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1193 (and then no reference count is incremented). |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1194 */ |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1195 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1196 PyNumber_Coerce(PyObject **pv, PyObject **pw) |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1197 { |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1198 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
|
1199 if (err <= 0) |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1200 return err; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1201 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
|
1202 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
|
1203 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1204 |
| 6 | 1205 |
| 2237 | 1206 /* Test whether an object can be called */ |
| 1207 | |
| 1208 int | |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1209 PyCallable_Check(PyObject *x) |
| 2237 | 1210 { |
| 1211 if (x == NULL) | |
| 1212 return 0; | |
| 1213 if (x->ob_type->tp_call != NULL || | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1214 PyFunction_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1215 PyMethod_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1216 PyCFunction_Check(x) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1217 PyClass_Check(x)) |
| 2237 | 1218 return 1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1219 if (PyInstance_Check(x)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1220 PyObject *call = PyObject_GetAttrString(x, "__call__"); |
| 2237 | 1221 if (call == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1222 PyErr_Clear(); |
| 2237 | 1223 return 0; |
| 1224 } | |
| 1225 /* Could test recursively but don't, for fear of endless | |
| 1226 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
|
1227 Py_DECREF(call); |
| 2237 | 1228 return 1; |
| 1229 } | |
| 1230 return 0; | |
| 1231 } | |
| 1232 | |
| 1233 | |
| 6 | 1234 /* |
| 1235 NoObject is usable as a non-NULL undefined value, used by the macro None. | |
| 1236 There is (and should be!) no way to create other objects of this type, | |
| 86 | 1237 so there is exactly one (which is indestructible, by the way). |
| 6 | 1238 */ |
| 1239 | |
| 739 | 1240 /* ARGSUSED */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1241 static PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1242 none_repr(PyObject *op) |
| 86 | 1243 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1244 return PyString_FromString("None"); |
| 6 | 1245 } |
| 1246 | |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1247 /* ARGUSED */ |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1248 static void |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1249 none_dealloc(PyObject* ignore) |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1250 { |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1251 /* This should never get called, but we also don't want to SEGV if |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1252 * we accidently decref None out of existance. |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1253 */ |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1254 abort(); |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1255 } |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1256 |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1257 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1258 static PyTypeObject PyNothing_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1259 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 1260 0, |
| 86 | 1261 "None", |
| 6 | 1262 0, |
| 1263 0, | |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1264 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1017
diff
changeset
|
1265 0, /*tp_print*/ |
| 86 | 1266 0, /*tp_getattr*/ |
| 1267 0, /*tp_setattr*/ | |
| 1268 0, /*tp_compare*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1269 (reprfunc)none_repr, /*tp_repr*/ |
| 86 | 1270 0, /*tp_as_number*/ |
| 1271 0, /*tp_as_sequence*/ | |
| 1272 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1273 0, /*tp_hash */ |
| 6 | 1274 }; |
| 1275 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1276 PyObject _Py_NoneStruct = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1277 PyObject_HEAD_INIT(&PyNothing_Type) |
| 6 | 1278 }; |
| 1279 | |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1280 /* NotImplemented is an object that can be used to signal that an |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1281 operation is not implemented for the given type combination. */ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1282 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1283 static PyObject * |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1284 NotImplemented_repr(PyObject *op) |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1285 { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1286 return PyString_FromString("NotImplemented"); |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1287 } |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1288 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1289 static PyTypeObject PyNotImplemented_Type = { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1290 PyObject_HEAD_INIT(&PyType_Type) |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1291 0, |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1292 "NotImplemented", |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1293 0, |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1294 0, |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1295 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1296 0, /*tp_print*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1297 0, /*tp_getattr*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1298 0, /*tp_setattr*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1299 0, /*tp_compare*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1300 (reprfunc)NotImplemented_repr, /*tp_repr*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1301 0, /*tp_as_number*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1302 0, /*tp_as_sequence*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1303 0, /*tp_as_mapping*/ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1304 0, /*tp_hash */ |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1305 }; |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1306 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1307 PyObject _Py_NotImplementedStruct = { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1308 PyObject_HEAD_INIT(&PyNotImplemented_Type) |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1309 }; |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1310 |
| 6 | 1311 |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
1312 #ifdef Py_TRACE_REFS |
| 6 | 1313 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1314 static PyObject refchain = {&refchain, &refchain}; |
| 6 | 1315 |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
1316 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1317 _Py_ResetReferences(void) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1318 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1319 refchain._ob_prev = refchain._ob_next = &refchain; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1320 _Py_RefTotal = 0; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1321 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1322 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1323 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1324 _Py_NewReference(PyObject *op) |
| 6 | 1325 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1326 _Py_RefTotal++; |
| 6 | 1327 op->ob_refcnt = 1; |
| 1328 op->_ob_next = refchain._ob_next; | |
| 1329 op->_ob_prev = &refchain; | |
| 1330 refchain._ob_next->_ob_prev = op; | |
| 1331 refchain._ob_next = op; | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
1332 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
1333 inc_count(op->ob_type); |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
1334 #endif |
| 6 | 1335 } |
| 1336 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
1337 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1338 _Py_ForgetReference(register PyObject *op) |
| 6 | 1339 { |
|
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
|
1340 #ifdef SLOW_UNREF_CHECK |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1341 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
|
1342 #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
|
1343 if (op->ob_refcnt < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1344 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
|
1345 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
|
1346 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
|
1347 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
|
1348 #ifdef SLOW_UNREF_CHECK |
| 86 | 1349 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1350 if (p == op) | |
| 1351 break; | |
| 1352 } | |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1869
diff
changeset
|
1353 if (p == &refchain) /* Not found */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1354 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
|
1355 #endif |
| 6 | 1356 op->_ob_next->_ob_prev = op->_ob_prev; |
| 1357 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
|
1358 op->_ob_next = op->_ob_prev = NULL; |
|
2639
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
1359 #ifdef COUNT_ALLOCS |
|
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
1360 op->ob_type->tp_free++; |
|
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
1361 #endif |
| 86 | 1362 } |
| 1363 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
1364 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1365 _Py_Dealloc(PyObject *op) |
| 86 | 1366 { |
|
1869
2cede61e9b1b
rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
1367 destructor dealloc = op->ob_type->tp_dealloc; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1368 _Py_ForgetReference(op); |
|
1869
2cede61e9b1b
rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
1369 (*dealloc)(op); |
| 6 | 1370 } |
| 1371 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
1372 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1373 _Py_PrintReferences(FILE *fp) |
| 6 | 1374 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1375 PyObject *op; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1376 fprintf(fp, "Remaining objects:\n"); |
| 6 | 1377 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1378 fprintf(fp, "[%d] ", op->ob_refcnt); | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1379 if (PyObject_Print(op, fp, 0) != 0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1380 PyErr_Clear(); |
| 6 | 1381 putc('\n', fp); |
| 1382 } | |
| 1383 } | |
| 1384 | |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1385 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1386 _Py_GetObjects(PyObject *self, PyObject *args) |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1387 { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1388 int i, n; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1389 PyObject *t = NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1390 PyObject *res, *op; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1391 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1392 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1393 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1394 op = refchain._ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1395 res = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1396 if (res == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1397 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1398 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
|
1399 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
|
1400 t != NULL && op->ob_type != (PyTypeObject *) t) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1401 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1402 if (op == &refchain) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1403 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1404 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1405 if (PyList_Append(res, op) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1406 Py_DECREF(res); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1407 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1408 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1409 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1410 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1411 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1412 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
1413 |
| 6 | 1414 #endif |
|
3330
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
1415 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
1416 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
1417 /* Hack to force loading of cobject.o */ |
| 4588 | 1418 PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
1419 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
1420 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
1421 /* Hack to force loading of abstract.o */ |
|
13575
7c119f050290
change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13551
diff
changeset
|
1422 int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1423 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1424 |
| 14006 | 1425 /* Python's malloc wrappers (see pymem.h) */ |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1426 |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1427 void * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1428 PyMem_Malloc(size_t nbytes) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1429 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1430 #if _PyMem_EXTRA > 0 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1431 if (nbytes == 0) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1432 nbytes = _PyMem_EXTRA; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1433 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1434 return PyMem_MALLOC(nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1435 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1436 |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1437 void * |
|
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1438 PyMem_Realloc(void *p, size_t nbytes) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1439 { |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1440 #if _PyMem_EXTRA > 0 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1441 if (nbytes == 0) |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1442 nbytes = _PyMem_EXTRA; |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1443 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1444 return PyMem_REALLOC(p, nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1445 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1446 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1447 void |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1448 PyMem_Free(void *p) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1449 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1450 PyMem_FREE(p); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1451 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1452 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1453 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1454 /* 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
|
1455 |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1456 void * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1457 PyObject_Malloc(size_t nbytes) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1458 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1459 return PyObject_MALLOC(nbytes); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1460 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1461 |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1462 void * |
|
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1463 PyObject_Realloc(void *p, size_t nbytes) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1464 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1465 return PyObject_REALLOC(p, nbytes); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1466 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1467 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1468 void |
|
13709
4e2aaf1e722a
Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1469 PyObject_Free(void *p) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1470 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
1471 PyObject_FREE(p); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1472 } |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1473 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1474 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1475 /* Hook to clear up weak references only once the _weakref module is |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1476 imported. We use a dummy implementation to simplify the code at each |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1477 call site instead of requiring a test for NULL. |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1478 */ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1479 |
|
16768
d446e4ec9b7b
The return value from PyObject_ClearWeakRefs() is no longer meaningful,
Fred Drake <fdrake@acm.org>
parents:
16742
diff
changeset
|
1480 static void |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1481 empty_clear_weak_refs(PyObject *o) |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1482 { |
|
16768
d446e4ec9b7b
The return value from PyObject_ClearWeakRefs() is no longer meaningful,
Fred Drake <fdrake@acm.org>
parents:
16742
diff
changeset
|
1483 return; |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1484 } |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1485 |
|
16768
d446e4ec9b7b
The return value from PyObject_ClearWeakRefs() is no longer meaningful,
Fred Drake <fdrake@acm.org>
parents:
16742
diff
changeset
|
1486 void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs; |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1487 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1488 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16236
diff
changeset
|
1489 |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1490 /* 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
|
1491 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
|
1492 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
|
1493 Py_ReprLeave() to avoid infinite recursion. |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1494 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1495 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
|
1496 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
|
1497 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
|
1498 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1499 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
|
1500 */ |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1501 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1502 #define KEY "Py_Repr" |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1503 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1504 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1505 Py_ReprEnter(PyObject *obj) |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1506 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1507 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1508 PyObject *list; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1509 int i; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1510 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1511 dict = PyThreadState_GetDict(); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1512 if (dict == NULL) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1513 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1514 list = PyDict_GetItemString(dict, KEY); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1515 if (list == NULL) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1516 list = PyList_New(0); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1517 if (list == NULL) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1518 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1519 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
|
1520 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1521 Py_DECREF(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1522 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1523 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1524 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1525 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
|
1526 return 1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1527 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1528 PyList_Append(list, obj); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1529 return 0; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1530 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1531 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1532 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1533 Py_ReprLeave(PyObject *obj) |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1534 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1535 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1536 PyObject *list; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1537 int i; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1538 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1539 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
|
1540 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
|
1541 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1542 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
|
1543 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
|
1544 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1545 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1546 /* 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
|
1547 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1548 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
|
1549 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
|
1550 break; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1551 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1552 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
1553 } |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1554 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1555 /* |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1556 trashcan |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1557 CT 2k0130 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1558 non-recursively destroy nested objects |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1559 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1560 CT 2k0223 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1561 everything is now done in a macro. |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1562 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1563 CT 2k0305 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1564 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
|
1565 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1566 CT 2k0309 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1567 modified to restore a possible error. |
| 12052 | 1568 |
| 1569 CT 2k0325 | |
| 1570 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
|
1571 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1572 CT 2k0422 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1573 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
|
1574 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
|
1575 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
|
1576 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
|
1577 */ |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1578 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1579 #define Py_TRASHCAN_TUPLE 1 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1580 #define Py_TRASHCAN_LIST 2 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1581 #define Py_TRASHCAN_DICT 3 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1582 #define Py_TRASHCAN_FRAME 4 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1583 #define Py_TRASHCAN_TRACEBACK 5 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1584 /* 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
|
1585 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1586 int _PyTrash_delete_nesting = 0; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1587 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1588 PyObject * _PyTrash_delete_later = NULL; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1589 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1590 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1591 _PyTrash_deposit_object(PyObject *op) |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1592 { |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1593 int typecode; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1594 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1595 if (PyTuple_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1596 typecode = Py_TRASHCAN_TUPLE; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1597 else if (PyList_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1598 typecode = Py_TRASHCAN_LIST; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1599 else if (PyDict_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1600 typecode = Py_TRASHCAN_DICT; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1601 else if (PyFrame_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1602 typecode = Py_TRASHCAN_FRAME; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1603 else if (PyTraceBack_Check(op)) |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1604 typecode = Py_TRASHCAN_TRACEBACK; |
|
13861
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13761
diff
changeset
|
1605 else /* We have a bug here -- those are the only types in GC */ { |
|
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13761
diff
changeset
|
1606 Py_FatalError("Type not supported in GC -- internal bug"); |
|
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13761
diff
changeset
|
1607 return; /* pacify compiler -- execution never here */ |
|
f3d5f71ccbb4
Removing warnings found by gcc -Wall
Moshe Zadka <moshez@math.huji.ac.il>
parents:
13761
diff
changeset
|
1608 } |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1609 op->ob_refcnt = typecode; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1610 |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1611 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
|
1612 _PyTrash_delete_later = op; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1613 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1614 |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1615 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1616 _PyTrash_destroy_chain(void) |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1617 { |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1618 while (_PyTrash_delete_later) { |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1619 PyObject *shredder = _PyTrash_delete_later; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1620 _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
|
1621 |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1622 switch (shredder->ob_refcnt) { |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1623 case Py_TRASHCAN_TUPLE: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1624 shredder->ob_type = &PyTuple_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1625 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1626 case Py_TRASHCAN_LIST: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1627 shredder->ob_type = &PyList_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1628 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1629 case Py_TRASHCAN_DICT: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1630 shredder->ob_type = &PyDict_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1631 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1632 case Py_TRASHCAN_FRAME: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1633 shredder->ob_type = &PyFrame_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1634 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1635 case Py_TRASHCAN_TRACEBACK: |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1636 shredder->ob_type = &PyTraceBack_Type; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1637 break; |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1638 } |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1639 _Py_NewReference(shredder); |
|
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
1640 |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1641 ++_PyTrash_delete_nesting; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1642 Py_DECREF(shredder); |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1643 --_PyTrash_delete_nesting; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1644 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1645 } |
|
16782
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16768
diff
changeset
|
1646 |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16768
diff
changeset
|
1647 #ifdef WITH_PYMALLOC |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16768
diff
changeset
|
1648 #include "obmalloc.c" |
|
5864a9c58c81
Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16768
diff
changeset
|
1649 #endif |
